Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support sourcemap #21

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support sourcemap
  • Loading branch information
ahaoboy committed Oct 30, 2024
commit 3695615f0be3f2038402400c728f4d3618f941d3
3 changes: 3 additions & 0 deletions ansi2/Cargo.toml

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

45 changes: 26 additions & 19 deletions ansi2/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ fn merge_range(a: (usize, usize), b: (usize, usize)) -> (usize, usize) {
(a1.min(b1), a2.max(b2))
}

fn offset_range(range: (usize, usize), offset: (usize, usize)) -> (usize, usize) {
let (a1, _) = range;
let (b1, b2) = offset;

(a1 + b1, a1 + b2)
}
impl Canvas {
pub fn new<S: AsRef<str>>(str: S, max_width: Option<usize>) -> Self {
let s = str.as_ref();
Expand Down Expand Up @@ -322,24 +328,25 @@ impl Canvas {
}
}

Sgr::Link(_, title) => match parse_ansi(&title) {
Ok((_, tokens)) => {
// FIXME: Avoid the influence of styles in link on subsequent characters
q.push_front(Token {
range: (0, 0),
sgr: Sgr::ColorReset,
});
for i in tokens.into_iter().rev() {
q.push_front(i);
Sgr::Link(_, title) => {
if title.contains("\x1b") {
if let Ok((_, tokens)) = parse_ansi(&title) {
// FIXME: Avoid the influence of styles in link on subsequent characters
q.push_front(Token {
range,
sgr: Sgr::ColorReset,
});
for mut i in tokens.into_iter().rev() {
i.range = offset_range(i.range, range);
q.push_front(i);
}
q.push_front(Token {
range,
sgr: Sgr::Underline,
});
}
q.push_front(Token {
range: (0, 0),

sgr: Sgr::Underline,
});
}
Err(_) => {
for i in title.chars() {
} else {
for (k, i) in title.chars().enumerate() {
if i == '\n' {
cur_x = 0;
cur_y += 1;
Expand All @@ -363,7 +370,7 @@ impl Canvas {
color_r,
bold_r,
blink_r,
text_r,
text_r: (range.0 + k, range.1 + k),
dim_r,
italic_r,
underline_r,
Expand All @@ -379,7 +386,7 @@ impl Canvas {
cur_x += 1;
}
}
},
}
Sgr::CursorHide => {
hide = true;
hide_r = range;
Expand Down
10 changes: 5 additions & 5 deletions ansi2/src/snapshots/ansi2__test__link_id-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Canvas {
),
text_r: (
0,
1,
44,
),
dim_r: (
0,
Expand Down Expand Up @@ -86,7 +86,7 @@ Canvas {
),
text_r: (
1,
2,
45,
),
dim_r: (
0,
Expand Down Expand Up @@ -138,7 +138,7 @@ Canvas {
),
text_r: (
2,
3,
46,
),
dim_r: (
0,
Expand Down Expand Up @@ -190,7 +190,7 @@ Canvas {
),
text_r: (
3,
4,
47,
),
dim_r: (
0,
Expand Down Expand Up @@ -242,7 +242,7 @@ Canvas {
),
text_r: (
4,
5,
48,
),
dim_r: (
0,
Expand Down
2 changes: 1 addition & 1 deletion ansi2/src/snapshots/ansi2__test__link_id-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ expression: canvas.minify()
),
text_r: (
0,
5,
48,
),
dim_r: (
0,
Expand Down
12 changes: 6 additions & 6 deletions ansi2/src/snapshots/ansi2__test__link_ll-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Canvas {
),
text_r: (
0,
1,
48,
),
dim_r: (
0,
Expand Down Expand Up @@ -86,7 +86,7 @@ Canvas {
),
text_r: (
1,
2,
49,
),
dim_r: (
0,
Expand Down Expand Up @@ -138,7 +138,7 @@ Canvas {
),
text_r: (
2,
3,
50,
),
dim_r: (
0,
Expand Down Expand Up @@ -190,7 +190,7 @@ Canvas {
),
text_r: (
3,
4,
51,
),
dim_r: (
0,
Expand Down Expand Up @@ -242,7 +242,7 @@ Canvas {
),
text_r: (
4,
5,
52,
),
dim_r: (
0,
Expand Down Expand Up @@ -294,7 +294,7 @@ Canvas {
),
text_r: (
5,
6,
53,
),
dim_r: (
0,
Expand Down
2 changes: 1 addition & 1 deletion ansi2/src/snapshots/ansi2__test__link_ll-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ expression: canvas.minify()
),
text_r: (
0,
6,
53,
),
dim_r: (
0,
Expand Down
Loading