Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(tui): a theme recolours the panes, its accent is a colour, boxes stay (#520)

lingrde4a4ba00417d
diff --git a/tui/Cargo.lock b/tui/Cargo.lock
index a6b62f05d..073ae2671 100644
--- a/tui/Cargo.lock
+++ b/tui/Cargo.lock
@@ -653,7 +653,7 @@ dependencies = [
[[package]]
name = "harness-tui"
-version = "0.1.7"
+version = "0.1.8"
dependencies = [
"alacritty_terminal",
"crossterm",
diff --git a/tui/Cargo.toml b/tui/Cargo.toml
index 90def56b7..33aa73108 100644
--- a/tui/Cargo.toml
+++ b/tui/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "harness-tui"
-version = "0.1.7"
+version = "0.1.8"
edition = "2024"
[dependencies]
diff --git a/tui/src/config.rs b/tui/src/config.rs
index 57aaa1f22..2996806ab 100644
--- a/tui/src/config.rs
+++ b/tui/src/config.rs
@@ -167,7 +167,9 @@ impl Look {
let preset = self.preset.as_deref().unwrap_or("classic");
// `@hn-look` tells structure which bundle is on; `@hn-focus` (from the preset or an
// explicit `focus`) tells how panes are drawn. Both are options, so `hn show` sees them.
- let mut out: Vec<(String, String)> = vec![("@hn-look".into(), preset.to_string())];
+ // (Only a preset the file names: none named is hn's own look, not one written back as
+ // `classic` the next time a setting is saved.)
+ let mut out: Vec<(String, String)> = self.preset.iter().map(|p| ("@hn-look".to_string(), p.clone())).collect();
// A preset's `@hn-focus` is its default; an explicit `focus` knob overrides it, so do not
// emit the preset's when the file named one (the later assignment would win anyway).
for (n, v) in Self::look_preset(preset) {
diff --git a/tui/src/options/mod.rs b/tui/src/options/mod.rs
index 212c95c70..e8d4122bb 100644
--- a/tui/src/options/mod.rs
+++ b/tui/src/options/mod.rs
@@ -291,13 +291,12 @@ impl Store {
/// blurred surfaces alike. Off unless chosen.
pub fn dim_others(&self) -> bool { self.get("@hn-dim", "", None).as_deref() == Some("on") }
- /// `@hn-border` as chosen — or, not chosen, `box` except under `@hn-look classic` (a look
- /// from before boxes, whose panes keep their lines until you choose).
+ /// `@hn-border` as chosen — `box` unless it says `line`. (`@hn-look classic` is not a choice
+ /// of lines: hn wrote it into every `[look]` it saved, a theme picked or a status bar moved.)
pub fn border_style(&self) -> &'static str {
match self.get("@hn-border", "", None).as_deref() {
Some("line") => "line",
- Some(_) => "box",
- None => if self.get("@hn-look", "", None).as_deref() == Some("classic") { "line" } else { "box" },
+ _ => "box",
}
}
diff --git a/tui/src/theme.rs b/tui/src/theme.rs
index 7d664b465..7d49371c8 100644
--- a/tui/src/theme.rs
+++ b/tui/src/theme.rs
@@ -437,10 +437,13 @@ pub fn accent_of(bg: [u8; 3], colours: &[(usize, [u8; 3])]) -> [u8; 3] {
if mx == 0.0 { 0.0 } else { (mx - mn) / mx }
};
let bg_lum = lum(bg);
+ // (Colour 7 is the palette's white: never an accent, or a dark theme's focused border is just
+ // its text colour. The rest: as far from the background as it is vivid, both 0 to 1.)
+ let hues: Vec<[u8; 3]> = colours.iter().filter(|(i, _)| (1..=6).contains(i)).map(|(_, c)| *c).collect();
let mut best = colours.iter().find(|(i, _)| *i == 6).or(colours.first()).map(|(_, c)| *c).unwrap_or([95, 215, 230]);
let mut best_score = f64::MIN;
- for (_, p) in colours {
- let score = (lum(*p) - bg_lum).abs() * 0.7 + chroma(*p) * 0.3;
+ for p in &hues {
+ let score = (lum(*p) - bg_lum).abs() / 255.0 + chroma(*p);
if score > best_score { best_score = score; best = *p; }
}
best
@@ -1473,5 +1476,14 @@ mod accent_theme_tests {
assert!(a.len() == 7 && a.starts_with('#'));
let b = theme_accent_hex("Gruvbox Dark").expect("known theme has an accent");
assert_ne!(a, b);
+ // A colour, never the theme's white or its text: Dracula's focused border is not #f8f8f2.
+ for name in ["Dracula", "Atom One Dark", "Gruvbox Dark", "Adwaita", "Nord"] {
+ let Some(t) = crate::terminal_themes::TERMINAL_THEMES.iter().find(|t| t.name == name) else { continue };
+ let c = theme_accent_rgb(t);
+ assert_ne!(c, t.foreground, "{name}");
+ assert_ne!(c, t.palette[7], "{name}");
+ let (mx, mn) = (c.iter().max().copied().unwrap_or(0) as f64, c.iter().min().copied().unwrap_or(0) as f64);
+ assert!(mx > 0.0 && (mx - mn) / mx > 0.25, "{name}: {c:?} is a colour");
+ }
}
}
diff --git a/tui/src/ui.rs b/tui/src/ui.rs
index 9b5314000..2bac423e5 100644
--- a/tui/src/ui.rs
+++ b/tui/src/ui.rs
@@ -369,9 +369,14 @@ fn which_key(buf: &mut Buffer, app: &App, body: Rect) {
fn window(buf: &mut Buffer, app: &mut App, body: Rect) -> Option<Position> {
let focus = app.focused();
let surfaces = app.options.pane_look();
+ // A theme chosen in Appearance is the panes' too, as a terminal's theme is: their default text
+ // and background, and the sixteen colours programs name (None: the terminal's own).
+ let themed = chosen_theme(app);
if surfaces {
crate::term_out::clear_extras(body);
buf.set_style(body, Style::default().bg(Color::Reset));
+ } else if let Some(t) = themed {
+ buf.set_style(body, Style::default().bg(theme::depth_fit(rgb(t.background))));
}
let rects = app.rects.clone();
let mut cursor = None;
@@ -380,9 +385,15 @@ fn window(buf: &mut Buffer, app: &mut App, body: Rect) -> Option<Position> {
let content = app.content_of(app.tab(), *rect);
// tmux's window-style / window-active-style: the default colours a pane's cells fall back to.
// tty_default_colours: the active pane's window-active-style where it sets a colour, else
- // window-style (both the pane's own, its window's or the global ones).
+ // window-style (both the pane's own, its window's or the global ones) — else the theme's.
let (a, w) = (app.style_of("window-active-style", app.active, Some(*id)), app.style_of("window-style", app.active, Some(*id)));
let window = if active { (a.fg.or(w.fg), a.bg.or(w.bg)) } else { (w.fg, w.bg) };
+ // (A style's `default` is Reset: the terminal's colour, which the theme stands in for.)
+ let set = |c: Option<Color>| c.filter(|c| *c != Color::Reset);
+ let window = match themed {
+ Some(t) => (set(window.0).or(Some(theme::depth_fit(rgb(t.foreground)))), set(window.1).or(Some(theme::depth_fit(rgb(t.background))))),
+ None => window,
+ };
if surfaces {
let f = crate::pane_frame::frame(*rect, app.window_area(app.tab()), app.box_inner(app.tab()), app.pane_status(app.tab()));
// Single and zoomed panes also sit directly on the terminal background.
@@ -409,6 +420,7 @@ fn window(buf: &mut Buffer, app: &mut App, body: Rect) -> Option<Position> {
if let Some(pos) = pane_body(buf, pane, content, active, window) { cursor = Some(pos) }
pane.dirty = false;
}
+ if let Some(t) = themed { theme_ansi(buf, content, t) }
// `@hn-dim on`: a pane you are not in, a little quieter (with one pane, nothing to set apart).
if !active && rects.len() > 1 && app.options.dim_others() {
let pal = theme::pane_palette();
@@ -665,6 +677,34 @@ fn empty_window(buf: &mut Buffer, app: &App, area: Rect) {
themed_home(buf, area);
}
+// ── a theme over the panes ──
+
+fn rgb(c: [u8; 3]) -> Color { Color::Rgb(c[0], c[1], c[2]) }
+
+/// The theme chosen in Appearance (`@hn-theme`), where one is and colour is on.
+fn chosen_theme(app: &App) -> Option<&'static crate::terminal_themes::TerminalTheme> {
+ if theme::no_color() { return None }
+ let name = app.options.get("@hn-theme", "", None).filter(|n| !n.is_empty())?;
+ crate::terminal_themes::TERMINAL_THEMES.iter().find(|t| t.name == name)
+}
+
+/// The sixteen colours a program names (red, bright green…), in [area], as [t] has them — what a
+/// terminal with that theme would show. Colours a program gives exactly (256-colour, RGB) stay.
+fn theme_ansi(buf: &mut Buffer, area: Rect, t: &crate::terminal_themes::TerminalTheme) {
+ let ansi = |c: Color| -> Color {
+ let i = match c {
+ Color::Black => 0, Color::Red => 1, Color::Green => 2, Color::Yellow => 3, Color::Blue => 4, Color::Magenta => 5, Color::Cyan => 6, Color::Gray => 7,
+ Color::DarkGray => 8, Color::LightRed => 9, Color::LightGreen => 10, Color::LightYellow => 11, Color::LightBlue => 12, Color::LightMagenta => 13, Color::LightCyan => 14, Color::White => 15,
+ Color::Indexed(i) if i < 16 => i as usize,
+ other => return other,
+ };
+ theme::depth_fit(rgb(t.palette[i]))
+ };
+ for y in area.y..area.bottom() { for x in area.x..area.right() {
+ if let Some(c) = buf.cell_mut((x, y)) { c.fg = ansi(c.fg); c.bg = ansi(c.bg); }
+ } }
+}
+
/// With a theme chosen (Settings → Theme), the home screen stands on the theme's background where
/// the terminal's showed — as the panes the daemon paints and the status line already do. (Its
/// text is the theme's already: `fg` dims the theme's own foreground.) Without one, as the
@@ -2919,6 +2959,22 @@ mod theme_render_tests {
assert!(outside(&mut app).is_empty(), "{:?}", outside(&mut app));
}
+ /// A chosen theme is the panes' too: the sixteen colours a program names take the theme's
+ /// palette (red is Dracula's red), and a colour given exactly stays as it was.
+ #[test]
+ fn a_theme_gives_the_panes_its_sixteen_colours() {
+ let t = crate::terminal_themes::TERMINAL_THEMES.iter().find(|t| t.name == "Dracula").unwrap();
+ let area = Rect::new(0, 0, 3, 1);
+ let mut buf = Buffer::empty(area);
+ buf[(0, 0)].set_fg(Color::Red);
+ buf[(1, 0)].set_fg(Color::Indexed(12)).set_bg(Color::Black);
+ buf[(2, 0)].set_fg(Color::Rgb(1, 2, 3));
+ theme_ansi(&mut buf, area, t);
+ assert_eq!(buf[(0, 0)].fg, theme::depth_fit(rgb(t.palette[1])));
+ assert_eq!((buf[(1, 0)].fg, buf[(1, 0)].bg), (theme::depth_fit(rgb(t.palette[12])), theme::depth_fit(rgb(t.palette[0]))));
+ assert_eq!(buf[(2, 0)].fg, Color::Rgb(1, 2, 3));
+ }
+
/// The home screen follows a chosen theme: the wordmark in its accent, the screen on its
/// background; with no theme, the terminal's own background stays.
#[test]