@@ -4,6 +4,8 @@ use std::vec::Vec;
44use ansi_term::Colour::{Fixed, Green, Red, Yellow};
55use ansi_term::Style;
66
7+use bytesize::ByteSize;
8+
79use console::AnsiCodeIterator;
810
911use syntect::easy::HighlightLines;
@@ -29,6 +31,7 @@ use crate::error::*;
2931use crate::input::OpenedInput;
3032use crate::line_range::RangeCheckResult;
3133use crate::preprocessor::{expand_tabs, replace_nonprintable};
34+use crate::style::StyleComponent;
3235use crate::terminal::{as_terminal_escaped, to_ansi_color};
3336use crate::vscreen::AnsiStyle;
3437use crate::wrapping::WrappingMode;
@@ -250,6 +253,21 @@ impl<'a> InteractivePrinter<'a> {
250253 }
251254 }
252255
256+ fn print_header_component_indent(&mut self, handle: &mut dyn Write) -> std::io::Result<()> {
257+ if self.config.style_components.grid() {
258+ write!(
259+ handle,
260+ "{}{}",
261+ " ".repeat(self.panel_width),
262+ self.colors
263+ .grid
264+ .paint(if self.panel_width > 0 { "│ " } else { "" }),
265+ )
266+ } else {
267+ write!(handle, "{}", " ".repeat(self.panel_width))
268+ }
269+ }
270+
253271 fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
254272 if self.config.tab_width > 0 {
255273 return expand_tabs(text, self.config.tab_width, cursor);
@@ -287,25 +305,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
287305 return Ok(());
288306 }
289307
290− if self.config.style_components.grid() {
291− self.print_horizontal_line(handle, '┬')?;
292−
293− write!(
294− handle,
295− "{}{}",
296− " ".repeat(self.panel_width),
297− self.colors
298− .grid
299− .paint(if self.panel_width > 0 { "│ " } else { "" }),
300− )?;
301− } else {
302− // Only pad space between files, if we haven't already drawn a horizontal rule
303− if add_header_padding && !self.config.style_components.rule() {
304− writeln!(handle)?;
305− }
306− write!(handle, "{}", " ".repeat(self.panel_width))?;
307− }
308−
309308 let mode = match self.content_type {
310309 Some(ContentType::BINARY) => " <BINARY>",
311310 Some(ContentType::UTF_16LE) => " <UTF-16LE>",
@@ -315,17 +314,60 @@ impl<'a> Printer for InteractivePrinter<'a> {
315314 };
316315
317316 let description = &input.description;
317+ let metadata = &input.metadata;
318+
319+ // We use this iterator to have a deterministic order for
320+ // header components. HashSet has arbitrary order, but Vec is ordered.
321+ let header_components: Vec<StyleComponent> = [
322+ (
323+ StyleComponent::HeaderFilename,
324+ self.config.style_components.header_filename(),
325+ ),
326+ (
327+ StyleComponent::HeaderFilesize,
328+ self.config.style_components.header_filesize(),
329+ ),
330+ ]
331+ .iter()
332+ .filter(|(_, is_enabled)| *is_enabled)
333+ .map(|(component, _)| *component)
334+ .collect();
335+
336+ // Print the cornering grid before the first header component
337+ if self.config.style_components.grid() {
338+ self.print_horizontal_line(handle, '┬')?;
339+ } else {
340+ // Only pad space between files, if we haven't already drawn a horizontal rule
341+ if add_header_padding && !self.config.style_components.rule() {
342+ writeln!(handle)?;
343+ }
344+ }
318345
319− writeln!(
320− handle,
321− "{}{}{}",
322− description
323− .kind()
324− .map(|kind| format!("{}: ", kind))
325− .unwrap_or_else(|| "".into()),
326− self.colors.filename.paint(description.title()),
327− mode
328− )?;
346+ header_components.iter().try_for_each(|component| {
347+ self.print_header_component_indent(handle)?;
348+
349+ match component {
350+ StyleComponent::HeaderFilename => writeln!(
351+ handle,
352+ "{}{}{}",
353+ description
354+ .kind()
355+ .map(|kind| format!("{}: ", kind))
356+ .unwrap_or_else(|| "".into()),
357+ self.colors.header_value.paint(description.title()),
358+ mode
359+ ),
360+
361+ StyleComponent::HeaderFilesize => {
362+ let bsize = metadata
363+ .size
364+ .map(|s| format!("{}", ByteSize(s)))
365+ .unwrap_or_else(|| "-".into());
366+ writeln!(handle, "Size: {}", self.colors.header_value.paint(bsize))
367+ }
368+ _ => Ok(()),
369+ }
370+ })?;
329371
330372 if self.config.style_components.grid() {
331373 if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
@@ -617,7 +659,7 @@ const DEFAULT_GUTTER_COLOR: u8 = 238;
617659pub struct Colors {
618660 pub grid: Style,
619661 pub rule: Style,
620− pub filename: Style,
662+ pub header_value: Style,
621663 pub git_added: Style,
622664 pub git_removed: Style,
623665 pub git_modified: Style,
@@ -646,7 +688,7 @@ impl Colors {
646688 Colors {
647689 grid: gutter_style,
648690 rule: gutter_style,
649− filename: Style::new().bold(),
691+ header_value: Style::new().bold(),
650692 git_added: Green.normal(),
651693 git_removed: Red.normal(),
652694 git_modified: Yellow.normal(),