Skip to content

Commit

Permalink
linemode have more options
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzzzzzt committed Jul 31, 2023
1 parent d899e06 commit e530296
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ unicode-width = "^0"
whoami = "^1"
xdg = "^2"
walkdir = "^2"
bitflags = { version = "^2", features = ["serde"] }

[dependencies.nix]
version = "^0"
Expand Down
5 changes: 5 additions & 0 deletions config/joshuto.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ tilde_in_titlebar = true
# none, absolute, relative
line_number_style = "none"

# size, mtime, user, gourp, perm. can be combined with |.
# `none` to disable, `all` to enable all
# all and none can't be combined with other options
linemode = "size"

[display.sort]
# lexical, mtime, natural, size, ext
sort_method = "natural"
Expand Down
5 changes: 4 additions & 1 deletion config/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ keymap = [

{ keys = ["m", "s"], command = "linemode size" },
{ keys = ["m", "m"], command = "linemode mtime" },
{ keys = ["m", "M"], command = "linemode sizemtime" },
{ keys = ["m", "M"], command = "linemode size | mtime" },
{ keys = ["m", "u"], command = "linemode user" },
{ keys = ["m", "U"], command = "linemode user | group" },
{ keys = ["m", "p"], command = "linemode perm" },

{ keys = ["g", "r"], command = "cd /" },
{ keys = ["g", "c"], command = "cd ~/.config" },
Expand Down
10 changes: 10 additions & 0 deletions docs/configuration/joshuto.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ tilde_in_titlebar = true
# - relative
line_number_style = "none"

# Options include
# - size
# - mtime
# - user
# - group
# - perm
# - none (can't be combined with other options)
# - all (can't be combined with other options)
linemode = "size"

# Configurations related to file sorting
[display.sort]
# Options include
Expand Down
6 changes: 5 additions & 1 deletion src/config/general/display_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub struct DisplayOptionRaw {

#[serde(default)]
pub line_number_style: String,

#[serde(default)]
pub linemode: LineMode,
}

impl std::default::Default for DisplayOptionRaw {
Expand All @@ -75,6 +78,7 @@ impl std::default::Default for DisplayOptionRaw {
sort_options: SortOptionRaw::default(),
tilde_in_titlebar: true,
line_number_style: "none".to_string(),
linemode: LineMode::default(),
}
}
}
Expand Down Expand Up @@ -125,7 +129,7 @@ impl From<DisplayOptionRaw> for DisplayOption {
default_tab_display_option: TabDisplayOption {
sort_options: raw.sort_options.into(),
// todo: make default line mode configurable
linemode: LineMode::Size,
linemode: raw.linemode,
..Default::default()
},
}
Expand Down
69 changes: 50 additions & 19 deletions src/config/option/linemodes.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
use serde_derive::Deserialize;

use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};

#[derive(Clone, Debug, Copy, Default)]
pub enum LineMode {
#[default]
Size,
MTime,
SizeMTime,
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(transparent)]
pub struct LineMode: u8 {
const size = 1 << 0;
const mtime = 1 << 1;
const user = 1 << 2;
const group = 1 << 3;
const perm = 1 << 4;
}
}

impl Default for LineMode {
fn default() -> Self {
Self::size
}
}

