Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(styling): add TOML syntax highlighting for gutter output (#905)
Maximilian Roos907afad0a5d3
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rsindex c48b1f297..b9265adb2 100644--- a/src/commands/config/show.rs+++ b/src/commands/config/show.rs@@ -402,7 +402,7 @@ fn render_user_config(out: &mut String) -> anyhow::Result<()> {}// Display TOML with syntax highlighting (gutter at column 0)- write!(out, "{}", format_toml(&contents))?;+ writeln!(out, "{}", format_toml(&contents))?;Ok(())}@@ -523,7 +523,7 @@ fn render_project_config(out: &mut String) -> anyhow::Result<()> {}// Display TOML with syntax highlighting (gutter at column 0)- write!(out, "{}", format_toml(&contents))?;+ writeln!(out, "{}", format_toml(&contents))?;Ok(())}diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rsindex 45ab4c423..a95583f35 100644--- a/src/commands/configure_shell.rs+++ b/src/commands/configure_shell.rs@@ -7,8 +7,8 @@ use anstyle::Style;use worktrunk::path::format_path_for_display;use worktrunk::shell::{self, Shell};use worktrunk::styling::{- INFO_SYMBOL, SUCCESS_SYMBOL, eprint, eprintln, format_bash_with_gutter, format_with_gutter,- prompt_message, warning_message,+ INFO_SYMBOL, SUCCESS_SYMBOL, eprint, eprintln, format_bash_with_gutter, format_toml,+ format_with_gutter, prompt_message, warning_message,};use crate::output::prompt::{PromptResponse, prompt_yes_no_preview};@@ -1246,19 +1246,25 @@ pub fn handle_show_theme() {eprintln!();- // Gutter - quoted content- eprintln!("{}", info_message("Gutter formatting (quoted content):"));+ // Gutter - error details (plain text, no syntax highlighting)+ eprintln!("{}", info_message("Gutter formatting (error details):"));eprintln!("{}",- format_with_gutter(- "[commit-generation]\ncommand = \"llm --model claude\"",- None,- )+ format_with_gutter("expected `=`, found newline at line 3 column 1", None,));eprintln!();- // Gutter - bash code+ // Gutter - TOML config (syntax highlighted)+ eprintln!("{}", info_message("Gutter formatting (config):"));+ eprintln!(+ "{}",+ format_toml("[commit.generation]\ncommand = \"llm --model claude\"")+ );++ eprintln!();++ // Gutter - bash code (syntax highlighted)eprintln!("{}", info_message("Gutter formatting (shell code):"));eprintln!("{}",diff --git a/src/output/commit_generation.rs b/src/output/commit_generation.rsindex b06ae411b..d0b475752 100644--- a/src/output/commit_generation.rs+++ b/src/output/commit_generation.rs@@ -8,9 +8,7 @@ use std::io::{self, IsTerminal};use color_print::cformat;use worktrunk::config::UserConfig;-use worktrunk::styling::{- eprintln, format_with_gutter, hint_message, info_message, success_message,-};+use worktrunk::styling::{eprintln, format_toml, hint_message, info_message, success_message};use super::prompt::{PromptResponse, prompt_yes_no_preview};@@ -146,7 +144,7 @@ pub fn prompt_commit_generation(config: &mut UserConfig) -> anyhow::Result<bool>"Would add to <bold>~/.config/worktrunk/config.toml</>:")));- eprintln!("{}", format_with_gutter(&config_preview, None));+ eprintln!("{}", format_toml(&config_preview));eprintln!();},)?;@@ -168,7 +166,7 @@ pub fn prompt_commit_generation(config: &mut UserConfig) -> anyhow::Result<bool>// Show what was addedeprintln!("{}", success_message(cformat!("Added to user config:")));- eprintln!("{}", format_with_gutter(&config_preview, None));+ eprintln!("{}", format_toml(&config_preview));eprintln!("{}",hint_message(cformat!("View config: <bright-black>wt config show</>"))diff --git a/src/styling/highlighting.rs b/src/styling/highlighting.rsindex 052dd623f..3559af5eb 100644--- a/src/styling/highlighting.rs+++ b/src/styling/highlighting.rs@@ -87,6 +87,9 @@ pub(super) fn bash_token_style(kind: &str) -> Option<Style> {// ============================================================================/// Formats TOML content with syntax highlighting using synoptic+///+/// Returns formatted output without trailing newline (consistent with format_with_gutter+/// and format_bash_with_gutter).pub fn format_toml(content: &str) -> String {// synoptic has built-in TOML support, so this always succeedslet mut highlighter = from_extension("toml", 4).expect("synoptic supports TOML");@@ -97,29 +100,31 @@ pub fn format_toml(content: &str) -> String {highlighter.run(&lines);// Render each line with gutter and appropriate styling- let mut output = String::new();- for (y, line) in lines.iter().enumerate() {- output.push_str(&format!("{gutter} {gutter:#} "));-- for token in highlighter.line(y, line) {- match token {- TokOpt::Some(text, kind) => {- if let Some(s) = toml_token_style(&kind) {- output.push_str(&format!("{s}{text}{s:#}"));- } else {- output.push_str(&text);- }- }- TokOpt::None(text) => {- output.push_str(&text);+ // Build lines without trailing newline - caller is responsible for element separation+ let output_lines: Vec<String> = lines+ .iter()+ .enumerate()+ .map(|(y, line)| {+ let mut line_output = format!("{gutter} {gutter:#} ");++ for token in highlighter.line(y, line) {+ let (text, style) = match token {+ TokOpt::Some(text, kind) => (text, toml_token_style(&kind)),+ TokOpt::None(text) => (text, None),+ };++ if let Some(s) = style {+ line_output.push_str(&format!("{s}{text}{s:#}"));+ } else {+ line_output.push_str(&text);}}- }- output.push('\n');- }+ line_output+ })+ .collect();- output+ output_lines.join("\n")}/// Maps TOML token kinds to anstyle styles@@ -299,6 +304,44 @@ mod tests {assert!(result.lines().count() >= 2);}+ #[test]+ fn test_format_toml_has_styled_and_unstyled_text() {+ // This test verifies that format_toml handles both styled tokens (string, table)+ // and unstyled text (TokOpt::None for whitespace, punctuation)+ use synoptic::{TokOpt, from_extension};++ let content = "key = \"value\"";+ let mut highlighter = from_extension("toml", 4).expect("synoptic supports TOML");+ let lines: Vec<String> = content.lines().map(|s| s.to_string()).collect();+ highlighter.run(&lines);++ // Collect token types+ let mut has_styled = false;+ let mut has_unstyled = false;+ for (y, line) in lines.iter().enumerate() {+ for token in highlighter.line(y, line) {+ match token {+ TokOpt::Some(_, kind) => {+ if toml_token_style(&kind).is_some() {+ has_styled = true;+ }+ }+ TokOpt::None(_) => {+ has_unstyled = true;+ }+ }+ }+ }++ // Should have styled token (the string "value")+ assert!(has_styled, "Should have at least one styled token");+ // Should have unstyled text (whitespace, "=", "key")+ assert!(+ has_unstyled,+ "Should have at least one unstyled text segment"+ );+ }+#[test]fn test_format_toml_multiline() {let content = "[table]\nkey1 = \"value1\"\nkey2 = 42\n# comment\nkey3 = false";diff --git a/tests/integration_tests/snapshots/integration__integration_tests__config_show_theme__show_theme.snap b/tests/integration_tests/snapshots/integration__integration_tests__config_show_theme__show_theme.snapindex 6f9a247c5..c091b9e05 100644--- a/tests/integration_tests/snapshots/integration__integration_tests__config_show_theme__show_theme.snap+++ b/tests/integration_tests/snapshots/integration__integration_tests__config_show_theme__show_theme.snap@@ -12,10 +12,13 @@ info:GIT_EDITOR: ""LANG: CLC_ALL: C+ PSModulePath: ""RUST_LOG: warnSHELL: ""TERM: alacrittyWORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"WT_TEST_DELAYED_STREAM_MS: "-1"WT_TEST_EPOCH: "1735776000"@@ -32,9 +35,12 @@ exit_code: 0[2m↳[22m [2mTo rebase onto main, run [90mwt merge[39m[22m[2m○[22m Showing [1m5[22m worktrees-[2m○[22m Gutter formatting (quoted content):-[107m [0m [commit-generation]-[107m [0m command = "llm --model claude"+[2m○[22m Gutter formatting (error details):+[107m [0m expected `=`, found newline at line 3 column 1++[2m○[22m Gutter formatting (config):+[107m [0m [1m[36m[commit.generation][0m+[107m [0m command = [32m"llm --model claude"[0m[2m○[22m Gutter formatting (shell code):[107m [0m [2m[0m[2m[34meval[0m[2m [0m[2m[32m"$([0m[2m[34mwt[0m[2m config shell init bash)"[0m[2m[0m