synh8 0.1.1

A synaptic-inspired TUI for managing APT packages on Debian/Ubuntu. Linux only.
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
//! Common types used throughout the application

use std::collections::HashSet;

use ratatui::prelude::*;

// ============================================================================
// Core API Types (Typestate Pattern)
// ============================================================================

/// Opaque handle to a package. Valid only for the current cache generation.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct PackageId(pub(crate) u32);

impl PackageId {
    /// Get the raw index (for internal use)
    pub fn index(self) -> usize {
        self.0 as usize
    }
}

/// What the user explicitly wants for a package
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum UserIntent {
    /// No user action - follow default behavior
    #[default]
    Default,
    /// User explicitly wants this installed/upgraded
    Install,
    /// User explicitly wants this removed
    Remove,
    /// User explicitly wants to keep current version (prevent auto-changes)
    Hold,
}

/// Why a package is changing
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ChangeReason {
    /// User explicitly requested this
    UserRequested,
    /// Required as a dependency of a user request
    Dependency,
    /// Will be auto-removed (orphan dependency)
    AutoRemove,
}

/// Type of change to a package
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ChangeAction {
    Install,
    Upgrade,
    Remove,
    Downgrade,
}

/// A computed change from the plan
/// A planned change to a package. Name is derived from PackageId, not stored.
#[derive(Clone, Debug)]
pub struct PlannedChange {
    pub package: PackageId,
    pub action: ChangeAction,
    pub reason: ChangeReason,
    pub download_size: u64,
    pub size_change: i64,
}

// ============================================================================
// Typestate Markers
// ============================================================================

/// Clean state - no pending changes
pub struct Clean;

/// Dirty state - has user marks but no computed plan
pub struct Dirty;

/// Planned state - dependencies resolved, changeset computed
pub struct Planned {
    pub changes: Vec<PlannedChange>,
    pub download_size: u64,
    pub install_size_change: i64,
    pub errors: Vec<String>,
}

/// Marker trait for states where the cache is readable
pub trait ReadableState {}
impl ReadableState for Clean {}
impl ReadableState for Dirty {}
impl ReadableState for Planned {}

// ============================================================================
// Legacy Types (for UI compatibility during migration)
// ============================================================================

/// Package status - no distinction between user-marked and dependency
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackageStatus {
    // Base states (not marked)
    Installed,        // · Package is installed, no changes pending
    NotInstalled,     //   Package is not installed, no changes pending
    Upgradable,       // ↑ Package can be upgraded (yellow)
    // Marked states (all marked packages look identical)
    MarkedForInstall, // + Package will be installed
    MarkedForUpgrade, // ↑ Package will be upgraded (green)
    MarkedForRemove,  // - Package will be removed
    // Other
    Keep,             // = Package kept at current version
    Broken,           // ✗ Package is broken
}

impl PackageStatus {
    pub fn symbol(&self) -> &'static str {
        match self {
            Self::Upgradable | Self::MarkedForUpgrade => "",
            Self::MarkedForInstall => "+",
            Self::MarkedForRemove => "-",
            Self::Keep => "=",
            Self::Installed => "·",
            Self::NotInstalled => " ",
            Self::Broken => "",
        }
    }

    pub fn color(&self) -> Color {
        match self {
            Self::Upgradable => Color::Yellow,
            Self::MarkedForUpgrade => Color::Green,
            Self::MarkedForInstall => Color::Green,
            Self::MarkedForRemove => Color::Red,
            Self::Keep => Color::Blue,
            Self::Installed => Color::DarkGray,
            Self::NotInstalled => Color::Gray,
            Self::Broken => Color::LightRed,
        }
    }

    /// Check if this status represents a marked (pending change) state
    pub fn is_marked(&self) -> bool {
        matches!(self,
            Self::MarkedForInstall |
            Self::MarkedForUpgrade |
            Self::MarkedForRemove
        )
    }
}

/// Filter categories (left panel)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FilterCategory {
    Upgradable,
    MarkedChanges,
    Installed,
    NotInstalled,
    All,
}

impl FilterCategory {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Upgradable => "Upgradable",
            Self::MarkedChanges => "Marked Changes",
            Self::Installed => "Installed",
            Self::NotInstalled => "Not Installed",
            Self::All => "All Packages",
        }
    }

    pub fn all() -> &'static [FilterCategory] {
        &[
            Self::Upgradable,
            Self::MarkedChanges,
            Self::Installed,
            Self::NotInstalled,
            Self::All,
        ]
    }
}

/// Displayed package info (extracted from rust-apt Package).
/// The package is identified by `id` (PackageId). Name is derived, not stored separately.
#[derive(Debug, Clone)]
pub struct PackageInfo {
    pub id: PackageId,        // Stable handle for this package - the ONLY identifier
    pub name: String,         // Full name including arch (e.g., "libfoo:i386") - for display/sort
    pub status: PackageStatus,
    pub section: String,
    pub installed_version: String,
    pub candidate_version: String,
    pub installed_size: u64,
    pub download_size: u64,
    pub description: String,
    pub architecture: String,
}

impl PackageInfo {
    pub fn size_str(bytes: u64) -> String {
        if bytes == 0 {
            return String::from("-");
        }
        const KB: u64 = 1024;
        const MB: u64 = KB * 1024;
        const GB: u64 = MB * 1024;

        if bytes >= GB {
            format!("{:.1} GB", bytes as f64 / GB as f64)
        } else if bytes >= MB {
            format!("{:.1} MB", bytes as f64 / MB as f64)
        } else if bytes >= KB {
            format!("{:.1} KB", bytes as f64 / KB as f64)
        } else {
            format!("{bytes} B")
        }
    }

