Skip to content

Commit

Permalink
Upgrade avt to the latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Oct 14, 2024
1 parent c6c9873 commit 0c8f301
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 56 deletions.
37 changes: 8 additions & 29 deletions native/vt_nif/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 native/vt_nif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ crate-type = ["dylib"]

[dependencies]
rustler = "0.27.0"
avt = "0.13.0"
avt = "0.14.0"
53 changes: 30 additions & 23 deletions native/vt_nif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,30 @@ fn dump_screen(env: Env, resource: ResourceArc<VtResource>) -> NifResult<(Atom,
let lines = vt
.view()
.iter()
.map(|line| {
line.group(|c, w| {
w > 1
|| BOX_DRAWING_RANGE.contains(c)
|| BRAILLE_PATTERNS_RANGE.contains(c)
|| BLOCK_ELEMENTS_RANGE.contains(c)
|| POWERLINE_TRIANGLES_RANGE.contains(c)
})
.map(|segment| segment_to_term(segment, env))
.collect::<Vec<_>>()
})
.map(|line| line_to_terms(line, env))
.collect::<Vec<_>>();

let cursor: Option<(usize, usize)> = vt.cursor().into();

Ok((atoms::ok(), (lines, cursor).encode(env)))
}

fn line_to_terms<'a>(line: &avt::Line, env: Env<'a>) -> Vec<Term<'a>> {
line.chunks(|c1, c2| c1.pen() != c2.pen() || is_special_char(c1) || is_special_char(c2))
.map(|cells| chunk_to_term(cells, env))
.collect::<Vec<_>>()
}

fn is_special_char(cell: &avt::Cell) -> bool {
let ch = &cell.char();

cell.width() > 1
|| BOX_DRAWING_RANGE.contains(ch)
|| BRAILLE_PATTERNS_RANGE.contains(ch)
|| BLOCK_ELEMENTS_RANGE.contains(ch)
|| POWERLINE_TRIANGLES_RANGE.contains(ch)
}

#[rustler::nif]
fn text(resource: ResourceArc<VtResource>) -> NifResult<String> {
let vt = convert_err(resource.vt.read(), "rw_lock")?;
Expand All @@ -112,11 +118,12 @@ fn text(resource: ResourceArc<VtResource>) -> NifResult<String> {
Ok(text.join(""))
}

fn segment_to_term(segment: avt::Segment, env: Env) -> Term {
let txt = segment.text();
fn chunk_to_term(cells: Vec<avt::Cell>, env: Env) -> Term {
let txt: String = cells.iter().map(|c| c.char()).collect();
let pen = cells[0].pen();
let mut pairs: Vec<(String, Term)> = Vec::new();

match segment.foreground() {
match pen.foreground() {
Some(avt::Color::Indexed(c)) => {
pairs.push(("fg".to_owned(), c.encode(env)));
}
Expand All @@ -129,7 +136,7 @@ fn segment_to_term(segment: avt::Segment, env: Env) -> Term {
None => (),
}

match segment.background() {
match pen.background() {
Some(avt::Color::Indexed(c)) => {
pairs.push(("bg".to_owned(), c.encode(env)));
}
Expand All @@ -142,37 +149,37 @@ fn segment_to_term(segment: avt::Segment, env: Env) -> Term {
None => (),
}

if segment.is_bold() {
if pen.is_bold() {
pairs.push(("bold".to_owned(), true.encode(env)));
}

if segment.is_faint() {
if pen.is_faint() {
pairs.push(("faint".to_owned(), true.encode(env)));
}

if segment.is_italic() {
if pen.is_italic() {
pairs.push(("italic".to_owned(), true.encode(env)));
}

if segment.is_underline() {
if pen.is_underline() {
pairs.push(("underline".to_owned(), true.encode(env)));
}

if segment.is_strikethrough() {
if pen.is_strikethrough() {
pairs.push(("strikethrough".to_owned(), true.encode(env)));
}

if segment.is_blink() {
if pen.is_blink() {
pairs.push(("blink".to_owned(), true.encode(env)));
}

if segment.is_inverse() {
if pen.is_inverse() {
pairs.push(("inverse".to_owned(), true.encode(env)));
}

let attrs = Term::map_from_pairs(env, &pairs).unwrap();

(txt, attrs, segment.char_width()).encode(env)
(txt, attrs, cells[0].width()).encode(env)
}

fn convert_err<T, E>(result: Result<T, E>, error: &'static str) -> Result<T, Error> {
Expand Down
9 changes: 6 additions & 3 deletions test/asciinema/vt_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ defmodule Asciinema.VtTest do
Vt.with_vt(8, 3, fn vt ->
Vt.feed(vt, "foobar\r\n")
Vt.feed(vt, "baz")
Vt.feed(vt, "qux")
Vt.feed(vt, "")
Vt.dump_screen(vt)
end)

assert {:ok,
{[[{"foobar ", %{}, 1}], [{"bazqux ", %{}, 1}], [{" ", %{}, 1}]], {6, 1}}} =
result
{[
[{"foobar ", %{}, 1}],
[{"baz", %{}, 1}, {"全", %{}, 2}, {" ", %{}, 1}],
[{" ", %{}, 1}]
], {4, 1}}} = result
end

test "feeding it a lot of data" do
Expand Down

0 comments on commit 0c8f301

Please sign in to comment.