impl LineMode {
pub fn from_string(name: &str) -> JoshutoResult<LineMode> {
match name {
"size" => Ok(LineMode::Size),
"mtime" => Ok(LineMode::MTime),
"sizemtime" => Ok(LineMode::SizeMTime),
_ => Err(JoshutoError::new(
JoshutoErrorKind::InvalidParameters,
format!("Linemode '{}' unknown.", name),
)),
"all" => Ok(LineMode::all()),
"none" => Ok(LineMode::empty()),
_ => {
let mut flags = name.split('|');

let mut linemode = LineMode::empty();

flags.try_for_each(|flag| {
match flag.trim() {
"size" => linemode |= LineMode::size,
"mtime" => linemode |= LineMode::mtime,
"user" => linemode |= LineMode::user,
"group" => linemode |= LineMode::group,
"perm" => linemode |= LineMode::perm,
flag => {
return Err(JoshutoError::new(
JoshutoErrorKind::InvalidParameters,
format!("Linemode '{}' unknown.", flag),
))
}
}

Ok(())
})?;

Ok(linemode)
}
}
}

pub fn as_str(&self) -> &'static str {
match self {
Self::Size => "size",
Self::MTime => "mtime",
Self::SizeMTime => "sizemtime",
}
pub fn as_string(&self) -> String {
self.iter_names()
.map(|f| f.0)
.collect::<Vec<_>>()
.join(" | ")
}
}
11 changes: 7 additions & 4 deletions src/key_command/impl_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ impl CommandComment for Command {
// These comments are displayed at the help page
fn comment(&self) -> &'static str {
match self {
Self::SetLineMode(linemode) => match linemode {
LineMode::Size => "Show files with size",
LineMode::MTime => "Show files with modified time",
LineMode::SizeMTime => "Show files with size and modified time",
Self::SetLineMode(linemode) => match *linemode {
LineMode::size => "Show files with size",
LineMode::mtime => "Show files with modified time",
LineMode::user => "Show files with user",
LineMode::group => "Show files with group",
LineMode::perm => "Show files with permission",
_ => "Show files with multi-attribution"
},
Self::Escape => "Escape from visual mode (cancel)",
Self::BulkRename => "Bulk rename",
Expand Down
2 changes: 1 addition & 1 deletion src/key_command/impl_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl std::fmt::Display for Command {
Self::CursorMoveUp { offset } => write!(f, "{} {}", self.command(), offset),
Self::CursorMoveDown { offset } => write!(f, "{} {}", self.command(), offset),

Self::SetLineMode(mode) => write!(f, "{} {}", self.command(), mode.as_str()),
Self::SetLineMode(mode) => write!(f, "{} {}", self.command(), mode.as_string()),

Self::ParentCursorMoveUp { offset } => write!(f, "{} {}", self.command(), offset),
Self::ParentCursorMoveDown { offset } => write!(f, "{} {}", self.command(), offset),
Expand Down
23 changes: 13 additions & 10 deletions src/ui/widgets/tui_dirlist_detailed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use ratatui::widgets::Widget;

use crate::config::option::{DisplayOption, LineMode, LineNumberStyle, TabDisplayOption};
use crate::fs::{FileType, JoshutoDirEntry, JoshutoDirList, LinkType};
use crate::util::format;
use crate::util::string::UnicodeTruncate;
use crate::util::style;
use crate::util::{format, unix};
use unicode_width::UnicodeWidthStr;

const MIN_LEFT_LABEL_WIDTH: i32 = 15;
Expand Down Expand Up @@ -144,15 +144,18 @@ fn print_entry(
let right_label_original = format!(
" {}{} ",
symlink_string,
match linemode {
LineMode::Size => get_entry_size_string(entry),
LineMode::MTime => format::mtime_to_string(entry.metadata.modified()),
LineMode::SizeMTime => format!(
"{} {}",
get_entry_size_string(entry),
format::mtime_to_string(entry.metadata.modified())
),
}
linemode
.iter_names()
.map(|f| match f.0 {
"size" => get_entry_size_string(entry),
"mtime" => format::mtime_to_string(entry.metadata.modified()),
"user" => unix::uid_to_string(entry.metadata.uid).unwrap_or("unknown".into()),
"group" => unix::gid_to_string(entry.metadata.gid).unwrap_or("unknown".into()),
"perm" => unix::mode_to_string(entry.metadata.mode),
_ => unreachable!(),
})
.collect::<Vec<_>>()
.join(" ")
);

// draw prefix first
Expand Down

0 comments on commit e530296

Please sign in to comment.