diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-05-31 21:40:46 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-05-31 22:19:51 -0400 |
commit | d76dba82a7d33ded533813d17d5b5b4e581a27b6 (patch) | |
tree | 9211570f11fe1ade91f38a1453067e0fa500980e | |
parent | 385214f14e94d85197dcdfbd52199f02367f8946 (diff) |
Make decorations respect --width
Fixes #61
Fixes #155
-rw-r--r-- | src/cli.rs | 8 | ||||
-rw-r--r-- | src/config.rs | 25 | ||||
-rw-r--r-- | src/delta.rs | 8 |
3 files changed, 25 insertions, 16 deletions
@@ -1,6 +1,5 @@ use std::process; -use console::Term; use structopt::clap::AppSettings::{ColorAlways, ColoredHelp, DeriveDisplayOrder}; use structopt::StructOpt; @@ -310,12 +309,6 @@ pub fn process_command_line_arguments<'a>(mut opt: Opt) -> config::Config<'a> { rewrite::apply_rewrite_rules(&mut opt); - // We do not use the full width, in case `less --status-column` is in effect. See #41 and #10. - // TODO: There seems to be some confusion in the accounting: we are actually leaving 2 - // characters unused for less at the right edge of the terminal, despite the subtraction of 1 - // here. - let available_terminal_width = (Term::stdout().size().1 - 1) as usize; - let paging_mode = match opt.paging_mode.as_ref() { "always" => PagingMode::Always, "never" => PagingMode::Never, @@ -347,7 +340,6 @@ pub fn process_command_line_arguments<'a>(mut opt: Opt) -> config::Config<'a> { assets.syntax_set, assets.theme_set, true_color, - available_terminal_width, paging_mode, ) } diff --git a/src/config.rs b/src/config.rs index 7706679b..e986f478 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,7 @@ +use std::cmp::min; +use std::process; + +use console::Term; use syntect::highlighting::Style as SyntectStyle; use syntect::highlighting::{Theme, ThemeSet}; use syntect::parsing::SyntaxSet; @@ -29,7 +33,7 @@ pub struct Config<'a> { pub file_style: Style, pub hunk_header_style: Style, pub syntax_set: SyntaxSet, - pub terminal_width: usize, + pub decorations_width: usize, pub true_color: bool, pub background_color_extends_to_terminal_width: bool, pub tab_width: usize, @@ -55,10 +59,23 @@ pub fn get_config<'a>( syntax_set: SyntaxSet, theme_set: ThemeSet, true_color: bool, - terminal_width: usize, paging_mode: PagingMode, ) -> Config<'a> { - let background_color_extends_to_terminal_width = opt.width != Some("variable".to_string()); + // Allow one character for e.g. `less --status-column` is in effect. See #41 and #10. + let available_terminal_width = (Term::stdout().size().1 - 1) as usize; + let (decorations_width, background_color_extends_to_terminal_width) = match opt.width.as_deref() + { + Some("variable") => (available_terminal_width, false), + Some(width) => { + let width = width.parse().unwrap_or_else(|_| { + eprintln!("Could not parse width as a positive integer: {:?}", width); + process::exit(1); + }); + (min(width, available_terminal_width), true) + } + None => (available_terminal_width, true), + }; + let theme_name_from_bat_pager = env::get_env_var("BAT_THEME"); let (is_light_mode, theme_name) = theme::get_is_light_mode_and_theme_name( opt.theme.as_ref(), @@ -122,7 +139,7 @@ pub fn get_config<'a>( file_style, hunk_header_style, true_color, - terminal_width, + decorations_width, background_color_extends_to_terminal_width, tab_width: opt.tab_width, syntax_set, diff --git a/src/delta.rs b/src/delta.rs index 34d66d54..1554cb9a 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -226,7 +226,7 @@ fn handle_commit_meta_header_line( painter.writer, &format!("{}{}", line, if pad { " " } else { "" }), &format!("{}{}", raw_line, if pad { " " } else { "" }), - config.terminal_width, + config.decorations_width, config.commit_style, decoration_ansi_term_style, )?; @@ -286,7 +286,7 @@ fn handle_generic_file_meta_header_line( painter.writer, &format!("{}{}", line, if pad { " " } else { "" }), &format!("{}{}", raw_line, if pad { " " } else { "" }), - config.terminal_width, + config.decorations_width, config.file_style, decoration_ansi_term_style, )?; @@ -332,7 +332,7 @@ fn handle_hunk_header_line( painter.writer, &format!("{} ", line), &format!("{} ", raw_line), - config.terminal_width, + config.decorations_width, config.hunk_header_style, decoration_ansi_term_style, )?; @@ -365,7 +365,7 @@ fn handle_hunk_header_line( painter.writer, &painter.output_buffer, &painter.output_buffer, - config.terminal_width, + config.decorations_width, config.hunk_header_style, decoration_ansi_term_style, )?; |