tuillem-tui 0.1.4

Ratatui TUI layer for tuillem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use chrono::{DateTime, Local, NaiveDate, Utc};
use ratatui::{
    Frame,
    layout::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{
        Block, Borders, List, ListItem, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState,
    },
};
use tuillem_core::actions::SessionSummary;

use crate::theme::Theme;

/// Split `text` into spans, highlighting case-insensitive matches of `query`.
fn highlight_matches<'a>(
    text: &str,
    query: &str,
    normal_style: Style,
    highlight_style: Style,
) -> Vec<Span<'a>> {
    if query.is_empty() {
        return vec![Span::styled(text.to_string(), normal_style)];
    }
    let lower_text = text.to_lowercase();
    let lower_query = query.to_lowercase();
    let mut spans = Vec::new();
    let mut last = 0;
    for (start, _) in lower_text.match_indices(&lower_query) {
        if start < last {
            // Overlapping match — skip
            continue;
        }
        if start > last {
            spans.push(Span::styled(text[last..start].to_string(), normal_style));
        }
        spans.push(Span::styled(
            text[start..start + lower_query.len()].to_string(),
            highlight_style,
        ));
        last = start + lower_query.len();
    }
    if last < text.len() {
        spans.push(Span::styled(text[last..].to_string(), normal_style));
    }
    spans
}

#[derive(Debug, Clone)]
pub struct Sidebar {
    pub selected: usize,
    pub scroll_offset: usize,
    pub search_input: String,
    pub search_focused: bool,
    /// Session IDs that matched an FTS content search (None = no search active)
    pub content_match_ids: Option<std::collections::HashSet<String>>,
    /// Number of session items visible in the last render (used for scroll calculations)
    visible_count: usize,
}

impl Sidebar {
    pub fn new() -> Self {
        Self {
            selected: 0,
            scroll_offset: 0,
            search_input: String::new(),
            search_focused: false,
            content_match_ids: None,
            visible_count: 0,
        }
    }

