coding-agent-search 0.6.2

Unified TUI search over local coding agent histories
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
//! Toast notification component for transient user feedback.
//!
//! Provides non-blocking notifications that auto-dismiss after a configurable duration.
//! Supports coalescing of similar messages to prevent notification spam.

use ftui::core::geometry::Rect;
use ftui::render::cell::PackedRgba;
use std::collections::VecDeque;
use std::time::{Duration, Instant};

use super::theme::ThemePalette;

/// Type of toast notification, determines styling and icon
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToastType {
    /// Informational message
    Info,
    /// Success/completion message
    Success,
    /// Warning that doesn't block operation
    Warning,
    /// Error that needs attention
    Error,
}

impl ToastType {
    /// Get the icon/prefix for this toast type
    pub fn icon(self) -> &'static str {
        match self {
            Self::Info => "i",
            Self::Success => "*",
            Self::Warning => "!",
            Self::Error => "x",
        }
    }

    /// Get the color for this toast type as a PackedRgba.
    pub fn color(self, palette: &ThemePalette) -> PackedRgba {
        match self {
            Self::Info => palette.accent,
            Self::Success => palette.user,
            Self::Warning => palette.system,
            Self::Error => PackedRgba::rgb(247, 118, 142),
        }
    }

    /// Get default duration for this toast type
    pub fn default_duration(self) -> Duration {
        match self {
            Self::Info => Duration::from_secs(3),
            Self::Success => Duration::from_secs(2),
            Self::Warning => Duration::from_secs(4),
            Self::Error => Duration::from_secs(6), // Errors stay longer
        }
    }
}

/// Position where toasts appear on screen
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ToastPosition {
    /// Top-right corner (default)
    #[default]
    TopRight,
    /// Top-left corner
    TopLeft,
    /// Bottom-right corner
    BottomRight,
    /// Bottom-left corner
    BottomLeft,
    /// Top-center
    TopCenter,
    /// Bottom-center
    BottomCenter,
}

/// A single toast notification
#[derive(Debug, Clone)]
pub struct Toast {
    /// Unique identifier for coalescing
    pub id: String,
    /// The message to display
    pub message: String,
    /// Type of toast (determines styling)
    pub toast_type: ToastType,
    /// When the toast was created
    pub created_at: Instant,
    /// How long until auto-dismiss
    pub duration: Duration,
    /// Number of coalesced messages (for "x5" badge)
    pub count: usize,
}

impl Toast {
    /// Create a new toast with default duration
    pub fn new(message: impl Into<String>, toast_type: ToastType) -> Self {
        let message = message.into();
        let id = format!("{:?}:{}", toast_type, message);
        Self {
            id,
            message,
            toast_type,
            created_at: Instant::now(),
            duration: toast_type.default_duration(),
            count: 1,
        }
    }

    /// Create a toast with custom duration
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    /// Create a toast with custom ID (for coalescing control)
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = id.into();
        self
    }

    /// Check if this toast has expired
    pub fn is_expired(&self) -> bool {
        self.created_at.elapsed() >= self.duration
    }

    /// Get remaining time as a fraction (0.0 = expired, 1.0 = just created)
    pub fn remaining_fraction(&self) -> f32 {
        let total = self.duration.as_secs_f32();
        if total <= 0.0 {
            return 0.0; // Treat zero/negative duration as immediately expired
        }
        let elapsed = self.created_at.elapsed().as_secs_f32();
        (1.0 - elapsed / total).clamp(0.0, 1.0)
    }

    /// Convenience constructors
    pub fn info(message: impl Into<String>) -> Self {
        Self::new(message, ToastType::Info)
    }

    pub fn success(message: impl Into<String>) -> Self {
        Self::new(message, ToastType::Success)
    }

    pub fn warning(message: impl Into<String>) -> Self {
        Self::new(message, ToastType::Warning)
    }

    pub fn error(message: impl Into<String>) -> Self {
        Self::new(message, ToastType::Error)
    }
}

/// Manages a collection of toast notifications
#[derive(Debug)]
pub struct ToastManager {
    /// Active toasts (newest first for top-down rendering)
    toasts: VecDeque<Toast>,
    /// Maximum number of visible toasts
    max_visible: usize,
    /// Position on screen
    position: ToastPosition,
    /// Whether to coalesce similar toasts
    coalesce: bool,
}

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

