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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use anyhow::Result;
use crossterm::{
    cursor::MoveTo,
    execute,
    style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
    terminal,
};
use std::{
    collections::HashSet,
    io::{self, Write},
    path::{Path, PathBuf},
};

use crate::models::FileEntry;
use crate::utils::get_owner_group;

#[derive(Debug, Clone, PartialEq)]
pub enum PaneFocus {
    Left,
    Right,
}

#[derive(Debug, Clone)]
pub struct Pane {
    pub current_dir: PathBuf,
    pub entries: Vec<FileEntry>,
    pub selected_index: usize,
    pub selected_items: HashSet<usize>,
    pub scroll_offset: usize,
}

impl Pane {
    pub fn new(path: PathBuf) -> Result<Self> {
        let mut pane = Self {
            current_dir: path.clone(),
            entries: Vec::new(),
            selected_index: 0,
            selected_items: HashSet::new(),
            scroll_offset: 0,
        };
        pane.load_directory(&path)?;
        Ok(pane)
    }

    pub fn load_directory(&mut self, path: &Path) -> Result<()> {
        self.entries.clear();
        self.selected_index = 0;
        self.selected_items.clear();
        self.scroll_offset = 0;

        // Add parent directory entry if not at root
        if let Some(parent) = path.parent() {
            if parent != path {
                self.entries.push(FileEntry {
                    name: "..".to_string(),
                    path: parent.to_path_buf(),
                    is_dir: true,
                    is_accessible: true,
                    is_symlink: false,
                    permissions: None,
                    owner: None,
                    group: None,
                    uid: None,
                    gid: None,
                });
            }
        }

        // Read directory entries
        match std::fs::read_dir(path) {
            Ok(read_dir) => {
                let mut dir_entries = Vec::new();
                let mut file_entries = Vec::new();

                for entry in read_dir.flatten() {
                    let path = entry.path();
                    let metadata = entry.metadata();
                    let symlink_metadata = entry.path().symlink_metadata();

                    let is_symlink = symlink_metadata
                        .as_ref()
                        .map(|m| m.file_type().is_symlink())
                        .unwrap_or(false);

                    let is_dir = metadata.as_ref().map(|m| m.is_dir()).unwrap_or(false);
                    let is_accessible = metadata.is_ok();

                    let permissions = metadata.as_ref().ok().map(|m| {
                        use std::os::unix::fs::PermissionsExt;
                        m.permissions().mode()
                    });

                    let (owner, group, uid, gid) = get_owner_group(&path);

                    let name = entry.file_name().to_string_lossy().to_string();

                    // Skip hidden files on Unix-like systems
                    #[cfg(unix)]
                    if name.starts_with('.') && name != ".." {
                        continue;
                    }

                    let file_entry = FileEntry {
                        name,
                        path,
                        is_dir,
                        is_accessible,
                        is_symlink,
                        permissions,
                        owner,
                        group,
                        uid,
                        gid,
                    };

                    if is_dir {
                        dir_entries.push(file_entry);
                    } else {
                        file_entries.push(file_entry);
                    }
                }

                // Sort directories and files separately
                dir_entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
                file_entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

                // Add sorted entries (directories first)
                self.entries.extend(dir_entries);
                self.entries.extend(file_entries);
            }
            Err(e) => {
                self.entries.push(FileEntry {
                    name: format!("⚠️  Error: {}", e),
                    path: path.to_path_buf(),
                    is_dir: false,
                    is_accessible: false,
                    is_symlink: false,
                    permissions: None,
                    owner: None,
                    group: None,
                    uid: None,
                    gid: None,
                });
            }
        }

        self.current_dir = path.to_path_buf();
        Ok(())
    }

    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    pub fn move_down(&mut self) {
        if self.selected_index < self.entries.len().saturating_sub(1) {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    pub fn navigate_to_selected(&mut self) -> Result<()> {
        if let Some(entry) = self.entries.get(self.selected_index) {
            if entry.is_dir && entry.is_accessible {
                let new_path = entry.path.clone();
                self.load_directory(&new_path)?;
            }
        }
        Ok(())
    }

    pub fn navigate_up(&mut self) -> Result<()> {
        if let Some(parent) = self.current_dir.parent() {
            let parent_path = parent.to_path_buf();
            self.load_directory(&parent_path)?;
        }
        Ok(())
    }

    pub fn toggle_selection(&mut self) {
        if let Some(entry) = self.entries.get(self.selected_index) {
            if entry.name != ".." {
                if self.selected_items.contains(&self.selected_index) {
                    self.selected_items.remove(&self.selected_index);
                } else {
                    self.selected_items.insert(self.selected_index);
                }
            }
        }
    }

    #[allow(dead_code)]
    pub fn get_selected_paths(&self) -> Vec<PathBuf> {
        if self.selected_items.is_empty() {
            if let Some(entry) = self.entries.get(self.selected_index) {
                if entry.name != ".." {
                    vec![entry.path.clone()]
                } else {
                    vec![]
                }
            } else {
                vec![]
            }
        } else {
            self.selected_items
                .iter()
                .filter_map(|&i| self.entries.get(i))
                .filter(|e| e.name != "..")
                .map(|e| e.path.clone())
                .collect()
        }
    }

    fn adjust_scroll(&mut self) {
        // This will be calculated based on available height
    }

    pub fn adjust_scroll_with_height(&mut self, visible_height: usize) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + visible_height {
            self.scroll_offset = self.selected_index.saturating_sub(visible_height - 1);
        }
    }
}

pub struct SplitPaneView {
    pub left_pane: Pane,
    pub right_pane: Pane,
    pub focus: PaneFocus,
    pub vertical_split: bool,
    pub split_ratio: f32, // 0.0 to 1.0, percentage for left/top pane
}

impl SplitPaneView {
    pub fn new(left_path: PathBuf, right_path: PathBuf) -> Result<Self> {
        Ok(Self {
            left_pane: Pane::new(left_path)?,
            right_pane: Pane::new(right_path)?,
            focus: PaneFocus::Left,
            vertical_split: true,
            split_ratio: 0.5,
        })
    }

