Skip to content

Commit

Permalink
Experimental emoji sanitization
Browse files Browse the repository at this point in the history
Related to #59
  • Loading branch information
Yomguithereal committed Nov 7, 2023
1 parent 869501d commit c857a27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/cmd/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,6 +65,7 @@ struct Args {
flag_limit: isize,
flag_rainbow: bool,
flag_expand: bool,
flag_sanitize_emojis: bool,
}

impl Args {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<usize>, max_width: usize) -> Vec<usize> {
widths.iter().map(|m| usize::min(*m, max_width)).collect()
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c857a27

Please sign in to comment.