mirror of
https://github.com/nushell/nushell.git
synced 2025-08-13 23:49:10 +02:00
add auto option for use_ansi_coloring config field
This commit is contained in:
@ -196,14 +196,14 @@ pub fn complete_item(
|
|||||||
.map(|cwd| Path::new(cwd.as_ref()).to_path_buf())
|
.map(|cwd| Path::new(cwd.as_ref()).to_path_buf())
|
||||||
.collect();
|
.collect();
|
||||||
let ls_colors = (engine_state.config.completions.use_ls_colors
|
let ls_colors = (engine_state.config.completions.use_ls_colors
|
||||||
&& engine_state.config.use_ansi_coloring)
|
&& engine_state.config.use_ansi_coloring.get())
|
||||||
.then(|| {
|
.then(|| {
|
||||||
let ls_colors_env_str = match stack.get_env_var(engine_state, "LS_COLORS") {
|
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(),
|
Some(v) => env_to_string("LS_COLORS", v, engine_state, stack).ok(),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
get_ls_colors(ls_colors_env_str)
|
get_ls_colors(ls_colors_env_str)
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut cwds = cwd_pathbufs.clone();
|
let mut cwds = cwd_pathbufs.clone();
|
||||||
let mut prefix_len = 0;
|
let mut prefix_len = 0;
|
||||||
|
@ -49,7 +49,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
|
|||||||
perf!(
|
perf!(
|
||||||
"add plugin file to engine_state",
|
"add plugin file to engine_state",
|
||||||
start_time,
|
start_time,
|
||||||
engine_state.get_config().use_ansi_coloring
|
engine_state.get_config().use_ansi_coloring.get()
|
||||||
);
|
);
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
@ -129,7 +129,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
|
|||||||
perf!(
|
perf!(
|
||||||
&format!("read plugin file {}", plugin_path.display()),
|
&format!("read plugin file {}", plugin_path.display()),
|
||||||
start_time,
|
start_time,
|
||||||
engine_state.get_config().use_ansi_coloring
|
engine_state.get_config().use_ansi_coloring.get()
|
||||||
);
|
);
|
||||||
start_time = std::time::Instant::now();
|
start_time = std::time::Instant::now();
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Span
|
|||||||
perf!(
|
perf!(
|
||||||
&format!("load plugin file {}", plugin_path.display()),
|
&format!("load plugin file {}", plugin_path.display()),
|
||||||
start_time,
|
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!(
|
perf!(
|
||||||
"migrate old plugin file",
|
"migrate old plugin file",
|
||||||
start_time,
|
start_time,
|
||||||
engine_state.get_config().use_ansi_coloring
|
engine_state.get_config().use_ansi_coloring.get()
|
||||||
);
|
);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ pub fn evaluate_repl(
|
|||||||
// from the Arc. This lets us avoid copying stack variables needlessly
|
// from the Arc. This lets us avoid copying stack variables needlessly
|
||||||
let mut unique_stack = stack.clone();
|
let mut unique_stack = stack.clone();
|
||||||
let config = engine_state.get_config();
|
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()?;
|
confirm_stdin_is_terminal()?;
|
||||||
|
|
||||||
@ -375,7 +375,7 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
|||||||
)))
|
)))
|
||||||
.with_quick_completions(config.completions.quick)
|
.with_quick_completions(config.completions.quick)
|
||||||
.with_partial_completions(config.completions.partial)
|
.with_partial_completions(config.completions.partial)
|
||||||
.with_ansi_colors(config.use_ansi_coloring)
|
.with_ansi_colors(config.use_ansi_coloring.get())
|
||||||
.with_cwd(Some(
|
.with_cwd(Some(
|
||||||
engine_state
|
engine_state
|
||||||
.cwd(None)
|
.cwd(None)
|
||||||
@ -395,7 +395,7 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
|
|||||||
let style_computer = StyleComputer::from_config(engine_state, &stack_arc);
|
let style_computer = StyleComputer::from_config(engine_state, &stack_arc);
|
||||||
|
|
||||||
start_time = std::time::Instant::now();
|
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({
|
line_editor.with_hinter(Box::new({
|
||||||
// As of Nov 2022, "hints" color_config closures only get `null` passed in.
|
// As of Nov 2022, "hints" color_config closures only get `null` passed in.
|
||||||
let style = style_computer.compute("hints", &Value::nothing(Span::unknown()));
|
let style = style_computer.compute("hints", &Value::nothing(Span::unknown()));
|
||||||
|
@ -265,7 +265,7 @@ pub fn eval_source(
|
|||||||
perf!(
|
perf!(
|
||||||
&format!("eval_source {}", &fname),
|
&format!("eval_source {}", &fname),
|
||||||
start_time,
|
start_time,
|
||||||
engine_state.get_config().use_ansi_coloring
|
engine_state.get_config().use_ansi_coloring.get()
|
||||||
);
|
);
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
|
@ -144,7 +144,7 @@ pub fn help_aliases(
|
|||||||
long_desc.push_str(&format!("{G}Expansion{RESET}:\n {alias_expansion}"));
|
long_desc.push_str(&format!("{G}Expansion{RESET}:\n {alias_expansion}"));
|
||||||
|
|
||||||
let config = stack.get_config(engine_state);
|
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);
|
long_desc = nu_utils::strip_ansi_string_likely(long_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +231,7 @@ pub fn help_modules(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = stack.get_config(engine_state);
|
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);
|
long_desc = nu_utils::strip_ansi_string_likely(long_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -654,7 +654,7 @@ Operating system commands:
|
|||||||
let list: bool = call.has_flag(engine_state, stack, "list")?;
|
let list: bool = call.has_flag(engine_state, stack, "list")?;
|
||||||
let escape: bool = call.has_flag(engine_state, stack, "escape")?;
|
let escape: bool = call.has_flag(engine_state, stack, "escape")?;
|
||||||
let osc: bool = call.has_flag(engine_state, stack, "osc")?;
|
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 {
|
if list {
|
||||||
return Ok(generate_ansi_code_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 list: bool = call.has_flag_const(working_set, "list")?;
|
||||||
let escape: bool = call.has_flag_const(working_set, "escape")?;
|
let escape: bool = call.has_flag_const(working_set, "escape")?;
|
||||||
let osc: bool = call.has_flag_const(working_set, "osc")?;
|
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 {
|
if list {
|
||||||
return Ok(generate_ansi_code_list(
|
return Ok(generate_ansi_code_list(
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use nu_cmd_base::hook::eval_hook;
|
use nu_cmd_base::hook::eval_hook;
|
||||||
use nu_engine::{command_prelude::*, env_to_strings};
|
use nu_engine::{command_prelude::*, env_to_strings};
|
||||||
use nu_path::{dots::expand_ndots, expand_tilde, AbsolutePath};
|
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_system::ForegroundChild;
|
||||||
use nu_utils::IgnoreCaseExt;
|
use nu_utils::IgnoreCaseExt;
|
||||||
use pathdiff::diff_paths;
|
use pathdiff::diff_paths;
|
||||||
@ -417,7 +419,7 @@ fn write_pipeline_data(
|
|||||||
stack.start_collect_value();
|
stack.start_collect_value();
|
||||||
|
|
||||||
// Turn off color as we pass data through
|
// 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.
|
// Invoke the `table` command.
|
||||||
let output =
|
let output =
|
||||||
|
@ -72,7 +72,7 @@ prints out the list properly."#
|
|||||||
None => None,
|
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))?;
|
let cwd = engine_state.cwd(Some(stack))?;
|
||||||
|
|
||||||
match input {
|
match input {
|
||||||
|
@ -1118,7 +1118,7 @@ enum TableView {
|
|||||||
|
|
||||||
fn maybe_strip_color(output: String, config: &Config) -> String {
|
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
|
// 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
|
// Draw the table without ansi colors
|
||||||
nu_utils::strip_ansi_string_likely(output)
|
nu_utils::strip_ansi_string_likely(output)
|
||||||
} else {
|
} else {
|
||||||
|
@ -258,7 +258,7 @@ fn get_documentation(
|
|||||||
long_desc.push_str(" ");
|
long_desc.push_str(" ");
|
||||||
long_desc.push_str(example.description);
|
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);
|
let _ = write!(long_desc, "\n > {}\n", example.example);
|
||||||
} else {
|
} else {
|
||||||
let code_string = nu_highlight_string(example.example, engine_state, stack);
|
let code_string = nu_highlight_string(example.example, engine_state, stack);
|
||||||
@ -329,7 +329,7 @@ fn get_documentation(
|
|||||||
|
|
||||||
long_desc.push('\n');
|
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)
|
nu_utils::strip_ansi_string_likely(long_desc)
|
||||||
} else {
|
} else {
|
||||||
long_desc
|
long_desc
|
||||||
|
@ -16,7 +16,7 @@ pub use helper::extract_value;
|
|||||||
pub use history::{HistoryConfig, HistoryFileFormat};
|
pub use history::{HistoryConfig, HistoryFileFormat};
|
||||||
pub use hooks::Hooks;
|
pub use hooks::Hooks;
|
||||||
pub use ls::LsConfig;
|
pub use ls::LsConfig;
|
||||||
pub use output::ErrorStyle;
|
pub use output::{ErrorStyle, UseAnsiColoring};
|
||||||
pub use plugin_gc::{PluginGcConfig, PluginGcConfigs};
|
pub use plugin_gc::{PluginGcConfig, PluginGcConfigs};
|
||||||
pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu};
|
pub use reedline::{CursorShapeConfig, EditBindings, NuCursorShape, ParsedKeybinding, ParsedMenu};
|
||||||
pub use rm::RmConfig;
|
pub use rm::RmConfig;
|
||||||
@ -49,7 +49,7 @@ pub struct Config {
|
|||||||
pub footer_mode: FooterMode,
|
pub footer_mode: FooterMode,
|
||||||
pub float_precision: i64,
|
pub float_precision: i64,
|
||||||
pub recursion_limit: i64,
|
pub recursion_limit: i64,
|
||||||
pub use_ansi_coloring: bool,
|
pub use_ansi_coloring: UseAnsiColoring,
|
||||||
pub completions: CompletionConfig,
|
pub completions: CompletionConfig,
|
||||||
pub edit_mode: EditBindings,
|
pub edit_mode: EditBindings,
|
||||||
pub history: HistoryConfig,
|
pub history: HistoryConfig,
|
||||||
@ -106,7 +106,7 @@ impl Default for Config {
|
|||||||
footer_mode: FooterMode::RowCount(25),
|
footer_mode: FooterMode::RowCount(25),
|
||||||
float_precision: 2,
|
float_precision: 2,
|
||||||
buffer_editor: Value::nothing(Span::unknown()),
|
buffer_editor: Value::nothing(Span::unknown()),
|
||||||
use_ansi_coloring: true,
|
use_ansi_coloring: UseAnsiColoring::default(),
|
||||||
bracketed_paste: true,
|
bracketed_paste: true,
|
||||||
edit_mode: EditBindings::default(),
|
edit_mode: EditBindings::default(),
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
use std::io::IsTerminal;
|
||||||
|
|
||||||
use super::{config_update_string_enum, prelude::*};
|
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)]
|
#[derive(Clone, Copy, Debug, IntoValue, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub enum ErrorStyle {
|
pub enum ErrorStyle {
|
||||||
@ -24,3 +26,61 @@ impl UpdateFromValue for ErrorStyle {
|
|||||||
config_update_string_enum(self, value, path, errors)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -70,7 +70,7 @@ impl std::fmt::Debug for CliError<'_> {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let config = self.1.get_config();
|
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;
|
let error_style = &config.error_style;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ pub(crate) fn read_config_file(
|
|||||||
|
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
let config = engine_state.get_config();
|
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
|
// Translate environment variables from Strings to Values
|
||||||
if let Err(e) = convert_env_values(engine_state, stack) {
|
if let Err(e) = convert_env_values(engine_state, stack) {
|
||||||
report_shell_error(engine_state, &e);
|
report_shell_error(engine_state, &e);
|
||||||
@ -53,7 +53,7 @@ pub(crate) fn read_config_file(
|
|||||||
} else {
|
} else {
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
let config = engine_state.get_config();
|
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) {
|
if let Err(e) = convert_env_values(engine_state, stack) {
|
||||||
report_shell_error(engine_state, &e);
|
report_shell_error(engine_state, &e);
|
||||||
}
|
}
|
||||||
|
@ -213,7 +213,7 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
engine_state.history_enabled = parsed_nu_cli_args.no_history.is_none();
|
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
|
// Set up logger
|
||||||
if let Some(level) = parsed_nu_cli_args
|
if let Some(level) = parsed_nu_cli_args
|
||||||
|
@ -197,7 +197,7 @@ pub(crate) fn run_repl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reload use_color from config in case it's different from the default value
|
// 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);
|
perf!("setup_config", start_time, use_color);
|
||||||
|
|
||||||
let start_time = std::time::Instant::now();
|
let start_time = std::time::Instant::now();
|
||||||
|
Reference in New Issue
Block a user