use super::cell::Color;
use super::event::Event;
use super::layout::Rect;
use super::screen::Screen;
#[derive(Clone, Debug)]
pub enum Action {
SendMessage(String),
SwitchMode(String),
ScrollUp,
ScrollDown,
PageUp,
PageDown,
ToggleSidebar,
CycleFocusForward,
CycleFocusBackward,
Quit,
ConfirmYes,
ConfirmNo,
ConfirmAll,
ExitStructureMode,
AnswerQuestion(String),
Interrupt,
None,
}
pub trait Widget {
fn render(&mut self, area: Rect, screen: &mut Screen);
fn handle_event(&mut self, event: &Event) -> Action {
let _ = event;
Action::None
}
fn focused(&self) -> bool {
false
}
fn set_focus(&mut self, _focused: bool) {}
}
pub mod styles {
use super::Color;
pub const STATUS_BAR_FG: Color = Color::WHITE;
pub const STATUS_BAR_BG: Color = Color::Ansi(236);
pub const INPUT_BAR_FG: Color = Color::WHITE;
pub const INPUT_BAR_BG: Color = Color::Ansi(235);
pub const SIDEBAR_FG: Color = Color::Ansi(252); pub const SIDEBAR_BG: Color = Color::Ansi(234); pub const SIDEBAR_BORDER: Color = Color::Ansi(240);
pub const USER_MSG_FG: Color = Color::GREEN;
pub const ASSISTANT_MSG_FG: Color = Color::WHITE;
pub const TOOL_MSG_FG: Color = Color::Ansi(14); pub const THINKING_FG: Color = Color::Ansi(97);
pub const SCROLLBAR_FG: Color = Color::Ansi(244); pub const SCROLLBAR_BG: Color = Color::Default;
pub const MODE_CASUAL_FG: Color = Color::GREEN;
pub const MODE_PLANNING_FG: Color = Color::YELLOW;
pub const MODE_AGENT_FG: Color = Color::CYAN;
pub const MODE_RESEARCH_FG: Color = Color::ORANGE;
pub const BOX_HORIZONTAL: char = '─';
pub const BOX_VERTICAL: char = '│';
pub const BOX_TOP_LEFT: char = '┌';
pub const BOX_TOP_RIGHT: char = '┐';
pub const BOX_BOTTOM_LEFT: char = '└';
pub const BOX_BOTTOM_RIGHT: char = '┘';
pub const BOX_LEFT_TEE: char = '├';
pub const BOX_RIGHT_TEE: char = '┤';
pub const BOX_TOP_TEE: char = '┬';
pub const BOX_BOTTOM_TEE: char = '┴';
pub const BOX_CROSS: char = '┼';
}
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
pub fn truncate_str(s: &str, max_len: usize) -> &str {
if s.len() <= max_len {
return s;
}
let mut boundary = max_len;
while boundary > 0 && !s.is_char_boundary(boundary) {
boundary -= 1;
}
&s[..boundary]
}
pub fn truncate_str_width(s: &str, max_width: usize) -> &str {
if s.width() <= max_width {
return s;
}
let mut acc = 0usize;
for (idx, ch) in s.char_indices() {
let w = ch.width().unwrap_or(0);
if w == 0 {
continue;
}
if acc + w > max_width {
return &s[..idx];
}
acc += w;
}
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_truncate_str_ascii() {
assert_eq!(truncate_str("hello", 3), "hel");
assert_eq!(truncate_str("hello", 10), "hello");
assert_eq!(truncate_str("hello", 0), "");
}
#[test]
fn test_truncate_str_multibyte() {
assert_eq!(truncate_str("🦀hello", 5), "🦀h"); assert_eq!(truncate_str("🦀hello", 3), ""); let s = "⚙hello";
assert_eq!(truncate_str(s, 4), "⚙h"); }
#[test]
fn test_truncate_str_no_panic() {
let s = "naïve café 🦀";
for i in 0..=s.len() + 5 {
let _ = truncate_str(s, i);
}
}
#[test]
fn test_truncate_str_width_ascii() {
assert_eq!(truncate_str_width("hello", 3), "hel");
assert_eq!(truncate_str_width("hello", 10), "hello");
assert_eq!(truncate_str_width("hello", 0), "");
}
#[test]
fn test_truncate_str_width_cjk() {
assert_eq!(truncate_str_width("你好", 1), "");
assert_eq!(truncate_str_width("你好", 2), "你");
assert_eq!(truncate_str_width("你好", 3), "你"); assert_eq!(truncate_str_width("你好", 4), "你好");
}
#[test]
fn test_truncate_str_width_combining_mark() {
let s = "e\u{0301}x";
assert_eq!(truncate_str_width(s, 1), "e\u{0301}"); assert_eq!(truncate_str_width(s, 2), "e\u{0301}x"); }
}