    /// Filter sessions by search query.
    /// Matches on title, tags (client-side), AND conversation content (via FTS results).
    pub fn filtered_sessions<'a>(&self, sessions: &'a [SessionSummary]) -> Vec<&'a SessionSummary> {
        if self.search_input.is_empty() {
            sessions.iter().collect()
        } else {
            let query = self.search_input.to_lowercase();
            sessions
                .iter()
                .filter(|s| {
                    let title_match = s.title.to_lowercase().contains(&query)
                        || s.tags.iter().any(|t| t.to_lowercase().contains(&query));
                    let content_match = self
                        .content_match_ids
                        .as_ref()
                        .is_some_and(|ids| ids.contains(&s.id));
                    title_match || content_match
                })
                .collect()
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn render(
        &mut self,
        frame: &mut Frame,
        area: Rect,
        sessions: &[SessionSummary],
        focused: bool,
        theme: &Theme,
        layout: &str,
        date_format: &str,
        confirm_delete: Option<&str>,   // session_id pending delete
        renaming: Option<(&str, &str)>, // (session_id, edit_buffer)
    ) {
        let border_style = if focused {
            Style::default().fg(theme.accent)
        } else {
            theme.border_style()
        };
        let title = if focused {
            Line::from(Span::styled(
                " Sessions [Tab] ",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            ))
        } else {
            Line::from(Span::styled(
                " Sessions ",
                Style::default().fg(theme.thinking_fg),
            ))
        };
        let block = Block::default()
            .borders(Borders::RIGHT)
            .border_style(border_style)
            .title_top(title)
            .style(theme.sidebar_style());

        let inner = block.inner(area);
        frame.render_widget(block, area);

        if inner.height < 3 {
            return;
        }

        // Search box at top
        let search_text = if self.search_input.is_empty() && !self.search_focused {
            Span::styled("/ search...", Style::default().fg(theme.thinking_fg))
        } else {
            Span::styled(
                format!("/ {}", self.search_input),
                Style::default().fg(theme.accent),
            )
        };
        let search_line = Paragraph::new(Line::from(search_text));
        let search_area = Rect {
            x: inner.x,
            y: inner.y,
            width: inner.width,
            height: 1,
        };
        frame.render_widget(search_line, search_area);

        // Blank line after search (both tight and loose)
        let list_area = Rect {
            x: inner.x,
            y: inner.y + 2, // +1 search, +1 blank
            width: inner.width,
            height: inner.height.saturating_sub(2),
        };

        let filtered = self.filtered_sessions(sessions);
        let is_loose = layout == "loose";
        let today = Local::now().date_naive();

        // Build list items with date group headers
        let mut items: Vec<ListItem> = Vec::new();
        let mut current_group: Option<String> = None;
        let mut item_index = 0;
        let mut rename_line_y: Option<u16> = None; // track Y position of renaming item
        let mut current_line: u16 = 0;
        let header_style = Style::default()
            .fg(theme.accent)
            .add_modifier(Modifier::ITALIC);

        for session in filtered.iter().skip(self.scroll_offset) {
            let group = date_group_label(&session.updated_at, today, date_format);
            if current_group.as_ref() != Some(&group) {
                // Add group header
                if current_group.is_some() && is_loose {
                    items.push(ListItem::new(Line::from("")));
                    current_line += 1;
                }
                items.push(ListItem::new(Line::from(Span::styled(
                    group.clone(),
                    header_style,
                ))));
                current_line += 1;
                current_group = Some(group);
            }

            let is_selected = item_index + self.scroll_offset == self.selected;
            let style = if is_selected {
                theme.sidebar_selected_style().add_modifier(Modifier::BOLD)
            } else {
                theme.sidebar_style()
            };

            // Check if this session has a pending action
            let is_confirming_delete = confirm_delete == Some(session.id.as_str());
            let is_renaming = renaming.is_some_and(|(id, _)| id == session.id);

            // Track the Y position of the renaming item
            if is_renaming {
                rename_line_y = Some(current_line);
            }

            let (title_line, preview_line) = if is_confirming_delete {
                // Delete confirmation
                (
                    Line::from(vec![
                        Span::styled(
                            "Delete? ",
                            Style::default()
                                .fg(theme.error)
                                .add_modifier(Modifier::BOLD),
                        ),
                        Span::styled("y/n", Style::default().fg(theme.warning)),
                    ]),
                    Line::from(Span::styled(
                        format!(" {}", session.title),
                        Style::default().fg(theme.thinking_fg),
                    )),
                )
            } else if let Some((_, buf)) = renaming.filter(|_| is_renaming) {
                (
                    Line::from(vec![
                        Span::styled("Rename: ", Style::default().fg(theme.accent)),
                        Span::styled(
                            buf.to_string(),
                            Style::default()
                                .fg(theme.fg)
                                .add_modifier(Modifier::UNDERLINED),
                        ),
                        Span::styled("_", Style::default().fg(theme.accent)),
                    ]),
                    Line::from(Span::styled(
                        " Enter:save  Esc:cancel",
                        Style::default().fg(theme.thinking_fg),
                    )),
                )
            } else {
                let search_q = &self.search_input;
                let hl_style = Style::default()
                    .fg(theme.warning)
                    .add_modifier(Modifier::BOLD);

                let mut title_spans: Vec<Span> =
                    highlight_matches(&session.title, search_q, style, hl_style);
                for tag in &session.tags {
                    title_spans.push(Span::raw(" "));
                    title_spans.push(Span::styled(
                        format!("[{}]", tag),
                        Style::default().fg(theme.tag),
                    ));
                }

                let preview_text = strip_markdown(session.preview.as_deref().unwrap_or(""));
                let max_w = inner.width.saturating_sub(4) as usize;
                let preview_truncated =
                    tuillem_markdown::width::truncate_with_ellipsis(&preview_text, max_w);

                let preview_style = Style::default().fg(theme.thinking_fg);
                let preview_spans = highlight_matches(
                    &format!(" {}", preview_truncated),
                    search_q,
                    preview_style,
                    hl_style,
                );

                (Line::from(title_spans), Line::from(preview_spans))
            };

            let mut item_lines = vec![title_line, preview_line];
            if is_loose {
                item_lines.push(Line::from(""));
            }

            let item_height = if is_loose { 3u16 } else { 2u16 };
            items.push(ListItem::new(item_lines).style(if is_selected {
                Style::default().bg(theme.sidebar_selected_bg)
            } else {
                Style::default()
            }));

            current_line += item_height;
            item_index += 1;

            // Stop if we've filled the visible area (rough estimate)
            let total_lines: usize = items.iter().map(|i| i.height()).sum();
            if total_lines >= list_area.height as usize {
                break;
            }
        }

        // Track how many session items fit on screen for scroll calculations
        self.visible_count = item_index;

        let list = List::new(items);
        frame.render_widget(list, list_area);

        // Show terminal cursor when renaming
        if let Some((_, buf)) = renaming
            && let Some(ry) = rename_line_y
        {
            let prefix_text = "Rename: ";
            let cursor_x = list_area.x + prefix_text.len() as u16 + buf.chars().count() as u16;
            let cursor_y = list_area.y + ry;
            if cursor_x < list_area.x + list_area.width && cursor_y < list_area.y + list_area.height
            {
                frame.set_cursor_position((cursor_x, cursor_y));
            }
        }

        // Render scrollbar if there are more sessions than visible
        let total_sessions = filtered.len();
        if total_sessions > self.visible_count {
            let mut scrollbar_state =
                ScrollbarState::new(total_sessions.saturating_sub(self.visible_count))
                    .position(self.scroll_offset);
            let thumb_color = if focused {
                theme.accent
            } else {
                theme.thinking_fg
            };
            let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
                .track_style(Style::default().fg(theme.border))
                .thumb_style(Style::default().fg(thumb_color));
            frame.render_stateful_widget(scrollbar, list_area, &mut scrollbar_state);
        }
    }

    /// Adjust scroll_offset so that `self.selected` is visible.
    pub fn ensure_visible(&mut self) {
        if self.selected < self.scroll_offset {
            self.scroll_offset = self.selected;
        }
        let visible = if self.visible_count > 0 {
            self.visible_count
        } else {
            10
        };
        if self.selected >= self.scroll_offset + visible {
            self.scroll_offset = self.selected + 1 - visible;
        }
    }

    pub fn move_up(&mut self, count: usize) {
        self.selected = self.selected.saturating_sub(count);
        // Keep selection visible
        if self.selected < self.scroll_offset {
            self.scroll_offset = self.selected;
        }
    }

    pub fn move_down(&mut self, session_count: usize, count: usize) {
        if session_count > 0 {
            self.selected = (self.selected + count).min(session_count - 1);
            // Keep selection visible — use visible_count from last render
            let visible = if self.visible_count > 0 {
                self.visible_count
            } else {
                10
            };
            if self.selected >= self.scroll_offset + visible {
                self.scroll_offset = self.selected + 1 - visible;
            }
        }
    }
}

