Skip to content

Commit c66a977

Browse files
committed
filter out ansi escape code from output
1 parent 2ad7284 commit c66a977

File tree

6 files changed

+42
-22
lines changed

6 files changed

+42
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## v1.3.16
22
- Fix nvim-notify timeout and add render style option
3+
- Filter ANSI escape code by default
34

45
## v1.3.15
56
- Add PlantUML support (ascii output)

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/sources/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ require'sniprun'.setup({
274274
275275
display_options = {
276276
terminal_scrollback = vim.o.scrollback, --# change terminal display scrollback lines
277-
terminal_line_number = false, --# whether show line number in terminal window
278-
terminal_signcolumn = false, --# whether show signcolumn in terminal window
277+
terminal_line_number = false, --# whether show line number in terminal window
278+
terminal_signcolumn = false, --# whether show signcolumn in terminal window
279279
terminal_position = "vertical", --# or "horizontal", to open as horizontal split instead of vertical split
280280
terminal_width = 45, --# change the terminal display option width (if vertical)
281281
terminal_height = 20, --# change the terminal display option height (if horizontal)
@@ -286,7 +286,7 @@ require'sniprun'.setup({
286286
--# no output should display nothing or '(no output)'
287287
show_no_output = {
288288
"Classic",
289-
"TempFloatingWindow", --# implies LongTempFloatingWindow, which has no effect on its own
289+
"TempFloatingWindow", --# implies LongTempFloatingWindow, which has no effect on its own
290290
},
291291
292292
--# customize highlight groups (setting this overrides colorscheme)
@@ -301,7 +301,8 @@ require'sniprun'.setup({
301301
live_mode_toggle='off' --# live mode toggle, see Usage - Running for more info
302302
303303
--# miscellaneous compatibility/adjustement settings
304-
inline_messages = false, --# boolean toggle for a one-line way to display messages
304+
ansi_escape = true, --# Remove ANSI escapes (usually color) from outputs
305+
inline_messages = false, --# boolean toggle for a one-line way to display output
305306
--# to workaround sniprun not being able to display anything
306307
307308
borders = 'single', --# display borders around floating windows

lua/sniprun.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ M.config_values = {
4949
"TempFloatingWindow", -- implies LongTempFloatingWindow, which is not a correct key here
5050
},
5151

52+
ansi_escape = true,
5253
inline_messages = 0,
5354
borders = 'single',
5455

@@ -68,7 +69,6 @@ M.config_values = {
6869
neovim_pid = 0,
6970
binary_path = binary_path,
7071
sniprun_path = sniprun_path,
71-
7272
}
7373

7474

src/display.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::error::SniprunError;
2+
use crate::interpreter::index_from_name;
23
use crate::{DataHolder, ReturnMessageType};
34
use log::info;
45
use neovim_lib::{Neovim, NeovimApi};
@@ -303,7 +304,8 @@ pub fn display_terminal_with_code(
303304
.lines()
304305
.fold("".to_string(), |cur_bloc, line_in_bloc| {
305306
cur_bloc + "> " + line_in_bloc + "\n"
306-
})
307+
}),
308+
ansi_option(data)
307309
)
308310
.replace('\n', "\\\n"),
309311
no_output_wrap(result, data, &DisplayType::TerminalWithCode(filter))
@@ -317,7 +319,8 @@ pub fn display_terminal_with_code(
317319
.lines()
318320
.fold("".to_string(), |cur_bloc, line_in_bloc| {
319321
cur_bloc + "> " + line_in_bloc + "\n"
320-
})
322+
}),
323+
ansi_option(data)
321324
)
322325
.replace('\n', "\\\n"),
323326
no_output_wrap(
@@ -462,7 +465,7 @@ fn shorten_err(message: &str) -> String {
462465
marker
463466
}
464467

465-
fn cleanup_and_escape(message: &str) -> String {
468+
fn cleanup_and_escape(message: &str, remove_ansi: bool) -> String {
466469
let mut escaped = String::with_capacity(message.len());
467470
for c in message.chars() {
468471
match c {
@@ -475,6 +478,12 @@ fn cleanup_and_escape(message: &str) -> String {
475478
}
476479
}
477480

481+
let escaped = if remove_ansi {
482+
String::from_utf8(strip_ansi_escapes::strip(&escaped.into_bytes())).unwrap()
483+
} else {
484+
escaped
485+
};
486+
478487
//remove trailing /starting newlines
479488
let answer_str = escaped
480489
.trim_start_matches('\n')
@@ -484,7 +493,7 @@ fn cleanup_and_escape(message: &str) -> String {
484493
}
485494

486495
fn no_output_wrap(message: &str, data: &DataHolder, current_type: &DisplayType) -> String {
487-
let message_clean = cleanup_and_escape(message);
496+
let message_clean = cleanup_and_escape(message, ansi_option(data));
488497
for dt in data.display_no_output.iter() {
489498
if dt == current_type && message_clean.is_empty() {
490499
info!("Empty message converted to 'no output')");
@@ -494,3 +503,16 @@ fn no_output_wrap(message: &str, data: &DataHolder, current_type: &DisplayType)
494503
info!("message '{}' cleaned out", message_clean);
495504
message_clean
496505
}
506+
507+
fn ansi_option(data: &DataHolder) -> bool {
508+
if let Some(config) = &data.interpreter_options {
509+
if let Some(ar) = config.as_map() {
510+
if let Some(i) = index_from_name("ansi_escape", ar) {
511+
if let Some(ansi_escape) = ar[i].1.as_bool() {
512+
return ansi_escape;
513+
}
514+
}
515+
}
516+
}
517+
true
518+
}

src/interpreter.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,18 +280,6 @@ impl<T: Interpreter> InterpreterUtils for T {
280280

281281
/// get an interpreter option
282282
fn get_interpreter_option(data: &DataHolder, option: &str) -> Option<neovim_lib::Value> {
283-
fn index_from_name(
284-
name: &str,
285-
config: &[(neovim_lib::Value, neovim_lib::Value)],
286-
) -> Option<usize> {
287-
for (i, kv) in config.iter().enumerate() {
288-
if name == kv.0.as_str().unwrap_or("") {
289-
return Some(i);
290-
}
291-
}
292-
// info!("key '{}' not found in interpreter option", name);
293-
None
294-
}
295283
// this is the ugliness required to fetch something from the interpreter options
296284
if let Some(config) = &data.interpreter_options {
297285
if let Some(ar) = config.as_map() {
@@ -383,3 +371,11 @@ pub trait ReplLikeInterpreter {
383371
)))
384372
}
385373
}
374+
pub fn index_from_name(name: &str, config: &[(neovim_lib::Value, neovim_lib::Value)]) -> Option<usize> {
375+
for (i, kv) in config.iter().enumerate() {
376+
if name == kv.0.as_str().unwrap_or("") {
377+
return Some(i);
378+
}
379+
}
380+
None
381+
}

0 commit comments

Comments
 (0)