use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Dir,
Image,
Video,
Audio,
Text,
Other,
}
impl Kind {
pub fn label(self) -> &'static str {
match self {
Kind::Dir => "folder",
Kind::Image => "image",
Kind::Video => "video",
Kind::Audio => "audio",
Kind::Text => "text",
Kind::Other => "file",
}
}
}
pub fn kind_of(name: &str) -> Kind {
let ext = name.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase()).unwrap_or_default();
match ext.as_str() {
"png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp" => Kind::Image,
"mp4" | "mkv" | "webm" | "mov" | "avi" | "m4v" => Kind::Video,
"mp3" | "flac" | "ogg" | "opus" | "wav" | "m4a" | "aac" => Kind::Audio,
"txt" | "md" | "rs" | "toml" | "json" | "yaml" | "yml" | "sh" | "py" | "js" | "ts" | "c" | "h" | "cpp" | "go" | "html" | "css" | "log"
| "conf" | "ini" | "lock" | "xml" | "csv" => Kind::Text,
_ if !name.contains('.') || name.starts_with('.') && !name[1..].contains('.') => Kind::Text,
_ => Kind::Other,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Badge {
Folder,
Image,
Video,
Audio,
Text,
Code,
Markdown,
Pdf,
Archive,
Binary,
Config,
Font,
Document,
Generic,
}
impl Badge {
pub const ALL: [Badge; 14] = [
Badge::Folder,
Badge::Image,
Badge::Video,
Badge::Audio,
Badge::Text,
Badge::Code,
Badge::Markdown,
Badge::Pdf,
Badge::Archive,
Badge::Binary,
Badge::Config,
Badge::Font,
Badge::Document,
Badge::Generic,
];
pub fn mark(self) -> &'static str {
match self {
Badge::Folder => "",
Badge::Image => "IMG",
Badge::Video => "VID",
Badge::Audio => "AUD",
Badge::Text => "TXT",
Badge::Code => "</>",
Badge::Markdown => "MD",
Badge::Pdf => "PDF",
Badge::Archive => "ZIP",
Badge::Binary => "BIN",
Badge::Config => "{}",
Badge::Font => "Aa",
Badge::Document => "DOC",
Badge::Generic => "…",
}
}
pub fn color(self) -> u32 {
match self {
Badge::Folder => 0x4F9DDE,
Badge::Image => 0x3FB27F,
Badge::Video => 0xE0566B,
Badge::Audio => 0xB46EE0,
Badge::Text => 0x8A96A3,
Badge::Code => 0x2FB8C9,
Badge::Markdown => 0x5B7BE0,
Badge::Pdf => 0xD9443A,
Badge::Archive => 0xC9902B,
Badge::Binary => 0x6B7280,
Badge::Config => 0xE0A93A,
Badge::Font => 0xD0679E,
Badge::Document => 0x3A7BD5,
Badge::Generic => 0x7C8794,
}
}
pub fn index(self) -> u8 {
Badge::ALL.iter().position(|b| *b == self).unwrap_or(13) as u8
}
pub fn from_index(i: u8) -> Badge {
Badge::ALL.get(i as usize).copied().unwrap_or(Badge::Generic)
}
}
pub fn badge_of(name: &str, kind: Kind, mode: &str) -> Badge {
if kind == Kind::Dir {
return Badge::Folder;
}
let ext = name.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase()).unwrap_or_default();
let by_ext = match ext.as_str() {
"md" | "markdown" => Some(Badge::Markdown),
"pdf" => Some(Badge::Pdf),
"zip" | "tar" | "gz" | "tgz" | "xz" | "zst" | "bz2" | "7z" | "rar" | "deb" | "rpm" => Some(Badge::Archive),
"json" | "toml" | "yaml" | "yml" | "ini" | "conf" | "cfg" | "lock" | "xml" | "env" => Some(Badge::Config),
"ttf" | "otf" | "woff" | "woff2" => Some(Badge::Font),
"doc" | "docx" | "odt" | "rtf" | "xls" | "xlsx" | "ods" | "csv" | "ppt" | "pptx" | "odp" => Some(Badge::Document),
"rs" | "sh" | "py" | "js" | "ts" | "c" | "h" | "cpp" | "go" | "html" | "css" | "java" | "rb" | "lua" | "zig" | "swift" | "kt" => {
Some(Badge::Code)
}
"exe" | "dll" | "so" | "dylib" | "bin" | "o" | "a" | "wasm" => Some(Badge::Binary),
_ => None,
};
if let Some(b) = by_ext {
return b;
}
let exec = mode.len() == 10 && mode[1..].contains('x');
match kind {
Kind::Image => Badge::Image,
Kind::Video => Badge::Video,
Kind::Audio => Badge::Audio,
_ if exec => Badge::Binary,
Kind::Text => Badge::Text,
_ => Badge::Generic,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry {
pub name: String,
pub path: PathBuf,
pub kind: Kind,
pub mode: String,
pub size: u64,
pub modified: Option<SystemTime>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Sort {
#[default]
Name,
Modified,
Size,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Flags {
pub all: bool,
pub human: bool,
pub sort: Sort,
pub reverse: bool,
}
impl Default for Flags {
fn default() -> Self {
Self { all: true, human: true, sort: Sort::Name, reverse: false }
}
}
impl Flags {
pub fn ls(&self) -> String {
let mut s = String::from("-l");
if self.all {
s.push('a');
}
if self.human {
s.push('h');
}
match self.sort {
Sort::Name => {}
Sort::Modified => s.push('t'),
Sort::Size => s.push('S'),
}
if self.reverse {
s.push('r');
}
s
}
}
#[cfg(unix)]
pub fn mode_string(m: &std::fs::Metadata) -> String {
use std::os::unix::fs::PermissionsExt;
let p = m.permissions().mode();
let t = if m.file_type().is_symlink() {
'l'
} else if m.is_dir() {
'd'
} else {
'-'
};
let mut s = String::from(t);
for (i, c) in "rwxrwxrwx".chars().enumerate() {
s.push(if p & (1 << (8 - i)) != 0 { c } else { '-' });
}
s
}
#[cfg(not(unix))]
pub fn mode_string(m: &std::fs::Metadata) -> String {
let d = if m.is_dir() { 'd' } else { '-' };
let w = if m.permissions().readonly() { '-' } else { 'w' };
format!("{d}r{w}-r--r--")
}
fn entry(name: String, path: PathBuf) -> Entry {
let meta = std::fs::symlink_metadata(&path).ok();
let target_dir = std::fs::metadata(&path).is_ok_and(|m| m.is_dir());
let kind = if target_dir { Kind::Dir } else { kind_of(&name) };
Entry {
mode: meta.as_ref().map(mode_string).unwrap_or_else(|| "?---------".into()),
size: meta.as_ref().map_or(0, |m| m.len()),
modified: meta.as_ref().and_then(|m| m.modified().ok()),
name,
path,
kind,
}
}
pub fn list_dir(dir: &Path, flags: &Flags) -> std::io::Result<Vec<Entry>> {
let v: Vec<Entry> = std::fs::read_dir(dir)?.flatten().map(|e| entry(e.file_name().to_string_lossy().into_owned(), e.path())).collect();
Ok(arrange(v, flags))
}
pub fn arrange(v: Vec<Entry>, flags: &Flags) -> Vec<Entry> {
let mut v: Vec<Entry> = v.into_iter().filter(|e| flags.all || !e.name.starts_with('.')).filter(|e| e.name != "." && e.name != "..").collect();
match flags.sort {
Sort::Name => v.sort_by_key(|e| e.name.to_lowercase()),
Sort::Modified => v.sort_by_key(|e| std::cmp::Reverse(e.modified)),
Sort::Size => v.sort_by_key(|e| std::cmp::Reverse(e.size)),
}
if flags.reverse {
v.reverse();
}
v
}
pub fn human_size(n: u64) -> String {
let units = ["", "K", "M", "G", "T"];
let mut v = n as f64;
let mut i = 0;
while v >= 1024.0 && i < units.len() - 1 {
v /= 1024.0;
i += 1;
}
if i == 0 {
n.to_string()
} else if v < 10.0 {
format!("{v:.1}{}", units[i])
} else {
format!("{v:.0}{}", units[i])
}
}
pub fn format_time(t: SystemTime) -> String {
let secs = t.duration_since(SystemTime::UNIX_EPOCH).map_or(0, |d| d.as_secs() as i64);
let (mon, day, h, m) = local_parts(secs);
const M: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
format!("{} {day:2} {h:02}:{m:02}", M[mon.min(11)])
}
#[cfg(unix)]
fn local_parts(secs: i64) -> (usize, u32, u32, u32) {
unsafe {
let t = secs as libc::time_t;
let mut tm: libc::tm = std::mem::zeroed();
libc::localtime_r(&t, &mut tm);
(tm.tm_mon as usize, tm.tm_mday as u32, tm.tm_hour as u32, tm.tm_min as u32)
}
}
#[cfg(not(unix))]
fn local_parts(secs: i64) -> (usize, u32, u32, u32) {
let dt = time::OffsetDateTime::from_unix_timestamp(secs).unwrap_or(time::OffsetDateTime::UNIX_EPOCH);
(u8::from(dt.month()) as usize - 1, dt.day() as u32, dt.hour() as u32, dt.minute() as u32)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum View {
#[default]
List,
Grid,
Carousel,
}
impl View {
pub fn next(self) -> Self {
match self {
View::List => View::Grid,
View::Grid => View::Carousel,
View::Carousel => View::List,
}
}
pub fn name(self) -> &'static str {
match self {
View::List => "list",
View::Grid => "grid",
View::Carousel => "carousel",
}
}
}
pub const TILE_SIZES: [i32; 4] = [96, 128, 176, 240];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
SwitchView,
Search,
Up,
Down,
Left,
Right,
Open,
Bigger,
Smaller,
Hidden,
Options,
Man,
PageUp,
PageDown,
First,
Last,
Parent,
Close,
Mark,
MarkAll,
Pipe,
}
pub const BINDINGS: &[crate::modes::Binding] = &[
b("switch_view", "t", "switch view (list / grid / carousel)"),
b("search", "s", "search this folder"),
b("up", "up", "previous row"),
b("down", "down", "next row"),
b("left", "left", "previous item"),
b("right", "right", "next item"),
b("open", "enter", "open (folder: cd into it; file: full-width view)"),
b("bigger", "z", "larger grid tiles"),
b("smaller", "x", "smaller grid tiles"),
b("hidden", "a", "show / hide dotfiles (ls -a)"),
b("options", "o", "options window"),
b("man", "m", "manual for commander mode"),
b("page_up", "pageup", "page up"),
b("page_down", "pagedown", "page down"),
b("first", "home", "first item"),
b("last", "end", "last item"),
b("parent", "p", "back: parent folder (cd ..)"),
b("back", "backspace", "back: parent folder (cd ..)"),
b("close", "q", "close the file view / window"),
b("mark", "space", "mark / unmark the item (for pipe)"),
b("mark_all", "*", "mark all / clear marks"),
b("pipe", "|", "pipe the marked (or selected) paths into a command"),
b("play_pause", "k", "player: play / pause"),
b("seek_back", "j", "player: back 10 s"),
b("seek_forward", "l", "player: forward 10 s"),
b("volume_down", "-", "player: volume down"),
b("volume_up", "=", "player: volume up"),
b("zoom_in", "=", "image view: zoom in (same key as volume up in the player)"),
b("zoom_out", "-", "image view: zoom out (same key as volume down in the player)"),
b("zoom_fit", "0", "image view: fit"),
b("zoom_actual", "1", "image view: 100% (actual pixels)"),
b("fullscreen", "e", "image / player: fullscreen, the whole window (Esc or the same key leaves)"),
b("menu", "i", "header overflow menu (\u{2022}\u{2022}\u{2022}) when the window is narrow"),
];
const fn b(id: &'static str, key: &'static str, describe: &'static str) -> crate::modes::Binding {
crate::modes::Binding { id, key, describe }
}
impl Action {
pub fn from_id(id: &str) -> Option<Self> {
Some(match id {
"switch_view" => Action::SwitchView,
"search" => Action::Search,
"up" => Action::Up,
"down" => Action::Down,
"left" => Action::Left,
"right" => Action::Right,
"open" => Action::Open,
"bigger" => Action::Bigger,
"smaller" => Action::Smaller,
"hidden" => Action::Hidden,
"options" => Action::Options,
"man" => Action::Man,
"page_up" => Action::PageUp,
"page_down" => Action::PageDown,
"first" => Action::First,
"last" => Action::Last,
"parent" | "back" => Action::Parent,
"close" => Action::Close,
"mark" => Action::Mark,
"mark_all" => Action::MarkAll,
"pipe" => Action::Pipe,
_ => return None,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Overlay {
Options,
Man,
Preview(PathBuf),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Effect {
None,
Cd(PathBuf),
External(PathBuf),
Run(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Place {
pub path: PathBuf,
pub selected: usize,
pub selected_name: Option<String>,
pub scroll: usize,
}
#[derive(Debug, Default)]
pub struct Commander {
pub nav: Vec<Place>,
pending: Option<(PathBuf, Option<Place>)>,
pub view: View,
pub tile: usize,
pub flags: Flags,
pub cwd: Option<PathBuf>,
pub entries: Vec<Entry>,
stamp: Option<(PathBuf, Option<SystemTime>, Flags)>,
pub error: Option<String>,
pub filter: String,
pub searching: bool,
pub selected: usize,
pub scroll: usize,
pub overlay: Option<Overlay>,
pub overlay_scroll: usize,
pub page: usize,
pub marks: std::collections::BTreeSet<PathBuf>,
pub piping: bool,
pub pipe_cmd: String,
pub remote: Option<String>,
pub notice: Option<String>,
remote_raw: Vec<Entry>,
}
impl Commander {
pub fn new() -> Self {
Self { tile: 1, ..Default::default() }
}
pub fn reset(&mut self) {
self.searching = false;
self.filter.clear();
self.overlay = None;
self.overlay_scroll = 0;
self.piping = false;
self.pipe_cmd.clear();
}
pub fn set_cwd(&mut self, dir: Option<PathBuf>) {
let Some(dir) = dir else { return };
let dir = self.logical(dir, false);
let mtime = std::fs::metadata(&dir).and_then(|m| m.modified()).ok();
let stamp = (dir.clone(), mtime, self.flags);
if self.remote.is_some() {
self.remote = None;
self.stamp = None;
}
if self.stamp.as_ref() == Some(&stamp) {
return;
}
let listing = list_dir(&dir, &self.flags).map_err(|e| format!("{}: {e}", dir.display()));
self.load(dir, listing);
self.stamp = Some(stamp);
}
pub fn set_remote(&mut self, label: String, dir: Option<PathBuf>, listing: Option<Result<Vec<Entry>, String>>) {
let label_changed = self.remote.as_ref() != Some(&label);
self.remote = Some(label.clone());
self.stamp = None;
let Some(dir) = dir else {
self.entries.clear();
if label_changed || self.cwd.is_some() {
self.marks.clear();
self.selected = 0;
}
self.cwd = None;
self.error = Some(format!(
"remote ({label}): no directory info. The remote shell does not report its folder (OSC 7) or there is no ssh side channel; see docs/modes.md"
));
return;
};
let listing = match listing {
Some(r) => r.map(|v| {
self.remote_raw = v.clone();
arrange(v, &self.flags)
}),
None if self.cwd.as_ref() == Some(&dir) && self.error.is_none() && !label_changed => return,
None => Ok(Vec::new()),
};
let dir = self.logical(dir, true);
self.load(dir, listing);
}
fn logical(&mut self, reported: PathBuf, remote: bool) -> PathBuf {
let same =
|a: &Path, b: &Path| a == b || (!remote && std::fs::canonicalize(a).ok().is_some_and(|x| std::fs::canonicalize(b).ok() == Some(x)));
if let Some((want, _)) = &self.pending {
if same(want, &reported) {
return want.clone();
}
if self.cwd.as_ref().is_some_and(|c| same(c, &reported)) {
return self.cwd.clone().unwrap_or(reported);
}
self.pending = None;
}
if let Some(c) = &self.cwd
&& same(c, &reported)
{
return c.clone();
}
if self.cwd.is_some() {
self.nav.clear();
}
reported
}
fn here(&self) -> Option<Place> {
let path = self.cwd.clone()?;
Some(Place { path, selected: self.selected, selected_name: self.selected_entry().map(|e| e.name.clone()), scroll: self.scroll })
}
fn go(&mut self, dir: PathBuf, restore: Option<Place>) -> Effect {
self.pending = Some((dir.clone(), restore));
Effect::Cd(dir)
}
pub fn back(&mut self) -> Effect {
if let Some(p) = self.nav.pop() {
if self.cwd.as_ref() == Some(&p.path) {
self.restore(&p);
return Effect::None;
}
let path = p.path.clone();
return self.go(path, Some(p));
}
match self.cwd.as_ref().and_then(|d| d.parent()) {
Some(p) => {
let p = p.to_path_buf();
self.go(p, None)
}
None => Effect::None,
}
}
pub fn close_file(&mut self) {
if let Some(p) = self.nav.last().cloned()
&& self.cwd.as_ref() == Some(&p.path)
{
self.nav.pop();
self.restore(&p);
}
}
pub fn jump(&mut self, dir: PathBuf) -> Effect {
self.nav.clear();
self.go(dir, None)
}
fn restore(&mut self, p: &Place) {
self.selected = p.selected_name.as_ref().and_then(|n| self.visible().iter().position(|e| &e.name == n)).unwrap_or(p.selected);
self.scroll = p.scroll;
self.clamp();
}
pub fn relist(&mut self) {
if self.remote.is_some() {
if let Some(d) = self.cwd.clone()
&& self.error.is_none()
{
let v = arrange(self.remote_raw.clone(), &self.flags);
self.load(d, Ok(v));
}
} else {
let d = self.cwd.clone();
self.set_cwd(d);
}
}
fn load(&mut self, dir: PathBuf, listing: Result<Vec<Entry>, String>) {
let came_from =
self.cwd.as_ref().filter(|old| **old != dir).and_then(|old| {
old.strip_prefix(&dir).ok().and_then(|r| r.components().next()).map(|c| c.as_os_str().to_string_lossy().into_owned())
});
if self.cwd.as_ref() != Some(&dir) {
self.selected = 0;
self.scroll = 0;
self.filter.clear();
self.marks.clear();
}
match listing {
Ok(v) => {
self.entries = v;
self.error = None;
}
Err(e) => {
self.entries.clear();
self.error = Some(e);
}
}
if let Some(name) = came_from
&& let Some(i) = self.visible().iter().position(|e| e.name == name)
{
self.selected = i;
}
let arrived = self.pending.as_ref().is_some_and(|(w, _)| *w == dir);
self.cwd = Some(dir);
self.clamp();
if arrived && let Some((_, Some(p))) = self.pending.take() {
self.restore(&p);
}
}
pub fn visible(&self) -> Vec<&Entry> {
let f = self.filter.to_lowercase();
self.entries.iter().filter(|e| f.is_empty() || e.name.to_lowercase().contains(&f)).collect()
}
fn clamp(&mut self) {
let n = self.visible().len();
self.selected = self.selected.min(n.saturating_sub(1));
}
pub fn selected_entry(&self) -> Option<&Entry> {
self.visible().get(self.selected).copied()
}
pub fn follow(&mut self, rows: usize) {
let rows = rows.max(1);
if self.selected < self.scroll {
self.scroll = self.selected;
} else if self.selected >= self.scroll + rows {
self.scroll = self.selected + 1 - rows;
}
}
pub fn pipe_key(&mut self, bytes: &[u8]) -> Option<Effect> {
if !self.piping {
return None;
}
match bytes {
b"\r" => {
self.piping = false;
let cmd = std::mem::take(&mut self.pipe_cmd);
if cmd.trim().is_empty() {
return Some(Effect::None);
}
return Some(Effect::Run(pipe_line(&self.pipe_paths(), &cmd)));
}
b"\x1b" => {
self.piping = false;
self.pipe_cmd.clear();
}
b"\x7f" | b"\x08" => {
self.pipe_cmd.pop();
}
_ => {
if let Ok(t) = std::str::from_utf8(bytes)
&& !t.chars().any(char::is_control)
{
self.pipe_cmd.push_str(t);
}
}
}
Some(Effect::None)
}
pub fn pipe_paths(&self) -> Vec<PathBuf> {
if self.marks.is_empty() {
self.selected_entry().map(|e| e.path.clone()).into_iter().collect()
} else {
let v: Vec<PathBuf> = self.entries.iter().filter(|e| self.marks.contains(&e.path)).map(|e| e.path.clone()).collect();
v
}
}
pub fn search_key(&mut self, bytes: &[u8]) -> bool {
if !self.searching {
return false;
}
match bytes {
b"\r" | b"\x1b" => self.searching = false,
b"\x7f" | b"\x08" => {
self.filter.pop();
}
_ => {
if let Ok(s) = std::str::from_utf8(bytes)
&& !s.starts_with('\x1b')
&& !s.chars().any(char::is_control)
{
self.filter.push_str(s);
self.selected = 0;
self.scroll = 0;
}
}
}
self.clamp();
true
}
pub fn apply(&mut self, a: Action, per_row: usize) -> Effect {
self.notice = None;
let n = self.visible().len();
let per_row = if self.view == View::Grid { per_row.max(1) } else { 1 };
match a {
Action::SwitchView => self.view = self.view.next(),
Action::Search => self.searching = !self.searching,
Action::Up => self.selected = self.selected.saturating_sub(per_row),
Action::Down => {
if self.selected + per_row < n {
self.selected += per_row
}
}
Action::Left => self.selected = self.selected.saturating_sub(1),
Action::Right => self.selected = (self.selected + 1).min(n.saturating_sub(1)),
Action::Bigger => self.tile = (self.tile + 1).min(TILE_SIZES.len() - 1),
Action::Smaller => self.tile = self.tile.saturating_sub(1),
Action::Hidden => self.flags.all = !self.flags.all,
Action::Options => self.overlay = if self.overlay == Some(Overlay::Options) { None } else { Some(Overlay::Options) },
Action::Man => {
self.overlay = if self.overlay == Some(Overlay::Man) { None } else { Some(Overlay::Man) };
self.overlay_scroll = 0;
}
Action::Open => return self.open_selected(),
Action::PageUp => self.selected = self.selected.saturating_sub(self.page.max(1)),
Action::PageDown => self.selected = (self.selected + self.page.max(1)).min(n.saturating_sub(1)),
Action::First => self.selected = 0,
Action::Last => self.selected = n.saturating_sub(1),
Action::Parent => return self.back(),
Action::Close => {
self.overlay = None;
self.piping = false;
}
Action::Mark => {
if let Some(e) = self.selected_entry().map(|e| e.path.clone())
&& !self.marks.remove(&e)
{
self.marks.insert(e);
}
if self.view != View::Grid || per_row == 1 {
self.selected = (self.selected + 1).min(n.saturating_sub(1));
}
}
Action::MarkAll => {
if self.marks.is_empty() {
let all: Vec<PathBuf> = self.visible().iter().map(|e| e.path.clone()).collect();
self.marks.extend(all);
} else {
self.marks.clear();
}
}
Action::Pipe => {
self.piping = !self.piping;
self.searching = false;
}
}
self.clamp();
Effect::None
}
pub fn open_selected(&mut self) -> Effect {
let Some(e) = self.selected_entry().cloned() else { return Effect::None };
let here = self.here();
self.nav.extend(here);
match e.kind {
Kind::Dir => self.go(e.path, None),
Kind::Video | Kind::Audio => Effect::External(e.path),
_ => {
self.overlay = Some(Overlay::Preview(e.path));
self.overlay_scroll = 0;
Effect::None
}
}
}
pub fn wanted_previews(&self, first: usize, count: usize) -> Vec<PathBuf> {
let vis = self.visible();
let range = match self.view {
View::List => 0..0,
View::Grid => first.min(vis.len())..(first + count).min(vis.len()),
View::Carousel => self.selected.saturating_sub(1)..(self.selected + 2).min(vis.len()),
};
vis[range].iter().filter(|e| e.kind == Kind::Image).map(|e| e.path.clone()).collect()
}
}
pub fn shell_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
pub fn pipe_line(paths: &[PathBuf], cmd: &str) -> String {
let args: Vec<String> = paths.iter().map(|p| shell_quote(&p.to_string_lossy())).collect();
format!("printf '%s\\n' {} | {}", args.join(" "), cmd.trim())
}
pub fn manual(k: &crate::modes::Keymap) -> Vec<String> {
let mut v = vec![
"COMMANDER MODE (the default mode)".to_string(),
String::new(),
"Shows the shell's working directory above the prompt and follows every cd.".into(),
"Keep typing commands as usual: only the keys below are taken, every other".into(),
"key (other alt keys too) goes to the shell. esc : c turns modes off / on.".into(),
String::new(),
"KEYS".into(),
];
for (id, _, d) in &k.keys {
v.push(format!(" {:<12} {d}", k.label(id)));
}
v.extend([
String::new(),
" ctrl+shift+k key mappings ctrl+shift+m modes".into(),
String::new(),
"REMAPPING".into(),
format!(" {}:", crate::modes::modes_path().display()),
" [commander]".into(),
" modifier = \"alt\"".into(),
" [commander.keys]".into(),
" switch_view = \"v\"".into(),
]);
v
}
pub fn text_from_bytes(b: &[u8], more: bool, name: &str) -> (Vec<String>, Option<String>) {
if b.contains(&0) {
return (Vec::new(), Some(format!("{name}: binary file")));
}
let mut v: Vec<String> = String::from_utf8_lossy(b).lines().take(5000).map(String::from).collect();
if more || v.len() == 5000 {
v.push("[…]".into());
}
(v, None)
}
pub fn text_preview(p: &Path) -> (Vec<String>, Option<String>) {
const MAX: u64 = 1 << 20;
let read = std::fs::File::open(p).and_then(|f| {
let mut b = Vec::new();
std::io::Read::read_to_end(&mut std::io::Read::take(f, MAX), &mut b)?;
Ok(b)
});
match read {
Ok(b) if b.contains(&0) => (Vec::new(), Some(format!("{}: binary file", p.display()))),
Ok(b) => {
let mut v: Vec<String> = String::from_utf8_lossy(&b).lines().take(5000).map(String::from).collect();
if b.len() as u64 >= MAX || v.len() == 5000 {
v.push("[…]".into());
}
(v, None)
}
Err(e) => (Vec::new(), Some(format!("{}: {e}", p.display()))),
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(super) fn tree() -> tempfile::TempDir {
let t = tempfile::tempdir().unwrap();
std::fs::create_dir(t.path().join("sub")).unwrap();
std::fs::write(t.path().join("b.png"), b"x").unwrap();
std::fs::write(t.path().join("a.txt"), b"hello").unwrap();
std::fs::write(t.path().join(".hidden"), b"").unwrap();
std::fs::write(t.path().join("clip.mp4"), vec![0; 3000]).unwrap();
t
}
#[test]
fn lists_like_ls_lah() {
let t = tree();
let v = list_dir(t.path(), &Flags::default()).unwrap();
let names: Vec<_> = v.iter().map(|e| e.name.as_str()).collect();
assert_eq!(names, [".hidden", "a.txt", "b.png", "clip.mp4", "sub"], "no . or .. even with -a");
assert_eq!(v[4].kind, Kind::Dir);
assert!(v[4].mode.starts_with('d'));
assert_eq!((v[2].kind, v[3].kind, v[1].kind), (Kind::Image, Kind::Video, Kind::Text));
let no_dot = list_dir(t.path(), &Flags { all: false, ..Default::default() }).unwrap();
assert!(no_dot.iter().all(|e| !e.name.starts_with('.')));
let by_size = list_dir(t.path(), &Flags { all: false, sort: Sort::Size, ..Default::default() }).unwrap();
assert_eq!(by_size.iter().find(|e| e.kind != Kind::Dir).unwrap().name, "clip.mp4");
assert_eq!(Flags::default().ls(), "-lah");
assert_eq!(human_size(1536), "1.5K");
assert_eq!(human_size(900), "900");
assert_eq!(format_time(SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(86400 * 40)).len(), 12);
}
#[test]
fn follows_cwd_search_and_navigation() {
let t = tree();
let mut c = Commander::new();
c.set_cwd(Some(t.path().to_path_buf()));
assert_eq!(c.visible().len(), 5);
c.searching = true;
for b in [&b"p"[..], b"n", b"g"] {
assert!(c.search_key(b));
}
assert_eq!(c.visible().iter().map(|e| e.name.as_str()).collect::<Vec<_>>(), ["b.png"]);
c.search_key(b"\x7f");
c.search_key(b"\x7f");
assert_eq!(c.filter, "p");
c.search_key(b"\r");
assert!(!c.searching && !c.search_key(b"q"));
c.filter.clear();
c.apply(Action::Down, 1);
assert_eq!(c.selected, 1);
c.view = View::Grid;
c.apply(Action::Down, 3);
assert_eq!(c.selected, 4);
c.apply(Action::Down, 3);
assert_eq!(c.selected, 4, "no row below");
c.apply(Action::Up, 3);
assert_eq!(c.selected, 1);
c.apply(Action::SwitchView, 1);
assert_eq!(c.view, View::Carousel);
c.apply(Action::SwitchView, 1);
assert_eq!(c.view, View::List);
c.set_cwd(Some(t.path().join("sub")));
assert_eq!((c.selected, c.visible().len()), (0, 0), "empty folder: no . or ..");
std::fs::write(t.path().join("sub/.dot"), b"").unwrap();
#[cfg(unix)]
std::fs::File::open(t.path().join("sub")).unwrap().set_modified(SystemTime::now() + std::time::Duration::from_secs(2)).unwrap();
c.set_cwd(Some(t.path().join("sub")));
assert_eq!(c.visible().len(), 1);
c.apply(Action::Hidden, 1);
c.set_cwd(Some(t.path().join("sub")));
assert_eq!(c.visible().len(), 0);
}
#[test]
fn open_by_kind() {
let t = tree();
let mut c = Commander::new();
c.set_cwd(Some(t.path().to_path_buf()));
let at = |c: &Commander, n: &str| c.visible().iter().position(|e| e.name == n).unwrap();
c.selected = at(&c, "sub");
assert_eq!(c.apply(Action::Open, 1), Effect::Cd(t.path().join("sub")));
c.selected = at(&c, "clip.mp4");
assert_eq!(c.apply(Action::Open, 1), Effect::External(t.path().join("clip.mp4")));
c.selected = at(&c, "b.png");
assert_eq!(c.apply(Action::Open, 1), Effect::None);
assert_eq!(c.overlay, Some(Overlay::Preview(t.path().join("b.png"))));
}
#[test]
fn previews_only_for_what_is_visible() {
let t = tempfile::tempdir().unwrap();
for i in 0..10 {
std::fs::write(t.path().join(format!("p{i}.png")), b"x").unwrap();
}
let mut c = Commander::new();
c.flags.all = false;
c.set_cwd(Some(t.path().to_path_buf()));
assert!(c.wanted_previews(0, 10).is_empty(), "list view loads none");
c.view = View::Grid;
let w = c.wanted_previews(4, 3);
assert_eq!(w, (4..7).map(|i| t.path().join(format!("p{i}.png"))).collect::<Vec<_>>());
c.view = View::Carousel;
c.selected = 5;
assert_eq!(c.wanted_previews(0, 99), (4..7).map(|i| t.path().join(format!("p{i}.png"))).collect::<Vec<_>>(), "focused + two neighbours");
c.selected = 0;
assert_eq!(c.wanted_previews(0, 99).len(), 2, "first item has one neighbour");
}
#[test]
fn bindings_cover_every_action() {
for b in BINDINGS.iter().filter(|b| {
!matches!(
b.id,
"play_pause"
| "seek_back"
| "seek_forward"
| "volume_down"
| "volume_up"
| "zoom_in"
| "zoom_out"
| "zoom_fit"
| "zoom_actual"
| "menu"
| "fullscreen"
)
}) {
assert!(Action::from_id(b.id).is_some(), "{}", b.id);
}
let k = crate::modes::Keymap::new(BINDINGS);
assert!(manual(&k).iter().any(|l| l.contains("alt+t") && l.contains("switch view")));
}
#[test]
fn paging_parent_marks_and_pipe() {
let t = tree();
let mut c = Commander::new();
c.set_cwd(Some(t.path().to_path_buf()));
c.page = 3;
c.apply(Action::PageDown, 1);
assert_eq!(c.selected, 3);
c.apply(Action::Last, 1);
assert_eq!(c.selected, 4);
c.apply(Action::PageUp, 1);
assert_eq!(c.selected, 1);
c.apply(Action::First, 1);
assert_eq!(c.selected, 0);
assert_eq!(c.apply(Action::Parent, 1), Effect::Cd(t.path().parent().unwrap().to_path_buf()));
c.selected = 1;
c.apply(Action::Pipe, 1);
for b in [&b"w"[..], b"c", b" ", b"-", b"l"] {
c.pipe_key(b);
}
let Some(Effect::Run(line)) = c.pipe_key(b"\r") else { panic!() };
assert_eq!(line, format!("printf '%s\\n' '{}' | wc -l", t.path().join("a.txt").display()));
c.selected = 0;
c.apply(Action::Mark, 1);
assert_eq!(c.marks.len(), 1, "the first entry is a real file");
c.selected = 0;
c.apply(Action::Mark, 1);
assert!(c.marks.is_empty());
c.selected = 2;
c.apply(Action::Mark, 1);
c.selected = 1;
c.apply(Action::Mark, 1);
assert_eq!(c.pipe_paths(), [t.path().join("a.txt"), t.path().join("b.png")]);
c.apply(Action::MarkAll, 1);
assert!(c.marks.is_empty(), "mark-all with marks clears them");
c.apply(Action::MarkAll, 1);
assert_eq!(c.marks.len(), 5);
assert_eq!(shell_quote("it's"), r"'it'\''s'");
c.apply(Action::Pipe, 1);
c.pipe_key(b"x");
assert_eq!(c.pipe_key(b"\x1b"), Some(Effect::None));
assert!(!c.piping && c.pipe_cmd.is_empty());
c.set_cwd(Some(t.path().join("sub")));
assert!(c.marks.is_empty(), "marks are per folder");
}
#[test]
fn back_selects_the_folder_you_came_from() {
let t = tree();
std::fs::create_dir_all(t.path().join("sub/deep/er")).unwrap();
let mut c = Commander::new();
c.set_cwd(Some(t.path().join("sub/deep/er")));
assert_eq!(c.apply(Action::Parent, 1), Effect::Cd(t.path().join("sub/deep")));
c.set_cwd(Some(t.path().join("sub")));
assert_eq!(c.selected_entry().unwrap().name, "deep");
c.set_cwd(Some(t.path().to_path_buf()));
assert_eq!(c.selected_entry().unwrap().name, "sub");
c.set_cwd(Some(t.path().join("sub")));
assert_eq!(c.selected, 0);
let k = crate::modes::Keymap::new(BINDINGS);
assert!(manual(&k).iter().any(|l| l.contains("alt+backspace") && l.contains("back")), "{:?}", manual(&k));
assert_eq!(Action::from_id("back"), Some(Action::Parent));
assert_eq!(crate::modes::key_bytes("backspace").unwrap()[0], b"\x1b\x7f");
c.set_cwd(Some(PathBuf::from("/")));
assert_eq!(c.apply(Action::Parent, 1), Effect::None);
}
#[test]
fn grid_moves_in_2d_and_carousel_by_item() {
let t = tree();
let mut c = Commander::new();
c.set_cwd(Some(t.path().to_path_buf()));
c.view = View::Grid;
c.apply(Action::Right, 3);
c.apply(Action::Down, 3);
assert_eq!(c.selected, 4);
c.apply(Action::Left, 3);
c.apply(Action::Up, 3);
assert_eq!(c.selected, 0);
c.view = View::Carousel;
c.apply(Action::Right, 3);
c.apply(Action::Right, 3);
assert_eq!(c.selected, 2);
c.apply(Action::Left, 3);
assert_eq!(c.selected, 1);
}
}
#[cfg(test)]
mod remote_tests {
use super::*;
fn e(name: &str, dir: bool) -> Entry {
Entry {
name: name.into(),
path: PathBuf::from(format!("/srv/{name}")),
kind: if dir { Kind::Dir } else { kind_of(name) },
mode: if dir { "drwxr-xr-x".into() } else { "-rw-r--r--".into() },
size: 1,
modified: None,
}
}
fn land(c: &mut Commander, e: &Effect) {
if let Effect::Cd(p) = e {
c.set_cwd(Some(std::fs::canonicalize(p).unwrap()));
}
}
#[cfg(unix)]
#[test]
fn a_symlink_round_trip_comes_back_to_the_link_not_the_targets_parent() {
let t = tests::tree();
let real = t.path().join("deep/real/target");
std::fs::create_dir_all(&real).unwrap();
for n in ["x1", "x2", "x3", "x4"] {
std::fs::write(real.join(n), b"").unwrap();
}
std::os::unix::fs::symlink(&real, t.path().join("sub/link")).unwrap();
let sub = std::fs::canonicalize(t.path().join("sub")).unwrap();
let mut c = Commander::new();
c.set_cwd(Some(sub.clone()));
c.selected = c.visible().iter().position(|e| e.name == "link").unwrap();
let go = c.open_selected();
assert_eq!(go, Effect::Cd(sub.join("link")));
land(&mut c, &go);
assert_eq!(c.cwd.as_deref(), Some(sub.join("link").as_path()));
c.selected = 2;
c.scroll = 1;
c.open_selected();
c.close_file();
assert_eq!((c.selected, c.scroll), (2, 1));
let back = c.apply(Action::Parent, 1);
assert_eq!(back, Effect::Cd(sub.clone()), "never the target's real parent");
land(&mut c, &back);
assert_eq!(c.cwd.as_deref(), Some(sub.as_path()));
assert_eq!(c.selected_entry().unwrap().name, "link");
assert!(c.nav.is_empty());
let mut d = Commander::new();
d.set_cwd(Some(sub.join("link")));
assert_eq!(d.apply(Action::Parent, 1), Effect::Cd(sub.clone()));
assert_ne!(d.apply(Action::Parent, 1), Effect::Cd(real.parent().unwrap().to_path_buf()));
}
#[test]
fn back_restores_the_selection_and_scroll_and_a_typed_cd_starts_over() {
let t = tests::tree();
let root = std::fs::canonicalize(t.path()).unwrap();
std::fs::create_dir(root.join("sub/inner")).unwrap();
let mut c = Commander::new();
c.set_cwd(Some(root.clone()));
c.selected = c.visible().iter().position(|e| e.name == "sub").unwrap();
c.scroll = 2;
let (sel, sc) = (c.selected, c.scroll);
let go = c.open_selected();
land(&mut c, &go);
let go = c.open_selected(); land(&mut c, &go);
assert_eq!(c.nav.len(), 2);
let b = c.apply(Action::Parent, 1);
land(&mut c, &b);
let b = c.apply(Action::Parent, 1);
land(&mut c, &b);
assert_eq!((c.cwd.as_deref(), c.selected, c.scroll), (Some(root.as_path()), sel, sc));
let go = c.open_selected();
land(&mut c, &go);
c.set_cwd(Some(root.join("sub/inner")));
assert!(c.nav.is_empty());
assert_eq!(c.apply(Action::Parent, 1), Effect::Cd(root.join("sub")));
assert_eq!(c.jump(root.clone()), Effect::Cd(root.clone()));
assert!(c.nav.is_empty());
}
#[test]
fn remote_folders_use_the_same_stack() {
let mut c = Commander::new();
c.set_remote("me@web1".into(), Some("/srv".into()), Some(Ok(vec![e("b.png", false), e("app", true)])));
c.selected = c.visible().iter().position(|e| e.name == "app").unwrap();
let sel = c.selected;
assert_eq!(c.open_selected(), Effect::Cd("/srv/app".into()));
c.set_remote("me@web1".into(), Some("/srv/app".into()), Some(Ok(vec![])));
assert_eq!(c.apply(Action::Parent, 1), Effect::Cd("/srv".into()));
c.set_remote("me@web1".into(), Some("/srv".into()), Some(Ok(vec![e("b.png", false), e("app", true)])));
assert_eq!((c.selected, c.nav.len()), (sel, 0));
}
#[test]
fn remote_listing_unknown_dir_and_back_to_local() {
let mut c = Commander::new();
c.set_remote("me@web1".into(), None, None);
assert!(c.entries.is_empty() && c.error.as_deref().unwrap().starts_with("remote (me@web1): no directory info"));
c.set_remote("me@web1".into(), Some("/srv".into()), None);
assert!(c.entries.is_empty() && c.error.is_none(), "loading: nothing stale");
c.set_remote("me@web1".into(), Some("/srv".into()), Some(Ok(vec![e("b.png", false), e(".env", false), e("app", true)])));
assert_eq!(c.visible().iter().map(|e| e.name.as_str()).collect::<Vec<_>>(), [".env", "app", "b.png"]);
c.flags.all = false;
c.relist();
assert_eq!(c.visible().len(), 2, "flags re-sort the remote listing, no local read");
c.selected = 0;
assert_eq!(c.open_selected(), Effect::Cd("/srv/app".into()));
c.selected = 1;
assert_eq!(c.open_selected(), Effect::None);
assert_eq!(c.overlay, Some(Overlay::Preview("/srv/b.png".into())));
c.overlay = None;
c.set_remote("me@web1".into(), Some("/srv/app/x".into()), Some(Ok(vec![])));
c.set_remote("me@web1".into(), Some("/srv/app".into()), None);
c.set_remote("me@web1".into(), Some("/srv".into()), Some(Ok(vec![e("app", true), e("zz", true)])));
assert_eq!(c.selected_entry().unwrap().name, "app");
let t = tempfile::tempdir().unwrap();
std::fs::write(t.path().join("local.txt"), b"").unwrap();
c.set_cwd(Some(t.path().to_path_buf()));
assert!(c.remote.is_none());
assert_eq!(c.visible()[0].name, "local.txt");
}
}
#[cfg(test)]
mod badge_tests {
use super::*;
#[test]
fn every_type_has_its_badge() {
let b = |n: &str| badge_of(n, kind_of(n), "-rw-r--r--");
assert_eq!(badge_of("src", Kind::Dir, "drwxr-xr-x"), Badge::Folder);
for (n, want) in [
("a.png", Badge::Image),
("a.mkv", Badge::Video),
("a.flac", Badge::Audio),
("notes.txt", Badge::Text),
("main.rs", Badge::Code),
("README.md", Badge::Markdown),
("paper.PDF", Badge::Pdf),
("x.tar.gz", Badge::Archive),
("app.exe", Badge::Binary),
("Cargo.toml", Badge::Config),
("a.json", Badge::Config),
("Inter.ttf", Badge::Font),
("sheet.xlsx", Badge::Document),
("a.csv", Badge::Document),
("thing.qqq", Badge::Generic),
] {
assert_eq!(b(n), want, "{n}");
}
assert_eq!(badge_of("mouser", kind_of("mouser"), "-rwxr-xr-x"), Badge::Binary);
assert_eq!(badge_of("Makefile", kind_of("Makefile"), "-rw-r--r--"), Badge::Text);
for x in Badge::ALL {
assert_eq!(Badge::from_index(x.index()), x);
assert!(x.mark().chars().count() <= 3);
}
}
}