feat(args): add --no-pager flag for disabling the pager

This commit is contained in:
Orhun Parmaksız 2021-12-03 18:04:00 +03:00
parent 48a8411859
commit 85010f8c64
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 18 additions and 7 deletions

View file

@ -21,18 +21,13 @@ pub struct Config {
pub sysctl: SysctlConfig,
}
/// Sysctl configuration.
#[derive(Debug, Default)]
pub struct SysctlConfig {
/// Whether if the errors should be ignored.
pub ignore_errors: bool,
}
/// Sysctl configuration.
#[derive(Debug)]
pub struct AppConfig {
/// Whether if the colors are disabled.
pub no_color: bool,
/// Whether if the pager is disabled.
pub no_pager: bool,
/// Sections and the corresponding colors.
pub section_colors: HashMap<Section, Color>,
/// Default color for the output
@ -45,6 +40,7 @@ impl Default for AppConfig {
fn default() -> Self {
Self {
no_color: false,
no_pager: false,
section_colors: map! {
Section::Abi => Color::Red,
Section::Fs => Color::Green,
@ -60,3 +56,9 @@ impl Default for AppConfig {
}
}
}
/// Sysctl configuration.
#[derive(Debug, Default)]
pub struct SysctlConfig {
/// Whether if the errors should be ignored.
pub ignore_errors: bool,
}

View file

@ -72,6 +72,10 @@ impl<'a> App<'a> {
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
if let Some(parameter) = self.sysctl.get_parameter(param_name) {
let mut fallback_to_default = false;
if self.config.no_pager {
parameter.display_documentation(&mut self.stdout)?;
return Ok(());
}
let pager = env::var("PAGER").unwrap_or_else(|_| String::from("less"));
match Command::new(&pager).stdin(Stdio::piped()).spawn() {
Ok(mut process) => {

View file

@ -22,6 +22,8 @@ pub struct Args {
pub display_type: DisplayType,
/// Whether if the unknown variable errors should be ignored.
pub ignore_errors: bool,
/// Do not pipe output into a pager.
pub no_pager: bool,
/// Parameter to explain.
pub param_to_explain: Option<String>,
/// Parameter names.
@ -51,6 +53,7 @@ impl Args {
"set the path of the kernel documentation",
"<path>",
);
opts.optflag("P", "no-pager", "Do not pipe output into a pager");
opts.optflag("h", "help", "display this help and exit");
opts.optflag("V", "version", "output version information and exit");
@ -90,6 +93,7 @@ impl Args {
kernel_docs: matches.opt_str("d").map(PathBuf::from),
display_type,
ignore_errors: matches.opt_present("e"),
no_pager: matches.opt_present("P"),
param_to_explain: matches.opt_str("explain"),
param_names: matches.free,
})

View file

@ -20,6 +20,7 @@ pub fn run(args: Args) -> Result<()> {
config.sysctl.ignore_errors = args.ignore_errors;
config.app.display_type = args.display_type;
config.app.no_color = env::var("NO_COLOR").is_ok();
config.app.no_pager = args.no_pager;
let mut sysctl = Sysctl::init(config.sysctl)?;
let mut app = App::new(&mut sysctl, &config.app)?;