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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::state::AppState;
use crate::state::types::AppMode;
use crate::theme::theme;
use ratatui::{
    Frame,
    prelude::Rect,
    style::Style,
    text::Line,
    widgets::{Block, BorderType, Borders, List, ListItem},
};

/// Dropdown menu rendering module.
mod dropdowns;
/// Search results list rendering module.
mod list;
/// News feed rendering module.
mod news;
/// Sort menu rendering module.
mod sort_menu;
/// Status bar rendering module.
mod status;
/// Title bar rendering module.
mod title;
pub use title::{
    clear_top_bar_menu_rects, render_top_bar_menu_cluster, top_bar_menu_cluster_width,
};
/// Utility functions for results rendering.
mod utils;

/// What: Context struct containing all extracted values needed for rendering.
///
/// Inputs: Values extracted from `AppState` to avoid borrow conflicts.
///
/// Output: Grouped context data.
///
/// Details: Reduces data flow complexity by grouping related values together.
pub struct RenderContext {
    /// Number of search results.
    pub results_len: usize,
    /// Optional repositories configuration.
    pub optional_repos: OptionalRepos,
    /// Menu states for dropdowns and menus.
    pub menu_states: MenuStates,
    /// Filter states for search results.
    pub filter_states: FilterStates,
}

/// What: Optional repository availability flags.
///
/// Inputs: Individual boolean flags for each optional repo.
///
/// Output: Struct containing all optional repo flags.
///
/// Details: Used to pass multiple optional repo flags as a single parameter.
#[allow(clippy::struct_excessive_bools)]
pub struct OptionalRepos {
    /// Whether `EndeavourOS` repository is available.
    pub has_eos: bool,
    /// Whether `CachyOS` repository is available.
    pub has_cachyos: bool,
    /// Whether `Artix` repository is available.
    pub has_artix: bool,
    /// Whether `Artix Omniverse` repository is available.
    pub has_artix_omniverse: bool,
    /// Whether `Artix Universe` repository is available.
    pub has_artix_universe: bool,
    /// Whether `Artix Lib32` repository is available.
    pub has_artix_lib32: bool,
    /// Whether `Artix Galaxy` repository is available.
    pub has_artix_galaxy: bool,
    /// Whether `Artix World` repository is available.
    pub has_artix_world: bool,
    /// Whether `Artix System` repository is available.
    pub has_artix_system: bool,
    /// Whether `BlackArch` repository is available.
    pub has_blackarch: bool,
    /// Whether `Manjaro` repository is available.
    pub has_manjaro: bool,
}

/// What: Menu open/closed states.
///
/// Inputs: Individual boolean flags for each menu.
///
/// Output: Struct containing all menu states.
///
/// Details: Used to pass multiple menu states as a single parameter.
#[allow(clippy::struct_excessive_bools)]
pub struct MenuStates {
    /// Whether the sort menu is open.
    pub sort_menu_open: bool,
}

/// What: Filter toggle states.
///
/// Inputs: Individual boolean flags for each filter.
///
/// Output: Struct containing all filter states.
///
/// Details: Used to pass multiple filter states as a single parameter.
#[allow(clippy::struct_excessive_bools)]
pub struct FilterStates {
    /// Whether to show AUR packages.
    pub show_aur: bool,
    /// Whether to show core repository packages.
    pub show_core: bool,
    /// Whether to show extra repository packages.
    pub show_extra: bool,
    /// Whether to show multilib repository packages.
    pub show_multilib: bool,
    /// Whether to show `EndeavourOS` repository packages.
    pub show_eos: bool,
    /// Whether to show `CachyOS` repository packages.
    pub show_cachyos: bool,
    /// Whether to show `Artix` repository packages.
    pub show_artix: bool,
    /// Whether to show `Artix Omniverse` repository packages.
    pub show_artix_omniverse: bool,
    /// Whether to show `Artix Universe` repository packages.
    pub show_artix_universe: bool,
    /// Whether to show `Artix Lib32` repository packages.
    pub show_artix_lib32: bool,
    /// Whether to show `Artix Galaxy` repository packages.
    pub show_artix_galaxy: bool,
    /// Whether to show `Artix World` repository packages.
    pub show_artix_world: bool,
    /// Whether to show `Artix System` repository packages.
    pub show_artix_system: bool,
    /// Whether to show `BlackArch` repository packages.
    pub show_blackarch: bool,
    /// Whether to show `Manjaro` repository packages.
    pub show_manjaro: bool,
}

