fix(preview): add a post-processing step to clean out ansi text from non-displayable characters (#509)

This commit is contained in:
Alexandre Pasmantier 2025-05-14 22:57:43 +02:00 committed by GitHub
parent cfe49ce81c
commit 1741a15e52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View File

@ -12,7 +12,10 @@ use tracing::debug;
use crate::{
channels::{entry::Entry, preview::PreviewCommand},
utils::command::shell_command,
utils::{
command::shell_command,
strings::{replace_non_printable, ReplaceNonPrintableConfig},
},
};
pub mod state;
@ -211,7 +214,12 @@ pub fn try_preview(
let preview: Preview = {
if child.status.success() {
let content = String::from_utf8_lossy(&child.stdout);
let (content, _) = replace_non_printable(
&child.stdout,
ReplaceNonPrintableConfig::default()
.keep_line_feed()
.keep_control_characters(),
);
Preview::new(
&entry.name,
content.to_string(),
@ -219,7 +227,12 @@ pub fn try_preview(
u16::try_from(content.lines().count()).unwrap_or(u16::MAX),
)
} else {
let content = String::from_utf8_lossy(&child.stderr);
let (content, _) = replace_non_printable(
&child.stderr,
ReplaceNonPrintableConfig::default()
.keep_line_feed()
.keep_control_characters(),
);
Preview::new(
&entry.name,
content.to_string(),

View File

@ -210,6 +210,16 @@ impl ReplaceNonPrintableConfig {
self.tab_width = tab_width;
self
}
pub fn keep_line_feed(&mut self) -> &mut Self {
self.replace_line_feed = false;
self
}
pub fn keep_control_characters(&mut self) -> &mut Self {
self.replace_control_characters = false;
self
}
}
impl Default for ReplaceNonPrintableConfig {