Skip to content

Commit 27952b2

Browse files
committed
✨ Add support --color
1 parent c15ce22 commit 27952b2

File tree

8 files changed

+54
-5
lines changed

8 files changed

+54
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ rust-version = "1.85"
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14+
anstream = "0.6.16"
1415
anyhow = { version = "1.0.100", features = ["backtrace"] }
1516
base64 = "0.22.1"
1617
bitflags = "2.10.0"

cli/src/cli.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub struct Cli {
2525
pub(crate) commands: Commands,
2626
#[command(flatten)]
2727
pub(crate) verbosity: VerbosityArgs,
28+
#[command(flatten)]
29+
pub(crate) color: ColorArgs,
2830
#[arg(
2931
long,
3032
global = true,
@@ -70,6 +72,18 @@ impl VerbosityArgs {
7072
}
7173
}
7274

75+
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
76+
pub(crate) struct ColorArgs {
77+
#[arg(
78+
long,
79+
global = true,
80+
value_name = "WHEN",
81+
default_value = "auto",
82+
help = "Control color output"
83+
)]
84+
pub(crate) color: ColorChoice,
85+
}
86+
7387
#[derive(Subcommand, Clone, Debug)]
7488
pub(crate) enum Commands {
7589
#[command(visible_alias = "c", about = "Create archive")]

cli/src/cli/value.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod argon2id_params;
2+
mod color_choice;
23
mod compression_level;
34
mod datetime;
45
mod pbkdf2_sha256_params;
56
mod private_chunk_type;
67

78
pub(crate) use argon2id_params::Argon2idParams;
9+
pub(crate) use color_choice::ColorChoice;
810
pub(crate) use compression_level::{DeflateLevel, XzLevel, ZstdLevel};
911
pub use datetime::DateTime;
1012
pub(crate) use pbkdf2_sha256_params::Pbkdf2Sha256Params;

cli/src/cli/value/color_choice.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use clap::ValueEnum;
2+
3+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, ValueEnum)]
4+
pub(crate) enum ColorChoice {
5+
#[default]
6+
Auto,
7+
Always,
8+
Never,
9+
}
10+
11+
impl From<ColorChoice> for anstream::ColorChoice {
12+
#[inline]
13+
fn from(value: ColorChoice) -> Self {
14+
match value {
15+
ColorChoice::Auto => anstream::ColorChoice::Auto,
16+
ColorChoice::Always => anstream::ColorChoice::Always,
17+
ColorChoice::Never => anstream::ColorChoice::Never,
18+
}
19+
}
20+
}

cli/src/command.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ impl Command for Cli {
6666
Commands::Create(cmd) => cmd.execute(),
6767
Commands::Append(cmd) => cmd.execute(),
6868
Commands::Extract(cmd) => cmd.execute(),
69-
Commands::List(cmd) => cmd.execute(),
69+
Commands::List(mut cmd) => {
70+
cmd.color = self.color.color;
71+
cmd.execute()
72+
}
7073
Commands::Split(cmd) => cmd.execute(),
7174
Commands::Concat(cmd) => cmd.execute(),
7275
Commands::Strip(cmd) => cmd.execute(),

cli/src/command/list.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::command::core::run_read_entries_mem;
33
use crate::{
44
chunk,
5-
cli::{FileArgsCompat, PasswordArgs},
5+
cli::{ColorChoice, FileArgsCompat, PasswordArgs},
66
command::{
77
Command, ask_password,
88
core::{PathFilter, collect_split_archives, read_paths, run_read_entries},
@@ -52,6 +52,8 @@ use tabled::{
5252
group(ArgGroup::new("null-requires").arg("null").requires("exclude_from")),
5353
)]
5454
pub(crate) struct ListCommand {
55+
#[arg(skip)]
56+
pub(crate) color: ColorChoice,
5557
#[arg(short, long, help = "Display extended file metadata as a table")]
5658
pub(crate) long: bool,
5759
#[arg(short, long, help = "Add a header row to each column")]
@@ -286,6 +288,7 @@ fn list_archive(args: ListCommand) -> anyhow::Result<()> {
286288
classify: args.classify,
287289
format: args.format,
288290
out_to_stderr: false,
291+
color: args.color,
289292
};
290293
let archive = args.file.archive();
291294
let files = args.file.files();
@@ -365,6 +368,7 @@ pub(crate) struct ListOptions {
365368
pub(crate) classify: bool,
366369
pub(crate) format: Option<Format>,
367370
pub(crate) out_to_stderr: bool,
371+
pub(crate) color: ColorChoice,
368372
}
369373

370374
pub(crate) fn run_list_archive<'a>(
@@ -444,9 +448,11 @@ fn print_entries<'a>(
444448
.collect::<Vec<_>>();
445449
globs.ensure_all_matched()?;
446450
if options.out_to_stderr {
447-
print_formatted_entries(entries, &options, io::stderr().lock())
451+
let out = anstream::AutoStream::new(io::stderr().lock(), options.color.into());
452+
print_formatted_entries(entries, &options, out)
448453
} else {
449-
print_formatted_entries(entries, &options, io::stdout().lock())
454+
let out = anstream::AutoStream::new(io::stdout().lock(), options.color.into());
455+
print_formatted_entries(entries, &options, out)
450456
}
451457
}
452458

cli/src/command/stdio.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
cli::{
3-
CipherAlgorithmArgs, CompressionAlgorithmArgs, DateTime, HashAlgorithmArgs, PasswordArgs,
3+
CipherAlgorithmArgs, ColorChoice, CompressionAlgorithmArgs, DateTime, HashAlgorithmArgs,
4+
PasswordArgs,
45
},
56
command::{
67
Command,
@@ -681,6 +682,7 @@ fn run_list_archive(args: StdioCommand) -> anyhow::Result<()> {
681682
Format::Line
682683
}),
683684
out_to_stderr: args.to_stdout,
685+
color: ColorChoice::Auto,
684686
};
685687
let files_globs = GlobPatterns::new(args.files.iter().map(|it| it.as_str()))?;
686688

0 commit comments

Comments
 (0)