dotstate 0.3.4

A modern, secure, and user-friendly dotfile manager built with Rust
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
use ratatui::widgets::{ListState, ScrollbarState};
use std::collections::HashMap;
use std::time::Instant;

/// Application screens
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Screen {
    MainMenu,
    DotfileSelection,
    StorageSetup,
    SyncWithRemote,
    ManageProfiles,
    ProfileSelection, // For selecting which profile to activate after setup
    ManagePackages,
    Settings,
}

/// GitHub auth state (also handles local repo setup)
#[derive(Debug, Clone)]
pub struct GitHubAuthState {
    // Setup mode selection
    pub setup_mode: SetupMode, // Current setup mode (Choosing, GitHub, Local)
    pub mode_selection_index: usize, // 0 = Create for me (GitHub), 1 = Use own repo (Local)

    // GitHub mode fields
    pub token_input: crate::utils::TextInput,
    pub repo_name_input: crate::utils::TextInput,
    pub repo_location_input: crate::utils::TextInput,
    pub is_private: bool,
    pub step: GitHubAuthStep,
    pub error_message: Option<String>,
    pub status_message: Option<String>,
    pub help_scroll: usize,
    pub input_focused: bool,            // Whether input is currently focused
    pub focused_field: GitHubAuthField, // Which field is currently focused
    pub is_editing_token: bool,         // Whether we're in "edit token" mode
    pub repo_already_configured: bool,  // Whether repo was already set up
    /// Intermediate data stored during GitHub setup process
    pub setup_data: Option<GitHubSetupData>,

    // Local mode fields
    pub local_repo_path_input: crate::utils::TextInput, // Path to user's local repository
    #[allow(dead_code)]
    pub local_step: LocalSetupStep, // Current step in local setup flow (reserved for future async flow)
}

