Skip to content

Commit

Permalink
new: improved message formatting in case of special characters (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed May 13, 2024
1 parent 27159e1 commit 1e04aac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [".", "crate/encstr"]
[workspace.package]
repository = "https://github.com/pamburus/hl"
authors = ["Pavel Ivanov <mr.pavel.ivanov@gmail.com>"]
version = "0.29.4-alpha.3"
version = "0.29.4-alpha.4"
edition = "2021"
license = "MIT"

Expand Down
77 changes: 72 additions & 5 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,45 @@ pub mod string {

let begin = buf.len();
MessageFormatRaw::new(self.string).format(buf)?;
if buf[begin..].starts_with(b"\"") || buf[begin..].contains(&b'=') {
buf.truncate(begin);
MessageFormatDoubleQuoted::new(self.string).format(buf)?;

let mut mask = Mask::none();

buf[begin..].iter().map(|&c| CHAR_GROUPS[c as usize]).for_each(|group| {
mask |= group;
});

if !mask.intersects(Mask::EqualSign | Mask::Control | Mask::Backslash)
&& !matches!(buf[begin..], [b'"', ..] | [b'\'', ..] | [b'`', ..])
{
return Ok(());
}
Ok(())

if !mask.intersects(Mask::DoubleQuote | Mask::Control | Mask::Backslash) {
buf.push(b'"');
buf.push(b'"');
buf[begin..].rotate_right(1);
return Ok(());
}

if !mask.intersects(Mask::SingleQuote | Mask::Control | Mask::Backslash) {
buf.push(b'\'');
buf.push(b'\'');
buf[begin..].rotate_right(1);
return Ok(());
}

const Z: Mask = Mask::none();
const XS: Mask = Mask::Control.or(Mask::ExtendedSpace);

if matches!(mask.and(Mask::Backtick.or(XS)), Z | XS) {
buf.push(b'`');
buf.push(b'`');
buf[begin..].rotate_right(1);
return Ok(());
}

buf.truncate(begin);
MessageFormatDoubleQuoted::new(self.string).format(buf)
}
}

Expand Down Expand Up @@ -1268,6 +1302,39 @@ mod tests {
};

let result = format_no_color(&rec);
assert_eq!(&result, r#""\"hello, world\"""#, "{}", result);
assert_eq!(&result, r#"'"hello, world"'"#, "{}", result);
}

#[test]
fn test_message_single_quoted() {
let rec = Record {
message: Some(EncodedString::raw(r#"'hello, world'"#).into()),
..Default::default()
};

let result = format_no_color(&rec);
assert_eq!(&result, r#""'hello, world'""#, "{}", result);
}

#[test]
fn test_message_single_and_double_quoted() {
let rec = Record {
message: Some(EncodedString::raw(r#"'hello, "world"'"#).into()),
..Default::default()
};

let result = format_no_color(&rec);
assert_eq!(&result, r#"`'hello, "world"'`"#, "{}", result);
}

#[test]
fn test_message_control_chars() {
let rec = Record {
message: Some(EncodedString::raw("hello, \x1b[33mworld\x1b[0m").into()),
..Default::default()
};

let result = format_no_color(&rec);
assert_eq!(&result, r#""hello, \u001b[33mworld\u001b[0m""#, "{}", result);
}
}

0 comments on commit 1e04aac

Please sign in to comment.