pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
use ratatui::{
    Frame,
    prelude::{Position, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, Paragraph},
};

use crate::i18n;
use crate::state::AppState;
use crate::state::types::AppMode;
use crate::theme::theme;

/// What: Build input line spans with optional selection highlighting.
///
/// Inputs:
/// - `app`: Application state (input, caret, selection, focus, normal mode)
/// - `search_focused`: Whether search pane is focused
/// - `th`: Theme
///
/// Output:
/// - Vector of spans for the input line
///
/// Details:
/// - Shows "> " prefix; in normal mode, highlights selected text with lavender background.
/// - Uses `news_search_input`/`news_search_caret` in News mode, otherwise uses `app.input`/`app.search_caret`.
fn build_input_spans<'a>(
    app: &AppState,
    search_focused: bool,
    th: &'a crate::theme::Theme,
) -> Vec<Span<'a>> {
    let mut input_spans: Vec<Span> = Vec::new();
    input_spans.push(Span::styled(
        "> ",
        Style::default().fg(if search_focused {
            th.sapphire
        } else {
            th.overlay1
        }),
    ));

    // Determine which input field and caret to use based on mode
    let (input_text, caret_ci, select_anchor) = if matches!(app.app_mode, AppMode::News) {
        (
            &app.news_search_input,
            app.news_search_caret,
            app.news_search_select_anchor,
        )
    } else {
        (&app.input, app.search_caret, app.search_select_anchor)
    };

    if search_focused && app.search_normal_mode {
        let (sel_from_ci, sel_to_ci) = select_anchor.map_or((caret_ci, caret_ci), |anchor| {
            (anchor.min(caret_ci), anchor.max(caret_ci))
        });
        let cc = input_text.chars().count();
        let sel_from_ci = sel_from_ci.min(cc);
        let sel_to_ci = sel_to_ci.min(cc);
        let from_b = {
            if sel_from_ci == 0 {
                0
            } else {
                input_text
                    .char_indices()
                    .map(|(i, _)| i)
                    .nth(sel_from_ci)
                    .unwrap_or(input_text.len())
            }
        };
        let to_b = {
            if sel_to_ci == 0 {
                0
            } else {
                input_text
                    .char_indices()
                    .map(|(i, _)| i)
                    .nth(sel_to_ci)
                    .unwrap_or(input_text.len())
            }
        };
        let pre = &input_text[..from_b];
        let sel = &input_text[from_b..to_b];
        let post = &input_text[to_b..];
        if !pre.is_empty() {
            input_spans.push(Span::styled(
                pre.to_string(),
                Style::default().fg(if search_focused { th.text } else { th.subtext0 }),
            ));
        }
        if sel_from_ci != sel_to_ci {
            input_spans.push(Span::styled(
                sel.to_string(),
                Style::default()
                    .fg(th.crust)
                    .bg(th.lavender)
                    .add_modifier(Modifier::BOLD),
            ));
        }
        if !post.is_empty() {
            input_spans.push(Span::styled(
                post.to_string(),
                Style::default().fg(if search_focused { th.text } else { th.subtext0 }),
            ));
        }
    } else {
        input_spans.push(Span::styled(
            input_text.as_str().to_string(),
            Style::default().fg(if search_focused { th.text } else { th.subtext0 }),
        ));
    }
    input_spans
}

/// What: Build title line with fuzzy/normal mode indicator.
///
/// Inputs:
/// - `app`: Application state
/// - `search_focused`: Whether search pane is focused
/// - `th`: Theme
///
/// Output:
/// - Tuple containing: title line with mode indicator, base title length, and mode text
///
/// Details:
/// - Returns base title length for rectangle calculation
/// - Returns mode text to avoid duplicate i18n lookups
fn build_title_line<'a>(
    app: &AppState,
    search_focused: bool,
    th: &'a crate::theme::Theme,
) -> (Line<'a>, usize, String) {
    let search_title_base = if matches!(app.app_mode, AppMode::News) {
        "News search".to_string()
    } else if search_focused {
        i18n::t(app, "app.titles.search_focused")
    } else {
        i18n::t(app, "app.titles.search")
    };
    let search_title_color = if search_focused {
        th.mauve
    } else {
        th.overlay1
    };

    // Calculate title length before building spans (needed for rectangle calculation)
    let base_title_len = search_title_base.chars().count();

    // Build title with fuzzy/normal indicator
    let mut title_spans = vec![Span::styled(
        search_title_base,
        Style::default().fg(search_title_color),
    )];

    // Add fuzzy/normal mode indicator
    let mode_text = if app.fuzzy_search_enabled {
        i18n::t(app, "app.search_mode_fuzzy")
    } else {
        i18n::t(app, "app.search_mode_normal")
    };
    let mode_color = if app.fuzzy_search_enabled {
        th.sapphire
    } else {
        th.subtext0
    };
    title_spans.push(Span::styled(
        format!(" [{mode_text}]"),
        Style::default().fg(mode_color),
    ));
    (Line::from(title_spans), base_title_len, mode_text)
}

