diff options
| author | Thomas Otto <th1000s@posteo.net> | 2023-06-06 23:04:14 +0200 |
|---|---|---|
| committer | Dan Davison <dandavison7@gmail.com> | 2024-08-01 21:40:55 -0400 |
| commit | c5f7428eddd07787602a1111180798d5b955c306 (patch) | |
| tree | 525f1b947f880e66ea3203be0422cdbba6b7f0f1 | |
| parent | 587fe8f0787d89c1dc499e1b9cb149d42b8fff6c (diff) | |
OutputType: PagerCfg and oneshot_write
Use new, smaller PagerCfg instead of the full Config, as for
pager output only 3 variables are relevant.
oneshot_write() can be used to write paginated output, usually
before exiting.
| -rw-r--r-- | src/features/navigate.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/subcommands/show_colors.rs | 3 | ||||
| -rw-r--r-- | src/subcommands/show_syntax_themes.rs | 2 | ||||
| -rw-r--r-- | src/subcommands/show_themes.rs | 9 | ||||
| -rw-r--r-- | src/utils/bat/output.rs | 42 |
6 files changed, 55 insertions, 10 deletions
diff --git a/src/features/navigate.rs b/src/features/navigate.rs index d04f808..71f9802 100644 --- a/src/features/navigate.rs +++ b/src/features/navigate.rs @@ -3,8 +3,8 @@ use std::io::Write; use std::io::{Error, ErrorKind}; use std::path::PathBuf; -use crate::config::Config; use crate::features::OptionValueFunction; +use crate::utils::bat::output::PagerCfg; pub fn make_feature() -> Vec<(String, OptionValueFunction)> { builtin_feature!([ @@ -68,7 +68,9 @@ pub fn make_navigate_regex( // current implementation, no writes to the delta less history file are propagated back to the real // history file so, for example, a (non-navigate) search performed in the delta less process will // not be stored in history. -pub fn copy_less_hist_file_and_append_navigate_regex(config: &Config) -> std::io::Result<PathBuf> { +pub fn copy_less_hist_file_and_append_navigate_regex( + config: &PagerCfg, +) -> std::io::Result<PathBuf> { let delta_less_hist_file = get_delta_less_hist_file()?; let initial_contents = ".less-history-file:\n".to_string(); let mut contents = if let Some(hist_file) = get_less_hist_file() { diff --git a/src/main.rs b/src/main.rs index 925e7dc..78c1a25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,8 +121,9 @@ fn run_app() -> std::io::Result<i32> { return Ok(0); } + let pager_cfg = (&config).into(); let mut output_type = - OutputType::from_mode(&env, config.paging_mode, config.pager.clone(), &config).unwrap(); + OutputType::from_mode(&env, config.paging_mode, config.pager.clone(), &pager_cfg).unwrap(); let mut writer = output_type.handle().unwrap(); if let (Some(minus_file), Some(plus_file)) = (&config.minus_file, &config.plus_file) { diff --git a/src/subcommands/show_colors.rs b/src/subcommands/show_colors.rs index 9d0428c..a7f5927 100644 --- a/src/subcommands/show_colors.rs +++ b/src/subcommands/show_colors.rs @@ -17,9 +17,10 @@ pub fn show_colors() -> std::io::Result<()> { let env = DeltaEnv::default(); let opt = cli::Opt::from_args_and_git_config(&env, assets); let config = config::Config::from(opt); + let pagercfg = (&config).into(); let mut output_type = - OutputType::from_mode(&env, PagingMode::QuitIfOneScreen, None, &config).unwrap(); + OutputType::from_mode(&env, PagingMode::QuitIfOneScreen, None, &pagercfg).unwrap(); let writer = output_type.handle().unwrap(); let mut painter = paint::Painter::new(writer, &config); diff --git a/src/subcommands/show_syntax_themes.rs b/src/subcommands/show_syntax_themes.rs index 1188059..6af8acd 100644 --- a/src/subcommands/show_syntax_themes.rs +++ b/src/subcommands/show_syntax_themes.rs @@ -16,7 +16,7 @@ pub fn show_syntax_themes() -> std::io::Result<()> { &env, PagingMode::QuitIfOneScreen, None, - &config::Config::from(cli::Opt::parse()), + &config::Config::from(cli::Opt::parse()).into(), ) .unwrap(); let mut writer = output_type.handle().unwrap(); diff --git a/src/subcommands/show_themes.rs b/src/subcommands/show_themes.rs index 86cae34..70987fc 100644 --- a/src/subcommands/show_themes.rs +++ b/src/subcommands/show_themes.rs @@ -40,8 +40,13 @@ pub fn show_themes(dark: bool, light: bool, computed_theme_is_light: bool) -> st &["delta", "--navigate", "--show-themes"], git_config, ); - let mut output_type = - OutputType::from_mode(&env, PagingMode::Always, None, &config::Config::from(opt)).unwrap(); + let mut output_type = OutputType::from_mode( + &env, + PagingMode::Always, + None, + &config::Config::from(opt).into(), + ) + .unwrap(); let title_style = ansi_term::Style::new().bold(); let writer = output_type.handle().unwrap(); diff --git a/src/utils/bat/output.rs b/src/utils/bat/output.rs index 91fb5ba..e93fe2c 100644 --- a/src/utils/bat/output.rs +++ b/src/utils/bat/output.rs @@ -13,6 +13,28 @@ use crate::env::DeltaEnv; use crate::fatal; use crate::features::navigate; +#[derive(Debug, Default)] +pub struct PagerCfg { + pub navigate: bool, + pub show_themes: bool, + pub navigate_regex: Option<String>, +} + +impl From<&config::Config> for PagerCfg { + fn from(cfg: &config::Config) -> Self { + PagerCfg { + navigate: cfg.navigate, + show_themes: cfg.show_themes, + navigate_regex: cfg.navigate_regex.clone(), + } + } +} +impl From<config::Config> for PagerCfg { + fn from(cfg: config::Config) -> Self { + (&cfg).into() + } +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[allow(dead_code)] pub enum PagingMode { @@ -30,11 +52,25 @@ pub enum OutputType { } impl OutputType { + /// Create a pager and write all data into it. Waits until the pager exits. + /// The expectation is that the program will exit afterwards. + pub fn oneshot_write(data: String) -> io::Result<()> { + let mut output_type = OutputType::from_mode( + &DeltaEnv::init(), + PagingMode::QuitIfOneScreen, + None, + &PagerCfg::default(), + ) + .unwrap(); + let mut writer = output_type.handle().unwrap(); + write!(&mut writer, "{}", data) + } + pub fn from_mode( env: &DeltaEnv, mode: PagingMode, pager: Option<String>, - config: &config::Config, + config: &PagerCfg, ) -> Result<Self> { use self::PagingMode::*; Ok(match mode { @@ -49,7 +85,7 @@ impl OutputType { env: &DeltaEnv, quit_if_one_screen: bool, pager_from_config: Option<String>, - config: &config::Config, + config: &PagerCfg, ) -> Result<Self> { let mut replace_arguments_to_less = false; @@ -127,7 +163,7 @@ fn _make_process_from_less_path( args: &[String], replace_arguments_to_less: bool, quit_if_one_screen: bool, - config: &config::Config, + config: &PagerCfg, ) -> Option<Command> { if let Ok(less_path) = grep_cli::resolve_binary(less_path) { let mut p = Command::new(less_path); |
