Skip to content

Commit

Permalink
refactor: remove last-modified and permissions, simplify header
Browse files Browse the repository at this point in the history
  • Loading branch information
mdibaiee committed Jan 17, 2022
1 parent 61e7f44 commit df1376b
Show file tree
Hide file tree
Showing 23 changed files with 37 additions and 103 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ 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 }
time = { version = "0.3.5", features = ["formatting"] }
bytesize = {version = "1.1.0", features = ["serde"]}

[dependencies.git2]
Expand Down
13 changes: 3 additions & 10 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
"full",
"plain",
"header",
"header-full",
"header-filename",
"header-filesize",
"header-permissions",
"header-lastmodified",
"grid",
"rule",
"numbers",
Expand Down Expand Up @@ -433,17 +430,13 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
'--style=\"..\"' option to the configuration file or export the \
BAT_STYLE environment variable (e.g.: export BAT_STYLE=\"..\").\n\n\
Possible values:\n\n \
* auto: enables all components and 'header', unless the output is piped.
* (default)\n \
* full: enables all available components.\n \
* full: enables all available components (default).\n \
* auto: same as 'full', unless the output is piped.\n \
* plain: disables all available components.\n \
* changes: show Git modification markers.\n \
* header: displays the filename and filesize.\n \
* header-full: displays all header-* fields.\n \
* header: displays the filename.\n \
* header-filename: displays the file name.\n \
* header-filesize: displays the size of the file in human-readable format.\n \
* header-last-modified: displays the last modification timestamp of the file.\n \
* header-permissions: displays the file owner, group and mode.\n \
* grid: vertical/horizontal lines to separate side bar\n \
and the header from the content.\n \
* rule: horizontal lines to delimit files.\n \
Expand Down
29 changes: 1 addition & 28 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::fs;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Read};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use clircle::{Clircle, Identifier};
use content_inspector::{self, ContentType};
Expand Down Expand Up @@ -88,17 +86,10 @@ impl<'a> InputKind<'a> {
}
}

#[derive(Clone)]
pub(crate) struct InputPermissions {
pub(crate) mode: u32,
}

#[derive(Clone, Default)]
pub(crate) struct InputMetadata {
pub(crate) user_provided_name: Option<PathBuf>,
pub(crate) size: Option<u64>,
pub(crate) permissions: Option<InputPermissions>,
pub(crate) modified: Option<SystemTime>,
pub(crate) size: Option<u64>
}

pub struct Input<'a> {
Expand Down Expand Up @@ -140,32 +131,14 @@ impl<'a> Input<'a> {
Self::_ordinary_file(path.as_ref())
}

#[cfg(unix)]
fn _input_permissions_os(metadata: fs::Metadata) -> Option<InputPermissions> {
Some(InputPermissions {
// the 3 digits from right are the familiar mode bits
// we are looking for
mode: metadata.permissions().mode() & 0o777,
})
}

#[cfg(not(unix))]
fn _input_permissions_os(_metadata: fs::Metadata) -> Option<InputPermissions> {
None
}

fn _ordinary_file(path: &Path) -> Self {
let kind = InputKind::OrdinaryFile(path.to_path_buf());
let metadata = match fs::metadata(path.to_path_buf()) {
Ok(meta) => {
let size = meta.len();
let modified = meta.modified().ok();
let permissions = Self::_input_permissions_os(meta);

InputMetadata {
size: Some(size),
modified,
permissions,
..InputMetadata::default()
}
}
Expand Down
51 changes: 4 additions & 47 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ansi_term::Colour::{Fixed, Green, Red, Yellow};
use ansi_term::Style;

use bytesize::ByteSize;
use time::{format_description, OffsetDateTime};

use console::AnsiCodeIterator;

Expand Down Expand Up @@ -298,7 +297,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
if add_header_padding && !self.config.style_components.rule() {
writeln!(handle)?;
}
write!(handle, "{}", " ".repeat(self.panel_width))?;
}

let mode = match self.content_type {
Expand All @@ -322,15 +320,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
(
StyleComponent::HeaderFilesize,
self.config.style_components.header_filesize(),
),
(
StyleComponent::HeaderPermissions,
self.config.style_components.header_permissions(),
),
(
StyleComponent::HeaderLastModified,
self.config.style_components.header_last_modified(),
),
)
]
.iter()
.filter(|(_, is_enabled)| *is_enabled)
Expand All @@ -347,7 +337,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
.grid
.paint(if self.panel_width > 0 { "│ " } else { "" }),
)?;
};
} else {
write!(handle, "{}", " ".repeat(self.panel_width))?;
}

match component {
StyleComponent::HeaderFilename => writeln!(
Expand All @@ -368,41 +360,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
.unwrap_or("".into());
writeln!(handle, "Size: {}", self.colors.header_value.paint(bsize))
}

StyleComponent::HeaderPermissions => {
let fmt_perms = format!(
"{:o}",
metadata
.permissions
.clone()
.map(|perm| perm.mode)
.unwrap_or(0)
);
writeln!(
handle,
"Permissions: {}",
self.colors.header_value.paint(fmt_perms)
)
}

StyleComponent::HeaderLastModified => {
let format = format_description::parse(
"[day] [month repr:short] [year] [hour]:[minute]:[second]",
)
.unwrap();
let fmt_modified = metadata
.modified
.map(|t| OffsetDateTime::from(t).format(&format).unwrap());

writeln!(
handle,
"Modified: {}",
self.colors
.header_value
.paint(fmt_modified.unwrap_or("".into()))
)
}

_ => Ok(()),
}
})?;
Expand Down
21 changes: 12 additions & 9 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ fn empty_file_leads_to_empty_output_with_rule_enabled() {
}