/// Intermediate data stored during GitHub setup process
#[derive(Debug, Clone)]
pub struct GitHubSetupData {
    pub token: String,
    pub repo_name: String,
    pub username: Option<String>,
    pub repo_exists: Option<bool>,
    pub is_private: bool, // Repository visibility (true = private, false = public)
    pub delay_until: Option<std::time::Instant>, // For delays between steps
    pub is_new_repo: bool, // Whether we're creating a new repo (vs cloning existing)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitHubAuthField {
    Token,
    RepoName,
    RepoLocation,
    IsPrivate,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitHubAuthStep {
    Input,
    Processing,
    /// State machine for processing setup steps
    SetupStep(GitHubSetupStep),
}

/// State machine for GitHub setup process
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitHubSetupStep {
    Connecting,
    ValidatingToken,
    CheckingRepo,
    CloningRepo,
    CreatingRepo,
    InitializingRepo,
    DiscoveringProfiles,
    Complete,
}

/// Setup mode for repository configuration
/// Determines which setup flow the user is in
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SetupMode {
    /// Initial screen - user chooses between GitHub and Local modes
    #[default]
    Choosing,
    /// GitHub mode - dotstate creates/manages the repository via GitHub API
    GitHub,
    /// Local mode - user provides their own pre-configured repository
    Local,
}

/// State machine for local setup process
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LocalSetupStep {
    #[default]
    Input,
    #[allow(dead_code)]
    Validating,
    #[allow(dead_code)]
    Complete,
}

impl Default for GitHubAuthState {
    fn default() -> Self {
        let default_repo_path = dirs::home_dir()
            .unwrap_or_else(|| std::path::PathBuf::from("."))
            .join(".config")
            .join("dotstate")
            .join("storage");

        Self {
            // Setup mode selection
            setup_mode: SetupMode::default(),
            mode_selection_index: 0, // Default to "Create for me (GitHub)"

            // GitHub mode fields
            token_input: crate::utils::TextInput::new(),
            repo_name_input: crate::utils::TextInput::with_text(crate::config::default_repo_name()),
            repo_location_input: crate::utils::TextInput::with_text(
                default_repo_path.to_string_lossy().to_string(),
            ),
            is_private: true, // Private by default
            step: GitHubAuthStep::Input,
            error_message: None,
            status_message: None,
            help_scroll: 0,
            input_focused: true, // Input starts focused
            focused_field: GitHubAuthField::Token,
            is_editing_token: false,
            repo_already_configured: false,
            setup_data: None,

            // Local mode fields
            local_repo_path_input: crate::utils::TextInput::with_text(
                default_repo_path.to_string_lossy().to_string(),
            ),
            local_step: LocalSetupStep::Input,
        }
    }
}

/// Sync with remote state
#[derive(Debug, Clone)]
pub struct SyncWithRemoteState {
    pub changed_files: Vec<String>,
    pub is_syncing: bool,
    pub sync_progress: Option<String>, // Current progress message (e.g., "Committing...", "Pulling...", "Pushing...")
    pub sync_result: Option<String>,   // Final result message
    pub show_result_popup: bool,       // Whether to show result popup
    pub pulled_changes_count: Option<usize>, // Number of changes pulled from remote
    pub list_state: ListState,
    pub scrollbar_state: ScrollbarState,
    pub diff_content: Option<String>, // Content of the diff for preview
    pub preview_scroll: usize,        // Scroll state for preview
    pub result_scroll: u16,           // Scroll state for result popup
    pub git_status: Option<crate::services::git_service::GitStatus>, // Detailed git status
}

impl Default for SyncWithRemoteState {
    fn default() -> Self {
        Self {
            changed_files: Vec::new(),
            is_syncing: false,
            sync_progress: None,
            sync_result: None,
            show_result_popup: false,
            pulled_changes_count: None,
            list_state: ListState::default(),
            scrollbar_state: ScrollbarState::new(0),
            diff_content: None,
            preview_scroll: 0,
            result_scroll: 0,
            git_status: None,
        }
    }
}

/// State for profile selection screen (after GitHub setup)
#[derive(Debug, Default)]
pub struct ProfileSelectionState {
    pub profiles: Vec<String>, // List of profile names
    pub list_state: ListState,
    #[allow(dead_code)] // Reserved for future use
    pub selected_profile: Option<String>, // Selected profile to activate
    pub show_exit_warning: bool, // Show warning when user tries to exit without selecting
    pub show_create_popup: bool, // Show create new profile popup
    pub create_name_input: crate::utils::TextInput, // Input for new profile name
}

/// Package manager state
#[derive(Debug)]
pub struct PackageManagerState {
    pub list_state: ListState,
    pub packages: Vec<crate::utils::profile_manifest::Package>, // From active profile
    pub popup_type: PackagePopupType,
    // Checking state
    pub is_checking: bool,
    pub checking_index: Option<usize>,
    pub package_statuses: Vec<PackageStatus>, // Installed/NotInstalled/Error
    pub checking_delay_until: Option<std::time::Instant>, // Delay between checks for UI responsiveness
    // Installation state
    pub installation_step: InstallationStep,
    pub installation_output: Vec<String>, // Live output from installation
    pub installation_output_scroll: u16,  // Scroll position for installation output
    pub installation_delay_until: Option<std::time::Instant>, // Delay between installation steps
    // Add/Edit popup state
    pub add_name_input: crate::utils::TextInput,
    pub add_description_input: crate::utils::TextInput,
    pub add_manager: Option<crate::utils::profile_manifest::PackageManager>,
    pub add_manager_selected: usize, // Index in available managers list
    pub add_package_name_input: crate::utils::TextInput,
    pub add_binary_name_input: crate::utils::TextInput,
    pub add_install_command_input: crate::utils::TextInput, // For custom only
    pub add_existence_check_input: crate::utils::TextInput, // For custom only
    pub add_manager_check_input: crate::utils::TextInput,   // Optional fallback
    pub add_is_custom: bool,                                // Whether in custom mode
    pub add_focused_field: AddPackageField,
    pub add_editing_index: Option<usize>, // None for add, Some(index) for edit
    pub add_validation_error: Option<String>, // Validation error to display in popup
    pub newly_added_index: Option<usize>, // Track newly added package to prompt install after check
    pub available_managers: Vec<crate::utils::profile_manifest::PackageManager>, // OS-filtered list
    pub manager_list_state: ListState,    // For manager selection
    // Delete popup state
    pub delete_confirm_input: crate::utils::TextInput,
    pub delete_index: Option<usize>,
    pub cache: crate::utils::package_cache::PackageCache,
    pub active_profile: String,
    // Import popup state
    pub import_available_sources: Vec<crate::utils::DiscoverySource>,
    pub import_active_tab: usize,
    pub import_focus: ImportFocus,
    pub import_source_cache: HashMap<crate::utils::DiscoverySource, ImportSourceCache>,
    pub import_selected: std::collections::HashSet<usize>, // Selected indices (into current source's packages)
    pub import_filter: crate::utils::TextInput,
    pub import_list_state: ListState,
    pub import_loading: bool,
    pub import_spinner_tick: usize, // For spinner animation
    pub import_discovery_rx: Option<std::sync::mpsc::Receiver<crate::utils::DiscoveryStatus>>, // Async discovery
}

/// Package popup types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PackagePopupType {
    None,
    Add,
    Edit,
    Delete,
    InstallMissing, // Prompt to install missing packages
    Import,         // Import packages from system
}

/// Focus state for import popup
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ImportFocus {
    Tabs,
    #[default]
    Filter,
    List,
}

/// Cache for a single discovery source
#[derive(Debug)]
pub struct ImportSourceCache {
    pub packages: Vec<crate::utils::DiscoveredPackage>,
    pub discovered_at: Instant,
    pub selected: std::collections::HashSet<usize>,
}

/// Package status
#[derive(Debug, Clone)]
pub enum PackageStatus {
    Unknown,
    Installed,
    NotInstalled,
    Error(String), // Error message if check failed
}

/// Which field is focused in the add/edit package popup
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AddPackageField {
    Name,
    Description,
    Manager,
    PackageName, // For managed packages
    BinaryName,
    InstallCommand, // Custom only
    ExistenceCheck, // Custom only
    ManagerCheck,   // Optional fallback
}

