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
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout},
    prelude::Rect,
    style::Style,
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, List},
};

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

/// What: Render Downgrade and Remove lists side-by-side in installed-only mode.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state (downgrade/remove lists, focus, selection)
/// - `area`: Target rectangle for the right pane (will be split 50/50)
///
/// Output:
/// - Draws Downgrade (left) and Remove (right) lists, records rects for mouse hit-testing.
///
/// Details:
/// - Splits the area horizontally into two equal panes.
/// - Import/Export buttons are not shown in installed-only mode.
pub fn render_installed_only(f: &mut Frame, app: &mut AppState, area: Rect) {
    let install_focused = matches!(app.focus, crate::state::Focus::Install);

    // In installed-only mode, split the right pane into Downgrade (left) and Remove (right)
    let right_split = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(area);

    // Downgrade List (left)
    render_downgrade_list(f, app, right_split[0], install_focused);

    // Remove List (right)
    render_remove_list(f, app, right_split[1], install_focused);

    // Record inner Install rect for mouse hit-testing (map to Remove list area)
    app.install_rect = Some((
        right_split[1].x + 1,
        right_split[1].y + 1,
        right_split[1].width.saturating_sub(2),
        right_split[1].height.saturating_sub(2),
    ));
    // Import/Export buttons not shown in installed-only mode
    app.install_import_rect = None;
    app.install_export_rect = None;
}

/// What: Render the Downgrade list in the left half of the installed-only pane.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state (downgrade list, focus, selection)
/// - `area`: Target rectangle for the downgrade list
/// - `install_focused`: Whether the install pane is focused
///
/// Output:
/// - Draws the downgrade list and records inner rect for mouse hit-testing.
fn render_downgrade_list(f: &mut Frame, app: &mut AppState, area: Rect, install_focused: bool) {
    let dg_indices: Vec<usize> = (0..app.downgrade_list.len()).collect();
    let downgrade_selected_idx = app.downgrade_state.selected();
    let downgrade_items = crate::ui::middle::install::build_package_list_items(
        &dg_indices,
        &app.downgrade_list,
        downgrade_selected_idx,
        install_focused
            && matches!(
                app.right_pane_focus,
                crate::state::RightPaneFocus::Downgrade
            ),
        |name| crate::ui::helpers::is_package_loading_preflight(app, name),
    );
    let downgrade_is_focused = install_focused
        && matches!(
            app.right_pane_focus,
            crate::state::RightPaneFocus::Downgrade
        );
    let inner_rect = render_package_list_widget(
        f,
        downgrade_items,
        area,
        i18n::t(app, "app.titles.downgrade_list_focused"),
        i18n::t(app, "app.titles.downgrade_list"),
        downgrade_is_focused,
        &mut app.downgrade_state,
    );
    app.downgrade_rect = Some((
        inner_rect.x,
        inner_rect.y,
        inner_rect.width,
        inner_rect.height,
    ));
}

/// What: Build a styled block for a package list widget.
///
/// Inputs:
/// - `title`: Block title text
/// - `is_focused`: Whether the list is focused
///
/// Output:
/// - Styled `Block` widget ready for use in a list.
///
/// Details:
/// - Applies focused/unfocused styling to title and border.
fn build_package_list_block(title: String, is_focused: bool) -> Block<'static> {
    let th = theme();
    Block::default()
        .title(Line::from(vec![Span::styled(
            title,
            Style::default().fg(if is_focused { th.mauve } else { th.overlay1 }),
        )]))
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(if is_focused { th.mauve } else { th.surface1 }))
}

/// What: Build and render a package list widget with title and styling.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `items`: List items to render
/// - `area`: Target rectangle
/// - `title_focused`: Title for focused state
/// - `title_unfocused`: Title for unfocused state
/// - `is_focused`: Whether the list is focused
/// - `state`: List state for rendering
///
/// Output:
/// - Renders the list widget and returns the inner rect.
///
/// Details:
/// - Creates a block with title, styles the list, and renders it.
fn render_package_list_widget(
    f: &mut Frame,
    items: Vec<ratatui::widgets::ListItem<'_>>,
    area: Rect,
    title_focused: String,
    title_unfocused: String,
    is_focused: bool,
    state: &mut ratatui::widgets::ListState,
) -> Rect {
    let th = theme();
    let title = if is_focused {
        title_focused
    } else {
        title_unfocused
    };
    let block = build_package_list_block(title, is_focused);
    let list = List::new(items)
        .style(
            Style::default()
                .fg(if is_focused { th.text } else { th.subtext0 })
                .bg(th.base),
        )
        .block(block)
        .highlight_style(Style::default().fg(th.text).bg(th.surface2))
        .highlight_symbol("");
    f.render_stateful_widget(list, area, state);
    Rect {
        x: area.x + 1,
        y: area.y + 1,
        width: area.width.saturating_sub(2),
        height: area.height.saturating_sub(2),
    }
}