/// What: Render the top results list and title controls.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Mutable application state (results, selection, rects)
/// - `area`: Target rectangle for the results block
///
/// Output:
/// - Draws the results list and updates hit-test rectangles for Sort/Filters/Buttons and status.
///
/// Details:
/// - Keeps selection centered when possible; shows repo/labels, versions, descriptions, and
///   install markers.
/// - Builds the title with Sort button and filter toggles (Config/Panels/Options render on the updates row).
/// - Renders dropdown overlays for Sort/Options/Config/Panels when open, and records rects.
/// - Reduces data flow complexity by extracting all data in one operation and batching mutations.
pub fn render_results(f: &mut Frame, app: &mut AppState, area: Rect) {
    if matches!(app.app_mode, AppMode::News) {
        render_news_results(f, app, area);
        return;
    }
    // Keep selection centered within the visible results list when possible
    utils::center_selection(app, area);

    // Extract all data needed for rendering in one operation to reduce data flow complexity
    let ctx = utils::extract_render_context(app);

    // Build title and record rects (mutates app)
    let title_spans = title::build_title_spans_from_context(app, &ctx, area);
    title::record_title_rects_from_context(app, &ctx, area);

    // Render list widget
    render_list_widget(f, app, area, &title_spans);

    // Render status and sort menu, record rects (all mutate app)
    status::render_status(f, app, area);
    let btn_x = app.sort_button_rect.map_or(area.x, |(x, _, _, _)| x);
    sort_menu::render_sort_menu(f, app, area, btn_x);
    utils::record_results_rect(app, area);
}

/// What: Render the list widget with title and items.
///
/// Inputs:
/// - `f`: Frame to render into
/// - `app`: Application state for list items and `list_state`
/// - `area`: Target rectangle for the results block
/// - `title_spans`: Pre-built title spans
///
/// Output:
/// - Renders the list widget with items and title.
///
/// Details:
/// - Builds list items only for visible viewport to improve performance.
/// - Mutates `app.list_state` during rendering.
fn render_list_widget(
    f: &mut Frame,
    app: &mut AppState,
    area: Rect,
    title_spans: &[ratatui::text::Span<'static>],
) {
    let th = theme();
    let list_offset = app.list_state.offset();
    let viewport_rows = area.height.saturating_sub(2) as usize;
    let start = list_offset;
    let end = std::cmp::min(app.results.len(), start + viewport_rows);
    // Settings are cached; avoid per-frame reloads by fetching once and cloning.
    let prefs = crate::theme::settings();

    let items: Vec<ListItem> = app
        .results
        .iter()
        .enumerate()
        .map(|(i, p)| {
            let in_viewport = i >= start && i < end;
            list::build_list_item(p, app, &th, &prefs, in_viewport)
        })
        .collect();

    let list = List::new(items)
        .style(Style::default().fg(th.text).bg(th.base))
        .block(
            Block::default()
                .title(Line::from(title_spans.to_vec()))
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(th.surface2)),
        )
        .highlight_style(Style::default().bg(th.surface1))
        .highlight_symbol("> ");

    f.render_stateful_widget(list, area, &mut app.list_state);
}

/// What: Render dropdown menus (Config/Lists, Panels, Options) on top layer.
///
/// This function should be called after all other UI elements are rendered
/// to ensure dropdowns appear on top.
pub use dropdowns::render_dropdowns;

