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.
This commit is contained in:
Thomas Otto 2023-06-06 23:04:14 +02:00 committed by Dan Davison
parent 587fe8f078
commit c5f7428edd
6 changed files with 55 additions and 10 deletions

View File

@ -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() {

View File

@ -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) {

View File

@ -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);

View File

@ -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();

View File

@ -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();

View File

@ -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);