Skip to content

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

Refactor: Introduce `Theme` struct for centralized styling

Maximilian Roosea6dc0db69f8
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index ac758f5d1..b40640ccc 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -1,5 +1,5 @@
use crate::display::{StyledLine, format_relative_time, shorten_path, truncate_at_word_boundary};
-use anstyle::{AnsiColor, Color, Style};
+use worktrunk::theme::Theme;
use super::WorktreeInfo;
use super::layout::LayoutConfig;
@@ -39,7 +39,8 @@ pub fn format_all_states(info: &WorktreeInfo) -> String {
pub fn format_header_line(layout: &LayoutConfig) {
let widths = &layout.widths;
- let dim_style = Style::new().dimmed();
+ let theme = Theme::new();
+ let dim_style = theme.dim;
let mut line = StyledLine::new();
@@ -121,14 +122,13 @@ pub fn format_header_line(layout: &LayoutConfig) {
pub fn format_worktree_line(info: &WorktreeInfo, layout: &LayoutConfig) {
let widths = &layout.widths;
- let primary_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
- let current_style = Style::new()
- .bold()
- .fg_color(Some(Color::Ansi(AnsiColor::Magenta)));
- let green_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
- let red_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red)));
- let yellow_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)));
- let dim_style = Style::new().dimmed();
+ let theme = Theme::new();
+ let primary_style = theme.primary;
+ let current_style = theme.current;
+ let green_style = theme.addition;
+ let red_style = theme.deletion;
+ let yellow_style = theme.neutral;
+ let dim_style = theme.dim;
let branch_display = info.branch.as_deref().unwrap_or("(detached)");
let short_head = &info.head[..8.min(info.head.len())];
diff --git a/src/error_format.rs b/src/error_format.rs
index 8cb0ac209..eff0e926b 100644
--- a/src/error_format.rs
+++ b/src/error_format.rs
@@ -1,4 +1,4 @@
-use anstyle::{AnsiColor, Color, Style};
+use crate::theme::Theme;
use std::io::IsTerminal;
/// Determines if colored output should be used based on environment
@@ -24,12 +24,12 @@ fn should_use_color() -> bool {
/// Format an error message with red color and ❌ emoji
pub fn format_error(msg: &str) -> String {
if should_use_color() {
- let error_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red)));
+ let theme = Theme::new();
format!(
"{}❌ {}{}",
- error_style.render(),
+ theme.error.render(),
msg,
- error_style.render_reset()
+ theme.error.render_reset()
)
} else {
format!("❌ {}", msg)
@@ -39,12 +39,12 @@ pub fn format_error(msg: &str) -> String {
/// Format a warning message with yellow color and 🟡 emoji
pub fn format_warning(msg: &str) -> String {
if should_use_color() {
- let warning_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)));
+ let theme = Theme::new();
format!(
"{}🟡 {}{}",
- warning_style.render(),
+ theme.warning.render(),
msg,
- warning_style.render_reset()
+ theme.warning.render_reset()
)
} else {
format!("🟡 {}", msg)
@@ -54,12 +54,12 @@ pub fn format_warning(msg: &str) -> String {
/// Format a hint message with dim color and 💡 emoji
pub fn format_hint(msg: &str) -> String {
if should_use_color() {
- let hint_style = Style::new().dimmed();
+ let theme = Theme::new();
format!(
"{}💡 {}{}",
- hint_style.render(),
+ theme.hint.render(),
msg,
- hint_style.render_reset()
+ theme.hint.render_reset()
)
} else {
format!("💡 {}", msg)
@@ -69,12 +69,12 @@ pub fn format_hint(msg: &str) -> String {
/// Format text with bold styling
pub fn bold(text: &str) -> String {
if should_use_color() {
- let bold_style = Style::new().bold();
+ let theme = Theme::new();
format!(
"{}{}{}",
- bold_style.render(),
+ theme.bold.render(),
text,
- bold_style.render_reset()
+ theme.bold.render_reset()
)
} else {
text.to_string()
@@ -86,19 +86,16 @@ pub fn bold(text: &str) -> String {
/// Example: `format_error_with_bold("Branch '", "feature-x", "' already exists")`
pub fn format_error_with_bold(prefix: &str, emphasized: &str, suffix: &str) -> String {
if should_use_color() {
- let error_style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red)));
- let bold_style = Style::new()
- .fg_color(Some(Color::Ansi(AnsiColor::Red)))
- .bold();
+ let theme = Theme::new();
format!(
"{}❌ {}{}{}{}{}{}",
- error_style.render(),
+ theme.error.render(),
prefix,
- bold_style.render(),
+ theme.error_bold.render(),
emphasized,
- error_style.render(), // Back to regular red
+ theme.error.render(), // Back to regular red
suffix,
- error_style.render_reset()
+ theme.error.render_reset()
)
} else {
format!("❌ {}{}{}", prefix, emphasized, suffix)
diff --git a/src/lib.rs b/src/lib.rs
index 49bbaedd2..56a9d540e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,5 +2,6 @@ pub mod config;
pub mod error_format;
pub mod git;
pub mod shell;
+pub mod theme;
// Note: display, commands, and llm modules are used by main.rs but not exposed as public API
diff --git a/src/theme.rs b/src/theme.rs
new file mode 100644
index 000000000..f6e243d5e
--- /dev/null
+++ b/src/theme.rs
@@ -0,0 +1,84 @@
+use anstyle::{AnsiColor, Color, Style};
+
+/// Centralized theme for terminal output styling.
+///
+/// Provides consistent colors and styles across the application.
+/// All style definitions are in one place for easy customization.
+#[derive(Debug, Clone)]
+pub struct Theme {
+ // Status/message styles
+ pub error: Style,
+ pub warning: Style,
+ pub hint: Style,
+ pub success: Style,
+
+ // Emphasis styles
+ pub bold: Style,
+ pub dim: Style,
+ pub error_bold: Style,
+
+ // Worktree-specific styles
+ pub primary: Style,
+ pub current: Style,
+
+ // Diff/stat styles
+ pub addition: Style,
+ pub deletion: Style,
+ pub neutral: Style,
+}
+
+impl Theme {
+ /// Create a new theme with default colors.
+ pub fn new() -> Self {
+ Self {
+ // Status/message styles
+ error: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))),
+ warning: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))),
+ hint: Style::new().dimmed(),
+ success: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))),
+
+ // Emphasis styles
+ bold: Style::new().bold(),
+ dim: Style::new().dimmed(),
+ error_bold: Style::new()
+ .fg_color(Some(Color::Ansi(AnsiColor::Red)))
+ .bold(),
+
+ // Worktree-specific styles
+ primary: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))),
+ current: Style::new()
+ .bold()
+ .fg_color(Some(Color::Ansi(AnsiColor::Magenta))),
+
+ // Diff/stat styles
+ addition: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))),
+ deletion: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))),
+ neutral: Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))),
+ }
+ }
+}
+
+impl Default for Theme {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_theme_creation() {
+ let theme = Theme::new();
+ // Just verify it doesn't panic
+ let _ = theme.error;
+ let _ = theme.primary;
+ }
+
+ #[test]
+ fn test_theme_default() {
+ let theme = Theme::default();
+ let _ = theme.warning;
+ }
+}