mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
dircolors: implement --print-ls-colors
This commit is contained in:
parent
7861cc9dca
commit
6d82523a99
4 changed files with 217 additions and 11 deletions
|
@ -4,6 +4,7 @@ Create the test fixtures by writing the output of the GNU dircolors commands to
|
|||
|
||||
```
|
||||
$ dircolors --print-database > /PATH_TO_COREUTILS/tests/fixtures/dircolors/internal.expected
|
||||
$ dircolors --print-ls-colors > /PATH_TO_COREUTILS/tests/fixtures/dircolors/ls_colors.expected
|
||||
$ dircolors -b > /PATH_TO_COREUTILS/tests/fixtures/dircolors/bash_def.expected
|
||||
$ dircolors -c > /PATH_TO_COREUTILS/tests/fixtures/dircolors/csh_def.expected
|
||||
```
|
||||
|
|
|
@ -21,6 +21,7 @@ mod options {
|
|||
pub const BOURNE_SHELL: &str = "bourne-shell";
|
||||
pub const C_SHELL: &str = "c-shell";
|
||||
pub const PRINT_DATABASE: &str = "print-database";
|
||||
pub const PRINT_LS_COLORS: &str = "print-ls-colors";
|
||||
pub const FILE: &str = "FILE";
|
||||
}
|
||||
|
||||
|
@ -39,6 +40,7 @@ use self::colors::INTERNAL_DB;
|
|||
pub enum OutputFmt {
|
||||
Shell,
|
||||
CShell,
|
||||
Display,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
|
@ -76,7 +78,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
// clap provides .conflicts_with / .conflicts_with_all, but we want to
|
||||
// manually handle conflicts so we can match the output of GNU coreutils
|
||||
if (matches.is_present(options::C_SHELL) || matches.is_present(options::BOURNE_SHELL))
|
||||
&& matches.is_present(options::PRINT_DATABASE)
|
||||
&& (matches.is_present(options::PRINT_DATABASE)
|
||||
|| matches.is_present(options::PRINT_LS_COLORS))
|
||||
{
|
||||
return Err(UUsageError::new(
|
||||
1,
|
||||
|
@ -85,6 +88,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
));
|
||||
}
|
||||
|
||||
if matches.is_present(options::PRINT_DATABASE) && matches.is_present(options::PRINT_LS_COLORS) {
|
||||
return Err(UUsageError::new(
|
||||
1,
|
||||
"options --print-database and --print-ls-colors are mutually exclusive",
|
||||
));
|
||||
}
|
||||
|
||||
if matches.is_present(options::PRINT_DATABASE) {
|
||||
if !files.is_empty() {
|
||||
return Err(UUsageError::new(
|
||||
|
@ -100,12 +110,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let mut out_format = OutputFmt::Unknown;
|
||||
if matches.is_present(options::C_SHELL) {
|
||||
out_format = OutputFmt::CShell;
|
||||
let mut out_format = if matches.is_present(options::C_SHELL) {
|
||||
OutputFmt::CShell
|
||||
} else if matches.is_present(options::BOURNE_SHELL) {
|
||||
out_format = OutputFmt::Shell;
|
||||
}
|
||||
OutputFmt::Shell
|
||||
} else if matches.is_present(options::PRINT_LS_COLORS) {
|
||||
OutputFmt::Display
|
||||
} else {
|
||||
OutputFmt::Unknown
|
||||
};
|
||||
|
||||
if out_format == OutputFmt::Unknown {
|
||||
match guess_syntax() {
|
||||
|
@ -186,6 +199,12 @@ pub fn uu_app<'a>() -> Command<'a> {
|
|||
.help("print the byte counts")
|
||||
.display_order(3),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(options::PRINT_LS_COLORS)
|
||||
.long("print-ls-colors")
|
||||
.help("output fully escaped colors for display")
|
||||
.display_order(4),
|
||||
)
|
||||
.arg(
|
||||
Arg::new(options::FILE)
|
||||
.hide(true)
|
||||
|
@ -254,6 +273,7 @@ enum ParseState {
|
|||
Continue,
|
||||
Pass,
|
||||
}
|
||||
|
||||
use std::collections::HashMap;
|
||||
use uucore::{format_usage, InvalidEncodingHandling};
|
||||
|
||||
|
@ -262,11 +282,12 @@ where
|
|||
T: IntoIterator,
|
||||
T::Item: Borrow<str>,
|
||||
{
|
||||
// 1440 > $(dircolors | wc -m)
|
||||
let mut result = String::with_capacity(1440);
|
||||
// 1790 > $(dircolors | wc -m)
|
||||
let mut result = String::with_capacity(1790);
|
||||
match fmt {
|
||||
OutputFmt::Shell => result.push_str("LS_COLORS='"),
|
||||
OutputFmt::CShell => result.push_str("setenv LS_COLORS '"),
|
||||
OutputFmt::Display => (),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
|
@ -345,13 +366,25 @@ where
|
|||
}
|
||||
if state != ParseState::Pass {
|
||||
if key.starts_with('.') {
|
||||
result.push_str(format!("*{}={}:", key, val).as_str());
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{1}m*{0}\t{1}\x1b[0m\n", key, val).as_str());
|
||||
} else {
|
||||
result.push_str(format!("*{}={}:", key, val).as_str());
|
||||
}
|
||||
} else if key.starts_with('*') {
|
||||
result.push_str(format!("{}={}:", key, val).as_str());
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{1}m{0}\t{1}\x1b[0m\n", key, val).as_str());
|
||||
} else {
|
||||
result.push_str(format!("{}={}:", key, val).as_str());
|
||||
}
|
||||
} else if lower == "options" || lower == "color" || lower == "eightbit" {
|
||||
// Slackware only. Ignore
|
||||
} else if let Some(s) = table.get(lower.as_str()) {
|
||||
result.push_str(format!("{}={}:", s, val).as_str());
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{1}m{0}\t{1}\x1b[0m\n", s, val).as_str());
|
||||
} else {
|
||||
result.push_str(format!("{}={}:", s, val).as_str());
|
||||
}
|
||||
} else {
|
||||
return Err(format!(
|
||||
"{}:{}: unrecognized keyword {}",
|
||||
|
@ -367,6 +400,10 @@ where
|
|||
match fmt {
|
||||
OutputFmt::Shell => result.push_str("';\nexport LS_COLORS"),
|
||||
OutputFmt::CShell => result.push('\''),
|
||||
OutputFmt::Display => {
|
||||
// remove latest "\n"
|
||||
result.pop();
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
|
|
|
@ -62,6 +62,14 @@ fn test_internal_db() {
|
|||
.stdout_is_fixture("internal.expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ls_colors() {
|
||||
new_ucmd!()
|
||||
.arg("--print-ls-colors")
|
||||
.run()
|
||||
.stdout_is_fixture("ls_colors.expected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bash_default() {
|
||||
new_ucmd!()
|
||||
|
@ -109,6 +117,18 @@ fn test_exclusive_option() {
|
|||
.arg("-cp")
|
||||
.fails()
|
||||
.stderr_contains("mutually exclusive");
|
||||
new_ucmd!()
|
||||
.args(&["-b", "--print-ls-colors"])
|
||||
.fails()
|
||||
.stderr_contains("mutually exclusive");
|
||||
new_ucmd!()
|
||||
.args(&["-c", "--print-ls-colors"])
|
||||
.fails()
|
||||
.stderr_contains("mutually exclusive");
|
||||
new_ucmd!()
|
||||
.args(&["-p", "--print-ls-colors"])
|
||||
.fails()
|
||||
.stderr_contains("mutually exclusive");
|
||||
}
|
||||
|
||||
fn test_helper(file_name: &str, term: &str) {
|
||||
|
|
148
tests/fixtures/dircolors/ls_colors.expected
vendored
Normal file
148
tests/fixtures/dircolors/ls_colors.expected
vendored
Normal file
|
@ -0,0 +1,148 @@
|
|||
[0mrs 0[0m
|
||||
[01;34mdi 01;34[0m
|
||||
[01;36mln 01;36[0m
|
||||
[00mmh 00[0m
|
||||
[40;33mpi 40;33[0m
|
||||
[01;35mso 01;35[0m
|
||||
[01;35mdo 01;35[0m
|
||||
[40;33;01mbd 40;33;01[0m
|
||||
[40;33;01mcd 40;33;01[0m
|
||||
[40;31;01mor 40;31;01[0m
|
||||
[00mmi 00[0m
|
||||
[37;41msu 37;41[0m
|
||||
[30;43msg 30;43[0m
|
||||
[00mca 00[0m
|
||||
[30;42mtw 30;42[0m
|
||||
[34;42mow 34;42[0m
|
||||
[37;44mst 37;44[0m
|
||||
[01;32mex 01;32[0m
|
||||
[01;31m*.tar 01;31[0m
|
||||
[01;31m*.tgz 01;31[0m
|
||||
[01;31m*.arc 01;31[0m
|
||||
[01;31m*.arj 01;31[0m
|
||||
[01;31m*.taz 01;31[0m
|
||||
[01;31m*.lha 01;31[0m
|
||||
[01;31m*.lz4 01;31[0m
|
||||
[01;31m*.lzh 01;31[0m
|
||||
[01;31m*.lzma 01;31[0m
|
||||
[01;31m*.tlz 01;31[0m
|
||||
[01;31m*.txz 01;31[0m
|
||||
[01;31m*.tzo 01;31[0m
|
||||
[01;31m*.t7z 01;31[0m
|
||||
[01;31m*.zip 01;31[0m
|
||||
[01;31m*.z 01;31[0m
|
||||
[01;31m*.dz 01;31[0m
|
||||
[01;31m*.gz 01;31[0m
|
||||
[01;31m*.lrz 01;31[0m
|
||||
[01;31m*.lz 01;31[0m
|
||||
[01;31m*.lzo 01;31[0m
|
||||
[01;31m*.xz 01;31[0m
|
||||
[01;31m*.zst 01;31[0m
|
||||
[01;31m*.tzst 01;31[0m
|
||||
[01;31m*.bz2 01;31[0m
|
||||
[01;31m*.bz 01;31[0m
|
||||
[01;31m*.tbz 01;31[0m
|
||||
[01;31m*.tbz2 01;31[0m
|
||||
[01;31m*.tz 01;31[0m
|
||||
[01;31m*.deb 01;31[0m
|
||||
[01;31m*.rpm 01;31[0m
|
||||
[01;31m*.jar 01;31[0m
|
||||
[01;31m*.war 01;31[0m
|
||||
[01;31m*.ear 01;31[0m
|
||||
[01;31m*.sar 01;31[0m
|
||||
[01;31m*.rar 01;31[0m
|
||||
[01;31m*.alz 01;31[0m
|
||||
[01;31m*.ace 01;31[0m
|
||||
[01;31m*.zoo 01;31[0m
|
||||
[01;31m*.cpio 01;31[0m
|
||||
[01;31m*.7z 01;31[0m
|
||||
[01;31m*.rz 01;31[0m
|
||||
[01;31m*.cab 01;31[0m
|
||||
[01;31m*.wim 01;31[0m
|
||||
[01;31m*.swm 01;31[0m
|
||||
[01;31m*.dwm 01;31[0m
|
||||
[01;31m*.esd 01;31[0m
|
||||
[01;35m*.avif 01;35[0m
|
||||
[01;35m*.jpg 01;35[0m
|
||||
[01;35m*.jpeg 01;35[0m
|
||||
[01;35m*.mjpg 01;35[0m
|
||||
[01;35m*.mjpeg 01;35[0m
|
||||
[01;35m*.gif 01;35[0m
|
||||
[01;35m*.bmp 01;35[0m
|
||||
[01;35m*.pbm 01;35[0m
|
||||
[01;35m*.pgm 01;35[0m
|
||||
[01;35m*.ppm 01;35[0m
|
||||
[01;35m*.tga 01;35[0m
|
||||
[01;35m*.xbm 01;35[0m
|
||||
[01;35m*.xpm 01;35[0m
|
||||
[01;35m*.tif 01;35[0m
|
||||
[01;35m*.tiff 01;35[0m
|
||||
[01;35m*.png 01;35[0m
|
||||
[01;35m*.svg 01;35[0m
|
||||
[01;35m*.svgz 01;35[0m
|
||||
[01;35m*.mng 01;35[0m
|
||||
[01;35m*.pcx 01;35[0m
|
||||
[01;35m*.mov 01;35[0m
|
||||
[01;35m*.mpg 01;35[0m
|
||||
[01;35m*.mpeg 01;35[0m
|
||||
[01;35m*.m2v 01;35[0m
|
||||
[01;35m*.mkv 01;35[0m
|
||||
[01;35m*.webm 01;35[0m
|
||||
[01;35m*.webp 01;35[0m
|
||||
[01;35m*.ogm 01;35[0m
|
||||
[01;35m*.mp4 01;35[0m
|
||||
[01;35m*.m4v 01;35[0m
|
||||
[01;35m*.mp4v 01;35[0m
|
||||
[01;35m*.vob 01;35[0m
|
||||
[01;35m*.qt 01;35[0m
|
||||
[01;35m*.nuv 01;35[0m
|
||||
[01;35m*.wmv 01;35[0m
|
||||
[01;35m*.asf 01;35[0m
|
||||
[01;35m*.rm 01;35[0m
|
||||
[01;35m*.rmvb 01;35[0m
|
||||
[01;35m*.flc 01;35[0m
|
||||
[01;35m*.avi 01;35[0m
|
||||
[01;35m*.fli 01;35[0m
|
||||
[01;35m*.flv 01;35[0m
|
||||
[01;35m*.gl 01;35[0m
|
||||
[01;35m*.dl 01;35[0m
|
||||
[01;35m*.xcf 01;35[0m
|
||||
[01;35m*.xwd 01;35[0m
|
||||
[01;35m*.yuv 01;35[0m
|
||||
[01;35m*.cgm 01;35[0m
|
||||
[01;35m*.emf 01;35[0m
|
||||
[01;35m*.ogv 01;35[0m
|
||||
[01;35m*.ogx 01;35[0m
|
||||
[00;36m*.aac 00;36[0m
|
||||
[00;36m*.au 00;36[0m
|
||||
[00;36m*.flac 00;36[0m
|
||||
[00;36m*.m4a 00;36[0m
|
||||
[00;36m*.mid 00;36[0m
|
||||
[00;36m*.midi 00;36[0m
|
||||
[00;36m*.mka 00;36[0m
|
||||
[00;36m*.mp3 00;36[0m
|
||||
[00;36m*.mpc 00;36[0m
|
||||
[00;36m*.ogg 00;36[0m
|
||||
[00;36m*.ra 00;36[0m
|
||||
[00;36m*.wav 00;36[0m
|
||||
[00;36m*.oga 00;36[0m
|
||||
[00;36m*.opus 00;36[0m
|
||||
[00;36m*.spx 00;36[0m
|
||||
[00;36m*.xspf 00;36[0m
|
||||
[00;90m*~ 00;90[0m
|
||||
[00;90m*# 00;90[0m
|
||||
[00;90m*.bak 00;90[0m
|
||||
[00;90m*.old 00;90[0m
|
||||
[00;90m*.orig 00;90[0m
|
||||
[00;90m*.part 00;90[0m
|
||||
[00;90m*.rej 00;90[0m
|
||||
[00;90m*.swp 00;90[0m
|
||||
[00;90m*.tmp 00;90[0m
|
||||
[00;90m*.dpkg-dist 00;90[0m
|
||||
[00;90m*.dpkg-old 00;90[0m
|
||||
[00;90m*.ucf-dist 00;90[0m
|
||||
[00;90m*.ucf-new 00;90[0m
|
||||
[00;90m*.ucf-old 00;90[0m
|
||||
[00;90m*.rpmnew 00;90[0m
|
||||
[00;90m*.rpmorig 00;90[0m
|
||||
[00;90m*.rpmsave 00;90[0m
|
Loading…
Add table
Add a link
Reference in a new issue