use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
pub name: String,
pub path: PathBuf,
pub is_dir: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaneId {
Left,
Right,
}
impl PaneId {
pub fn other(self) -> Self {
match self {
PaneId::Left => PaneId::Right,
PaneId::Right => PaneId::Left,
}
}
}
#[derive(Debug, Clone)]
pub struct Pane {
pub cwd: PathBuf,
pub entries: Vec<Entry>,
pub cursor: usize,
pub last_error: Option<String>,
pub scroll_offset: usize,
}
impl Pane {
pub fn new(cwd: PathBuf) -> Self {
Self {
cwd,
entries: Vec::new(),
cursor: 0,
last_error: None,
scroll_offset: 0,
}
}
pub fn move_cursor_up(&mut self) {
if self.cursor > 0 {
self.cursor -= 1;
if self.cursor < self.scroll_offset {
self.scroll_offset = self.cursor;
}
}
}
pub fn move_cursor_down(&mut self) {
if !self.entries.is_empty() && self.cursor < self.entries.len() - 1 {
self.cursor += 1;
const VISIBLE_ITEMS: usize = 20;
const KEEP_BELOW: usize = 5;
if self.cursor >= self.scroll_offset + VISIBLE_ITEMS - KEEP_BELOW {
self.scroll_offset = self.cursor.saturating_sub(VISIBLE_ITEMS - KEEP_BELOW - 1);
}
}
}
pub fn ensure_cursor_visible(&mut self, visible_items: usize) {
if self.cursor < self.scroll_offset {
self.scroll_offset = self.cursor;
} else if self.cursor >= self.scroll_offset + visible_items {
self.scroll_offset = self.cursor.saturating_sub(visible_items - 1);
}
}
pub fn selected(&self) -> Option<&Entry> {
self.entries.get(self.cursor)
}
pub fn load_entries(&mut self) {
self.last_error = None;
self.entries.clear();
self.cursor = 0;
self.scroll_offset = 0;
match std::fs::read_dir(&self.cwd) {
Ok(entries_iter) => {
let mut dirs = Vec::new();
let mut files = Vec::new();
for entry_result in entries_iter {
match entry_result {
Ok(entry) => {
let file_name = entry.file_name();
let name = file_name.to_string_lossy().to_string();
if name == "." {
continue;
}
let path = entry.path();
let is_dir = entry.metadata().map(|m| m.is_dir()).unwrap_or(false);
let entry_item = Entry { name, path, is_dir };
if is_dir {
dirs.push(entry_item);
} else {
files.push(entry_item);
}
}
Err(e) => {
self.last_error = Some(format!("Error reading entry: {}", e));
}
}
}
dirs.sort_by(|a, b| a.name.cmp(&b.name));
files.sort_by(|a, b| a.name.cmp(&b.name));
if self.cwd.parent().is_some() {
self.entries.push(Entry {
name: "..".to_string(),
path: self.cwd.parent().unwrap().to_path_buf(),
is_dir: true,
});
}
self.entries.extend(dirs);
self.entries.extend(files);
}
Err(e) => {
self.last_error = Some(format!("Cannot read directory: {}", e));
}
}
}
pub fn enter_directory(&mut self) {
if let Some(entry) = self.selected() {
if entry.is_dir {
self.cwd = entry.path.clone();
self.load_entries();
}
}
}
pub fn goto_parent(&mut self) {
if let Some(parent) = self.cwd.parent() {
self.cwd = parent.to_path_buf();
self.load_entries();
}
}
}
#[derive(Debug, Clone)]
pub struct DualPane {
pub left: Pane,
pub right: Pane,
pub active: PaneId,
}
impl DualPane {
pub fn new(left_cwd: PathBuf, right_cwd: PathBuf) -> Self {
Self {
left: Pane::new(left_cwd),
right: Pane::new(right_cwd),
active: PaneId::Left,
}
}
pub fn active_pane(&self) -> &Pane {
match self.active {
PaneId::Left => &self.left,
PaneId::Right => &self.right,
}
}
pub fn active_pane_mut(&mut self) -> &mut Pane {
match self.active {
PaneId::Left => &mut self.left,
PaneId::Right => &mut self.right,
}
}
pub fn switch_active(&mut self) {
self.active = self.active.other();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cursor_does_not_go_below_zero() {
let mut pane = Pane::new(PathBuf::from("/tmp"));
pane.move_cursor_up();
assert_eq!(pane.cursor, 0);
}
#[test]
fn cursor_does_not_exceed_entries() {
let mut pane = Pane::new(PathBuf::from("/tmp"));
pane.entries = vec![Entry {
name: "a".into(),
path: "/tmp/a".into(),
is_dir: false,
}];
pane.move_cursor_down();
pane.move_cursor_down();
assert_eq!(pane.cursor, 0);
}
#[test]
fn switch_active_toggles_pane() {
let mut dp = DualPane::new("/".into(), "/tmp".into());
assert_eq!(dp.active, PaneId::Left);
dp.switch_active();
assert_eq!(dp.active, PaneId::Right);
dp.switch_active();
assert_eq!(dp.active, PaneId::Left);
}
#[test]
fn load_entries_sorts_directories_first() {
let mut pane = Pane::new(std::env::temp_dir());
pane.load_entries();
let mut found_file = false;
for entry in &pane.entries {
if !entry.is_dir {
found_file = true;
} else if found_file {
panic!("Found directory after file; sort order violated");
}
}
let mut prev_dir: Option<&str> = None;
for entry in &pane.entries {
if entry.is_dir {
if let Some(prev) = prev_dir {
assert!(
prev <= entry.name.as_str(),
"Directories not sorted alphabetically"
);
}
prev_dir = Some(&entry.name);
}
}
}
#[test]
fn enter_directory_changes_cwd() {
let temp_dir = std::env::temp_dir();
let mut pane = Pane::new(temp_dir.clone());
pane.entries = vec![Entry {
name: "test_entry".into(),
path: temp_dir.clone(),
is_dir: true,
}];
pane.cursor = 0;
pane.enter_directory();
assert_eq!(pane.cwd, temp_dir);
}
#[test]
fn goto_parent_navigates_up() {
let mut pane = Pane::new(PathBuf::from("/tmp/some/nested/path"));
let parent = pane.cwd.parent().unwrap().to_path_buf();
pane.goto_parent();
assert_eq!(pane.cwd, parent);
}
#[test]
fn goto_parent_at_root_does_nothing() {
let mut pane = Pane::new(PathBuf::from("/"));
let _original_cwd = pane.cwd.clone();
pane.goto_parent();
assert_eq!(pane.cwd, PathBuf::from("/"));
}
}