Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Feat: Add TOML syntax highlighting to config list

Maximilian Roosf1b94aa803ca
diff --git a/Cargo.lock b/Cargo.lock
index eed3a9fb8..748e2cec6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -221,6 +221,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+[[package]]
+name = "char_index"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "10ef8669476802b7127a0a97612a0c34113e949ee65c695e4ac259f1f49aaa25"
+
[[package]]
name = "chrono"
version = "0.4.42"
@@ -823,6 +829,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+[[package]]
+name = "if_chain"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd62e6b5e86ea8eeeb8db1de02880a6abc01a397b2ebb64b5d74ac255318f5cb"
+
[[package]]
name = "indexmap"
version = "2.12.0"
@@ -925,6 +937,12 @@ version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
+[[package]]
+name = "nohash-hasher"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
+
[[package]]
name = "num-conv"
version = "0.1.0"
@@ -1415,6 +1433,19 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "synoptic"
+version = "2.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3961017ab3836f3d19579098fae565969c5e7c32c66d746bc706cfbbb047e080"
+dependencies = [
+ "char_index",
+ "if_chain",
+ "nohash-hasher",
+ "regex",
+ "unicode-width",
+]
+
[[package]]
name = "tempfile"
version = "3.23.0"
@@ -1997,6 +2028,7 @@ dependencies = [
"serde",
"serde_json",
"shell-escape",
+ "synoptic",
"tempfile",
"terminal_size",
"toml",
diff --git a/Cargo.toml b/Cargo.toml
index 7dfb03b61..85da73949 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -29,6 +29,7 @@ rayon = "1.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
shell-escape = "0.1"
+synoptic = "2"
terminal_size = "0.4"
toml = "0.9"
unicode-width = "0.2"
diff --git a/src/commands/config.rs b/src/commands/config.rs
index 282d9a7fa..2b26e932b 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -2,7 +2,7 @@ use anstyle::{AnsiColor, Color};
use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};
use std::path::PathBuf;
use worktrunk::git::{GitError, Repository};
-use worktrunk::styling::{AnstyleStyle, HINT, HINT_EMOJI, println};
+use worktrunk::styling::{AnstyleStyle, HINT, HINT_EMOJI, format_toml, print, println};
/// Example configuration file content
const CONFIG_EXAMPLE: &str = include_str!("../../config.example.toml");
@@ -91,14 +91,8 @@ fn display_global_config() -> Result<(), GitError> {
return Ok(());
}
- // Display each line with indentation
- for line in contents.lines() {
- if !line.trim().is_empty() {
- println!(" {dim}{line}{dim:#}");
- } else {
- println!();
- }
- }
+ // Display TOML with syntax highlighting
+ print!("{}", format_toml(&contents, " "));
Ok(())
}
@@ -135,14 +129,8 @@ fn display_project_config() -> Result<(), GitError> {
return Ok(());
}
- // Display each line with indentation
- for line in contents.lines() {
- if !line.trim().is_empty() {
- println!(" {dim}{line}{dim:#}");
- } else {
- println!();
- }
- }
+ // Display TOML with syntax highlighting
+ print!("{}", format_toml(&contents, " "));
Ok(())
}
diff --git a/src/styling.rs b/src/styling.rs
index 3965f3ed6..a05ed6a14 100644
--- a/src/styling.rs
+++ b/src/styling.rs
@@ -6,6 +6,7 @@
//! - Semantic style constants for domain-specific use
use anstyle::{AnsiColor, Color, Style};
+use synoptic::{TokOpt, from_extension};
use unicode_width::UnicodeWidthStr;
// ============================================================================
@@ -158,6 +159,91 @@ impl StyledLine {
}
}
+// ============================================================================
+// TOML Syntax Highlighting
+// ============================================================================
+
+/// Formats TOML content with syntax highlighting using synoptic
+pub fn format_toml(content: &str, indent: &str) -> String {
+ // Get TOML highlighter from synoptic's built-in rules (tab_width = 4)
+ let mut highlighter = match from_extension("toml", 4) {
+ Some(h) => h,
+ None => {
+ // Fallback: return dimmed content if TOML highlighter not available
+ let dim = Style::new().dimmed();
+ let mut output = String::new();
+ for line in content.lines() {
+ output.push_str(&format!("{indent}{dim}{line}{dim:#}\n"));
+ }
+ return output;
+ }
+ };
+
+ let mut output = String::new();
+ let lines: Vec<String> = content.lines().map(|s| s.to_string()).collect();
+
+ // Process all lines through the highlighter
+ highlighter.run(&lines);
+
+ // Render each line with appropriate styling
+ for (y, line) in lines.iter().enumerate() {
+ // Add indentation first
+ output.push_str(indent);
+
+ // Render each token with appropriate styling
+ for token in highlighter.line(y, line) {
+ match token {
+ TokOpt::Some(text, kind) => {
+ let style = toml_token_style(&kind);
+ if let Some(s) = style {
+ output.push_str(&format!("{s}{text}{s:#}"));
+ } else {
+ output.push_str(&text);
+ }
+ }
+ TokOpt::None(text) => {
+ output.push_str(&text);
+ }
+ }
+ }
+
+ output.push('\n');
+ }
+
+ output
+}
+
+/// Maps TOML token kinds to anstyle styles
+///
+/// Token names come from synoptic's TOML highlighter:
+/// - "string": quoted strings
+/// - "comment": hash-prefixed comments
+/// - "boolean": true/false values
+/// - "table": table headers [...]
+/// - "digit": numeric values
+fn toml_token_style(kind: &str) -> Option<Style> {
+ match kind {
+ // Strings (quoted values)
+ "string" => Some(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)))),
+
+ // Comments (hash-prefixed)
+ "comment" => Some(Style::new().dimmed()),
+
+ // Table headers [table] and [[array]]
+ "table" => Some(
+ Style::new()
+ .fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
+ .bold(),
+ ),
+
+ // Booleans and numbers
+ "boolean" | "digit" => Some(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)))),
+
+ // Everything else (operators, punctuation, keys)
+ _ => None,
+ }
+}
+
// ============================================================================
// Tests
// ============================================================================
@@ -166,6 +252,54 @@ impl StyledLine {
mod tests {
use super::*;
+ #[test]
+ fn test_toml_formatting() {
+ let toml_content = r#"worktree-path = "../{repo}.{branch}"
+
+[llm]
+args = []
+
+# This is a comment
+[[approved-commands]]
+project = "github.com/user/repo"
+command = "npm install"
+"#;
+
+ let output = format_toml(toml_content, " ");
+
+ // Check that output contains ANSI escape codes
+ assert!(
+ output.contains("\x1b["),
+ "Output should contain ANSI escape codes"
+ );
+
+ // Check that strings are highlighted (green = 32)
+ assert!(
+ output.contains("\x1b[32m"),
+ "Should contain green color for strings"
+ );
+
+ // Check that comments are dimmed (dim = 2)
+ assert!(
+ output.contains("\x1b[2m"),
+ "Should contain dim style for comments"
+ );
+
+ // Check that table headers are highlighted (cyan = 36, bold = 1)
+ assert!(
+ output.contains("\x1b[36m") || output.contains("\x1b[1m"),
+ "Should contain cyan or bold for tables"
+ );
+
+ // Check indentation is preserved
+ assert!(
+ output
+ .lines()
+ .all(|line| line.starts_with(" ") || line.is_empty()),
+ "All lines should be indented"
+ );
+ }
+
// StyledString tests
#[test]
fn test_styled_string_width() {
diff --git a/tests/snapshots/integration__integration_tests__config_list__config_list_no_project.snap b/tests/snapshots/integration__integration_tests__config_list__config_list_no_project.snap
index 30fd5cdbf..3fd967f59 100644
--- a/tests/snapshots/integration__integration_tests__config_list__config_list_no_project.snap
+++ b/tests/snapshots/integration__integration_tests__config_list__config_list_no_project.snap
@@ -12,7 +12,7 @@ info:
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpq0vCwQ
+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpOhSuMo
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1704067200"
@@ -21,7 +21,7 @@ success: true
exit_code: 0
----- stdout -----
Global Config: [TEMP_HOME]/.config/worktrunk/config.toml
- worktree-path = "../{main-worktree}.{branch}"
+ worktree-path = "../{main-worktree}.{branch}"
Project Config: [REPO]/.config/wt.toml
💡 Not found
diff --git a/tests/snapshots/integration__integration_tests__config_list__config_list_with_project.snap b/tests/snapshots/integration__integration_tests__config_list__config_list_with_project.snap
index 0243ff9e9..d348b0915 100644
--- a/tests/snapshots/integration__integration_tests__config_list__config_list_with_project.snap
+++ b/tests/snapshots/integration__integration_tests__config_list__config_list_with_project.snap
@@ -12,7 +12,7 @@ info:
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
- HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuCBKlR
+ HOME: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8ZWNvA
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1704067200"
@@ -21,16 +21,16 @@ success: true
exit_code: 0
----- stdout -----
Global Config: [TEMP_HOME]/.config/worktrunk/config.toml
- worktree-path = "../{main-worktree}.{branch}"
-
- [[approved-commands]]
- project = "test-project"
- command = "npm install"
+ worktree-path = "../{main-worktree}.{branch}"
+
+ [[approved-commands]]
+ project = "test-project"
+ command = "npm install"
Project Config: [REPO]/.config/wt.toml
- post-create-command = "npm install"
-
- [post-start-command]
- server = "npm run dev"
+ post-create-command = "npm install"
+
+ [post-start-command]
+ server = "npm run dev"
----- stderr -----