impl ToastManager {
    /// Create a new toast manager with defaults
    pub fn new() -> Self {
        Self {
            toasts: VecDeque::new(),
            max_visible: 5,
            position: ToastPosition::TopRight,
            coalesce: true,
        }
    }

    /// Set maximum visible toasts
    pub fn with_max_visible(mut self, max: usize) -> Self {
        self.max_visible = max;
        self
    }

    /// Set toast position
    pub fn with_position(mut self, position: ToastPosition) -> Self {
        self.position = position;
        self
    }

    /// Enable/disable coalescing
    pub fn with_coalesce(mut self, coalesce: bool) -> Self {
        self.coalesce = coalesce;
        self
    }

    /// Add a new toast
    pub fn push(&mut self, toast: Toast) {
        // Try to coalesce with existing toast
        if self.coalesce
            && let Some(existing) = self.toasts.iter_mut().find(|t| t.id == toast.id)
        {
            existing.count = existing.count.saturating_add(1);
            existing.created_at = Instant::now(); // Reset timer
            return;
        }

        // Add new toast at front
        self.toasts.push_front(toast);

        // Trim excess
        let retention_limit = self.max_visible.saturating_mul(2);
        while self.toasts.len() > retention_limit {
            self.toasts.pop_back();
        }
    }

    /// Remove expired toasts
    pub fn tick(&mut self) {
        self.toasts.retain(|t| !t.is_expired());
    }

    /// Clear all toasts
    pub fn clear(&mut self) {
        self.toasts.clear();
    }

    /// Dismiss the oldest toast
    pub fn dismiss_oldest(&mut self) {
        self.toasts.pop_back();
    }

    /// Dismiss all toasts of a specific type
    pub fn dismiss_type(&mut self, toast_type: ToastType) {
        self.toasts.retain(|t| t.toast_type != toast_type);
    }

    /// Get visible toasts (limited by `max_visible`)
    pub fn visible(&self) -> impl Iterator<Item = &Toast> {
        self.toasts.iter().take(self.max_visible)
    }

    /// Check if there are any active toasts
    pub fn is_empty(&self) -> bool {
        self.toasts.is_empty()
    }

    /// Get count of active toasts
    pub fn len(&self) -> usize {
        self.toasts.len()
    }

    /// Get the position setting
    pub fn position(&self) -> ToastPosition {
        self.position
    }

    /// Calculate the render area for toasts given the full terminal area
    pub fn render_area(&self, full_area: Rect) -> Rect {
        const HORIZONTAL_MARGIN: u16 = 2;
        const TOAST_ROW_HEIGHT: usize = 3;
        const VERTICAL_MARGIN: u16 = 1;

        let toast_width = 40.min(full_area.width.saturating_sub(4));
        let visible_count = self.visible().count();
        let max_height = full_area.height.saturating_sub(VERTICAL_MARGIN * 2);
        let toast_height = visible_count
            .saturating_mul(TOAST_ROW_HEIGHT)
            .min(usize::from(max_height))
            .try_into()
            .unwrap_or(max_height);

        if toast_width == 0 || toast_height == 0 {
            return Rect::new(full_area.x, full_area.y, 0, 0);
        }

        let x = match self.position {
            ToastPosition::TopLeft | ToastPosition::BottomLeft => {
                full_area.x.saturating_add(HORIZONTAL_MARGIN)
            }
            ToastPosition::TopRight | ToastPosition::BottomRight => full_area
                .right()
                .saturating_sub(toast_width.saturating_add(HORIZONTAL_MARGIN)),
            ToastPosition::TopCenter | ToastPosition::BottomCenter => full_area
                .x
                .saturating_add(full_area.width.saturating_sub(toast_width) / 2),
        };

        let y = match self.position {
            ToastPosition::TopLeft | ToastPosition::TopRight | ToastPosition::TopCenter => {
                full_area.y.saturating_add(VERTICAL_MARGIN)
            }
            ToastPosition::BottomLeft
            | ToastPosition::BottomRight
            | ToastPosition::BottomCenter => full_area
                .bottom()
                .saturating_sub(toast_height.saturating_add(VERTICAL_MARGIN)),
        };

        Rect::new(x, y, toast_width, toast_height)
    }
}

