From c857a2784ea7d45f85d853b51e72b3be462922fe Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Tue, 7 Nov 2023 18:31:37 +0100 Subject: [PATCH] Experimental emoji sanitization Related to #59 --- src/cmd/view.rs | 19 ++++++++++++++++++- src/util.rs | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cmd/view.rs b/src/cmd/view.rs index 6a3b1989..93431155 100644 --- a/src/cmd/view.rs +++ b/src/cmd/view.rs @@ -44,6 +44,7 @@ view options: handle them. -e, --expand Expand the table so that in can be easily piped to a pager such as \"less\", with no with constraints. + -E, --sanitize-emojis Replace emojis by their shortcode to avoid formatting issues. Common options: -h, --help Display this message @@ -64,6 +65,7 @@ struct Args { flag_limit: isize, flag_rainbow: bool, flag_expand: bool, + flag_sanitize_emojis: bool, } impl Args { @@ -122,7 +124,15 @@ pub fn run(argv: &[&str]) -> CliResult<()> { match r_iter.next() { None => break, Some((i, record)) => { - records.push(prepend(&record?, &i.to_string())); + let mut record = record?; + + if args.flag_sanitize_emojis { + record = sanitize_emojis(&record); + } + + record = prepend(&record, &i.to_string()); + + records.push(record); if limit > 0 && records.len() == limit { break; @@ -368,6 +378,13 @@ fn prepend(record: &csv::StringRecord, item: &str) -> csv::StringRecord { new_record } +fn sanitize_emojis(record: &csv::StringRecord) -> csv::StringRecord { + record + .iter() + .map(|cell| util::sanitize_emojis(cell)) + .collect() +} + fn adjust_column_widths(widths: &Vec, max_width: usize) -> Vec { widths.iter().map(|m| usize::min(*m, max_width)).collect() } diff --git a/src/util.rs b/src/util.rs index b3d364c8..5e5833b2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -514,7 +514,7 @@ pub fn sanitize_emojis(string: &str) -> String { None => Cow::Borrowed(grapheme), Some(emoji) => Cow::Owned(format!( ":{}:", - emoji.shortcode().unwrap_or("unknown-emoji") + emoji.shortcode().unwrap_or("unknown_emoji") )), }) .collect()