diff --git a/CHANGELOG.md b/CHANGELOG.md index 8399111..a7798ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v1.3.16 - Fix nvim-notify timeout and add render style option +- Filter ANSI escape code by default ## v1.3.15 - Add PlantUML support (ascii output) diff --git a/Cargo.lock b/Cargo.lock index df695c8..9b17954 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,7 +789,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "sniprun" -version = "1.3.15" +version = "1.3.16-beta" dependencies = [ "close_fds", "dirs", diff --git a/doc/sources/README.md b/doc/sources/README.md index d40244e..83f67c3 100755 --- a/doc/sources/README.md +++ b/doc/sources/README.md @@ -274,8 +274,8 @@ require'sniprun'.setup({ display_options = { terminal_scrollback = vim.o.scrollback, --# change terminal display scrollback lines - terminal_line_number = false, --# whether show line number in terminal window - terminal_signcolumn = false, --# whether show signcolumn in terminal window + terminal_line_number = false, --# whether show line number in terminal window + terminal_signcolumn = false, --# whether show signcolumn in terminal window terminal_position = "vertical", --# or "horizontal", to open as horizontal split instead of vertical split terminal_width = 45, --# change the terminal display option width (if vertical) terminal_height = 20, --# change the terminal display option height (if horizontal) @@ -286,7 +286,7 @@ require'sniprun'.setup({ --# no output should display nothing or '(no output)' show_no_output = { "Classic", - "TempFloatingWindow", --# implies LongTempFloatingWindow, which has no effect on its own + "TempFloatingWindow", --# implies LongTempFloatingWindow, which has no effect on its own }, --# customize highlight groups (setting this overrides colorscheme) @@ -301,7 +301,8 @@ require'sniprun'.setup({ live_mode_toggle='off' --# live mode toggle, see Usage - Running for more info --# miscellaneous compatibility/adjustement settings - inline_messages = false, --# boolean toggle for a one-line way to display messages + ansi_escape = true, --# Remove ANSI escapes (usually color) from outputs + inline_messages = false, --# boolean toggle for a one-line way to display output --# to workaround sniprun not being able to display anything borders = 'single', --# display borders around floating windows diff --git a/lua/sniprun.lua b/lua/sniprun.lua index adf9ce1..49be442 100644 --- a/lua/sniprun.lua +++ b/lua/sniprun.lua @@ -49,6 +49,7 @@ M.config_values = { "TempFloatingWindow", -- implies LongTempFloatingWindow, which is not a correct key here }, + ansi_escape = true, inline_messages = 0, borders = 'single', @@ -68,7 +69,6 @@ M.config_values = { neovim_pid = 0, binary_path = binary_path, sniprun_path = sniprun_path, - } diff --git a/src/display.rs b/src/display.rs index 532a9a4..74196c5 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,4 +1,5 @@ use crate::error::SniprunError; +use crate::interpreter::index_from_name; use crate::{DataHolder, ReturnMessageType}; use log::info; use neovim_lib::{Neovim, NeovimApi}; @@ -303,7 +304,8 @@ pub fn display_terminal_with_code( .lines() .fold("".to_string(), |cur_bloc, line_in_bloc| { cur_bloc + "> " + line_in_bloc + "\n" - }) + }), + ansi_option(data) ) .replace('\n', "\\\n"), no_output_wrap(result, data, &DisplayType::TerminalWithCode(filter)) @@ -317,7 +319,8 @@ pub fn display_terminal_with_code( .lines() .fold("".to_string(), |cur_bloc, line_in_bloc| { cur_bloc + "> " + line_in_bloc + "\n" - }) + }), + ansi_option(data) ) .replace('\n', "\\\n"), no_output_wrap( @@ -462,7 +465,7 @@ fn shorten_err(message: &str) -> String { marker } -fn cleanup_and_escape(message: &str) -> String { +fn cleanup_and_escape(message: &str, remove_ansi: bool) -> String { let mut escaped = String::with_capacity(message.len()); for c in message.chars() { match c { @@ -475,6 +478,12 @@ fn cleanup_and_escape(message: &str) -> String { } } + let escaped = if remove_ansi { + String::from_utf8(strip_ansi_escapes::strip(&escaped.into_bytes())).unwrap() + } else { + escaped + }; + //remove trailing /starting newlines let answer_str = escaped .trim_start_matches('\n') @@ -484,7 +493,7 @@ fn cleanup_and_escape(message: &str) -> String { } fn no_output_wrap(message: &str, data: &DataHolder, current_type: &DisplayType) -> String { - let message_clean = cleanup_and_escape(message); + let message_clean = cleanup_and_escape(message, ansi_option(data)); for dt in data.display_no_output.iter() { if dt == current_type && message_clean.is_empty() { info!("Empty message converted to 'no output')"); @@ -494,3 +503,16 @@ fn no_output_wrap(message: &str, data: &DataHolder, current_type: &DisplayType) info!("message '{}' cleaned out", message_clean); message_clean } + +fn ansi_option(data: &DataHolder) -> bool { + if let Some(config) = &data.interpreter_options { + if let Some(ar) = config.as_map() { + if let Some(i) = index_from_name("ansi_escape", ar) { + if let Some(ansi_escape) = ar[i].1.as_bool() { + return ansi_escape; + } + } + } + } + true +} diff --git a/src/interpreter.rs b/src/interpreter.rs index 6403356..1d5b515 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -280,18 +280,6 @@ impl InterpreterUtils for T { /// get an interpreter option fn get_interpreter_option(data: &DataHolder, option: &str) -> Option { - fn index_from_name( - name: &str, - config: &[(neovim_lib::Value, neovim_lib::Value)], - ) -> Option { - for (i, kv) in config.iter().enumerate() { - if name == kv.0.as_str().unwrap_or("") { - return Some(i); - } - } - // info!("key '{}' not found in interpreter option", name); - None - } // this is the ugliness required to fetch something from the interpreter options if let Some(config) = &data.interpreter_options { if let Some(ar) = config.as_map() { @@ -383,3 +371,11 @@ pub trait ReplLikeInterpreter { ))) } } +pub fn index_from_name(name: &str, config: &[(neovim_lib::Value, neovim_lib::Value)]) -> Option { + for (i, kv) in config.iter().enumerate() { + if name == kv.0.as_str().unwrap_or("") { + return Some(i); + } + } + None +}