diff --git a/ansi2/src/lex.rs b/ansi2/src/lex.rs index 8351e62..3725bc8 100644 --- a/ansi2/src/lex.rs +++ b/ansi2/src/lex.rs @@ -138,6 +138,9 @@ pub enum Token { Sgr3(u32, u32, u32), Sgr4(u32, u32, u32, u32), + // url, title + Link(String, String), + AlternativeFont(u32), NotReversed, Faint, @@ -471,6 +474,14 @@ fn parse_sgr6(input: &str) -> IResult<&str, Token> { Token::ColorFgBg(AnsiColor::Color8(n), AnsiColor::Rgb(r, g, b)), )); } + + if ctrl == 48 && ty == 5 && g == 38 && b == 5 { + return Ok(( + rem, + Token::ColorFgBg(AnsiColor::Color256(r), AnsiColor::Color256(n)), + )); + } + todo!() } fn parse_sgr10(input: &str) -> IResult<&str, Token> { @@ -549,6 +560,18 @@ fn parse_unknown(input: &str) -> IResult<&str, Token> { Ok((rem, Token::Unknown(n as u32))) } + +fn parse_link(input: &str) -> IResult<&str, Token> { + let (rem, (_, url, _, title, _)) = tuple(( + tag("\x1b]8;;"), + take_until("\\"), + tag("\\"), + take_until("]8;;\\"), + tag("]8;;\\"), + ))(input)?; + return Ok((rem, Token::Link(url.to_string(), title.to_string()))); +} + pub(crate) fn parse_ansi(input: &str) -> IResult<&str, Vec> { many0(alt(( alt(( @@ -597,6 +620,7 @@ pub(crate) fn parse_ansi(input: &str) -> IResult<&str, Vec> { parse_sgr6, parse_sgr10, )), + parse_link, parse_unknown, parse_anychar, )))(input) diff --git a/ansi2/src/lib.rs b/ansi2/src/lib.rs index 455dfd3..369b8a5 100644 --- a/ansi2/src/lib.rs +++ b/ansi2/src/lib.rs @@ -201,6 +201,24 @@ impl Canvas { Token::ColorDefaultBackground => { cur_bg_c = AnsiColor::Color8(0); } + + Token::Link(_, title) => { + for i in title.chars() { + let node = Node { + char: i, + bg_color: cur_bg_c, + color: cur_c, + bold, + blink, + }; + if cur_x >= max_width { + cur_x = 0; + cur_y += 1; + } + set_node(&mut pixels, node, cur_x, cur_y); + cur_x += 1; + } + } _ => {} } @@ -263,7 +281,22 @@ mod test { } #[test] fn test_base() { - let s = "black bright black black bright black"; + let s = + "black bright black black bright black"; + let r = parse_ansi(s).unwrap(); + println!("{:?}", r); + } + + #[test] + fn test_link() { + let s = "]8;;file:///Users/xxx/src/new-nu-parser/Cargo.toml\\Cargo.toml]8;;"; + let r = parse_ansi(s).unwrap(); + println!("{:?}", r); + } + + #[test] + fn test_sgr6() { + let s = ""; let r = parse_ansi(s).unwrap(); println!("{:?}", r); } diff --git a/assets/table.ans b/assets/table.ans new file mode 100644 index 0000000..8752e61 --- /dev/null +++ b/assets/table.ans @@ -0,0 +1,11 @@ +╭─#─┬────name────┬─type─┬──size───┬───modified───╮ +│ 0 │ ]8;;file:///Users/xxx/src/new-nu-parser/Cargo.lock\Cargo.lock]8;;\ │ file │ 60.5 KB │ 12 hours ago │ +│ 1 │ ]8;;file:///Users/xxx/src/new-nu-parser/Cargo.toml\Cargo.toml]8;;\ │ file │ 463 B │ 12 hours ago │ +│ 2 │ ]8;;file:///Users/xxx/src/new-nu-parser/LICENSE\LICENSE]8;;\ │ file │ 1.1 KB │ 12 hours ago │ +│ 3 │ ]8;;file:///Users/xxx/src/new-nu-parser/README.md\README.md]8;;\ │ file │ 4.2 KB │ 12 hours ago │ +│ 4 │ ]8;;file:///Users/xxx/src/new-nu-parser/benches\benches]8;;\ │ dir │ 128 B │ 12 hours ago │ +│ 5 │ ]8;;file:///Users/xxx/src/new-nu-parser/src\src]8;;\ │ dir │ 448 B │ 12 hours ago │ +│ 6 │ ]8;;file:///Users/xxx/src/new-nu-parser/target\target]8;;\ │ dir │ 256 B │ 11 hours ago │ +│ 7 │ ]8;;file:///Users/xxx/src/new-nu-parser/tests\tests]8;;\ │ dir │ 1.0 KB │ 12 hours ago │ +│ 8 │ ]8;;file:///Users/xxx/src/new-nu-parser/toolkit.nu\toolkit.nu]8;;\ │ file │ 436 B │ 12 hours ago │ +╰───┴────────────┴──────┴─────────┴──────────────╯ diff --git a/readme.md b/readme.md index 1cbb75f..d8f4277 100644 --- a/readme.md +++ b/readme.md @@ -77,4 +77,9 @@ neofetch | ansi2 --format=svg --mode=dark > neofetch.svg - \ No newline at end of file + + + +## todo + +- [ ] link \ No newline at end of file