Skip to content

Commit f66f939

Browse files
committed
feat: debug output text
1 parent 20fb864 commit f66f939

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/main.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,28 @@ use crate::infra::result::IntoResult;
2020
use anyhow::Result;
2121
use clap::CommandFactory;
2222
use clap::Parser;
23+
use colored::Colorize;
2324
use std::env;
2425

2526
pub mod api;
2627
pub mod args;
2728
pub mod display;
2829
pub mod infra;
2930

31+
fn show_non_printable_chars(text: String) -> String {
32+
#[inline]
33+
fn make_red(str: &str) -> String {
34+
format!("{}", str.red())
35+
}
36+
37+
text.replace(' ', &make_red("·"))
38+
.replace('\0', &make_red("␀\0"))
39+
.replace('\t', &make_red("␉\t"))
40+
.replace('\n', &make_red("␊\n"))
41+
.replace('\r', &make_red("␍\r"))
42+
.replace("\r\n", &make_red("␍␊\r\n"))
43+
}
44+
3045
fn panic_if_err<T>(result: &Result<T>) {
3146
if let Err(e) = result {
3247
panic!("{}", e)
@@ -160,13 +175,22 @@ async fn main() -> Result<()> {
160175
return ().into_ok();
161176
}
162177

163-
if output.ends_with("\n\n") {
164-
print!("{}", &output[..output.len() - 1]);
165-
} else if output.ends_with('\n') {
166-
print!("{}", output);
167-
} else {
168-
println!("{}", output);
169-
}
178+
let output = {
179+
let output = if output.ends_with("\n\n") {
180+
output[..output.len() - 1].to_owned()
181+
} else if output.ends_with('\n') {
182+
output
183+
} else {
184+
format!("{}\n", output)
185+
};
186+
if args.debug {
187+
show_non_printable_chars(output)
188+
} else {
189+
output
190+
}
191+
};
192+
193+
print!("{}", output);
170194

171195
().into_ok()
172196
}

0 commit comments

Comments
 (0)