use crate::config::Config;
use unicode_width::UnicodeWidthChar;
pub const RESET: &str = "\x1b[0m";
pub fn visible_len(s: &str) -> usize {
let mut width = 0;
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '\x1b' {
if let Some(&next) = chars.peek() {
if next == '[' {
chars.next(); for c2 in chars.by_ref() {
if ('@'..='~').contains(&c2) {
break;
}
}
}
}
} else {
width += c.width().unwrap_or(0);
}
}
width
}
pub fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
if visible_len(s) <= max_width {
return s.to_string();
}
const ELLIPSIS: &str = "…";
const ELLIPSIS_WIDTH: usize = 1;
if max_width <= ELLIPSIS_WIDTH {
return ELLIPSIS.chars().take(max_width).collect();
}
let target_width = max_width - ELLIPSIS_WIDTH;
let mut current_width = 0;
let mut result = String::with_capacity(s.len());
let mut chars = s.chars().peekable();
while let Some(c) = chars.next() {
if c == '\x1b' {
result.push(c);
if let Some(&next) = chars.peek() {
if next == '[' {
result.push(chars.next().unwrap()); for c2 in chars.by_ref() {
result.push(c2);
if ('@'..='~').contains(&c2) {
break;
}
}
}
}
} else {
let char_width = c.width().unwrap_or(0);
if current_width + char_width > target_width {
break;
}
result.push(c);
current_width += char_width;
}
}
result.push_str(ELLIPSIS);
result.push_str(RESET);
result
}
pub fn get_terminal_width(config: &Config) -> usize {
if let Ok(w_str) = std::env::var("CLAUDE_TERMINAL_WIDTH") {
if let Ok(w) = w_str.parse::<usize>() {
return w;
}
}
if let Ok(w_str) = std::env::var("COLUMNS") {
if let Ok(w) = w_str.parse::<usize>() {
return w;
}
}
if let Some((w, _)) = terminal_size::terminal_size() {
return w.0 as usize;
}
#[cfg(unix)]
{
if let Ok(f) = std::fs::File::open("/dev/tty") {
if let Some((w, _)) = terminal_size::terminal_size_of(&f) {
return w.0 as usize;
}
}
}
config.display.default_terminal_width
}
pub fn get_username() -> Option<String> {
std::env::var("USER")
.or_else(|_| std::env::var("USERNAME"))
.ok()
}
pub fn get_thinking_mode_enabled() -> bool {
use serde::Deserialize;
#[derive(Deserialize)]
struct ClaudeSettings {
#[serde(rename = "alwaysThinkingEnabled")]
always_thinking_enabled: Option<bool>,
}
#[cfg(unix)]
let home = std::env::var_os("HOME");
#[cfg(windows)]
let home = std::env::var_os("USERPROFILE");
let home = match home {
Some(h) => h,
None => return false,
};
let settings_path = std::path::Path::new(&home)
.join(".claude")
.join("settings.json");
let content = match std::fs::read_to_string(&settings_path) {
Ok(c) => c,
Err(_) => return false,
};
let settings: ClaudeSettings = match serde_json::from_str(&content) {
Ok(s) => s,
Err(_) => return false,
};
settings.always_thinking_enabled.unwrap_or(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_visible_len() {
assert_eq!(visible_len("Hello"), 5);
assert_eq!(visible_len("\x1b[31mHello\x1b[0m"), 5);
assert_eq!(visible_len("\x1b[38;2;10;20;30mHello"), 5);
assert_eq!(visible_len(""), 0);
assert_eq!(visible_len("foo bar"), 7);
assert_eq!(visible_len("\x1b[1;34mBoldBlue"), 8);
}
#[test]
fn test_truncate_with_ellipsis() {
assert_eq!(truncate_with_ellipsis("Hello World", 11), "Hello World");
assert_eq!(truncate_with_ellipsis("Hello World", 5), "Hell…\x1b[0m");
let s = "\x1b[31mHello World\x1b[0m";
assert_eq!(truncate_with_ellipsis(s, 5), "\x1b[31mHell…\x1b[0m");
}
#[test]
fn test_visible_len_wide_glyphs() {
assert_eq!(visible_len("🧪"), 2);
assert_eq!(visible_len("Test🧪"), 6);
assert_eq!(visible_len("🧪🔬"), 4);
assert_eq!(visible_len("日本語"), 6);
assert_eq!(visible_len("Hello日本"), 9);
assert_eq!(visible_len("Test🧪日本"), 10);
assert_eq!(visible_len("\x1b[31m日本語\x1b[0m"), 6);
assert_eq!(visible_len("\x1b[31m🧪Test\x1b[0m"), 6);
}
#[test]
fn test_truncate_wide_glyphs() {
let emoji_str = "Test🧪Data";
assert_eq!(truncate_with_ellipsis(emoji_str, 7), "Test🧪…\x1b[0m");
assert_eq!(truncate_with_ellipsis(emoji_str, 6), "Test…\x1b[0m");
let cjk_str = "日本語";
assert_eq!(truncate_with_ellipsis(cjk_str, 6), "日本語");
assert_eq!(truncate_with_ellipsis(cjk_str, 5), "日本…\x1b[0m");
assert_eq!(truncate_with_ellipsis(cjk_str, 3), "日…\x1b[0m");
let mixed = "Test日本語";
assert_eq!(truncate_with_ellipsis(mixed, 7), "Test日…\x1b[0m");
assert_eq!(truncate_with_ellipsis(mixed, 10), "Test日本語");
let path = "/home/user/日本語/test";
let truncated = truncate_with_ellipsis(path, 15);
assert!(visible_len(&truncated) <= 15);
}
#[test]
fn test_truncate_wide_boundary() {
let s = "abc日";
assert_eq!(truncate_with_ellipsis(s, 5), "abc日");
assert_eq!(truncate_with_ellipsis(s, 4), "abc…\x1b[0m");
}
}