fsnav 0.4.0

A fast terminal file navigator with advanced features: search, preview, bookmarks, and split-pane view
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bookmark {
    pub name: String,
    pub path: PathBuf,
    pub shortcut: Option<char>,
    pub created_at: std::time::SystemTime,
    pub last_accessed: Option<std::time::SystemTime>,
    pub access_count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookmarksManager {
    bookmarks: Vec<Bookmark>,
    shortcuts: HashMap<char, usize>, // Maps shortcut to bookmark index
    config_path: PathBuf,
}

impl BookmarksManager {
    pub fn new() -> Result<Self> {
        let config_dir = Self::get_config_dir()?;
        let config_path = config_dir.join("bookmarks.json");

        let mut manager = Self {
            bookmarks: Vec::new(),
            shortcuts: HashMap::new(),
            config_path,
        };

        // Load existing bookmarks if file exists
        if manager.config_path.exists() {
            manager.load()?;
        } else {
            // Create default bookmarks
            manager.create_default_bookmarks();
            manager.save()?;
        }

        Ok(manager)
    }

    fn get_config_dir() -> Result<PathBuf> {
        let home = dirs::home_dir().context("Failed to get home directory")?;
        let config_dir = home.join(".config").join("fsnav");

        // Create directory if it doesn't exist
        if !config_dir.exists() {
            fs::create_dir_all(&config_dir)?;
        }

        Ok(config_dir)
    }

    fn create_default_bookmarks(&mut self) {
        // Add common directories as default bookmarks
        if let Some(home) = dirs::home_dir() {
            self.add_bookmark_internal("Home".to_string(), home.clone(), Some('h'));

            let downloads = home.join("Downloads");
            if downloads.exists() {
                self.add_bookmark_internal("Downloads".to_string(), downloads, Some('d'));
            }

            let documents = home.join("Documents");
            if documents.exists() {
                self.add_bookmark_internal("Documents".to_string(), documents, Some('o'));
            }

            let desktop = home.join("Desktop");
            if desktop.exists() {
                self.add_bookmark_internal("Desktop".to_string(), desktop, Some('k'));
            }
        }

        // Add root directory
        self.add_bookmark_internal("Root".to_string(), PathBuf::from("/"), Some('r'));

        // Add common system directories
        if Path::new("/usr/local").exists() {
            self.add_bookmark_internal("Local".to_string(), PathBuf::from("/usr/local"), Some('l'));
        }

        if Path::new("/etc").exists() {
            self.add_bookmark_internal("Config".to_string(), PathBuf::from("/etc"), Some('e'));
        }

        if Path::new("/tmp").exists() {
            self.add_bookmark_internal("Temp".to_string(), PathBuf::from("/tmp"), Some('t'));
        }
    }

    fn add_bookmark_internal(&mut self, name: String, path: PathBuf, shortcut: Option<char>) {
        let bookmark = Bookmark {
            name,
            path,
            shortcut,
            created_at: std::time::SystemTime::now(),
            last_accessed: None,
            access_count: 0,
        };

        let index = self.bookmarks.len();
        self.bookmarks.push(bookmark);

        if let Some(key) = shortcut {
            self.shortcuts.insert(key, index);
        }
    }

    pub fn add_bookmark(
        &mut self,
        name: String,
        path: PathBuf,
        shortcut: Option<char>,
    ) -> Result<()> {
        // Check if path exists
        if !path.exists() {
            return Err(anyhow::anyhow!("Path does not exist: {}", path.display()));
        }

        // Check if bookmark already exists
        if self.bookmarks.iter().any(|b| b.path == path) {
            return Err(anyhow::anyhow!("Bookmark already exists for this path"));
        }

        // Check if shortcut is already taken
        if let Some(key) = shortcut {
            if self.shortcuts.contains_key(&key) {
                return Err(anyhow::anyhow!("Shortcut '{}' is already in use", key));
            }
        }

        self.add_bookmark_internal(name, path, shortcut);
        self.save()?;
        Ok(())
    }

    pub fn remove_bookmark(&mut self, index: usize) -> Result<()> {
        if index >= self.bookmarks.len() {
            return Err(anyhow::anyhow!("Invalid bookmark index"));
        }

        let bookmark = self.bookmarks.remove(index);

        // Remove shortcut if it exists
        if let Some(key) = bookmark.shortcut {
            self.shortcuts.remove(&key);
        }

        // Update shortcut indices for remaining bookmarks
        self.shortcuts = self
            .shortcuts
            .iter()
            .map(|(&k, &v)| if v > index { (k, v - 1) } else { (k, v) })
            .collect();

        self.save()?;
        Ok(())
    }

    pub fn rename_bookmark(&mut self, index: usize, new_name: String) -> Result<()> {
        if index >= self.bookmarks.len() {
            return Err(anyhow::anyhow!("Invalid bookmark index"));
        }

        self.bookmarks[index].name = new_name;
        self.save()?;
        Ok(())
    }

    #[allow(dead_code)]
    pub fn update_shortcut(&mut self, index: usize, new_shortcut: Option<char>) -> Result<()> {
        if index >= self.bookmarks.len() {
            return Err(anyhow::anyhow!("Invalid bookmark index"));
        }

        // Remove old shortcut
        if let Some(old_key) = self.bookmarks[index].shortcut {
            self.shortcuts.remove(&old_key);
        }

        // Check if new shortcut is already taken
        if let Some(key) = new_shortcut {
            if self.shortcuts.contains_key(&key) {
                return Err(anyhow::anyhow!("Shortcut '{}' is already in use", key));
            }
            self.shortcuts.insert(key, index);
        }

        self.bookmarks[index].shortcut = new_shortcut;
        self.save()?;
        Ok(())
    }

    pub fn get_bookmark_by_shortcut(&mut self, shortcut: char) -> Option<&Bookmark> {
        if let Some(&index) = self.shortcuts.get(&shortcut) {
            if let Some(bookmark) = self.bookmarks.get_mut(index) {
                bookmark.last_accessed = Some(std::time::SystemTime::now());
                bookmark.access_count += 1;
                let _ = self.save(); // Ignore save errors for access updates
                return self.bookmarks.get(index);
            }
        }
        None
    }

    pub fn get_bookmark_by_index(&mut self, index: usize) -> Option<&Bookmark> {
        if let Some(bookmark) = self.bookmarks.get_mut(index) {
            bookmark.last_accessed = Some(std::time::SystemTime::now());
            bookmark.access_count += 1;
            let _ = self.save(); // Ignore save errors for access updates
            return self.bookmarks.get(index);
        }
        None
    }

    pub fn list_bookmarks(&self) -> &[Bookmark] {
        &self.bookmarks
    }

    #[allow(dead_code)]
    pub fn find_bookmark_by_path(&self, path: &Path) -> Option<usize> {
        self.bookmarks.iter().position(|b| b.path == path)
    }

    #[allow(dead_code)]
    pub fn sort_by_frequency(&mut self) {
        self.bookmarks
            .sort_by(|a, b| b.access_count.cmp(&a.access_count));

        // Rebuild shortcuts map
        self.shortcuts.clear();
        for (index, bookmark) in self.bookmarks.iter().enumerate() {
            if let Some(key) = bookmark.shortcut {
                self.shortcuts.insert(key, index);
            }
        }

        let _ = self.save();
    }

    #[allow(dead_code)]
    pub fn sort_by_name(&mut self) {
        self.bookmarks.sort_by(|a, b| a.name.cmp(&b.name));

        // Rebuild shortcuts map
        self.shortcuts.clear();
        for (index, bookmark) in self.bookmarks.iter().enumerate() {
            if let Some(key) = bookmark.shortcut {
                self.shortcuts.insert(key, index);
            }
        }

        let _ = self.save();
    }

    pub fn get_available_shortcuts(&self) -> Vec<char> {
        let mut available = Vec::new();
        for c in 'a'..='z' {
            if !self.shortcuts.contains_key(&c) {
                available.push(c);
            }
        }
        for c in '0'..='9' {
            if !self.shortcuts.contains_key(&c) {
                available.push(c);
            }
        }
        available
    }

    fn load(&mut self) -> Result<()> {
        let content = fs::read_to_string(&self.config_path)?;
        let data: SavedBookmarks = serde_json::from_str(&content)?;

        self.bookmarks = data.bookmarks;

        // Rebuild shortcuts map
        self.shortcuts.clear();
        for (index, bookmark) in self.bookmarks.iter().enumerate() {
            if let Some(key) = bookmark.shortcut {
                self.shortcuts.insert(key, index);
            }
        }

        Ok(())
    }

    fn save(&self) -> Result<()> {
        let data = SavedBookmarks {
            version: 1,
            bookmarks: self.bookmarks.clone(),
        };

        let json = serde_json::to_string_pretty(&data)?;
        fs::write(&self.config_path, json)?;
        Ok(())
    }

    #[allow(dead_code)]
    pub fn export_to_file(&self, path: &Path) -> Result<()> {
        let data = SavedBookmarks {
            version: 1,
            bookmarks: self.bookmarks.clone(),
        };

        let json = serde_json::to_string_pretty(&data)?;
        fs::write(path, json)?;
        Ok(())
    }

    #[allow(dead_code)]
    pub fn import_from_file(&mut self, path: &Path) -> Result<()> {
        let content = fs::read_to_string(path)?;
        let data: SavedBookmarks = serde_json::from_str(&content)?;

        // Merge with existing bookmarks
        for bookmark in data.bookmarks {
            // Skip if path already bookmarked
            if !self.bookmarks.iter().any(|b| b.path == bookmark.path) {
                let index = self.bookmarks.len();

                // Find new shortcut if current one is taken
                let shortcut = if let Some(key) = bookmark.shortcut {
                    if self.shortcuts.contains_key(&key) {
                        None // Will need to assign manually
                    } else {
                        Some(key)
                    }
                } else {
                    None
                };

                self.bookmarks.push(Bookmark {
                    shortcut,
                    ..bookmark
                });

                if let Some(key) = shortcut {
                    self.shortcuts.insert(key, index);
                }
            }
        }

        self.save()?;
        Ok(())
    }
}

#[derive(Serialize, Deserialize)]
struct SavedBookmarks {
    version: u32,
    bookmarks: Vec<Bookmark>,
}

// Directory for home_dir fallback
mod dirs {
    use std::path::PathBuf;

    pub fn home_dir() -> Option<PathBuf> {
        std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .ok()
            .map(PathBuf::from)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_bookmark_operations() {
        let temp_dir = TempDir::new().unwrap();
        std::env::set_var("HOME", temp_dir.path());

        let mut manager = BookmarksManager::new().unwrap();

        // Test adding bookmark
        let test_path = temp_dir.path().join("test");
        fs::create_dir(&test_path).unwrap();

        manager
            .add_bookmark("Test".to_string(), test_path.clone(), Some('x'))
            .unwrap();

        // Test finding by shortcut
        assert!(manager.get_bookmark_by_shortcut('x').is_some());

        // Test finding by path
        let index = manager.find_bookmark_by_path(&test_path);
        assert!(index.is_some());

        // Test removing bookmark
        manager.remove_bookmark(index.unwrap()).unwrap();
        assert!(manager.get_bookmark_by_shortcut('x').is_none());
    }

    #[test]
    fn test_shortcut_conflicts() {
        let temp_dir = TempDir::new().unwrap();
        std::env::set_var("HOME", temp_dir.path());

        let mut manager = BookmarksManager::new().unwrap();

        let path1 = temp_dir.path().join("test1");
        let path2 = temp_dir.path().join("test2");
        fs::create_dir(&path1).unwrap();
        fs::create_dir(&path2).unwrap();

        manager
            .add_bookmark("Test1".to_string(), path1, Some('x'))
            .unwrap();

        // Should fail due to shortcut conflict
        let result = manager.add_bookmark("Test2".to_string(), path2, Some('x'));
        assert!(result.is_err());
    }
}