#[cfg(test)]
mod tests {
    use super::{Toast, ToastManager, ToastPosition, ToastType};
    use ftui::core::geometry::Rect;
    use std::collections::VecDeque;
    use std::time::Duration;

    #[test]
    fn test_toast_creation() {
        let toast = Toast::info("Test message");
        assert_eq!(toast.message, "Test message");
        assert_eq!(toast.toast_type, ToastType::Info);
        assert_eq!(toast.count, 1);
    }

    #[test]
    fn test_toast_type_defaults() {
        assert_eq!(ToastType::Info.default_duration(), Duration::from_secs(3));
        assert_eq!(ToastType::Error.default_duration(), Duration::from_secs(6));
    }

    #[test]
    fn test_toast_manager_push() {
        let mut manager = ToastManager::new();
        manager.push(Toast::info("First"));
        manager.push(Toast::success("Second"));
        assert_eq!(manager.len(), 2);
    }

    #[test]
    fn test_toast_coalescing() {
        let mut manager = ToastManager::new().with_coalesce(true);
        manager.push(Toast::info("Same message"));
        manager.push(Toast::info("Same message"));
        manager.push(Toast::info("Same message"));

        assert_eq!(manager.len(), 1);
        assert_eq!(manager.visible().next().unwrap().count, 3);
    }

    #[test]
    fn test_toast_coalesced_count_saturates() {
        let mut manager = ToastManager::new().with_coalesce(true);
        manager.push(Toast::info("Same message"));
        manager.toasts.front_mut().unwrap().count = usize::MAX;

        manager.push(Toast::info("Same message"));

        assert_eq!(manager.len(), 1);
        assert_eq!(manager.visible().next().unwrap().count, usize::MAX);
    }

    #[test]
    fn test_toast_coalescing_disabled() {
        let mut manager = ToastManager::new().with_coalesce(false);
        manager.push(Toast::info("Same message"));
        manager.push(Toast::info("Same message"));

        assert_eq!(manager.len(), 2);
    }

    #[test]
    fn test_toast_position() {
        let manager = ToastManager::new().with_position(ToastPosition::BottomLeft);
        assert_eq!(manager.position(), ToastPosition::BottomLeft);
    }

    #[test]
    fn test_toast_retention_limit_saturates() {
        let mut manager = ToastManager::new()
            .with_max_visible(usize::MAX)
            .with_coalesce(false);

        manager.push(Toast::info("First"));
        manager.push(Toast::info("Second"));

        assert_eq!(manager.len(), 2);
    }

    #[test]
    fn test_render_area_respects_full_area_origin() {
        let mut manager = ToastManager::new().with_position(ToastPosition::TopLeft);
        manager.push(Toast::info("Origin-aware"));

        let area = manager.render_area(Rect::new(10, 5, 80, 20));

        assert_eq!(area.x, 12);
        assert_eq!(area.y, 6);
        assert_eq!(area.width, 40);
        assert_eq!(area.height, 3);
    }

    #[test]
    fn test_render_area_caps_large_visible_count_without_truncation() {
        let manager = ToastManager {
            toasts: (0..21_846)
                .map(|idx| Toast::info(format!("Toast {idx}")))
                .collect::<VecDeque<_>>(),
            max_visible: usize::MAX,
            position: ToastPosition::TopRight,
            coalesce: false,
        };

        let area = manager.render_area(Rect::new(0, 0, 80, u16::MAX));

        assert_eq!(area.height, u16::MAX - 2);
    }

    #[test]
    fn test_dismiss_type() {
        let mut manager = ToastManager::new();
        manager.push(Toast::info("Info 1"));
        manager.push(Toast::error("Error 1"));
        manager.push(Toast::info("Info 2"));

        manager.dismiss_type(ToastType::Info);
        assert_eq!(manager.len(), 1);
        assert_eq!(
            manager.visible().next().unwrap().toast_type,
            ToastType::Error
        );
    }
}