Skip to content

Commit

Permalink
refactor(output): extract grid implementation into nls_term_grid crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bydlw98 committed Mar 29, 2024
1 parent fcf33af commit 62a37b2
Show file tree
Hide file tree
Showing 17 changed files with 301 additions and 1,029 deletions.
10 changes: 10 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 @@ -35,6 +35,7 @@ ignore = "0.4.22"
lexopt = "0.3.0"
libc = "0.2.153"
log = "0.4.21"
nls_term_grid = "0.1.0"
once_cell = "1.19.0"
terminal_size = "0.3.0"
unicode-width = "0.1.11"
Expand Down
92 changes: 47 additions & 45 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::os::unix::fs::{FileTypeExt, MetadataExt};
use std::os::windows::fs::MetadataExt;
use std::path::{Path, PathBuf};

use nls_term_grid::{Alignment, GridCell};

use crate::config::{Config, TimestampUsed};
#[cfg(unix)]
use crate::os::unix::*;
Expand Down Expand Up @@ -200,64 +202,64 @@ impl EntryBuf {
&self.file_name_key
}

pub fn file_name_cell(&self, config: &Config) -> DisplayCell {
pub fn file_name_cell(&self, config: &Config) -> GridCell {
match &self.metadata {
Some(metadata) => format_filename(&self.path, &self.file_name, metadata, config),
None => DisplayCell::from(self.file_name.clone()),
None => GridCell::from(self.file_name.clone()),
}
}

#[cfg(unix)]
pub fn ino_cell(&self, config: &Config) -> DisplayCell {
pub fn ino_cell(&self, config: &Config) -> GridCell {
let inode_style = config.theme.inode_style();

match &self.ino {
Some(ino) => DisplayCell::from_num_with_style(*ino, inode_style),
None => DisplayCell::error_cell(Alignment::Right),
Some(ino) => GridCell::from_num_with_style(*ino, inode_style),
None => GridCell::error_cell(Alignment::Right),
}
}

#[cfg(windows)]
pub fn ino_cell(&self, config: &Config) -> DisplayCell {
pub fn ino_cell(&self, config: &Config) -> GridCell {
let inode_style = config.theme.inode_style();

match get_file_id_identifier(&self.path, self.follow_links) {
Ok(file_id) => DisplayCell::from_u128_with_style(file_id, inode_style),
Ok(file_id) => GridCell::from_u128_with_style(file_id, inode_style),
Err(err) => {
eprintln!(
"nls: unable to get inode number of '{}': {}",
self.path.display(),
err
);

DisplayCell::error_cell(Alignment::Right)
GridCell::error_cell(Alignment::Right)
}
}
}

#[cfg(not(any(unix, windows)))]
pub fn ino_cell(&self, config: &Config) -> DisplayCell {
pub fn ino_cell(&self, config: &Config) -> GridCell {
let inode_style = config.theme.inode_style();

match &self.metadata {
Some(_) => DisplayCell::from_ascii_str_with_style('-', inode_style),
None => DisplayCell::error_cell(false),
Some(_) => GridCell::from_ascii_str_with_style('-', inode_style),
None => GridCell::error_cell(false),
}
}

pub fn allocated_size(&self) -> Option<u64> {
self.allocated_size
}

pub fn allocated_size_cell(&self, config: &Config) -> DisplayCell {
pub fn allocated_size_cell(&self, config: &Config) -> GridCell {
match &self.allocated_size {
Some(allocated_size) => format_size(*allocated_size, config),
None => DisplayCell::error_cell(Alignment::Right),
None => GridCell::error_cell(Alignment::Right),
}
}

#[cfg(unix)]
pub fn mode_cell(&self, config: &Config) -> DisplayCell {
pub fn mode_cell(&self, config: &Config) -> GridCell {
match &self.metadata {
Some(metadata) => {
if config.mode_format.is_rwx() {
Expand All @@ -266,12 +268,12 @@ impl EntryBuf {
pwsh_mode_cell(metadata.mode(), &self.file_name, &self.path, config)
}
}
None => DisplayCell::from_ascii_str_with_style("??????????", None),
None => GridCell::from_ascii_str_with_style("??????????", None),
}
}

#[cfg(windows)]
pub fn mode_cell(&self, config: &Config) -> DisplayCell {
pub fn mode_cell(&self, config: &Config) -> GridCell {
if config.mode_format.is_pwsh() {
pwsh_mode_cell(
self.metadata
Expand All @@ -292,97 +294,97 @@ impl EntryBuf {
}

#[cfg(not(any(unix, windows)))]
pub fn mode_cell(&self, config: &Config) -> DisplayCell {
pub fn mode_cell(&self, config: &Config) -> GridCell {
match &self.metadata {
Some(metadata) => {
let file_type = metadata.file_type();
let ls_colors = &config.ls_colors;
if file_type.is_file() {
DisplayCell::from_ascii_str_with_style("-", ls_colors.file_style())
GridCell::from_ascii_str_with_style("-", ls_colors.file_style())
} else if file_type.is_dir() {
DisplayCell::from_ascii_str_with_style("d", ls_colors.dir_style())
GridCell::from_ascii_str_with_style("d", ls_colors.dir_style())
} else if file_type.is_symlink() {
DisplayCell::from_ascii_str_with_style("l", ls_colors.symlink_style())
GridCell::from_ascii_str_with_style("l", ls_colors.symlink_style())
} else {
DisplayCell::from_ascii_str_with_style("?", None)
GridCell::from_ascii_str_with_style("?", None)
}
}
None => DisplayCell::from_ascii_str_with_style("?", None),
None => GridCell::from_ascii_str_with_style("?", None),
}
}

#[cfg(unix)]
pub fn nlink_cell(&self, config: &Config) -> DisplayCell {
pub fn nlink_cell(&self, config: &Config) -> GridCell {
let nlink_style = config.theme.nlink_style();
match &self.metadata {
Some(metadata) => DisplayCell::from_num_with_style(metadata.nlink(), nlink_style),
None => DisplayCell::error_cell(Alignment::Right),
Some(metadata) => GridCell::from_num_with_style(metadata.nlink(), nlink_style),
None => GridCell::error_cell(Alignment::Right),
}
}

#[cfg(windows)]
pub fn nlink_cell(&self, config: &Config) -> DisplayCell {
pub fn nlink_cell(&self, config: &Config) -> GridCell {
self.windows_metadata.nlink_cell(config)
}

#[cfg(not(any(unix, windows)))]
pub fn nlink_cell(&self, config: &Config) -> DisplayCell {
pub fn nlink_cell(&self, config: &Config) -> GridCell {
let nlink_style = config.theme.nlink_style();
match &self.metadata {
Some(_) => DisplayCell::from_ascii_str_with_style('1', nlink_style),
None => DisplayCell::error_cell(Alignment::Right),
Some(_) => GridCell::from_ascii_str_with_style('1', nlink_style),
None => GridCell::error_cell(Alignment::Right),
}
}

#[cfg(unix)]
pub fn owner_cell(&self, config: &Config) -> DisplayCell {
pub fn owner_cell(&self, config: &Config) -> GridCell {
match &self.metadata {
Some(metadata) => get_username_cell_by_uid(metadata.uid(), config),
None => DisplayCell::error_cell(Alignment::Left),
None => GridCell::error_cell(Alignment::Left),
}
}

#[cfg(windows)]
pub fn owner_cell(&self, config: &Config) -> DisplayCell {
pub fn owner_cell(&self, config: &Config) -> GridCell {
self.windows_metadata.owner_cell(config)
}

#[cfg(not(any(unix, windows)))]
pub fn owner_cell(&self, config: &Config) -> DisplayCell {
pub fn owner_cell(&self, config: &Config) -> GridCell {
let owner_style = config.theme.owner_style();
match &self.metadata {
Some(_) => DisplayCell::from_ascii_str_with_style("-", owner_style),
None => DisplayCell::error_cell(Alignment::Left),
Some(_) => GridCell::from_ascii_str_with_style("-", owner_style),
None => GridCell::error_cell(Alignment::Left),
}
}

#[cfg(unix)]
pub fn group_cell(&self, config: &Config) -> DisplayCell {
pub fn group_cell(&self, config: &Config) -> GridCell {
match &self.metadata {
Some(metadata) => get_groupname_cell_by_gid(metadata.gid(), config),
None => DisplayCell::error_cell(Alignment::Left),
None => GridCell::error_cell(Alignment::Left),
}
}

#[cfg(windows)]
pub fn group_cell(&self, config: &Config) -> DisplayCell {
pub fn group_cell(&self, config: &Config) -> GridCell {
self.windows_metadata.group_cell(config)
}

#[cfg(not(any(unix, windows)))]
pub fn group_cell(&self, config: &Config) -> DisplayCell {
pub fn group_cell(&self, config: &Config) -> GridCell {
let group_style = config.theme.group_style();
match &self.metadata {
Some(_) => DisplayCell::from_ascii_str_with_style("-", group_style),
None => DisplayCell::error_cell(true),
Some(_) => GridCell::from_ascii_str_with_style("-", group_style),
None => GridCell::error_cell(true),
}
}

pub fn size(&self) -> Option<u64> {
self.size
}

pub fn size_cell(&self, config: &Config) -> DisplayCell {
pub fn size_cell(&self, config: &Config) -> GridCell {
#[cfg(unix)]
if let Some(metadata) = &self.metadata {
let file_type = metadata.file_type();
Expand All @@ -393,18 +395,18 @@ impl EntryBuf {

match &self.size {
Some(size) => format_size(*size, config),
None => DisplayCell::error_cell(Alignment::Right),
None => GridCell::error_cell(Alignment::Right),
}
}

pub fn timestamp(&self) -> Option<i64> {
self.timestamp
}

pub fn timestamp_cell(&self, config: &Config) -> DisplayCell {
pub fn timestamp_cell(&self, config: &Config) -> GridCell {
match &self.timestamp {
Some(timestamp) => format_timestamp(*timestamp, config),
None => DisplayCell::error_cell(Alignment::Left),
None => GridCell::error_cell(Alignment::Left),
}
}
}
21 changes: 11 additions & 10 deletions src/os/unix/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use std::mem;
use std::ptr;
use std::sync::Mutex;

use nls_term_grid::GridCell;
use once_cell::sync::Lazy;

use crate::config::Config;
use crate::output::DisplayCell;
use crate::output::GridCellExts;

pub fn get_username_cell_by_uid(uid: u32, config: &Config) -> DisplayCell {
pub fn get_username_cell_by_uid(uid: u32, config: &Config) -> GridCell {
static USERS_CACHE: Lazy<Mutex<Vec<Account>>> = Lazy::new(|| Mutex::new(Vec::with_capacity(2)));

match USERS_CACHE.lock() {
Expand Down Expand Up @@ -39,7 +40,7 @@ pub fn get_username_cell_by_uid(uid: u32, config: &Config) -> DisplayCell {
}
}

pub fn get_groupname_cell_by_gid(gid: u32, config: &Config) -> DisplayCell {
pub fn get_groupname_cell_by_gid(gid: u32, config: &Config) -> GridCell {
static GROUPS_CACHE: Lazy<Mutex<Vec<Account>>> =
Lazy::new(|| Mutex::new(Vec::with_capacity(2)));

Expand Down Expand Up @@ -71,7 +72,7 @@ pub fn get_groupname_cell_by_gid(gid: u32, config: &Config) -> DisplayCell {

#[derive(Debug, Default)]
struct Account {
name_cell: DisplayCell,
name_cell: GridCell,
numeric_id: u32,
}

Expand All @@ -81,7 +82,7 @@ impl Account {

if config.numeric_uid_gid {
Self {
name_cell: DisplayCell::from_num_with_style(uid as u64, owner_style),
name_cell: GridCell::from_num_with_style(uid as u64, owner_style),
numeric_id: uid,
}
} else {
Expand All @@ -99,12 +100,12 @@ impl Account {
let username_string = CStr::from_ptr(pwd.pw_name).to_string_lossy();

Self {
name_cell: DisplayCell::from_str_with_style(&username_string, owner_style),
name_cell: GridCell::from_str_with_style(&username_string, owner_style),
numeric_id: uid,
}
} else {
Self {
name_cell: DisplayCell::from_num_with_style(uid as u64, owner_style),
name_cell: GridCell::from_num_with_style(uid as u64, owner_style),
numeric_id: uid,
}
}
Expand All @@ -117,7 +118,7 @@ impl Account {

if config.numeric_uid_gid {
Self {
name_cell: DisplayCell::from_num_with_style(gid as u64, group_style),
name_cell: GridCell::from_num_with_style(gid as u64, group_style),
numeric_id: gid,
}
} else {
Expand All @@ -134,15 +135,15 @@ impl Account {
if return_code == 0 && (result == &mut grp) {
let groupname_string = CStr::from_ptr(grp.gr_name).to_string_lossy();
let groupname_cell =
DisplayCell::from_str_with_style(&groupname_string, group_style);
GridCell::from_str_with_style(&groupname_string, group_style);

Self {
name_cell: groupname_cell,
numeric_id: gid,
}
} else {
Self {
name_cell: DisplayCell::from_num_with_style(gid as u64, group_style),
name_cell: GridCell::from_num_with_style(gid as u64, group_style),
numeric_id: gid,
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/os/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub use mode::{pwsh_mode_cell, rwx_mode_cell};
use std::fs::Metadata;
use std::os::unix::fs::MetadataExt;

use nls_term_grid::{Alignment, GridCell};

use sys_prelude::*;

use crate::config::{AllocatedSizeBlocks, Config};
use crate::output::{Alignment, DisplayCell};
use crate::output::GridCellExts;

pub fn get_allocated_size(metadata: &Metadata, config: &Config) -> u64 {
match config.allocated_size_blocks {
Expand All @@ -23,14 +25,14 @@ pub fn get_allocated_size(metadata: &Metadata, config: &Config) -> u64 {
}
}

pub fn format_rdev(rdev: u64, config: &Config) -> DisplayCell {
pub fn format_rdev(rdev: u64, config: &Config) -> GridCell {
let raw_rdev = rdev as libc::dev_t;
let major = unsafe { c::major(raw_rdev) };
let minor = unsafe { c::minor(raw_rdev) };
let major_minor_string = format!("{},{:>4}", major, minor);
let rdev_style = config.theme.size_style();

let mut rdev_cell = DisplayCell::from_ascii_str_with_style(&major_minor_string, rdev_style);
let mut rdev_cell = GridCell::from_ascii_str_with_style(&major_minor_string, rdev_style);
rdev_cell.alignment = Alignment::Right;

rdev_cell
Expand Down

0 comments on commit 62a37b2

Please sign in to comment.