impl Default for PackageManagerState {
    fn default() -> Self {
        Self {
            list_state: ListState::default(),
            packages: Vec::new(),
            popup_type: PackagePopupType::None,
            is_checking: false,
            checking_index: None,
            package_statuses: Vec::new(),
            add_name_input: crate::utils::TextInput::new(),
            add_description_input: crate::utils::TextInput::new(),
            add_manager: None,
            add_manager_selected: 0,
            add_package_name_input: crate::utils::TextInput::new(),
            add_binary_name_input: crate::utils::TextInput::new(),
            add_install_command_input: crate::utils::TextInput::new(),
            add_existence_check_input: crate::utils::TextInput::new(),
            add_manager_check_input: crate::utils::TextInput::new(),
            add_is_custom: false,
            add_focused_field: AddPackageField::Name,
            add_editing_index: None,
            add_validation_error: None,
            newly_added_index: None,
            available_managers: Vec::new(),
            manager_list_state: ListState::default(),
            delete_confirm_input: crate::utils::TextInput::new(),
            delete_index: None,
            checking_delay_until: None,
            installation_step: InstallationStep::NotStarted,
            installation_output: Vec::new(),
            installation_output_scroll: 0,
            installation_delay_until: None,
            cache: crate::utils::package_cache::PackageCache::default(),
            active_profile: String::new(),
            import_available_sources: Vec::new(),
            import_active_tab: 0,
            import_focus: ImportFocus::default(),
            import_source_cache: HashMap::new(),
            import_selected: std::collections::HashSet::new(),
            import_filter: crate::utils::TextInput::new(),
            import_list_state: ListState::default(),
            import_loading: false,
            import_spinner_tick: 0,
            import_discovery_rx: None,
        }
    }
}

impl PackageManagerState {
    /// Get the currently active source (selected tab)
    #[must_use]
    pub fn import_active_source(&self) -> Option<crate::utils::DiscoverySource> {
        self.import_available_sources
            .get(self.import_active_tab)
            .copied()
    }

    /// Get cached packages for current source
    #[must_use]
    pub fn import_current_packages(&self) -> &[crate::utils::DiscoveredPackage] {
        self.import_active_source()
            .and_then(|s| self.import_source_cache.get(&s))
            .map_or(&[], |c| c.packages.as_slice())
    }

    /// Check if current source cache is valid (exists and not expired)
    #[must_use]
    pub fn import_cache_valid(&self, max_age_secs: u64) -> bool {
        self.import_active_source()
            .and_then(|s| self.import_source_cache.get(&s))
            .is_some_and(|c| {
                c.discovered_at.elapsed().as_secs() < max_age_secs && !c.packages.is_empty()
            })
    }
}

/// Installation state machine
#[derive(Debug)]
pub enum InstallationStep {
    NotStarted,
    Installing {
        package_index: usize,
        package_name: String,
        total_packages: usize,
        packages_to_install: Vec<usize>, // Indices of packages that need installation
        installed: Vec<usize>,           // Successfully installed package indices
        failed: Vec<(usize, String)>,    // Failed package indices with error messages
        status_rx: Option<std::sync::mpsc::Receiver<InstallationStatus>>, // Channel receiver for status updates
    },
    Complete {
        installed: Vec<usize>,
        failed: Vec<(usize, String)>, // (index, error message)
    },
}

/// Installation status message from background thread
#[derive(Debug, Clone)]
pub enum InstallationStatus {
    Output(String), // Output line
    Complete {
        success: bool,
        error: Option<String>,
    }, // Installation complete
}

/// Application UI state
#[derive(Debug)]
pub struct UiState {
    pub current_screen: Screen,
    pub selected_index: usize,
    pub has_changes_to_push: bool, // Whether there are uncommitted or unpushed changes
    pub git_status: Option<crate::services::git_service::GitStatus>, // Detailed git status
    /// State for profile selection after GitHub setup
    pub profile_selection: ProfileSelectionState,
    /// Whether a text input is currently focused (blocks navigation keybindings)
    /// When true, keymap navigation is disabled so users can type freely
    pub input_mode_active: bool,
    /// Whether the help overlay is currently showing
    pub show_help_overlay: bool,
}

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

impl UiState {
    #[must_use]
    pub fn new() -> Self {
        Self {
            current_screen: Screen::MainMenu,
            selected_index: 0,
            has_changes_to_push: false,
            git_status: None,
            profile_selection: ProfileSelectionState::default(),
            input_mode_active: false,
            show_help_overlay: false,
        }
    }
}

// Legacy render functions removed - replaced by components:
// - render_welcome() -> WelcomeComponent
// - render_main_menu() -> MainMenuComponent
// - render_message() -> MessageComponent
// - render_synced_files() -> SyncedFilesComponent
// - render_dotfile_selection() -> DotfileSelectionScreen (self-contained)
// popup_area removed - use crate::utils::center_popup instead