Skip to content

Commit

Permalink
support link
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Oct 16, 2024
1 parent 94044fd commit 8ad37e2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
24 changes: 24 additions & 0 deletions ansi2/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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<Token>> {
many0(alt((
alt((
Expand Down Expand Up @@ -597,6 +620,7 @@ pub(crate) fn parse_ansi(input: &str) -> IResult<&str, Vec<Token>> {
parse_sgr6,
parse_sgr10,
)),
parse_link,
parse_unknown,
parse_anychar,
)))(input)
Expand Down
35 changes: 34 additions & 1 deletion ansi2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
_ => {}
}

Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions assets/table.ans
Original file line number Diff line number Diff line change
@@ -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 │
╰───┴────────────┴──────┴─────────┴──────────────╯
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ neofetch | ansi2 --format=svg --mode=dark > neofetch.svg
<a href="https://github.com/ahaoboy/ansi2">
<img src="assets/vitest.svg">
</a>
</div>
</div>


## todo

- [ ] link

0 comments on commit 8ad37e2

Please sign in to comment.