feat(docs): support displaying multiple docs with extensible search

This commit is contained in:
Orhun Parmaksız 2021-12-11 02:42:42 +03:00
parent fcc3d32fd6
commit 40ec7ba14d
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 16 additions and 15 deletions

View file

@ -127,7 +127,7 @@ impl Parameter {
/// Prints the description of the kernel parameter to the given output.
pub fn display_documentation<W: Write>(&self, output: &mut W) -> Result<()> {
if let Some(documentation) = self.get_documentation() {
writeln!(output, "{}", documentation)?;
writeln!(output, "{}\n", documentation)?;
} else {
writeln!(output, "No documentation available")?;
}

View file

@ -78,11 +78,11 @@ impl<'a> App<'a> {
/// Displays the documentation of a parameter.
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
let no_pager = self.sysctl.config.no_pager;
if let Some(parameter) = self.sysctl.get_parameter(param_name) {
for parameter in self.sysctl.get_parameters(param_name) {
let mut fallback_to_default = false;
if no_pager {
parameter.display_documentation(&mut self.stdout)?;
return Ok(());
continue;
}
let pager = env::var("PAGER").unwrap_or_else(|_| String::from("less"));
match Command::new(&pager).stdin(Stdio::piped()).spawn() {

View file

@ -34,8 +34,8 @@ pub struct Args {
pub preload_files: bool,
/// Pattern for matching the parameters.
pub pattern: Option<Regex>,
/// Parameter to explain.
pub param_to_explain: Option<String>,
/// Whether if the documentation should be shown.
pub explain_params: bool,
/// Free string fragments.
pub values: Vec<String>,
}
@ -61,11 +61,10 @@ impl Args {
);
opts.optflag("q", "quiet", "do not echo variable set");
opts.optflag("d", "", "alias of -h");
opts.optopt(
opts.optflag(
"E",
"explain",
"provide a detailed explanation for a variable",
"<var>",
);
opts.optopt(
"d",
@ -90,7 +89,7 @@ impl Args {
|| matches.opt_present("A")
|| matches.opt_present("X")
|| !matches.free.is_empty()
|| matches.opt_str("explain").is_some()
|| matches.opt_present("E")
|| preload_files;
if show_help || env_args.len() == 1 {
@ -142,7 +141,7 @@ impl Args {
pattern: matches
.opt_str("r")
.map(|v| Regex::new(&v).expect("invalid regex")),
param_to_explain: matches.opt_str("E"),
explain_params: matches.opt_present("E"),
values: matches.free,
})
}

View file

@ -28,18 +28,20 @@ pub fn run(args: Args) -> Result<()> {
let mut sysctl = Sysctl::init(config)?;
let mut app = App::new(&mut sysctl)?;
if let Some(param) = args.param_to_explain {
app.update_documentation(args.kernel_docs.as_ref())?;
app.display_documentation(&param)?;
} else if args.values.is_empty() {
if args.values.is_empty() {
app.display_parameters(args.pattern)?;
} else if args.explain_params {
app.update_documentation(args.kernel_docs.as_ref())?;
for param in args.values {
app.display_documentation(&param)?;
}
} else if args.preload_files {
for file in args.values {
app.preload_values(file)?;
}
} else {
for param_name in args.values {
app.process_parameter(param_name, true)?;
for param in args.values {
app.process_parameter(param, true)?;
}
}