mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-08 04:25:31 +00:00
✨ add raw level arg
This commit is contained in:
parent
0caf6c3f55
commit
2c917dfa29
@ -44,6 +44,10 @@ pub struct CliArgs {
|
|||||||
/// Ouch and claps subcommands
|
/// Ouch and claps subcommands
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
pub cmd: Subcommand,
|
pub cmd: Subcommand,
|
||||||
|
|
||||||
|
/// Compression raw level as each algo has
|
||||||
|
#[arg(short = 'r', long, global = true, default_value_t = u32::MAX)]
|
||||||
|
pub raw_level: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser, PartialEq, Eq, Debug)]
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
||||||
|
@ -31,6 +31,7 @@ pub fn compress_files(
|
|||||||
quiet: bool,
|
quiet: bool,
|
||||||
question_policy: QuestionPolicy,
|
question_policy: QuestionPolicy,
|
||||||
file_visibility_policy: FileVisibilityPolicy,
|
file_visibility_policy: FileVisibilityPolicy,
|
||||||
|
raw_level: u32,
|
||||||
) -> crate::Result<bool> {
|
) -> crate::Result<bool> {
|
||||||
// If the input files contain a directory, then the total size will be underestimated
|
// If the input files contain a directory, then the total size will be underestimated
|
||||||
let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file);
|
let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file);
|
||||||
@ -43,20 +44,73 @@ pub fn compress_files(
|
|||||||
Gzip => Box::new(
|
Gzip => Box::new(
|
||||||
// by default, ParCompress uses a default compression level of 3
|
// by default, ParCompress uses a default compression level of 3
|
||||||
// instead of the regular default that flate2 uses
|
// instead of the regular default that flate2 uses
|
||||||
|
if raw_level == u32::MAX {
|
||||||
gzp::par::compress::ParCompress::<gzp::deflate::Gzip>::builder()
|
gzp::par::compress::ParCompress::<gzp::deflate::Gzip>::builder()
|
||||||
.compression_level(Default::default())
|
.compression_level(Default::default())
|
||||||
.from_writer(encoder),
|
.from_writer(encoder)
|
||||||
|
} else {
|
||||||
|
gzp::par::compress::ParCompress::<gzp::deflate::Gzip>::builder()
|
||||||
|
.compression_level(gzp::par::compress::Compression::new(raw_level))
|
||||||
|
.from_writer(encoder)
|
||||||
|
},
|
||||||
),
|
),
|
||||||
Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())),
|
Bzip => {
|
||||||
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?),
|
if raw_level == u32::MAX {
|
||||||
Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)),
|
Box::new(bzip2::write::BzEncoder::new(encoder, Default::default()))
|
||||||
Snappy => Box::new(gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder().from_writer(encoder)),
|
} else {
|
||||||
|
Box::new(bzip2::write::BzEncoder::new(
|
||||||
|
encoder,
|
||||||
|
bzip2::Compression::new(raw_level),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Lz4 => {
|
||||||
|
if raw_level == u32::MAX {
|
||||||
|
Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?)
|
||||||
|
} else {
|
||||||
|
Box::new(lzzzz::lz4f::WriteCompressor::new(
|
||||||
|
encoder,
|
||||||
|
lzzzz::lz4f::PreferencesBuilder::new()
|
||||||
|
.compression_level(raw_level as i32)
|
||||||
|
.build(),
|
||||||
|
)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Lzma => {
|
||||||
|
if raw_level == u32::MAX {
|
||||||
|
Box::new(xz2::write::XzEncoder::new(encoder, 6))
|
||||||
|
} else {
|
||||||
|
Box::new(xz2::write::XzEncoder::new(encoder, raw_level))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Snappy => {
|
||||||
|
if raw_level == u32::MAX {
|
||||||
|
Box::new(gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder().from_writer(encoder))
|
||||||
|
} else {
|
||||||
|
Box::new(
|
||||||
|
gzp::par::compress::ParCompress::<gzp::snap::Snap>::builder()
|
||||||
|
.compression_level(gzp::par::compress::Compression::new(raw_level))
|
||||||
|
.from_writer(encoder),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Zstd => {
|
Zstd => {
|
||||||
let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default());
|
if raw_level == u32::MAX {
|
||||||
|
Box::new(
|
||||||
|
zstd::stream::write::Encoder::new(encoder, Default::default())
|
||||||
|
.unwrap()
|
||||||
|
.auto_finish(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Box::new(
|
||||||
|
zstd::stream::write::Encoder::new(encoder, raw_level as i32)
|
||||||
|
.unwrap()
|
||||||
|
.auto_finish(),
|
||||||
|
)
|
||||||
|
}
|
||||||
// Safety:
|
// Safety:
|
||||||
// Encoder::new() can only fail if `level` is invalid, but Default::default()
|
// Encoder::new() can only fail if `level` is invalid, but Default::default()
|
||||||
// is guaranteed to be valid
|
// is guaranteed to be valid
|
||||||
Box::new(zstd_encoder.unwrap().auto_finish())
|
|
||||||
}
|
}
|
||||||
Tar | Zip => unreachable!(),
|
Tar | Zip => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -80,6 +80,7 @@ pub fn run(
|
|||||||
args.quiet,
|
args.quiet,
|
||||||
question_policy,
|
question_policy,
|
||||||
file_visibility_policy,
|
file_visibility_policy,
|
||||||
|
args.raw_level,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Ok(true) = compress_result {
|
if let Ok(true) = compress_result {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user