diff options
author | Mahdi Dibaiee <mdibaiee@pm.me> | 2022-02-07 19:48:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-07 20:48:57 +0100 |
commit | 312c8ef01f1a80f4a806629a7dacd32508b267a5 (patch) | |
tree | 27299be2d3bccaef527847f7d33ee765e6ff69cb | |
parent | f3f819493208f32b7a1374578568970ec381f5ac (diff) |
Add new `--style` called `header-filesize` and display it by default (#1988)
Also rename `header` to `header-filename`.
Related to #1701
111 files changed, 302 insertions, 42 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 97bc6e7b..ae734641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Features +- New style component `header-filesize` to show size of the displayed file in the header. See #1988 (@mdibaiee) + ## Bugfixes ## Other @@ -82,6 +82,7 @@ dependencies = [ "atty", "bincode", "bugreport", + "bytesize", "clap", "clircle", "console", @@ -170,6 +171,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f" [[package]] +name = "bytesize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" + +[[package]] name = "cc" version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -65,6 +65,7 @@ dirs-next = { version = "2.0.0", optional = true } grep-cli = { version = "0.1.6", optional = true } regex = { version = "1.0", optional = true } walkdir = { version = "2.0", optional = true } +bytesize = { version = "1.1.0" } [dependencies.git2] version = "0.13" diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index e0460f74..0a1a81cf 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -396,11 +396,19 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .validator(|val| { let mut invalid_vals = val.split(',').filter(|style| { !&[ - "auto", "full", "plain", "header", "grid", "rule", "numbers", "snip", + "auto", + "full", + "plain", + "header", + "header-filename", + "header-filesize", + "grid", + "rule", + "numbers", + "snip", #[cfg(feature = "git")] - "changes", - ] - .contains(style) + "changes", + ].contains(style) }); if let Some(invalid) = invalid_vals.next() { @@ -426,7 +434,9 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { * auto: same as 'full', unless the output is piped.\n \ * plain: disables all available components.\n \ * changes: show Git modification markers.\n \ - * header: show filenames before the content.\n \ + * header: alias for 'header-filename'.\n \ + * header-filename: show filenames before the content.\n \ + * header-filesize: show file sizes before the content.\n \ * grid: vertical/horizontal lines to separate side bar\n \ and the header from the content.\n \ * rule: horizontal lines to delimit files.\n \ diff --git a/src/input.rs b/src/input.rs index ffaca0ae..3c295822 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,5 @@ use std::convert::TryFrom; +use std::fs; use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use std::path::{Path, PathBuf}; @@ -87,6 +88,7 @@ impl<'a> InputKind<'a> { #[derive(Clone, Default)] pub(crate) struct InputMetadata { pub(crate) user_provided_name: Option<PathBuf>, + pub(crate) size: Option<u64>, } pub struct Input<'a> { @@ -130,9 +132,14 @@ impl<'a> Input<'a> { fn _ordinary_file(path: &Path) -> Self { let kind = InputKind::OrdinaryFile(path.to_path_buf()); + let metadata = InputMetadata { + size: fs::metadata(path).map(|m| m.len()).ok(), + ..InputMetadata::default() + }; + Input { descr |