/// What: Render the Remove list in the right half of the installed-only pane.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state (remove list, focus, selection)
/// - `area`: Target rectangle for the remove list
/// - `install_focused`: Whether the install pane is focused
///
/// Output:
/// - Draws the remove list.
fn render_remove_list(f: &mut Frame, app: &mut AppState, area: Rect, install_focused: bool) {
    let rm_indices: Vec<usize> = (0..app.remove_list.len()).collect();
    let remove_selected_idx = app.remove_state.selected();
    let remove_items = crate::ui::middle::install::build_package_list_items(
        &rm_indices,
        &app.remove_list,
        remove_selected_idx,
        install_focused && matches!(app.right_pane_focus, crate::state::RightPaneFocus::Remove),
        |name| crate::ui::helpers::is_package_loading_preflight(app, name),
    );
    let remove_is_focused =
        install_focused && matches!(app.right_pane_focus, crate::state::RightPaneFocus::Remove);
    render_package_list_widget(
        f,
        remove_items,
        area,
        i18n::t(app, "app.titles.remove_list_focused"),
        i18n::t(app, "app.titles.remove_list"),
        remove_is_focused,
        &mut app.remove_state,
    );
}

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

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

    /// What: Verify installed-only mode renders both downgrade and remove lists.
    ///
    /// Inputs:
    /// - Installed-only mode with packages in both downgrade and remove lists
    ///
    /// Output:
    /// - Both lists render and `app.install_rect` maps to Remove list area.
    ///
    /// Details:
    /// - Tests that area is split 50/50 and rects are recorded correctly.
    #[test]
    fn installed_only_renders_both_lists() {
        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.downgrade_list.push(crate::state::PackageItem {
            name: "downgrade-pkg".to_string(),
            version: "1.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: None,
            out_of_date: None,
            orphaned: false,
        });
        app.remove_list.push(crate::state::PackageItem {
            name: "remove-pkg".to_string(),
            version: "2.0.0".to_string(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: None,
            out_of_date: None,
            orphaned: false,
        });

        term.draw(|f| {
            let area = f.area();
            render_installed_only(f, &mut app, area);
        })
        .expect("Failed to render installed-only pane");

        assert!(app.install_rect.is_some());
        assert!(app.downgrade_rect.is_some());
        // Import/Export buttons should be hidden
        assert!(app.install_import_rect.is_none());
        assert!(app.install_export_rect.is_none());
    }

    /// What: Verify installed-only mode clears button rects.
    ///
    /// Inputs:
    /// - Installed-only mode activated
    ///
    /// Output:
    /// - `app.install_import_rect` and `app.install_export_rect` are set to `None`.
    ///
    /// Details:
    /// - Tests that Import/Export buttons are not shown in installed-only mode.
    #[test]
    fn installed_only_hides_buttons() {
        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);
        // Set initial button rects
        app.install_import_rect = Some((10, 10, 10, 1));
        app.install_export_rect = Some((20, 10, 10, 1));

        term.draw(|f| {
            let area = f.area();
            render_installed_only(f, &mut app, area);
        })
        .expect("Failed to render installed-only pane without buttons");

        assert!(app.install_import_rect.is_none());
        assert!(app.install_export_rect.is_none());
    }

    /// What: Verify installed-only mode splits area correctly.
    ///
    /// Inputs:
    /// - Area of width 100 split into two lists
    ///
    /// Output:
    /// - Downgrade and Remove lists each get approximately 50% width.
    ///
    /// Details:
    /// - Tests that layout splitting produces two equal panes.
    #[test]
    fn installed_only_splits_area_correctly() {
        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);

        term.draw(|f| {
            let area = f.area();
            render_installed_only(f, &mut app, area);
        })
        .expect("Failed to render installed-only pane with split area");

        let downgrade_rect = app
            .downgrade_rect
            .expect("downgrade_rect should be set after rendering");
        let install_rect = app
            .install_rect
            .expect("install_rect should be set after rendering"); // Maps to Remove list

        // Both should have similar widths (accounting for borders)
        let downgrade_width = downgrade_rect.2;
        let remove_width = install_rect.2;
        // Widths should be approximately equal (within 1 pixel due to rounding)
        assert!((i32::from(downgrade_width) - i32::from(remove_width)).abs() <= 1);
    }

    /// What: Verify installed-only mode records downgrade rect.
    ///
    /// Inputs:
    /// - Installed-only mode with downgrade list
    ///
    /// Output:
    /// - `app.downgrade_rect` is set to inner rectangle of downgrade list.
    ///
    /// Details:
    /// - Tests that downgrade rect excludes borders.
    #[test]
    fn installed_only_records_downgrade_rect() {
        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);

        term.draw(|f| {
            let area = f.area();
            render_installed_only(f, &mut app, area);
        })
        .expect("Failed to render installed-only pane to record downgrade rect");

        assert!(app.downgrade_rect.is_some());
        let (x, y, w, h) = app
            .downgrade_rect
            .expect("downgrade_rect should be Some after is_some() check");
        // Rect should exclude borders
        assert_eq!(x, 1);
        assert_eq!(y, 1);
        assert!(w > 0);
        assert!(h > 0);
    }

    /// What: Verify installed-only mode handles empty lists.
    ///
    /// Inputs:
    /// - Installed-only mode with empty downgrade and remove lists
    ///
    /// Output:
    /// - Both lists render without panic.
    ///
    /// Details:
    /// - Tests edge case where lists are empty.
    #[test]
    fn installed_only_handles_empty_lists() {
        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.downgrade_list.clear();
        app.remove_list.clear();

        term.draw(|f| {
            let area = f.area();
            render_installed_only(f, &mut app, area);
        })
        .expect("Failed to render installed-only pane with empty lists");

        // Should render empty lists without panic
        assert!(app.install_rect.is_some());
        assert!(app.downgrade_rect.is_some());
    }
}