mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
ls: further refactor --color and classification
This commit is contained in:
parent
e382f7fa83
commit
ff39538375
2 changed files with 19 additions and 73 deletions
|
@ -35,8 +35,6 @@ use std::{cmp::Reverse, process::exit};
|
||||||
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
|
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
|
||||||
use time::{strftime, Timespec};
|
use time::{strftime, Timespec};
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use unicode_width::UnicodeWidthStr;
|
|
||||||
#[cfg(unix)]
|
|
||||||
use uucore::libc::{mode_t, S_IXGRP, S_IXOTH, S_IXUSR};
|
use uucore::libc::{mode_t, S_IXGRP, S_IXOTH, S_IXUSR};
|
||||||
|
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
@ -1445,45 +1443,6 @@ fn get_file_name(name: &Path, strip: Option<&Path>) -> String {
|
||||||
name.to_string_lossy().into_owned()
|
name.to_string_lossy().into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[cfg(not(unix))]
|
|
||||||
// fn display_file_name(
|
|
||||||
// path: &Path,
|
|
||||||
// strip: Option<&Path>,
|
|
||||||
// metadata: &Metadata,
|
|
||||||
// config: &Config,
|
|
||||||
// ) -> Cell {
|
|
||||||
// let mut name = escape_name(get_file_name(path, strip), &config.quoting_style);
|
|
||||||
// let file_type = metadata.file_type();
|
|
||||||
|
|
||||||
// match config.indicator_style {
|
|
||||||
// IndicatorStyle::Classify | IndicatorStyle::FileType => {
|
|
||||||
// if file_type.is_dir() {
|
|
||||||
// name.push('/');
|
|
||||||
// }
|
|
||||||
// if file_type.is_symlink() {
|
|
||||||
// name.push('@');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// IndicatorStyle::Slash => {
|
|
||||||
// if file_type.is_dir() {
|
|
||||||
// name.push('/');
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// _ => (),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if config.format == Format::Long && metadata.file_type().is_symlink() {
|
|
||||||
// if let Ok(target) = path.read_link() {
|
|
||||||
// // We don't bother updating width here because it's not used for long listings
|
|
||||||
// let target_name = target.to_string_lossy().to_string();
|
|
||||||
// name.push_str(" -> ");
|
|
||||||
// name.push_str(&target_name);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// name.into()
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
macro_rules! has {
|
macro_rules! has {
|
||||||
($mode:expr, $perm:expr) => {
|
($mode:expr, $perm:expr) => {
|
||||||
|
@ -1491,42 +1450,32 @@ macro_rules! has {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
fn classify_file(md: &Metadata) -> Option<char> {
|
fn classify_file(md: &Metadata) -> Option<char> {
|
||||||
let file_type = md.file_type();
|
let file_type = md.file_type();
|
||||||
|
|
||||||
|
#[allow(clippy::clippy::collapsible_else_if)]
|
||||||
if file_type.is_dir() {
|
if file_type.is_dir() {
|
||||||
Some('/')
|
Some('/')
|
||||||
} else if file_type.is_symlink() {
|
} else if file_type.is_symlink() {
|
||||||
Some('@')
|
Some('@')
|
||||||
} else if file_type.is_socket() {
|
} else {
|
||||||
Some('=')
|
#[cfg(unix)]
|
||||||
} else if file_type.is_fifo() {
|
{
|
||||||
Some('|')
|
if file_type.is_socket() {
|
||||||
} else if file_type.is_file() {
|
Some('=')
|
||||||
let mode = md.mode() as mode_t;
|
} else if file_type.is_fifo() {
|
||||||
if has!(mode, S_IXUSR | S_IXGRP | S_IXOTH) {
|
Some('|')
|
||||||
Some('*')
|
} else if file_type.is_file() || has!(md.mode(), S_IXUSR | S_IXGRP | S_IXOTH) {
|
||||||
} else {
|
Some('*')
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
#[cfg(not(unix))]
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(unix))]
|
|
||||||
fn classify_file(md: &Metadata) -> Option<char> {
|
|
||||||
let file_type = md.file_type();
|
|
||||||
if file_type.is_dir() {
|
|
||||||
Some('/')
|
|
||||||
} else if file_type.is_symlink() {
|
|
||||||
Some('@')
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity)]
|
|
||||||
fn display_file_name(
|
fn display_file_name(
|
||||||
path: &Path,
|
path: &Path,
|
||||||
strip: Option<&Path>,
|
strip: Option<&Path>,
|
||||||
|
@ -1541,7 +1490,7 @@ fn display_file_name(
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ls_colors) = &config.color {
|
if let Some(ls_colors) = &config.color {
|
||||||
name = color_name(&ls_colors, path, name, metadata).to_string();
|
name = color_name(&ls_colors, path, name, metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.indicator_style != IndicatorStyle::None {
|
if config.indicator_style != IndicatorStyle::None {
|
||||||
|
@ -1589,11 +1538,8 @@ fn display_file_name(
|
||||||
|
|
||||||
fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String {
|
fn color_name(ls_colors: &LsColors, path: &Path, name: String, md: &Metadata) -> String {
|
||||||
match ls_colors.style_for_path_with_metadata(path, Some(&md)) {
|
match ls_colors.style_for_path_with_metadata(path, Some(&md)) {
|
||||||
Some(style) => {
|
Some(style) => style.to_ansi_term_style().paint(name).to_string(),
|
||||||
dbg!(style);
|
None => name,
|
||||||
style.to_ansi_term_style().paint(name).to_string()
|
|
||||||
}
|
|
||||||
None => dbg!(name),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -622,7 +622,7 @@ fn test_ls_recursive() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ls_ls_color() {
|
fn test_ls_color() {
|
||||||
let scene = TestScenario::new(util_name!());
|
let scene = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &scene.fixtures;
|
||||||
at.mkdir("a");
|
at.mkdir("a");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue