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 3329a8f
Show file tree
Hide file tree
Showing 25 changed files with 39 additions and 141 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
2 changes: 1 addition & 1 deletion src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl App {
.collect::<Vec<_>>()
})
.or(env_style_components)
.unwrap_or_else(|| vec![StyleComponent::Auto])
.unwrap_or_else(|| vec![StyleComponent::Full])
.into_iter()
.map(|style| style.components(self.interactive_output))
.fold(HashSet::new(), |mut acc, components| {
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: show filenames before the content.\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
30 changes: 1 addition & 29 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::convert::TryFrom;
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 +85,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 +130,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
37 changes: 1 addition & 36 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ pub enum StyleComponent {
Grid,
Rule,
Header,
HeaderFull,
HeaderFilename,
HeaderFilesize,
HeaderPermissions,
HeaderLastModified,
LineNumbers,
Snip,
Full,
Expand All @@ -27,15 +24,7 @@ impl StyleComponent {
match self {
StyleComponent::Auto => {
if interactive_terminal {
&[
#[cfg(feature = "git")]
StyleComponent::Changes,
StyleComponent::Grid,
StyleComponent::HeaderFilename,
StyleComponent::HeaderFilesize,
StyleComponent::LineNumbers,
StyleComponent::Snip,
]
StyleComponent::Full.components(interactive_terminal)
} else {
StyleComponent::Plain.components(interactive_terminal)
}
Expand All @@ -46,18 +35,9 @@ impl StyleComponent {
StyleComponent::Rule => &[StyleComponent::Rule],
StyleComponent::Header => &[
StyleComponent::HeaderFilename,
StyleComponent::HeaderFilesize,
],
StyleComponent::HeaderFull => &[
StyleComponent::HeaderFilename,
StyleComponent::HeaderFilesize,
StyleComponent::HeaderPermissions,
StyleComponent::HeaderLastModified,
],
StyleComponent::HeaderFilename => &[StyleComponent::HeaderFilename],
StyleComponent::HeaderFilesize => &[StyleComponent::HeaderFilesize],
StyleComponent::HeaderPermissions => &[StyleComponent::HeaderPermissions],
StyleComponent::HeaderLastModified => &[StyleComponent::HeaderLastModified],
StyleComponent::LineNumbers => &[StyleComponent::LineNumbers],
StyleComponent::Snip => &[StyleComponent::Snip],
StyleComponent::Full => &[
Expand All @@ -66,8 +46,6 @@ impl StyleComponent {
StyleComponent::Grid,
StyleComponent::HeaderFilename,
StyleComponent::HeaderFilesize,
StyleComponent::HeaderPermissions,
StyleComponent::HeaderLastModified,
StyleComponent::LineNumbers,
StyleComponent::Snip,
],
Expand All @@ -87,11 +65,8 @@ impl FromStr for StyleComponent {
"grid" => Ok(StyleComponent::Grid),
"rule" => Ok(StyleComponent::Rule),
"header" => Ok(StyleComponent::Header),
"header-full" => Ok(StyleComponent::HeaderFull),
"header-filename" => Ok(StyleComponent::HeaderFilename),
"header-filesize" => Ok(StyleComponent::HeaderFilesize),
"header-permissions" => Ok(StyleComponent::HeaderPermissions),
"header-lastmodified" => Ok(StyleComponent::HeaderLastModified),
"numbers" => Ok(StyleComponent::LineNumbers),
"snip" => Ok(StyleComponent::Snip),
"full" => Ok(StyleComponent::Full),
Expand Down Expand Up @@ -125,8 +100,6 @@ impl StyleComponents {
pub fn header(&self) -> bool {
self.header_filename()
|| self.header_filesize()
|| self.header_permissions()
|| self.header_last_modified()
}

pub fn header_filename(&self) -> bool {
Expand All @@ -137,14 +110,6 @@ impl StyleComponents {
self.0.contains(&StyleComponent::HeaderFilesize)
}

pub fn header_permissions(&self) -> bool {
self.0.contains(&StyleComponent::HeaderPermissions)
}

pub fn header_last_modified(&self) -> bool {
self.0.contains(&StyleComponent::HeaderLastModified)
}

pub fn numbers(&self) -> bool {
self.0.contains(&StyleComponent::LineNumbers)
}
Expand Down
Loading

0 comments on commit 3329a8f

Please sign in to comment.