#[test]
fn filename_basic() {
fn header_basic() {
bat()
.arg("test.txt")
.arg("--decorations=always")
Expand All @@ -928,12 +928,12 @@ fn filename_basic() {
.arg("--file-name=foo")
.assert()
.success()
.stdout("File: foo\n")
.stdout("File: foo\nSize: 12 B\n")
.stderr("");
}

#[test]
fn filename_binary() {
fn header_binary() {
bat()
.arg("test.binary")
.arg("--decorations=always")
Expand All @@ -942,15 +942,15 @@ fn filename_binary() {
.arg("--file-name=foo")
.assert()
.success()
.stdout("File: foo <BINARY>\n")
.stdout("File: foo <BINARY>\nSize: 4 B\n")
.stderr("");
}

#[test]
fn filename_stdin() {
bat()
.arg("--decorations=always")
.arg("--style=header")
.arg("--style=header-filename")
.arg("-r=0:0")
.arg("-")
.write_stdin("stdin\n")
Expand All @@ -966,7 +966,7 @@ fn filename_stdin_binary() {
let vec = vec![0; 1];
bat_with_config()
.arg("--decorations=always")
.arg("--style=header")
.arg("--style=header-filename")
.write_stdin(vec)
.arg("--file-name=foo")
.assert()
Expand All @@ -979,7 +979,7 @@ fn filename_stdin_binary() {
fn filename_multiple_ok() {
bat()
.arg("--decorations=always")
.arg("--style=header")
.arg("--style=header-filename")
.arg("-r=0:0")
.arg("test.txt")
.arg("--file-name=foo")
Expand All @@ -995,7 +995,7 @@ fn filename_multiple_ok() {
fn filename_multiple_err() {
bat()
.arg("--decorations=always")
.arg("--style=header")
.arg("--style=header-filename")
.arg("-r=0:0")
.arg("test.txt")
.arg("--file-name=foo")
Expand All @@ -1012,7 +1012,7 @@ fn header_padding() {
.arg("test.txt")
.arg("single-line.txt")
.assert()
.stdout("File: test.txt\nhello world\n\nFile: single-line.txt\nSingle Line\n")
.stdout("File: test.txt\nSize: 12 B\nhello world\n\nFile: single-line.txt\nSize: 11 B\nSingle Line\n")
.stderr("");
}

Expand All @@ -1027,9 +1027,11 @@ fn header_padding_rule() {
.assert()
.stdout(
"File: test.txt
Size: 12 B
hello world
────────────────────────────────────────────────────────────────────────────────
File: single-line.txt
Size: 11 B
Single Line
",
)
Expand Down Expand Up @@ -1219,6 +1221,7 @@ fn grid_for_file_without_newline() {
"\
───────┬────────────────────────────────────────────────────────────────────────
│ File: single-line.txt
│ Size: 11 B
───────┼────────────────────────────────────────────────────────────────────────
1 │ Single Line
───────┴────────────────────────────────────────────────────────────────────────
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/changes_grid_header.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
──┬─────────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
──┼─────────────────────────────────────────────────────────────────────────────
+ │ /// A rectangle. First line is changed to prevent a regression of #1869
│ struct Rectangle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
───────┬────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
───────┼────────────────────────────────────────────────────────────────────────
1 + │ /// A rectangle. First line is changed to prevent a regression of #1869
2 │ struct Rectangle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
───────┬────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
───────┼────────────────────────────────────────────────────────────────────────
1 + │ /// A rectangle. First line is changed to prevent a regression of #1869
2 │ struct Rectangle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
──┬─────────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
──┼─────────────────────────────────────────────────────────────────────────────
+ │ /// A rectangle. First line is changed to prevent a regression of #1869
│ struct Rectangle {
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/changes_header.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
File: sample.rs
Size: 533 B
+ /// A rectangle. First line is changed to prevent a regression of #1869
struct Rectangle {
width: u32,
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/changes_header_numbers.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
File: sample.rs
Size: 533 B
1 + /// A rectangle. First line is changed to prevent a regression of #1869
2 struct Rectangle {
3 width: u32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
File: sample.rs
Size: 533 B
1 + /// A rectangle. First line is changed to prevent a regression of #1869
2 struct Rectangle {
3 width: u32,
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/changes_header_rule.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
File: sample.rs
Size: 533 B
+ /// A rectangle. First line is changed to prevent a regression of #1869
struct Rectangle {
width: u32,
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/full.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
───────┬────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
───────┼────────────────────────────────────────────────────────────────────────
1 + │ /// A rectangle. First line is changed to prevent a regression of #1869
2 │ struct Rectangle {
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/grid_header.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
────────────────────────────────────────────────────────────────────────────────
File: sample.rs
Size: 533 B
────────────────────────────────────────────────────────────────────────────────
/// A rectangle. First line is changed to prevent a regression of #1869
struct Rectangle {
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/output/grid_header_numbers.snapshot.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
─────┬──────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
─────┼──────────────────────────────────────────────────────────────────────────
1 │ /// A rectangle. First line is changed to prevent a regression of #1869
2 │ struct Rectangle {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
─────┬──────────────────────────────────────────────────────────────────────────
│ File: sample.rs
│ Size: 533 B
─────┼──────────────────────────────────────────────────────────────────────────
1 │ /// A rectangle. First line is changed to prevent a regression of #1869
2 │ struct Rectangle {
Expand Down

0 comments on commit df1376b

Please sign in to comment.