/// What: Calculate and store fuzzy indicator rectangle.
///
/// Inputs:
/// - `app`: Mutable application state
/// - `area`: Search input area
/// - `base_title_len`: Length of base title text
/// - `mode_text`: Mode indicator text
///
/// Output:
/// - None (modifies app state)
///
/// Details:
/// - Stores clickable rectangle for mouse interaction
fn store_fuzzy_indicator_rect(
    app: &mut AppState,
    area: Rect,
    base_title_len: usize,
    mode_text: &str,
) {
    let mode_indicator_len = mode_text.chars().count() + 3; // +3 for " [ ]"
    let max_indicator_width = mode_indicator_len.min(20);
    let max_indicator_width_u16 = u16::try_from(max_indicator_width).unwrap_or(u16::MAX);
    let available_width = area.width.saturating_sub(max_indicator_width_u16);
    let title_end_x = area.x.saturating_add(1).saturating_add(
        u16::try_from(
            base_title_len
                .min(available_width as usize)
                .min(u16::MAX as usize),
        )
        .unwrap_or(u16::MAX),
    );
    app.fuzzy_indicator_rect = Some((
        title_end_x,
        area.y,
        max_indicator_width_u16,
        1, // height
    ));
}

/// What: Render the search input widget in the center of the middle row.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state (input, caret, selection, focus, fuzzy indicator rect)
/// - `area`: Target rectangle for the search input
///
/// Output:
/// - Draws the search input with optional text selection highlighting and sets cursor position.
///
/// Details:
/// - Shows "> " prefix; in normal mode, highlights selected text with lavender background.
/// - Cursor position is calculated based on caret index and character width.
/// - Records fuzzy indicator rectangle for mouse click detection.
pub fn render_search(f: &mut Frame, app: &mut AppState, area: Rect) {
    let th = theme();
    let search_focused = matches!(app.focus, crate::state::Focus::Search);

    // Build input line with optional selection highlight in Search normal mode
    let input_spans = build_input_spans(app, search_focused, &th);
    let input_line = Line::from(input_spans);

    // Build title with fuzzy/normal indicator
    let (search_title, base_title_len, mode_text) = build_title_line(app, search_focused, &th);
    let input = Paragraph::new(input_line)
        .style(
            Style::default()
                .fg(if search_focused { th.text } else { th.subtext0 })
                .bg(th.base),
        )
        .block(
            Block::default()
                .title(search_title)
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(if search_focused {
                    th.mauve
                } else {
                    th.surface1
                })),
        );
    f.render_widget(input, area);

    // Store clickable rectangle for fuzzy indicator (right side of title)
    store_fuzzy_indicator_rect(app, area, base_title_len, &mode_text);

    // Cursor in input
    let right = area.x + area.width.saturating_sub(1);
    // Cursor x: align to caret in characters from start (prefix "> ")
    // Use news_search_input/news_search_caret in News mode, otherwise use app.input/app.search_caret
    let (input_text, caret_pos) = if matches!(app.app_mode, AppMode::News) {
        (&app.news_search_input, app.news_search_caret)
    } else {
        (&app.input, app.search_caret)
    };
    let caret_cols: u16 = if search_focused {
        let mut ci: u16 = 0;
        let mut it = input_text.chars();
        for _ in 0..caret_pos {
            if it.next().is_some() {
                ci = ci.saturating_add(1);
            } else {
                break;
            }
        }
        ci
    } else {
        u16::try_from(input_text.len()).unwrap_or(u16::MAX)
    };
    let x = std::cmp::min(area.x + 1 + 2 + caret_cols, right);
    let y = area.y + 1;
    f.set_cursor_position(Position::new(x, y));
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::{Terminal, backend::TestBackend};

    /// What: Initialize minimal English translations for search tests.
    ///
    /// Inputs:
    /// - `app`: `AppState` to populate with translations
    ///
    /// Output:
    /// - Populates `app.translations` and `app.translations_fallback` with search-related translations
    ///
    /// Details:
    /// - Sets up only the translations needed for search rendering tests.
    fn init_test_translations(app: &mut crate::state::AppState) {
        use std::collections::HashMap;
        let mut translations = HashMap::new();
        translations.insert("app.titles.search".to_string(), "Search".to_string());
        translations.insert(
            "app.titles.search_focused".to_string(),
            "Search".to_string(),
        );
        app.translations = translations.clone();
        app.translations_fallback = translations;
    }

    /// What: Verify search input renders and sets cursor position correctly when focused.
    ///
    /// Inputs:
    /// - Search input with text "hello" and caret at position 3
    ///
    /// Output:
    /// - Search input renders without panic, cursor position is set correctly.
    ///
    /// Details:
    /// - Tests that cursor position calculation accounts for the "> " prefix and character width.
    #[test]
    fn search_renders_and_sets_cursor_when_focused() {
        let backend = TestBackend::new(100, 30);
        let mut term = Terminal::new(backend).expect("Failed to create terminal for test");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        app.focus = crate::state::Focus::Search;
        app.input = "hello".into();
        app.search_caret = 3;

        term.draw(|f| {
            let area = f.area();
            render_search(f, &mut app, area);
        })
        .expect("Failed to render search pane");

        // Cursor position is set by set_cursor_position - verify rendering succeeded
        // TestBackend doesn't expose cursor position directly, but rendering
        // completing without panic verifies the function works correctly
    }

    /// What: Verify search input renders without selection highlighting when not in normal mode.
    ///
    /// Inputs:
    /// - Search input with text, focused but not in normal mode
    ///
    /// Output:
    /// - Search input renders without selection spans.
    ///
    /// Details:
    /// - Tests that selection highlighting only appears when both focused and in normal mode.
    #[test]
    fn search_renders_without_selection_when_not_normal_mode() {
        let backend = TestBackend::new(100, 30);
        let mut term = Terminal::new(backend).expect("Failed to create terminal for test");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        app.focus = crate::state::Focus::Search;
        app.input = "test".into();
        app.search_normal_mode = false;
        app.search_caret = 2;
        app.search_select_anchor = Some(1);

        term.draw(|f| {
            let area = f.area();
            render_search(f, &mut app, area);
        })
        .expect("Failed to render search pane without selection");

        // Should render without panic even with selection anchor set but not in normal mode
    }

    /// What: Verify search input renders with text selection highlighting in normal mode.
    ///
    /// Inputs:
    /// - Search input with text "hello", caret at 3, anchor at 1, in normal mode
    ///
    /// Output:
    /// - Search input renders with selection highlighting between anchor and caret.
    ///
    /// Details:
    /// - Tests that selected text (characters 1-3) is highlighted with lavender background.
    #[test]
    fn search_renders_with_selection_in_normal_mode() {
        let backend = TestBackend::new(100, 30);
        let mut term = Terminal::new(backend).expect("Failed to create terminal for test");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        app.focus = crate::state::Focus::Search;
        app.input = "hello".into();
        app.search_normal_mode = true;
        app.search_caret = 3;
        app.search_select_anchor = Some(1);

        term.draw(|f| {
            let area = f.area();
            render_search(f, &mut app, area);
        })
        .expect("Failed to render search pane with selection");

        // Should render with selection highlighting
    }

    /// What: Verify search input renders correctly when not focused.
    ///
    /// Inputs:
    /// - Search input with text, but focus is on another pane
    ///
    /// Output:
    /// - Search input renders with unfocused styling.
    ///
    /// Details:
    /// - Tests that unfocused search uses different colors and cursor position calculation.
    #[test]
    fn search_renders_when_unfocused() {
        let backend = TestBackend::new(100, 30);
        let mut term = Terminal::new(backend).expect("Failed to create terminal for test");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        app.focus = crate::state::Focus::Recent;
        app.input = "test".into();
        app.search_caret = 2;

        term.draw(|f| {
            let area = f.area();
            render_search(f, &mut app, area);
        })
        .expect("Failed to render unfocused search pane");

        // Should render without panic with unfocused styling
    }

    /// What: Verify cursor position calculation handles empty input correctly.
    ///
    /// Inputs:
    /// - Empty search input with caret at 0
    ///
    /// Output:
    /// - Cursor position is set after the "> " prefix.
    ///
    /// Details:
    /// - Tests edge case where input is empty and caret is at start.
    #[test]
    fn search_handles_empty_input() {
        let backend = TestBackend::new(100, 30);
        let mut term = Terminal::new(backend).expect("failed to create test terminal");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        app.focus = crate::state::Focus::Search;
        app.input = String::new();
        app.search_caret = 0;

        term.draw(|f| {
            let area = f.area();
            render_search(f, &mut app, area);
        })
        .expect("failed to draw test terminal");

        // Should handle empty input without panic
    }
}