    pub fn toggle_focus(&mut self) {
        self.focus = match self.focus {
            PaneFocus::Left => PaneFocus::Right,
            PaneFocus::Right => PaneFocus::Left,
        };
    }

    pub fn toggle_layout(&mut self) {
        self.vertical_split = !self.vertical_split;
    }

    pub fn adjust_split(&mut self, delta: f32) {
        self.split_ratio = (self.split_ratio + delta).clamp(0.2, 0.8);
    }

    pub fn get_active_pane(&self) -> &Pane {
        match self.focus {
            PaneFocus::Left => &self.left_pane,
            PaneFocus::Right => &self.right_pane,
        }
    }

    pub fn get_active_pane_mut(&mut self) -> &mut Pane {
        match self.focus {
            PaneFocus::Left => &mut self.left_pane,
            PaneFocus::Right => &mut self.right_pane,
        }
    }

    pub fn sync_directories(&mut self) -> Result<()> {
        let target_dir = self.get_active_pane().current_dir.clone();
        match self.focus {
            PaneFocus::Left => self.right_pane.load_directory(&target_dir)?,
            PaneFocus::Right => self.left_pane.load_directory(&target_dir)?,
        }
        Ok(())
    }

    pub fn render(&mut self) -> Result<()> {
        let mut stdout = io::stdout();
        let (terminal_width, terminal_height) = terminal::size()?;

        // Clear screen
        execute!(stdout, terminal::Clear(terminal::ClearType::All))?;

        if self.vertical_split {
            self.render_vertical_split(&mut stdout, terminal_width, terminal_height)?;
        } else {
            self.render_horizontal_split(&mut stdout, terminal_width, terminal_height)?;
        }

        // Render status bar
        self.render_status_bar(&mut stdout, terminal_width, terminal_height)?;

        stdout.flush()?;
        Ok(())
    }

    fn render_vertical_split(
        &mut self,
        stdout: &mut io::Stdout,
        width: u16,
        height: u16,
    ) -> Result<()> {
        let split_pos = (width as f32 * self.split_ratio) as u16;
        let left_width = split_pos.saturating_sub(1);
        let right_width = width.saturating_sub(split_pos + 1);

        // Render left pane
        Self::render_pane(
            stdout,
            &mut self.left_pane,
            0,
            0,
            left_width,
            height - 2,
            self.focus == PaneFocus::Left,
        )?;

        // Render divider
        for y in 0..height - 2 {
            execute!(
                stdout,
                MoveTo(split_pos, y),
                SetForegroundColor(Color::DarkGrey),
                Print(""),
                ResetColor
            )?;
        }

        // Render right pane
        Self::render_pane(
            stdout,
            &mut self.right_pane,
            split_pos + 1,
            0,
            right_width,
            height - 2,
            self.focus == PaneFocus::Right,
        )?;

        Ok(())
    }

