Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

optimize session table width allocation

Raine Virta5eb9db623292
diff --git a/src/tui.rs b/src/tui.rs
index 314d0ad..6e406c0 100644
--- a/src/tui.rs
+++ b/src/tui.rs
@@ -42,6 +42,7 @@ const YELLOW: Color = Color::Rgb(220, 200, 100);
const BLUE: Color = Color::Rgb(120, 170, 230);
const DIM: Color = Color::Rgb(100, 104, 114);
const ACTIVE_MODEL_WIDTH: u16 = 40;
+const SESSION_MODEL_WIDTH: u16 = 36;
const RECENT_MODEL_WIDTH: u16 = 36;
const SESSION_SPARKLINE_MIN_WIDTH: u16 = 170;
@@ -715,19 +716,7 @@ fn token_sparkline_line(
])
}
-fn render_sessions(
- frame: &mut ratatui::Frame<'_>,
- area: Rect,
- sessions: &[SessionSummary],
- selected: usize,
- focused: bool,
-) {
- if sessions.is_empty() {
- render_empty_table_state(frame, area, "Sessions", focused, "No sessions");
- return;
- }
-
- let show_sparkline = area.width >= SESSION_SPARKLINE_MIN_WIDTH;
+fn session_table_widths(show_sparkline: bool) -> Vec<Constraint> {
let mut widths = vec![
Constraint::Length(1),
Constraint::Length(8),
@@ -736,7 +725,11 @@ fn render_sessions(
Constraint::Length(4),
Constraint::Length(4),
Constraint::Length(10),
- Constraint::Percentage(20),
+ if show_sparkline {
+ Constraint::Length(SESSION_MODEL_WIDTH)
+ } else {
+ Constraint::Fill(1)
+ },
Constraint::Length(7),
Constraint::Length(9),
Constraint::Length(9),
@@ -746,6 +739,23 @@ fn render_sessions(
widths.push(Constraint::Fill(1));
}
widths.push(Constraint::Length(10));
+ widths
+}
+
+fn render_sessions(
+ frame: &mut ratatui::Frame<'_>,
+ area: Rect,
+ sessions: &[SessionSummary],
+ selected: usize,
+ focused: bool,
+) {
+ if sessions.is_empty() {
+ render_empty_table_state(frame, area, "Sessions", focused, "No sessions");
+ return;
+ }
+
+ let show_sparkline = area.width >= SESSION_SPARKLINE_MIN_WIDTH;
+ let widths = session_table_widths(show_sparkline);
let model_width = table_column_width(area, &widths, 7);
let sparkline_width = show_sparkline
.then(|| table_column_width(area, &widths, 12))
@@ -1482,6 +1492,25 @@ mod tests {
);
}
+ #[test]
+ fn session_columns_preserve_fixed_widths_before_allocating_flexible_space() {
+ let compact = session_table_widths(false);
+ let area = Rect::new(0, 0, 150, 10);
+
+ assert_eq!(table_column_width(area, &compact, 2), 18);
+ assert_eq!(table_column_width(area, &compact, 6), 10);
+ assert_eq!(table_column_width(area, &compact, 8), 7);
+ assert!(table_column_width(area, &compact, 7) >= 20);
+
+ let wide = session_table_widths(true);
+ let wide_area = Rect::new(0, 0, 200, 10);
+ assert_eq!(
+ table_column_width(wide_area, &wide, 7),
+ usize::from(SESSION_MODEL_WIDTH)
+ );
+ assert!(table_column_width(wide_area, &wide, 12) >= 20);
+ }
+
#[test]
fn active_model_column_is_bounded_while_spacer_expands() {
let widths = [