Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Move `get_terminal_width` and `visual_width` to styling
Maximilian Roos5169a858a237
diff --git a/src/display.rs b/src/display.rsindex 79b51d13d..a02aeb988 100644--- a/src/display.rs+++ b/src/display.rs@@ -91,9 +91,10 @@ pub fn shorten_path(path: &Path, prefix: &Path) -> String {/// Truncate text at word boundary with ellipsis, respecting terminal widthpub fn truncate_at_word_boundary(text: &str, max_width: usize) -> String {- use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};+ use unicode_width::UnicodeWidthChar;+ use worktrunk::styling::visual_width;- if text.width() <= max_width {+ if visual_width(text) <= max_width {return text.to_string();}@@ -124,23 +125,8 @@ pub fn truncate_at_word_boundary(text: &str, max_width: usize) -> String {format!("{}…", truncated)}-/// Get terminal width, defaulting to 80 if detection fails-pub fn get_terminal_width() -> usize {- // Check COLUMNS environment variable first (for testing and scripts)- if let Ok(cols) = std::env::var("COLUMNS")- && let Ok(width) = cols.parse::<usize>()- {- return width;- }-- // Fall back to actual terminal size- terminal_size::terminal_size()- .map(|(terminal_size::Width(w), _)| w as usize)- .unwrap_or(80)-}--// Re-export truncate_visible from styling for convenience-pub use worktrunk::styling::truncate_visible;+// Re-export from styling for convenience+pub use worktrunk::styling::{get_terminal_width, truncate_visible};#[cfg(test)]mod tests {diff --git a/src/styling/format.rs b/src/styling/format.rsindex deeab7087..287e69895 100644--- a/src/styling/format.rs+++ b/src/styling/format.rs@@ -9,8 +9,8 @@ use unicode_width::UnicodeWidthStr;#[cfg(feature = "syntax-highlighting")]use super::highlighting::bash_token_style;-/// Default terminal width fallback if detection fails-const DEFAULT_TERMINAL_WIDTH: usize = 80;+// Import canonical implementations from parent module+use super::{get_terminal_width, visual_width};/// Width overhead added by format_with_gutter()///@@ -29,29 +29,6 @@ pub fn strip_ansi_codes(s: &str) -> String {s.ansi_strip().into_owned()}-/// Calculate visual width of a string, ignoring ANSI escape codes-fn visual_width(s: &str) -> usize {- strip_ansi_codes(s).width()-}--/// Get terminal width, defaulting to 80 if detection fails-///-/// Checks COLUMNS environment variable first (for testing and scripts),-/// then falls back to actual terminal size detection.-fn get_terminal_width() -> usize {- // Check COLUMNS environment variable first (for testing and scripts)- if let Ok(cols) = std::env::var("COLUMNS")- && let Ok(width) = cols.parse::<usize>()- {- return width;- }-- // Fall back to actual terminal size- terminal_size::terminal_size()- .map(|(terminal_size::Width(w), _)| w as usize)- .unwrap_or(DEFAULT_TERMINAL_WIDTH)-}-/// Wraps text at word boundaries to fit within the specified width////// # Argumentsdiff --git a/src/styling/mod.rs b/src/styling/mod.rsindex 31e9ed9a9..249428d7d 100644--- a/src/styling/mod.rs+++ b/src/styling/mod.rs@@ -30,6 +30,36 @@ pub use format::{GUTTER_OVERHEAD, format_bash_with_gutter, format_with_gutter, spub use highlighting::format_toml;pub use line::{StyledLine, StyledString, truncate_visible};+/// Default terminal width fallback if detection fails+const DEFAULT_TERMINAL_WIDTH: usize = 80;++/// Get terminal width, defaulting to 80 if detection fails+///+/// Checks COLUMNS environment variable first (for testing and scripts),+/// then falls back to actual terminal size detection.+pub fn get_terminal_width() -> usize {+ // Check COLUMNS environment variable first (for testing and scripts)+ if let Ok(cols) = std::env::var("COLUMNS")+ && let Ok(width) = cols.parse::<usize>()+ {+ return width;+ }++ // Fall back to actual terminal size+ terminal_size::terminal_size()+ .map(|(terminal_size::Width(w), _)| w as usize)+ .unwrap_or(DEFAULT_TERMINAL_WIDTH)+}++/// Calculate visual width of a string, ignoring ANSI escape codes+///+/// Uses unicode-width for proper handling of wide characters (CJK, emoji).+pub fn visual_width(s: &str) -> usize {+ use ansi_str::AnsiStr;+ use unicode_width::UnicodeWidthStr;+ s.ansi_strip().width()+}+/// Wrap text with an OSC 8 hyperlink////// See: <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>