Skip to content

Commit 8a4d205

Browse files
Use handwritten ansi parser (#109)
This removes the heavy `regex` dependency from the ANSI feature. Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
1 parent a66421d commit 8a4d205

File tree

5 files changed

+496
-73
lines changed

5 files changed

+496
-73
lines changed

.github/workflows/ci.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ on:
55
pull_request:
66
branches: [master]
77
jobs:
8+
build-msrv:
9+
name: Build on MSRV (1.40)
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
include:
14+
- os: ubuntu-latest
15+
target: x86_64-unknown-linux-gnu
16+
rust: 1.40.0
17+
- os: windows-latest
18+
target: i686-pc-windows-msvc
19+
rust: 1.40.0
20+
runs-on: ${{ matrix.os }}
21+
steps:
22+
- name: Install rust
23+
uses: actions-rs/toolchain@v1
24+
with:
25+
profile: minimal
26+
toolchain: ${{ matrix.rust }}
27+
target: ${{ matrix.target }}
28+
override: true
29+
- name: Checkout
30+
uses: actions/checkout@v2
31+
- name: Build
32+
uses: actions-rs/cargo@v1
33+
with:
34+
command: check
35+
args: --target ${{ matrix.target }}
836
test:
937
name: Tests
1038
strategy:
@@ -13,19 +41,19 @@ jobs:
1341
include:
1442
- os: macos-latest
1543
target: x86_64-apple-darwin
16-
rust: 1.40.0
44+
rust: 1.51.0
1745
- os: ubuntu-latest
1846
target: x86_64-unknown-linux-gnu
19-
rust: 1.40.0
47+
rust: 1.51.0
2048
- os: ubuntu-latest
2149
target: i686-unknown-linux-gnu
22-
rust: 1.40.0
50+
rust: 1.51.0
2351
- os: windows-latest
2452
target: i686-pc-windows-msvc
25-
rust: 1.40.0
53+
rust: 1.51.0
2654
- os: windows-latest
2755
target: x86_64-pc-windows-msvc
28-
rust: 1.40.0
56+
rust: 1.51.0
2957
- os: ubuntu-latest
3058
target: x86_64-unknown-linux-gnu
3159
rust: stable
@@ -70,7 +98,7 @@ jobs:
7098
uses: actions-rs/toolchain@v1
7199
with:
72100
profile: minimal
73-
toolchain: 1.40.0
101+
toolchain: 1.51.0
74102
target: ${{ matrix.target }}
75103
override: true
76104
- name: Checkout

Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ readme = "README.md"
1313

1414
[features]
1515
default = ["unicode-width", "ansi-parsing"]
16-
windows-console-colors = ["ansi-parsing", "winapi-util"]
17-
ansi-parsing = ["regex"]
16+
windows-console-colors = ["ansi-parsing", "regex", "winapi-util"]
17+
ansi-parsing = []
1818

1919
[dependencies]
2020
once_cell = "1"
@@ -27,3 +27,12 @@ unicode-width = { version = "0.1", optional = true }
2727
winapi = { version = "0.3", features = ["winbase", "winuser", "consoleapi", "processenv", "wincon"] }
2828
winapi-util = { version = "0.1.3", optional = true }
2929
encode_unicode = "0.3"
30+
31+
[dev-dependencies]
32+
criterion = "0.3.5"
33+
proptest = "1.0.0"
34+
regex = "1.4.2"
35+
36+
[[bench]]
37+
name = "ansi_parser"
38+
harness = false

benches/ansi_parser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use console::{strip_ansi_codes, AnsiCodeIterator};
2+
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
3+
4+
use std::{fs, path::Path};
5+
6+
pub fn parse_throughput(c: &mut Criterion) {
7+
let session_log_path = Path::new("tests")
8+
.join("data")
9+
.join("sample_zellij_session.log");
10+
let session_log = fs::read_to_string(session_log_path).unwrap();
11+
12+
let mut group = c.benchmark_group("ansi-parsing");
13+
group.throughput(Throughput::Bytes(session_log.len() as u64));
14+
group.bench_function("parse", |b| {
15+
b.iter(|| {
16+
let v: Vec<_> = AnsiCodeIterator::new(&session_log).collect();
17+
black_box(v);
18+
})
19+
});
20+
group.bench_function("strip", |b| {
21+
b.iter(|| black_box(strip_ansi_codes(&session_log)))
22+
});
23+
group.finish();
24+
}
25+
26+
criterion_group!(throughput, parse_throughput);
27+
criterion_main!(throughput);

0 commit comments

Comments
 (0)