Skip to content

Commit

Permalink
filter out ansi escape code from output
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelb committed Sep 12, 2024
1 parent 2ad7284 commit c66a977
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions doc/sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/sniprun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand All @@ -68,7 +69,6 @@ M.config_values = {
neovim_pid = 0,
binary_path = binary_path,
sniprun_path = sniprun_path,

}


Expand Down
30 changes: 26 additions & 4 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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))
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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')
Expand All @@ -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')");
Expand All @@ -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
}
20 changes: 8 additions & 12 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,6 @@ impl<T: Interpreter> InterpreterUtils for T {

/// get an interpreter option
fn get_interpreter_option(data: &DataHolder, option: &str) -> Option<neovim_lib::Value> {
fn index_from_name(
name: &str,
config: &[(neovim_lib::Value, neovim_lib::Value)],
) -> Option<usize> {
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() {
Expand Down Expand Up @@ -383,3 +371,11 @@ pub trait ReplLikeInterpreter {
)))
}
}
pub fn index_from_name(name: &str, config: &[(neovim_lib::Value, neovim_lib::Value)]) -> Option<usize> {
for (i, kv) in config.iter().enumerate() {
if name == kv.0.as_str().unwrap_or("") {
return Some(i);
}
}
None
}

0 comments on commit c66a977

Please sign in to comment.