Skip to content

Commit 04efe31

Browse files
integrated --style-mode from upstream htty PR: 25
1 parent 1cb6c10 commit 04efe31

13 files changed

Lines changed: 1658 additions & 1288 deletions

File tree

docs/htty-core/htty_core.html

Lines changed: 316 additions & 230 deletions
Large diffs are not rendered by default.

docs/htty-core/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/htty.html

Lines changed: 1104 additions & 1040 deletions
Large diffs are not rendered by default.

docs/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

htty-core/src/python/htty_core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
htty-core: A thin wrapper around a forked [ht](https://github.com/andyk/ht) binary for use with [htty](https://matrixmanatyrservice.github.io/htty/htty.html).
33
"""
44

5-
from .core import Cols, Command, HtArgs, HtEvent, Rows, find_ht_binary, run
5+
from .core import Cols, Command, HtArgs, HtEvent, Rows, StyleMode, find_ht_binary, run
66

7-
__all__ = ["HtArgs", "HtEvent", "find_ht_binary", "run", "Command", "Rows", "Cols", "__version__"]
7+
__all__ = ["HtArgs", "HtEvent", "find_ht_binary", "run", "Command", "Rows", "Cols", "StyleMode", "__version__"]
88
# [[[cog
99
# import os
1010
# cog.out(f'__version__ = "{os.environ["HTTY_VERSION"]}"')

htty-core/src/python/htty_core/core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class StrEnum(str, Enum):
1818
# Import constants for type alias annotations
1919
from .constants import DEFAULT_TERMINAL_COLS, DEFAULT_TERMINAL_ROWS
2020

21+
22+
class StyleMode(StrEnum):
23+
"""Style mode for terminal output."""
24+
25+
PLAIN = "plain"
26+
STYLED = "styled"
27+
28+
2129
# Type aliases for common parameters
2230
Command = Annotated[Union[str, list[str]], "run this command (as a subprocess of ht)"]
2331
Rows = Annotated[
@@ -146,11 +154,13 @@ def __init__(
146154
subscribes: Optional[list[HtEvent]] = None,
147155
rows: Rows = None,
148156
cols: Cols = None,
157+
style_mode: Optional[StyleMode] = None,
149158
) -> None:
150159
self.command = command
151160
self.subscribes = subscribes or []
152161
self.rows = rows
153162
self.cols = cols
163+
self.style_mode = style_mode
154164

155165
def get_command(self, ht_binary: Optional[str] = None) -> list[str]:
156166
"""Build the command line arguments for running ht.
@@ -175,6 +185,10 @@ def get_command(self, ht_binary: Optional[str] = None) -> list[str]:
175185
if self.rows is not None and self.cols is not None:
176186
cmd_args.extend(["--size", f"{self.cols}x{self.rows}"])
177187

188+
# Add style mode if specified
189+
if self.style_mode is not None:
190+
cmd_args.extend(["--style-mode", self.style_mode])
191+
178192
# Add separator and the command to run
179193
cmd_args.append("--")
180194
if isinstance(self.command, str):

htty-core/src/rust/api/http.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async fn alis_message(
8686
use session::Event::*;
8787

8888
match event {
89-
Ok(Init(time, cols, rows, _pid, seq, _text)) => Some(Ok(json_message(json!({
89+
Ok(Init(time, cols, rows, _pid, seq, _text, _)) => Some(Ok(json_message(json!({
9090
"time": time,
9191
"cols": cols,
9292
"rows": rows,
@@ -101,7 +101,7 @@ async fn alis_message(
101101
format!("{cols}x{rows}")
102102
])))),
103103

104-
Ok(Snapshot(_, _, _, _)) => None,
104+
Ok(Snapshot(_, _, _, _, _)) => None,
105105

106106
Ok(Pid(_, _)) => None,
107107

@@ -169,7 +169,7 @@ async fn event_stream_message(
169169
Ok(e @ Init(..)) if sub.init => Some(Ok(json_message(e.to_json()))),
170170
Ok(e @ Output(_, _)) if sub.output => Some(Ok(json_message(e.to_json()))),
171171
Ok(e @ Resize(_, _, _)) if sub.resize => Some(Ok(json_message(e.to_json()))),
172-
Ok(e @ Snapshot(_, _, _, _)) if sub.snapshot => Some(Ok(json_message(e.to_json()))),
172+
Ok(e @ Snapshot(_, _, _, _, _)) if sub.snapshot => Some(Ok(json_message(e.to_json()))),
173173
Ok(e @ Pid(_, _)) if sub.pid => Some(Ok(json_message(e.to_json()))),
174174
Ok(e @ ExitCode(_, _)) if sub.exit_code => Some(Ok(json_message(e.to_json()))),
175175
Ok(e @ Debug(_, _)) if sub.debug => Some(Ok(json_message(e.to_json()))),

htty-core/src/rust/api/stdio.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::Subscription;
2+
use crate::cli::StyleMode;
23
use crate::command::{self, Command, InputSeq};
34
use crate::session;
45
use anyhow::Result;
@@ -24,6 +25,11 @@ struct ResizeArgs {
2425
rows: usize,
2526
}
2627

28+
#[derive(Debug, Deserialize)]
29+
struct SetStyleModeArgs {
30+
mode: String,
31+
}
32+
2733
pub async fn start(
2834
command_tx: mpsc::Sender<Command>,
2935
clients_tx: mpsc::Sender<session::Client>,
@@ -88,7 +94,7 @@ pub async fn start(
8894
println!("{}", e.to_json());
8995
}
9096

91-
Some(Ok(e @ Snapshot(_, _, _, _))) if sub.snapshot => {
97+
Some(Ok(e @ Snapshot(_, _, _, _, _))) if sub.snapshot => {
9298
println!("{}", e.to_json());
9399
}
94100

@@ -153,6 +159,13 @@ fn build_command(value: serde_json::Value) -> Result<Command, String> {
153159

154160
Some("takeSnapshot") => Ok(Command::Snapshot),
155161

162+
Some("setStyleMode") => {
163+
let args: SetStyleModeArgs = args_from_json_value(value)?;
164+
let style_mode = args.mode.parse::<StyleMode>()
165+
.map_err(|e| format!("invalid style mode: {}", e))?;
166+
Ok(Command::SetStyleMode(style_mode))
167+
}
168+
156169
Some("exit") => Ok(Command::Exit),
157170

158171
other => Err(format!("invalid command type: {other:?}")),
@@ -323,6 +336,7 @@ fn parse_key(key: String) -> InputSeq {
323336
#[cfg(test)]
324337
mod test {
325338
use super::{cursor_key, parse_line, standard_key, Command};
339+
use crate::cli::StyleMode;
326340
use crate::command::InputSeq;
327341

328342
#[test]
@@ -525,6 +539,21 @@ mod test {
525539
assert!(matches!(command, Command::Snapshot));
526540
}
527541

542+
#[test]
543+
fn parse_set_style_mode() {
544+
let command = parse_line(r#"{ "type": "setStyleMode", "mode": "styled" }"#).unwrap();
545+
assert!(matches!(command, Command::SetStyleMode(StyleMode::Styled)));
546+
547+
let command = parse_line(r#"{ "type": "setStyleMode", "mode": "plain" }"#).unwrap();
548+
assert!(matches!(command, Command::SetStyleMode(StyleMode::Plain)));
549+
}
550+
551+
#[test]
552+
fn parse_set_style_mode_invalid() {
553+
parse_line(r#"{ "type": "setStyleMode", "mode": "invalid" }"#).expect_err("should fail");
554+
parse_line(r#"{ "type": "setStyleMode" }"#).expect_err("should fail");
555+
}
556+
528557
#[test]
529558
fn parse_exit() {
530559
let command = parse_line(r#"{ "type": "exit" }"#).unwrap();

htty-core/src/rust/command.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use crate::cli::StyleMode;
2+
13
#[derive(Debug)]
24
pub enum Command {
35
Input(Vec<InputSeq>),
46
Snapshot,
57
Resize(usize, usize),
8+
SetStyleMode(StyleMode),
69
Exit,
710
Debug(String),
811
Completed(std::path::PathBuf),

htty-core/src/rust/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ async fn run_event_loop(
232232
session.resize(cols, rows);
233233
}
234234

235+
Some(Command::SetStyleMode(style_mode)) => {
236+
session.set_style_mode(style_mode);
237+
}
238+
235239
Some(Command::Debug(message)) => {
236240
// Emit all debug messages as debug events
237241
session.emit_debug_event(&message);

0 commit comments

Comments
 (0)