impl Default for Sidebar {
    fn default() -> Self {
        Self::new()
    }
}

/// Determine the date group label for a session based on its updated_at timestamp.
/// Recent dates use friendly labels; older dates use the configured format.
pub fn date_group_label(updated_at: &str, today: NaiveDate, date_format: &str) -> String {
    let date = DateTime::parse_from_rfc3339(updated_at)
        .map(|dt| dt.with_timezone(&Local).date_naive())
        .or_else(|_| {
            updated_at
                .parse::<DateTime<Utc>>()
                .map(|dt| dt.with_timezone(&Local).date_naive())
        })
        .unwrap_or(today);

    let days_ago = (today - date).num_days();

    let chrono_fmt = match date_format {
        "yyyy-mm-dd" => "%Y-%m-%d",
        "mm/dd/yyyy" => "%m/%d/%Y",
        "dd.mm.yyyy" => "%d.%m.%Y",
        "dd/mm/yyyy" => "%d/%m/%Y",
        _ => "%d/%m/%Y",
    };

    match days_ago {
        0 => "Today".to_string(),
        1 => "Yesterday".to_string(),
        2..=6 => date.format("%A").to_string(), // e.g. "Monday"
        _ => date.format(chrono_fmt).to_string(),
    }
}

/// Strip common markdown syntax for a clean single-line preview.
fn strip_markdown(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let collapsed = text.replace('\n', " ");
    let mut chars = collapsed.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '*' | '_' => {
                while chars.peek() == Some(&c) {
                    chars.next();
                }
            }
            '`' => {
                while chars.peek() == Some(&'`') {
                    chars.next();
                }
            }
            '#' if out.is_empty() || out.ends_with(' ') => {
                while chars.peek() == Some(&'#') {
                    chars.next();
                }
                if chars.peek() == Some(&' ') {
                    chars.next();
                }
            }
            '~' => {
                while chars.peek() == Some(&'~') {
                    chars.next();
                }
            }
            '[' => {
                // [text](url) → text
                let mut link_text = String::new();
                for lc in chars.by_ref() {
                    if lc == ']' {
                        break;
                    }
                    link_text.push(lc);
                }
                out.push_str(&link_text);
                if chars.peek() == Some(&'(') {
                    chars.next();
                    for lc in chars.by_ref() {
                        if lc == ')' {
                            break;
                        }
                    }
                }
            }
            _ => out.push(c),
        }
    }
    // Collapse multiple spaces
    let mut result = String::with_capacity(out.len());
    let mut prev_space = false;
    for c in out.chars() {
        if c == ' ' {
            if !prev_space {
                result.push(' ');
            }
            prev_space = true;
        } else {
            result.push(c);
            prev_space = false;
        }
    }
    result.trim().to_string()
}