/// What: Render news results in the results area.
///
/// Inputs:
/// - `f`: Frame to render into.
/// - `app`: Application state.
/// - `area`: Area to render within.
///
/// Output: Renders news feed items as a list.
///
/// Details: Renders news feed items from `app.news_results` as a list with source labels.
fn render_news_results(f: &mut Frame, app: &mut AppState, area: Rect) {
    let th = theme();
    // Record results rect first (before any other mutations)
    app.results_rect = Some((area.x, area.y, area.width, area.height));

    // Extract all immutable data we need first (clone to avoid borrowing)
    let news_loading = app.news_loading;
    let news_results = app.news_results.clone();
    let news_read_ids = app.news_read_ids.clone();
    let news_read_urls = app.news_read_urls.clone();
    let needs_select_none = news_loading && news_results.is_empty();

    // Now do all mutable operations first
    // Handle news_list_state mutation
    if needs_select_none {
        app.news_list_state.select(None);
    }

    // Build title spans and record rects (mutates app button/filter rects)
    let title_spans = news::build_news_title_spans_and_record_rects(app, area);

    // Build and render list inline to avoid storing items across mutable operations
    f.render_stateful_widget(
        List::new(
            news::build_news_list_items(
                app,
                news_loading,
                &news_results,
                &news_read_ids,
                &news_read_urls,
            )
            .0,
        )
        .style(Style::default().fg(th.text).bg(th.base))
        .block(
            Block::default()
                .title(Line::from(title_spans))
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .border_style(Style::default().fg(th.surface2)),
        )
        .highlight_style(Style::default().bg(th.surface1))
        .highlight_symbol("> "),
        area,
        &mut app.news_list_state,
    );
    let btn_x = app.sort_button_rect.map_or(area.x, |(x, _, _, _)| x);
    sort_menu::render_sort_menu(f, app, area, btn_x);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::types::NewsFeedSource;

    /// What: Ensure rendering matches the main UI split: updates row (menu cluster) plus results pane.
    ///
    /// Inputs:
    /// - Single search result plus an operational status message.
    ///
    /// Output:
    /// - Sort and Config/Panels/Options rects, plus status and results rects, become `Some`.
    ///
    /// Details:
    /// - Uses a `TestBackend` terminal, renders `updates::render_updates_button` on a 1-row strip then
    ///   `render_results` on the remainder, mirroring `crate::ui::ui`.
    #[test]
    fn results_sets_title_button_rects_and_status_rect() {
        use ratatui::{Terminal, backend::TestBackend};
        let backend = TestBackend::new(120, 20);
        let mut term = Terminal::new(backend).expect("failed to create test terminal");
        let mut app = crate::state::AppState::default();
        init_test_translations(&mut app);
        // Seed minimal results to render
        app.results = vec![crate::state::PackageItem {
            name: "pkg".into(),
            version: "1".into(),
            description: String::new(),
            source: crate::state::Source::Aur,
            popularity: Some(1.0),
            out_of_date: None,
            orphaned: false,
        }];
        app.arch_status_text = "All systems operational".into();
        app.arch_status_color = crate::state::ArchStatusColor::Operational;

        term.draw(|f| {
            let area = f.area();
            let chunks = ratatui::layout::Layout::default()
                .direction(ratatui::layout::Direction::Vertical)
                .constraints([
                    ratatui::layout::Constraint::Length(1),
                    ratatui::layout::Constraint::Min(0),
                ])
                .split(area);
            crate::ui::updates::render_updates_button(f, &mut app, chunks[0]);
            render_results(f, &mut app, chunks[1]);
        })
        .expect("failed to draw test terminal");

        assert!(app.sort_button_rect.is_some());
        assert!(app.options_button_rect.is_some());
        assert!(app.config_button_rect.is_some());
        assert!(app.panels_button_rect.is_some());
        assert!(app.arch_status_rect.is_some());
        assert!(app.results_rect.is_some());
    }

    /// What: Initialize minimal English translations for tests.
    ///
    /// Inputs:
    /// - `app`: `AppState` to populate with translations
    ///
    /// Output:
    /// - Populates `app.translations` and `app.translations_fallback` with minimal English translations
    ///
    /// Details:
    /// - Sets up only the translations needed for tests to pass
    fn init_test_translations(app: &mut crate::state::AppState) {
        use std::collections::HashMap;
        let mut translations = HashMap::new();
        translations.insert("app.results.title".to_string(), "Results".to_string());
        translations.insert("app.results.buttons.sort".to_string(), "Sort".to_string());
        translations.insert(
            "app.results.buttons.options".to_string(),
            "Options".to_string(),
        );
        translations.insert(
            "app.results.buttons.panels".to_string(),
            "Panels".to_string(),
        );
        translations.insert(
            "app.results.buttons.config_lists".to_string(),
            "Config/Lists".to_string(),
        );
        translations.insert("app.results.buttons.menu".to_string(), "Menu".to_string());
        translations.insert("app.results.filters.aur".to_string(), "AUR".to_string());
        translations.insert("app.results.filters.core".to_string(), "core".to_string());
        translations.insert("app.results.filters.extra".to_string(), "extra".to_string());
        translations.insert(
            "app.results.filters.multilib".to_string(),
            "multilib".to_string(),
        );
        translations.insert("app.results.filters.eos".to_string(), "EOS".to_string());
        translations.insert(
            "app.results.filters.cachyos".to_string(),
            "CachyOS".to_string(),
        );
        translations.insert("app.results.filters.artix".to_string(), "Artix".to_string());
        translations.insert(
            "app.results.filters.artix_omniverse".to_string(),
            "OMNI".to_string(),
        );
        translations.insert(
            "app.results.filters.artix_universe".to_string(),
            "UNI".to_string(),
        );
        translations.insert(
            "app.results.filters.artix_lib32".to_string(),
            "LIB32".to_string(),
        );
        translations.insert(
            "app.results.filters.artix_galaxy".to_string(),
            "GALAXY".to_string(),
        );
        translations.insert(
            "app.results.filters.artix_world".to_string(),
            "WORLD".to_string(),
        );
        translations.insert(
            "app.results.filters.artix_system".to_string(),
            "SYSTEM".to_string(),
        );
        translations.insert(
            "app.results.filters.blackarch".to_string(),
            "BlackArch".to_string(),
        );
        translations.insert(
            "app.results.filters.manjaro".to_string(),
            "Manjaro".to_string(),
        );
        translations.insert("app.news.filters.arch".to_string(), "Arch".to_string());
        translations.insert(
            "app.news.filters.advisories".to_string(),
            "Advisories".to_string(),
        );
        translations.insert(
            "app.news.filters.installed_only".to_string(),
            "Installed".to_string(),
        );
        app.translations = translations.clone();
        app.translations_fallback = translations;
    }

    #[test]
    fn news_filters_leave_gap_between_aur_comments_and_read_toggle() {
        use ratatui::{Terminal, backend::TestBackend};

        let backend = TestBackend::new(160, 10);
        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.app_mode = AppMode::News;
        app.news_results = vec![crate::state::types::NewsFeedItem {
            id: "1".into(),
            date: "2025-01-01".into(),
            title: "Example update".into(),
            summary: None,
            url: None,
            source: NewsFeedSource::AurComment,
            severity: None,
            packages: Vec::new(),
        }];

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

        let buffer = term.backend().buffer();
        let mut title_line = String::new();
        for x in 0..buffer.area.width {
            title_line.push_str(buffer[(x, 0)].symbol());
        }

        let trimmed = title_line.trim_end();
        assert!(
            trimmed.contains("[AUR Comments]  [All]"),
            "expected spacing between AUR comments and read filters, saw: {trimmed}"
        );
    }
}