    fn render_horizontal_split(
        &mut self,
        stdout: &mut io::Stdout,
        width: u16,
        height: u16,
    ) -> Result<()> {
        let split_pos = ((height - 2) as f32 * self.split_ratio) as u16;
        let top_height = split_pos;
        let bottom_height = (height - 2).saturating_sub(split_pos + 1);

        // Render top pane
        Self::render_pane(
            stdout,
            &mut self.left_pane,
            0,
            0,
            width,
            top_height,
            self.focus == PaneFocus::Left,
        )?;

        // Render divider
        execute!(
            stdout,
            MoveTo(0, split_pos),
            SetForegroundColor(Color::DarkGrey),
            Print("".repeat(width as usize)),
            ResetColor
        )?;

        // Render bottom pane
        Self::render_pane(
            stdout,
            &mut self.right_pane,
            0,
            split_pos + 1,
            width,
            bottom_height,
            self.focus == PaneFocus::Right,
        )?;

        Ok(())
    }

    fn render_pane(
        stdout: &mut io::Stdout,
        pane: &mut Pane,
        x: u16,
        y: u16,
        width: u16,
        height: u16,
        is_active: bool,
    ) -> Result<()> {
        // Header
        let header_color = if is_active {
            Color::Blue
        } else {
            Color::DarkGrey
        };

        execute!(
            stdout,
            MoveTo(x, y),
            SetBackgroundColor(header_color),
            SetForegroundColor(Color::White),
            Print(format!(
                " {} ",
                pane.current_dir
                    .to_string_lossy()
                    .chars()
                    .take((width - 2) as usize)
                    .collect::<String>()
            )),
            Print(" ".repeat(
                (width as usize).saturating_sub(pane.current_dir.to_string_lossy().len() + 2)
            )),
            ResetColor
        )?;

        // File list
        let list_height = (height - 1) as usize;
        pane.adjust_scroll_with_height(list_height);

        let end_index = (pane.scroll_offset + list_height).min(pane.entries.len());

        for (i, entry) in pane.entries[pane.scroll_offset..end_index]
            .iter()
            .enumerate()
        {
            let row = y + 1 + i as u16;
            let display_index = pane.scroll_offset + i;
            let is_selected = pane.selected_items.contains(&display_index);
            let is_highlighted = display_index == pane.selected_index;

            execute!(stdout, MoveTo(x, row))?;

            if is_highlighted && is_active {
                execute!(
                    stdout,
                    SetBackgroundColor(Color::DarkGreen),
                    SetForegroundColor(Color::White)
                )?;
            } else if is_highlighted {
                execute!(
                    stdout,
                    SetBackgroundColor(Color::DarkGrey),
                    SetForegroundColor(Color::White)
                )?;
            }

            let marker = if is_selected { "[✓]" } else { "   " };
            let prefix = if is_highlighted { ">" } else { " " };

            let display_name = entry.display_name();
            let truncated_name = if display_name.len() > (width - 5) as usize {
                format!("{}...", &display_name[..(width - 8) as usize])
            } else {
                display_name
            };

            execute!(
                stdout,
                Print(format!("{}{} {}", prefix, marker, truncated_name))
            )?;

            if is_highlighted {
                let padding = (width as usize)
                    .saturating_sub(prefix.len() + marker.len() + truncated_name.len() + 1);
                execute!(stdout, Print(" ".repeat(padding)))?;
            }

            execute!(stdout, ResetColor)?;
        }

        Ok(())
    }

    fn render_status_bar(&self, stdout: &mut io::Stdout, width: u16, height: u16) -> Result<()> {
        let status =
            " Tab: Switch Pane | F5: Sync Dirs | F6: Toggle Layout | +/-: Adjust Split | q: Quit";

        execute!(
            stdout,
            MoveTo(0, height - 1),
            SetBackgroundColor(Color::DarkGrey),
            SetForegroundColor(Color::White),
            Print(&status),
            Print(" ".repeat((width as usize).saturating_sub(status.len()))),
            ResetColor
        )?;

        Ok(())
    }
}