    pub fn installed_size_str(&self) -> String {
        Self::size_str(self.installed_size)
    }

    pub fn download_size_str(&self) -> String {
        Self::size_str(self.download_size)
    }
}

/// Which pane has focus
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusedPane {
    Filters,
    Packages,
    Details,
}

/// Which tab is shown in details pane
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DetailsTab {
    Info,
    Dependencies,
    ReverseDeps,
}

/// Application state machine
#[derive(Debug, PartialEq, Eq)]
pub enum AppState {
    Listing,
    Searching,          // User is typing a search query
    ShowingMarkConfirm, // Popup showing additional changes when marking a package
    ShowingChanges,     // Final confirmation before applying all changes
    ShowingChangelog,   // Viewing package changelog
    ShowingSettings,    // Settings/preferences view
    ConfirmExit,        // Confirm exit with pending changes
    Upgrading,
    Done,
}

/// Sort options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortBy {
    Name,
    Section,
    InstalledVersion,
    CandidateVersion,
}

impl SortBy {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Name => "Name",
            Self::Section => "Section",
            Self::InstalledVersion => "Installed version",
            Self::CandidateVersion => "Candidate version",
        }
    }

    pub fn all() -> &'static [SortBy] {
        &[Self::Name, Self::Section, Self::InstalledVersion, Self::CandidateVersion]
    }
}

/// User settings (not persisted yet)
#[derive(Debug, Clone)]
pub struct Settings {
    pub visible_columns: HashSet<Column>,
    pub sort_by: SortBy,
    pub sort_ascending: bool,
}

impl Default for Settings {
    fn default() -> Self {
        let mut visible_columns = HashSet::new();
        visible_columns.insert(Column::Status);
        visible_columns.insert(Column::Name);
        visible_columns.insert(Column::CandidateVersion);
        Self {
            visible_columns,
            sort_by: SortBy::CandidateVersion,
            sort_ascending: true,
        }
    }
}

/// Result of toggling a package
#[derive(Debug)]
pub enum ToggleResult {
    /// Package was marked, with optional additional deps
    Marked {
        package: PackageId,
        additional: Vec<PackageId>,
    },
    /// Package was unmarked, with cascade
    Unmarked {
        package: PackageId,
        also_unmarked: Vec<PackageId>,
    },
    /// Toggle had no effect (e.g., dependency with untraceable origin)
    NoChange {
        package: PackageId,
    },
}

/// Preview of changes when marking or unmarking a package (or bulk selection).
/// Displayed in a confirmation modal before the action is finalized.
#[derive(Debug, Clone)]
pub enum MarkPreview {
    /// Marking package(s) for install/upgrade
    Mark {
        package_name: String,
        is_upgrade: bool,
        additional_installs: Vec<String>,
        additional_upgrades: Vec<String>,
        additional_removes: Vec<String>,
        download_size: u64,
        /// PackageIds explicitly acted on in a bulk visual-mode operation.
        /// Empty for single-package toggles. Used by cancel_mark() for reversal.
        bulk_acted_ids: Vec<PackageId>,
    },
    /// Unmarking package(s) — reverting a previous mark
    Unmark {
        package_name: String,
        /// Was the original package user-marked (vs a dependency)?
        was_user_marked: bool,
        /// Packages that were also unmarked as a cascade effect
        also_unmarked: Vec<String>,
        /// PackageIds explicitly acted on in a bulk visual-mode operation.
        /// Empty for single-package toggles. Used by cancel_mark() for reversal.
        bulk_acted_ids: Vec<PackageId>,
    },
}

/// Column configuration for the package table
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Column {
    Status,
    Name,
    Section,
    InstalledVersion,
    CandidateVersion,
    DownloadSize,
}

impl Column {
    /// All columns in display order.
    pub fn all() -> &'static [Column] {
        &[
            Self::Status,
            Self::Name,
            Self::Section,
            Self::InstalledVersion,
            Self::CandidateVersion,
            Self::DownloadSize,
        ]
    }

    /// Human-readable label for the settings UI.
    pub fn label(&self) -> &'static str {
        match self {
            Self::Status => "Status column (S)",
            Self::Name => "Name column",
            Self::Section => "Section column",
            Self::InstalledVersion => "Installed version column",
            Self::CandidateVersion => "Candidate version column",
            Self::DownloadSize => "Download size column",
        }
    }

    pub fn header(&self) -> &'static str {
        match self {
            Self::Status => "S",
            Self::Name => "Package",
            Self::Section => "Section",
            Self::InstalledVersion => "Installed",
            Self::CandidateVersion => "Candidate",
            Self::DownloadSize => "Download",
        }
    }

    pub fn width(&self, col_widths: &ColumnWidths) -> Constraint {
        match self {
            Self::Status => Constraint::Length(3),
            Self::Name => Constraint::Min(col_widths.name),
            Self::Section => Constraint::Length(col_widths.section),
            Self::InstalledVersion => Constraint::Length(col_widths.installed),
            Self::CandidateVersion => Constraint::Length(col_widths.candidate),
            Self::DownloadSize => Constraint::Length(10),
        }
    }

    pub fn visible_columns(settings: &Settings) -> Vec<Column> {
        Self::all()
            .iter()
            .copied()
            .filter(|col| settings.visible_columns.contains(col))
            .collect()
    }
}

/// Column width storage
#[derive(Debug, Clone)]
pub struct ColumnWidths {
    pub name: u16,
    pub section: u16,
    pub installed: u16,
    pub candidate: u16,
}

impl ColumnWidths {
    pub fn new() -> Self {
        Self {
            name: 10,
            section: 7,
            installed: 9,
            candidate: 9,
        }
    }

}