small software-rendered rust tty
better parsing for SGR codes
bendn 9 months ago
parent a959610 · commit 6804f00
-rw-r--r--Cargo.toml1
-rw-r--r--src/colors.rs4
-rw-r--r--src/main.rs2
-rw-r--r--src/term.rs113
-rw-r--r--x92
5 files changed, 150 insertions, 62 deletions
diff --git a/Cargo.toml b/Cargo.toml
index c65b6fd..8c2289f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ edition = "2024"
anstream = "0.6.18"
anyhow = "1.0.98"
atools = "0.1.6"
+chumsky = { git = "https://github.com/zesterer/chumsky", version = "0.11.0", features = ["nightly"] }
color-hex = "0.2.0"
ctlfun = { git = "https://github.com/bend-n/ctlfun" }
fimg = { git = "https://github.com/bend-n/fimg" }
diff --git a/src/colors.rs b/src/colors.rs
index 1a67947..effec8d 100644
--- a/src/colors.rs
+++ b/src/colors.rs
@@ -21,6 +21,10 @@ pub const FOUR: [[u8; 3]; 16] = [
[255, 255, 255],
];
+pub fn four(x: u16) -> [u8; 3] {
+ FOUR[x.min(0xf) as usize]
+}
+
pub const EIGHT: [[u8; 3]; 256] = [
[0, 0, 0],
[128, 0, 0],
diff --git a/src/main.rs b/src/main.rs
index bcba804..dbb9e93 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@
deadline_api,
deref_patterns,
generic_const_exprs,
+ impl_trait_in_bindings,
if_let_guard
)]
use std::fs::File;
@@ -127,6 +128,7 @@ fn main() -> Result<()> {
Backspace => b"",
Equal if shifting => b"+",
Equal => b"=",
+ Tab => b"\t",
Minus if shifting => b"_",
Minus => b"-",
LeftBracket if shifting => b"{",
diff --git a/src/term.rs b/src/term.rs
index f99a8b8..481b233 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -92,54 +92,32 @@ impl Terminal {
params,
end: b'm',
..
- }) => match params {
- &[Value(0)] => self.style = Style::default(),
- &[Value(x @ (30..=37))] => {
- self.style.color = colors::FOUR[x as usize - 30]
- }
- &[Value(39)] => self.style.color = colors::FOREGROUND,
- &[Value(x @ (40..=47))] => {
- self.style.bg = colors::FOUR[x as usize - 40]
- }
- &[Value(49)] => self.style.bg = colors::BACKGROUND,
- &[Value(x @ (90..=97))] => {
- self.style.color = colors::FOUR[x as usize - 82]
- }
- &[Value(x @ (100..=107))] => {
- self.style.bg = colors::FOUR[x as usize - 92]
- }
- &[Value(38), Value(5), Value(i)] => {
- self.style.color = colors::EIGHT[i.min(0xff) as usize];
- }
- &[Value(48), Value(5), Value(i)] => {
- self.style.bg = colors::EIGHT[i.min(0xff) as usize];
- }
- &[Value(38), Value(2), Value(r), Value(g), Value(b)] => {
- self.style.color =
- [r, g, b].map(|x| x.min(0xff) as u8);
- }
- &[Value(48), Value(2), Value(r), Value(g), Value(b)] => {
- self.style.bg = [r, g, b].map(|x| x.min(0xff) as u8);
- }
- &[
- Value(38),
- Value(2),
- Value(r),
- Value(g),
- Value(b),
- Value(48),
- Value(2),
- Value(rb),
- Value(gb),
- Value(bb),
- ] => {
- self.style.bg =
- [rb, gb, bb].map(|x| x.min(0xff) as u8);
- self.style.color =
- [r, g, b].map(|x| x.min(0xff) as u8);
+ }) if params.is_empty() => {
+ self.style = Style::default();
+ }
+ Control(ControlFunction {
+ start: b'[',
+ params,
+ end: b'm',
+ ..
+ }) => {
+ let input: Vec<u16> =
+ params.iter().map(_.value_or(0)).collect();
+ for &action in parse_sgr()
+ .parse(&input)
+ .into_result()
+ .iter()
+ .flatten()
+ {
+ use StyleAction::*;
+ match action {
+ Reset => self.style = Style::default(),
+ SetFg(c) => self.style.color = c,
+ SetBg(c) => self.style.bg = c,
+ _ => {}
+ }
}
- _ => {}
- },
+ }
Control(ControlFunction {
start: b'[',
params,
@@ -217,3 +195,44 @@ impl Terminal {
}
}
}
+#[derive(Clone, Copy, Debug)]
+enum StyleAction {
+ Reset,
+ ModeSet(u16),
+ SetFg([u8; 3]),
+ SetBg([u8; 3]),
+}
+fn value<'a>(
+ r: impl std::ops::RangeBounds<u16>,
+) -> impl Parser<'a, &'a [u16], u16> {
+ any().filter(move |x| r.contains(x))
+}
+use chumsky::prelude::*;
+#[implicit_fn::implicit_fn]
+fn parse_sgr<'a>() -> impl Parser<'a, &'a [u16], Vec<StyleAction>> {
+ use StyleAction::*;
+ let color = choice((
+ // 5;n
+ just(5)
+ .ignore_then(any())
+ .map(|x: u16| colors::EIGHT[x.min(0xff) as usize]),
+ just(2)
+ .ignore_then(any().repeated().collect_exactly::<[u16; 3]>())
+ .map(_.map(|x| x.min(0xff) as u8)),
+ ));
+ choice((
+ just(0u16).to(Reset),
+ value(1..10).map(ModeSet),
+ value(30..=37).map(_ - 30).map(colors::four).map(SetFg),
+ just(38).ignore_then(color.map(SetFg)),
+ just(39).to(SetFg(colors::FOREGROUND)),
+ value(40..=47).map(_ - 40).map(colors::four).map(SetBg),
+ just(48).ignore_then(color.map(SetBg)),
+ just(49).to(SetBg(colors::BACKGROUND)),
+ value(90..=97).map(_ - 82).map(colors::four).map(SetFg),
+ value(100..=107).map(_ - 92).map(colors::four).map(SetBg),
+ ))
+ .repeated()
+ .collect()
+ .labelled("style")
+}
diff --git a/x b/x
index 543ad87..bf884e9 100644
--- a/x
+++ b/x
@@ -1,19 +1,81 @@
-]0;os@klunk:~/pattypan[?2004h[os@klunk pattypan]$ trans :zh hi
-[?2004l hi
-/hī/
+[?2004h[os@klunk pattypan]$ pastel pick
+[?2004l
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
+  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ 
-你好
-(Nǐ hǎo)
-Definitions of hi
-[ English -> 简体中文 ]
+ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ Hex: #5f4312
+ ▀▀            ▀▀ RGB: rgb(95, 67, 18)
+ ▀▀            ▀▀ HSL: hsl(38, 68.1%, 22.2%)
+ ▀▀            ▀▀
+ ▀▀            ▀▀ Most similar:
+ ▀▀            ▀▀       saddlebrown
+ ▀▀            ▀▀       sienna
+ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀       darkolivegreen
-interjection
- 嗨!
- Hi!, Hey!, Alas!, Oh!
- 你好!
- Hello!, Hi!, Hallo!
+[?2004h[os@klunk pattypan]$ carg
+[?2004l Rust's package manager
-hi
- 你好, 嗨
-]0;os@klunk:~/pattypan[?2004h[os@klunk pattypan]$ \ No newline at end of file
+Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
+ cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]...
+
+Options:
+ -V, --version Print version info and exit
+ --list List installed commands
+ --explain <CODE> Provide a detailed explanation of a rustc error message
+ -v, --verbose... Use verbose output (-vv very verbose/build.rs output)
+ -q, --quiet Do not print cargo log messages
+ --color <WHEN> Coloring [possible values: auto, always, never]
+ -C <DIRECTORY> Change to DIRECTORY before doing anything (nightly-only)
+ --locked Assert that `Cargo.lock` will remain unchanged
+ --offline Run without accessing the network
+ --frozen Equivalent to specifying both --locked and --offline
+ --config <KEY=VALUE|PATH> Override a configuration value
+ -Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for
+ details
+ -h, --help Print help
+
+Commands:
+ build, b Compile the current package
+ check, c Analyze the current package and report errors, but don't build object files
+ clean Remove the target directory
+ doc, d Build this package's and its dependencies' documentation
+ new Create a new cargo package
+ init Create a new cargo package in an existing directory
+ add Add dependencies to a manifest file
+ remove Remove dependencies from a manifest file
+ run, r Run a binary or example of the local package
+ test, t Run the tests
+ bench Run the benchmarks
+ update Update dependencies listed in Cargo.lock
+ search Search registry for crates
+ publish Package and upload this package to the registry
+ install Install a Rust binary
+ uninstall Uninstall a Rust binary
+ ... See all commands with --list
+
+See 'cargo help <command>' for more information on a specific command.
+[?2004h[os@klunk pattypan]$ \ No newline at end of file