Skip to content

Commit

Permalink
cli: add --no-pager
Browse files Browse the repository at this point in the history
  • Loading branch information
chooglen committed Nov 29, 2022
1 parent 40fca71 commit 3546054
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,14 @@ pub struct GlobalArgs {
help_heading = "Global Options"
)]
pub color: Option<ColorChoice>,
/// Disable the pager
#[arg(
long,
value_name = "WHEN",
global = true,
help_heading = "Global Options"
)]
pub no_pager: bool,
/// Additional configuration options
// TODO: Introduce a `--config` option with simpler syntax for simple
// cases, designed so that `--config ui.color=auto` works
Expand Down Expand Up @@ -1411,6 +1419,9 @@ pub fn parse_args(
.config_toml
.push(format!("ui.color=\"{}\"", choice.to_string()));
}
if args.global_args.no_pager {
ui.set_pagination(crate::ui::PaginationChoice::No);
}
if !args.global_args.config_toml.is_empty() {
ui.extra_toml_settings(&args.global_args.config_toml)?;
}
Expand Down
23 changes: 23 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::formatter::{Formatter, FormatterFactory};

pub struct Ui {
color: bool,
paginate: PaginationChoice,
progress_indicator: bool,
cwd: PathBuf,
formatter_factory: FormatterFactory,
Expand Down Expand Up @@ -93,6 +94,18 @@ fn use_color(choice: ColorChoice) -> bool {
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PaginationChoice {
No,
Auto,
}

impl Default for PaginationChoice {
fn default() -> Self {
PaginationChoice::Auto
}
}

fn pager_setting(settings: &UserSettings) -> String {
settings
.config()
Expand All @@ -110,6 +123,7 @@ impl Ui {
color,
cwd,
formatter_factory,
paginate: PaginationChoice::Auto,
progress_indicator,
output: UiOutput::new_terminal(),
settings,
Expand All @@ -124,8 +138,17 @@ impl Ui {
}
}

/// Sets the pagination value.
pub fn set_pagination(&mut self, choice: PaginationChoice) {
self.paginate = choice;
}

/// Switches the output to use the pager, if allowed.
pub fn request_pager(&mut self) {
if self.paginate == PaginationChoice::No {
return;
}

match self.output {
UiOutput::Paged { .. } => {}
UiOutput::Terminal { .. } => {
Expand Down
1 change: 1 addition & 0 deletions tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ fn test_help() {
--no-commit-working-copy Don't commit the working copy
--at-operation <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
--color <WHEN> When to colorize output (always, never, auto)
--no-pager Disable the pager
--config-toml <TOML> Additional configuration options
-v, --verbose Enable verbose logging
"###);
Expand Down

0 comments on commit 3546054

Please sign in to comment.