1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

dircolors: move the FILE_ATTRIBUTE_CODES datastructures and use it

This commit is contained in:
Sylvestre Ledru 2023-12-02 11:00:55 +01:00
parent 13a4c9114c
commit 0e8c171c80
5 changed files with 273 additions and 233 deletions

View file

@ -16,7 +16,7 @@ path = "src/dircolors.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true }
uucore = { workspace = true, features = ["colors"] }
[[bin]]
name = "dircolors"

View file

@ -12,6 +12,7 @@ use std::io::{BufRead, BufReader};
use std::path::Path;
use clap::{crate_version, Arg, ArgAction, Command};
use uucore::colors::FILE_ATTRIBUTE_CODES;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::{help_about, help_section, help_usage};
@ -276,7 +277,6 @@ enum ParseState {
Pass,
}
use std::collections::HashMap;
use uucore::{format_usage, parse_glob};
#[allow(clippy::cognitive_complexity)]
@ -294,45 +294,6 @@ where
OutputFmt::Unknown => unreachable!(),
}
let mut table: HashMap<&str, &str> = HashMap::with_capacity(48);
table.insert("normal", "no");
table.insert("norm", "no");
table.insert("file", "fi");
table.insert("reset", "rs");
table.insert("dir", "di");
table.insert("lnk", "ln");
table.insert("link", "ln");
table.insert("symlink", "ln");
table.insert("orphan", "or");
table.insert("missing", "mi");
table.insert("fifo", "pi");
table.insert("pipe", "pi");
table.insert("sock", "so");
table.insert("blk", "bd");
table.insert("block", "bd");
table.insert("chr", "cd");
table.insert("char", "cd");
table.insert("door", "do");
table.insert("exec", "ex");
table.insert("left", "lc");
table.insert("leftcode", "lc");
table.insert("right", "rc");
table.insert("rightcode", "rc");
table.insert("end", "ec");
table.insert("endcode", "ec");
table.insert("suid", "su");
table.insert("setuid", "su");
table.insert("sgid", "sg");
table.insert("setgid", "sg");
table.insert("sticky", "st");
table.insert("other_writable", "ow");
table.insert("owr", "ow");
table.insert("sticky_other_writable", "tw");
table.insert("owt", "tw");
table.insert("capability", "ca");
table.insert("multihardlink", "mh");
table.insert("clrtoeol", "cl");
let term = env::var("TERM").unwrap_or_else(|_| "none".to_owned());
let term = term.as_str();
@ -384,7 +345,7 @@ where
}
} else if lower == "options" || lower == "color" || lower == "eightbit" {
// Slackware only. Ignore
} else if let Some(s) = table.get(lower.as_str()) {
} else if let Some(s) = FILE_ATTRIBUTE_CODES.get(lower.as_str()) {
if *fmt == OutputFmt::Display {
result.push_str(format!("\x1b[{val}m{s}\t{val}\x1b[0m\n").as_str());
} else {

View file

@ -72,6 +72,7 @@ windows-sys = { workspace = true, optional = true, default-features = false, fea
default = []
# * non-default features
backup-control = []
colors = []
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
entries = ["libc"]
fs = ["dunce", "libc", "winapi-util", "windows-sys"]

View file

@ -3,6 +3,9 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use once_cell::sync::Lazy;
use std::collections::HashMap;
/* The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
* slackware version of dircolors) are recognized but ignored.
* Global config options can be specified before TERM or COLORTERM entries
@ -11,7 +14,9 @@
* COLORTERM ?*
*/
static TERMS: &[&str] = &[
pub static TERMS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
let mut m = HashMap::new();
[
"Eterm",
"ansi",
"*color*",
@ -37,7 +42,13 @@ static TERMS: &[&str] = &[
"tmux*",
"vt100",
"xterm*",
];
]
.iter()
.for_each(|&term| {
m.insert(term, "");
});
m
});
/*
# Below are the color init strings for the basic file types.
@ -53,7 +64,10 @@ static TERMS: &[&str] = &[
#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
*/
static FILE_TYPES: &[(&str, &str)] = &[
// FILE_TYPES with Lazy initialization
pub static FILE_TYPES: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
let mut m = HashMap::new();
[
("RESET", "0"), // reset to "normal" color
("DIR", "01;34"), // directory
("LINK", "01;36"), // symbolic link
@ -72,14 +86,22 @@ static FILE_TYPES: &[(&str, &str)] = &[
("OTHER_WRITABLE", "34;42"), // dir that is other-writable (o+w) and not sticky
("STICKY", "37;44"), // dir with the sticky bit set (+t) and not other-writable
("EXEC", "01;32"), // files with execute permission
];
]
.iter()
.for_each(|&(k, v)| {
m.insert(k, v);
});
m
});
/*
# List any file extensions like '.gz' or '.tar' that you would like ls
# to color below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
*/
static FILE_COLORS: &[(&str, &str)] = &[
pub static FILE_COLORS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
let mut m = HashMap::new();
[
// Executables (Windows)
(".cmd", "01;32"),
(".exe", "01;32"),
@ -222,4 +244,58 @@ static FILE_COLORS: &[(&str, &str)] = &[
(".rpmnew", "00;90"),
(".rpmorig", "00;90"),
(".rpmsave", "00;90"),
];
]
.iter()
.for_each(|&(k, v)| {
m.insert(k, v);
});
m
});
pub static FILE_ATTRIBUTE_CODES: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
let mut m = HashMap::new();
[
("normal", "no"),
("norm", "no"),
("file", "fi"),
("reset", "rs"),
("dir", "di"),
("lnk", "ln"),
("link", "ln"),
("symlink", "ln"),
("orphan", "or"),
("missing", "mi"),
("fifo", "pi"),
("pipe", "pi"),
("sock", "so"),
("blk", "bd"),
("block", "bd"),
("chr", "cd"),
("char", "cd"),
("door", "do"),
("exec", "ex"),
("left", "lc"),
("leftcode", "lc"),
("right", "rc"),
("rightcode", "rc"),
("end", "ec"),
("endcode", "ec"),
("suid", "su"),
("setuid", "su"),
("sgid", "sg"),
("setgid", "sg"),
("sticky", "st"),
("other_writable", "ow"),
("owr", "ow"),
("sticky_other_writable", "tw"),
("owt", "tw"),
("capability", "ca"),
("multihardlink", "mh"),
("clrtoeol", "cl"),
]
.iter()
.for_each(|&(k, v)| {
m.insert(k, v);
});
m
});

View file

@ -35,6 +35,8 @@ pub use crate::parser::shortcut_value_parser;
// * feature-gated modules
#[cfg(feature = "backup-control")]
pub use crate::features::backup_control;
#[cfg(feature = "colors")]
pub use crate::features::colors;
#[cfg(feature = "encoding")]
pub use crate::features::encoding;
#[cfg(feature = "format")]