add auto option for use_ansi_coloring config field

This commit is contained in:
Tim 'Piepmatz' Hesse 2024-12-20 21:53:52 +01:00
parent 039d0a685a
commit 556585fba7
17 changed files with 97 additions and 35 deletions

View File

@ -196,14 +196,14 @@ pub fn complete_item(
.map(|cwd| Path::new(cwd.as_ref()).to_path_buf())
.collect();
let ls_colors = (engine_state.config.completions.use_ls_colors
&& engine_state.config.use_ansi_coloring)
.then(|| {
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => env_to_string("LS_COLORS", v, engine_state, stack).ok(),
None => None,
};
get_ls_colors(ls_colors_env_str)
});
&& engine_state.config.use_ansi_coloring.get())
.then(|| {
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
Some(v) => env_to_string("LS_COLORS", v, engine_state, stack).ok(),
None => None,
};
get_ls_colors(ls_colors_env_str)
});
let mut cwds = cwd_pathbufs.clone();
let mut prefix_len = 0;

View File

@ -49,7 +49,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
perf!(
"add plugin file to engine_state",
start_time,
engine_state.get_config().use_ansi_coloring
engine_state.get_config().use_ansi_coloring.get()
);
start_time = std::time::Instant::now();
@ -129,7 +129,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
perf!(
&format!("read plugin file {}", plugin_path.display()),
start_time,
engine_state.get_config().use_ansi_coloring
engine_state.get_config().use_ansi_coloring.get()
);
start_time = std::time::Instant::now();
@ -145,7 +145,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
perf!(
&format!("load plugin file {}", plugin_path.display()),
start_time,
engine_state.get_config().use_ansi_coloring
engine_state.get_config().use_ansi_coloring.get()
);
}
}
@ -345,7 +345,7 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState) -> bool {
perf!(
"migrate old plugin file",
start_time,
engine_state.get_config().use_ansi_coloring
engine_state.get_config().use_ansi_coloring.get()
);
true
}

View File

@ -61,7 +61,7 @@ pub fn evaluate_repl(
// from the Arc. This lets us avoid copying stack variables needlessly
let mut unique_stack = stack.clone();
let config = engine_state.get_config();
let use_color = config.use_ansi_coloring;
let use_color = config.use_ansi_coloring.get();
confirm_stdin_is_terminal()?;
@ -375,7 +375,7 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
)))
.with_quick_completions(config.completions.quick)
.with_partial_completions(config.completions.partial)
.with_ansi_colors(config.use_ansi_coloring)
.with_ansi_colors(config.use_ansi_coloring.get())
.with_cwd(Some(
engine_state
.cwd(None)
@ -395,7 +395,7 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
let style_computer = StyleComputer::from_config(engine_state, &stack_arc);
start_time = std::time::Instant::now();
line_editor = if config.use_ansi_coloring {
line_editor = if config.use_ansi_coloring.get() {
line_editor.with_hinter(Box::new({
// As of Nov 2022, "hints" color_config closures only get `null` passed in.
let style = style_computer.compute("hints", &Value::nothing(Span::unknown()));

View File

@ -265,7 +265,7 @@ pub fn eval_source(
perf!(
&format!("eval_source {}", &fname),
start_time,
engine_state.get_config().use_ansi_coloring
engine_state.get_config().use_ansi_coloring.get()
);
exit_code

View File

@ -144,7 +144,7 @@ pub fn help_aliases(
long_desc.push_str(&format!("{G}Expansion{RESET}:\n {alias_expansion}"));
let config = stack.get_config(engine_state);
if !config.use_ansi_coloring {
if !config.use_ansi_coloring.get() {
long_desc = nu_utils::strip_ansi_string_likely(long_desc);
}

View File

@ -231,7 +231,7 @@ pub fn help_modules(
}
let config = stack.get_config(engine_state);
if !config.use_ansi_coloring {
if !config.use_ansi_coloring.get() {
long_desc = nu_utils::strip_ansi_string_likely(long_desc);
}

View File

@ -654,7 +654,7 @@ Operating system commands:
let list: bool = call.has_flag(engine_state, stack, "list")?;
let escape: bool = call.has_flag(engine_state, stack, "escape")?;
let osc: bool = call.has_flag(engine_state, stack, "osc")?;
let use_ansi_coloring = stack.get_config(engine_state).use_ansi_coloring;
let use_ansi_coloring = stack.get_config(engine_state).use_ansi_coloring.get();
if list {
return Ok(generate_ansi_code_list(
@ -691,7 +691,7 @@ Operating system commands:
let list: bool = call.has_flag_const(working_set, "list")?;
let escape: bool = call.has_flag_const(working_set, "escape")?;
let osc: bool = call.has_flag_const(working_set, "osc")?;
let use_ansi_coloring = working_set.get_config().use_ansi_coloring;
let use_ansi_coloring = working_set.get_config().use_ansi_coloring.get();
if list {
return Ok(generate_ansi_code_list(

View File

@ -1,7 +1,9 @@
use nu_cmd_base::hook::eval_hook;
use nu_engine::{command_prelude::*, env_to_strings};
use nu_path::{dots::expand_ndots, expand_tilde, AbsolutePath};
use nu_protocol::{did_you_mean, process::ChildProcess, ByteStream, NuGlob, OutDest, Signals};
use nu_protocol::{
did_you_mean, process::ChildProcess, ByteStream, NuGlob, OutDest, Signals, UseAnsiColoring,
};
use nu_system::ForegroundChild;
use nu_utils::IgnoreCaseExt;
use pathdiff::diff_paths;
@ -417,7 +419,7 @@ fn write_pipeline_data(
stack.start_collect_value();
// Turn off color as we pass data through
Arc::make_mut(&mut engine_state.config).use_ansi_coloring = false;
Arc::make_mut(&mut engine_state.config).use_ansi_coloring = UseAnsiColoring::False;
// Invoke the `table` command.
let output =

View File

@ -72,7 +72,7 @@ prints out the list properly."#
None => None,
};
let use_color: bool = color_param && config.use_ansi_coloring;
let use_color: bool = color_param && config.use_ansi_coloring.get();
let cwd = engine_state.cwd(Some(stack))?;
match input {

View File

@ -1118,7 +1118,7 @@ enum TableView {
fn maybe_strip_color(output: String, config: &Config) -> String {
// the terminal is for when people do ls from vim, there should be no coloring there
if !config.use_ansi_coloring || !std::io::stdout().is_terminal() {
if !config.use_ansi_coloring.get() || !std::io::stdout().is_terminal() {
// Draw the table without ansi colors
nu_utils::strip_ansi_string_likely(output)
} else {

View File

@ -258,7 +258,7 @@ fn get_documentation(
long_desc.push_str(" ");
long_desc.push_str(example.description);
if !nu_config.use_ansi_coloring {
if !nu_config.use_ansi_coloring.get() {
let _ = write!(long_desc, "\n > {}\n", example.example);
} else {
let code_string = nu_highlight_string(example.example, engine_state, stack);
@ -329,7 +329,7 @@ fn get_documentation(
long_desc.push('\n');
if !nu_config.use_ansi_coloring {
if !nu_config.use_ansi_coloring.get() {
nu_utils::strip_ansi_string_likely(long_desc)
} else {
long_desc

View File

@ -16,7 +16,7 @@ pub use helper::extract_value;
pub use history::{HistoryConfig, HistoryFileFormat};
pub use hooks::Hooks;
pub use ls::LsConfig;
pub use output::ErrorStyle;
pub use output::{ErrorStyle, UseAnsiColoring};
pub use plugin_gc::{PluginGcConfig, PluginGcConfigs};
pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu};
pub use rm::RmConfig;
@ -49,7 +49,7 @@ pub struct Config {
pub footer_mode: FooterMode,
pub float_precision: i64,
pub recursion_limit: i64,
pub use_ansi_coloring: bool,
pub use_ansi_coloring: UseAnsiColoring,
pub completions: CompletionConfig,
pub edit_mode: EditBindings,
pub history: HistoryConfig,
@ -106,7 +106,7 @@ impl Default for Config {
footer_mode: FooterMode::RowCount(25),
float_precision: 2,
buffer_editor: Value::nothing(Span::unknown()),
use_ansi_coloring: true,
use_ansi_coloring: UseAnsiColoring::default(),
bracketed_paste: true,
edit_mode: EditBindings::default(),

View File

@ -1,5 +1,7 @@
use std::io::IsTerminal;
use super::{config_update_string_enum, prelude::*};
use crate as nu_protocol;
use crate::{self as nu_protocol, FromValue};
#[derive(Clone, Copy, Debug, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
pub enum ErrorStyle {
@ -24,3 +26,61 @@ impl UpdateFromValue for ErrorStyle {
config_update_string_enum(self, value, path, errors)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, IntoValue, Serialize, Deserialize)]
pub enum UseAnsiColoring {
#[default]
Auto,
True,
False,
}
impl UseAnsiColoring {
pub fn get(self) -> bool {
match self {
Self::Auto => std::io::stdout().is_terminal(),
Self::True => true,
Self::False => false,
}
}
}
impl FromValue for UseAnsiColoring {
fn from_value(v: Value) -> Result<Self, ShellError> {
if let Ok(v) = v.as_bool() {
return Ok(match v {
true => Self::True,
false => Self::False,
});
}
#[derive(FromValue)]
enum UseAnsiColoringString {
Auto,
True,
False,
}
Ok(match UseAnsiColoringString::from_value(v)? {
UseAnsiColoringString::Auto => Self::Auto,
UseAnsiColoringString::True => Self::True,
UseAnsiColoringString::False => Self::False,
})
}
}
impl UpdateFromValue for UseAnsiColoring {
fn update<'a>(
&mut self,
value: &'a Value,
path: &mut ConfigPath<'a>,
errors: &mut ConfigErrors,
) {
let Ok(value) = UseAnsiColoring::from_value(value.clone()) else {
errors.type_mismatch(path, UseAnsiColoring::expected_type(), value);
return;
};
*self = value;
}
}

View File

@ -70,7 +70,7 @@ impl std::fmt::Debug for CliError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let config = self.1.get_config();
let ansi_support = config.use_ansi_coloring;
let ansi_support = config.use_ansi_coloring.get();
let error_style = &config.error_style;

View File

@ -39,7 +39,7 @@ pub(crate) fn read_config_file(
let start_time = std::time::Instant::now();
let config = engine_state.get_config();
let use_color = config.use_ansi_coloring;
let use_color = config.use_ansi_coloring.get();
// Translate environment variables from Strings to Values
if let Err(e) = convert_env_values(engine_state, stack) {
report_shell_error(engine_state, &e);
@ -53,7 +53,7 @@ pub(crate) fn read_config_file(
} else {
let start_time = std::time::Instant::now();
let config = engine_state.get_config();
let use_color = config.use_ansi_coloring;
let use_color = config.use_ansi_coloring.get();
if let Err(e) = convert_env_values(engine_state, stack) {
report_shell_error(engine_state, &e);
}

View File

@ -213,7 +213,7 @@ fn main() -> Result<()> {
engine_state.history_enabled = parsed_nu_cli_args.no_history.is_none();
let use_color = engine_state.get_config().use_ansi_coloring;
let use_color = engine_state.get_config().use_ansi_coloring.get();
// Set up logger
if let Some(level) = parsed_nu_cli_args

View File

@ -197,7 +197,7 @@ pub(crate) fn run_repl(
}
// Reload use_color from config in case it's different from the default value
let use_color = engine_state.get_config().use_ansi_coloring;
let use_color = engine_state.get_config().use_ansi_coloring.get();
perf!("setup_config", start_time, use_color);
let start_time = std::time::Instant::now();