Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding line number printing when output is piped out #2983

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Fix handling of inputs with OSC ANSI escape sequences, see #2541 and #2544 (@eth-p)
- Fix handling of inputs with combined ANSI color and attribute sequences, see #2185 and #2856 (@eth-p)
- Fix panel width when line 10000 wraps, see #2854 (@eth-p)
- Fix compatibility issue with cat. see #2983 (@domenicomastrangelo)
- Fix compile issue of `time` dependency caused by standard library regression #3045 (@cyqsimon)
- Fix override behavior of --plain and --paging, see issue #2731 and PR #3108 (@einfachIrgendwer0815)

Expand Down
27 changes: 17 additions & 10 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ impl App {
}

pub fn config(&self, inputs: &[Input]) -> Result<Config> {
let style_components = self.style_components()?;
let loop_through = !(self.interactive_output
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.get_flag("force-colorization"));

let style_components = self.style_components(loop_through)?;

let extra_plain = self.matches.get_count("plain") > 1;
let plain_last_index = self
Expand Down Expand Up @@ -248,14 +257,7 @@ impl App {
},
paging_mode,
term_width: maybe_term_width.unwrap_or(Term::stdout().size().1 as usize),
loop_through: !(self.interactive_output
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
|| self.matches.get_flag("force-colorization")),
loop_through: loop_through,
tab_width: self
.matches
.get_one::<String>("tabs")
Expand Down Expand Up @@ -416,7 +418,7 @@ impl App {
None
}

fn style_components(&self) -> Result<StyleComponents> {
fn style_components(&self, loop_through: bool) -> Result<StyleComponents> {
let matches = &self.matches;
let mut styled_components = match self.forced_style_components() {
Some(forced_components) => forced_components,
Expand All @@ -437,6 +439,11 @@ impl App {
StyleComponent::Default
.components(self.interactive_output)
.into_iter()
.map(|style| style.components(self.interactive_output, loop_through))
.fold(HashSet::new(), |mut acc, components| {
acc.extend(components.iter().cloned());
acc
})
.cloned(),
)),
};
Expand Down
4 changes: 3 additions & 1 deletion src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ fn run() -> Result<bool> {
let languages: String = get_languages(&config, cache_dir)?;
let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))];
let plain_config = Config {
style_components: StyleComponents::new(StyleComponent::Plain.components(false)),
style_components: StyleComponents::new(
StyleComponent::Plain.components(false, config.loop_through),
),
paging_mode: PagingMode::QuitIfOneScreen,
..Default::default()
};
Expand Down
16 changes: 14 additions & 2 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::io;
use std::io::IsTerminal;
use std::vec::Vec;

use nu_ansi_term::Color::{Fixed, Green, Red, Yellow};
Expand Down Expand Up @@ -138,7 +139,7 @@ impl<'a> Printer for SimplePrinter<'a> {
&mut self,
out_of_range: bool,
handle: &mut OutputHandle,
_line_number: usize,
line_number: usize,
line_buffer: &[u8],
) -> Result<()> {
// Skip squeezed lines.
Expand Down Expand Up @@ -166,7 +167,18 @@ impl<'a> Printer for SimplePrinter<'a> {
write!(handle, "{line}")?;
} else {
match handle {
OutputHandle::IoWrite(handle) => handle.write_all(line_buffer)?,
OutputHandle::IoWrite(handle) => {
if self.config.style_components.numbers()
&& !std::io::stdout().is_terminal()
{
handle.write_all(
format!("{line_number:4} {}", String::from_utf8_lossy(line_buffer))
.as_bytes(),
)?;
} else {
handle.write_all(line_buffer)?;
}
}
OutputHandle::FmtWrite(handle) => {
write!(
handle,
Expand Down
36 changes: 25 additions & 11 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ pub enum StyleComponent {
}

impl StyleComponent {
pub fn components(self, interactive_terminal: bool) -> &'static [StyleComponent] {
pub fn components(
self,
interactive_terminal: bool,
loop_through: bool,
) -> &'static [StyleComponent] {
match self {
StyleComponent::Auto => {
if interactive_terminal {
StyleComponent::Default.components(interactive_terminal)
StyleComponent::Default.components(interactive_terminal, loop_through)
} else {
StyleComponent::Plain.components(interactive_terminal)
StyleComponent::Plain.components(interactive_terminal, loop_through)
}
}
#[cfg(feature = "git")]
Expand All @@ -49,14 +53,24 @@ impl StyleComponent {
StyleComponent::LineNumbers,
StyleComponent::Snip,
],
StyleComponent::Default => &[
#[cfg(feature = "git")]
StyleComponent::Changes,
StyleComponent::Grid,
StyleComponent::HeaderFilename,
StyleComponent::LineNumbers,
StyleComponent::Snip,
],
StyleComponent::Default => {
if loop_through {
return &[
#[cfg(feature = "git")]
StyleComponent::Changes,
StyleComponent::Plain,
];
} else {
return &[
#[cfg(feature = "git")]
StyleComponent::Changes,
StyleComponent::Grid,
StyleComponent::HeaderFilename,
StyleComponent::LineNumbers,
StyleComponent::Snip,
];
}
}
StyleComponent::Plain => &[],
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2712,6 +2712,16 @@ fn highlighting_independant_from_map_syntax_case() {
}

#[test]
fn piped_output_with_lines() {
let expected = " 1 hello\n 2 world\n";

bat()
.arg("-n")
.write_stdin("hello\nworld\n")
.assert()
.success()
.stdout(expected)

fn strip_ansi_always_strips_ansi() {
bat()
.arg("--style=plain")
Expand Down
Loading