mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-14 17:00:17 +01:00
Adding line number printing when output is piped out
This commit is contained in:
parent
8f8c953ab6
commit
bb411abbcd
@ -135,7 +135,7 @@ impl<'a> Printer for SimplePrinter<'a> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
out_of_range: bool,
|
out_of_range: bool,
|
||||||
handle: &mut OutputHandle,
|
handle: &mut OutputHandle,
|
||||||
_line_number: usize,
|
line_number: usize,
|
||||||
line_buffer: &[u8],
|
line_buffer: &[u8],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Skip squeezed lines.
|
// Skip squeezed lines.
|
||||||
@ -163,7 +163,18 @@ impl<'a> Printer for SimplePrinter<'a> {
|
|||||||
write!(handle, "{line}")?;
|
write!(handle, "{line}")?;
|
||||||
} else {
|
} else {
|
||||||
match handle {
|
match handle {
|
||||||
OutputHandle::IoWrite(handle) => handle.write_all(line_buffer)?,
|
OutputHandle::IoWrite(handle) => {
|
||||||
|
if self.config.style_components.numbers() {
|
||||||
|
handle.write_all(
|
||||||
|
format!(
|
||||||
|
"{line_number:4} {}",
|
||||||
|
String::from_utf8_lossy(line_buffer)
|
||||||
|
).as_bytes()
|
||||||
|
)?;
|
||||||
|
} else {
|
||||||
|
handle.write_all(line_buffer)?;
|
||||||
|
}
|
||||||
|
},
|
||||||
OutputHandle::FmtWrite(handle) => {
|
OutputHandle::FmtWrite(handle) => {
|
||||||
write!(
|
write!(
|
||||||
handle,
|
handle,
|
||||||
|
@ -48,7 +48,7 @@ fn basic() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\n")
|
.stdout(" 1 hello world\n")
|
||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ fn stdin() {
|
|||||||
.write_stdin("foo\nbar\n")
|
.write_stdin("foo\nbar\n")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("foo\nbar\n");
|
.stdout(" 1 foo\n 2 bar\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -68,7 +68,7 @@ fn concatenate() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\nhello world\n");
|
.stdout(" 1 hello world\n 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -80,7 +80,7 @@ fn concatenate_stdin() {
|
|||||||
.write_stdin("stdin\n")
|
.write_stdin("stdin\n")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\nstdin\nhello world\n");
|
.stdout(" 1 hello world\n 1 stdin\n 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -90,7 +90,7 @@ fn concatenate_empty_first() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\n");
|
.stdout(" 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -100,7 +100,7 @@ fn concatenate_empty_last() {
|
|||||||
.arg("empty.txt")
|
.arg("empty.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\n");
|
.stdout(" 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -121,7 +121,7 @@ fn concatenate_empty_between() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\nhello world\n");
|
.stdout(" 1 hello world\n 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -132,7 +132,7 @@ fn concatenate_empty_first_and_last() {
|
|||||||
.arg("empty.txt")
|
.arg("empty.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("hello world\n");
|
.stdout(" 1 hello world\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -142,7 +142,7 @@ fn concatenate_single_line() {
|
|||||||
.arg("single-line.txt")
|
.arg("single-line.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("Single LineSingle Line");
|
.stdout(" 1 Single Line 1 Single Line");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -153,7 +153,7 @@ fn concatenate_single_line_empty() {
|
|||||||
.arg("single-line.txt")
|
.arg("single-line.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("Single LineSingle Line");
|
.stdout(" 1 Single Line 1 Single Line");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -174,7 +174,7 @@ fn line_range_2_3() {
|
|||||||
.arg("--line-range=2:3")
|
.arg("--line-range=2:3")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 2\nline 3\n");
|
.stdout(" 2 line 2\n 3 line 3\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -184,7 +184,7 @@ fn line_range_first_two() {
|
|||||||
.arg("--line-range=:2")
|
.arg("--line-range=:2")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 1\nline 2\n");
|
.stdout(" 1 line 1\n 2 line 2\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -194,7 +194,7 @@ fn line_range_last_3() {
|
|||||||
.arg("--line-range=2:")
|
.arg("--line-range=2:")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 2\nline 3\nline 4\n");
|
.stdout(" 2 line 2\n 3 line 3\n 4 line 4\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -205,7 +205,7 @@ fn line_range_multiple() {
|
|||||||
.arg("--line-range=4:4")
|
.arg("--line-range=4:4")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 1\nline 2\nline 4\n");
|
.stdout(" 1 line 1\n 2 line 2\n 4 line 4\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -215,7 +215,7 @@ fn squeeze_blank() {
|
|||||||
.arg("--squeeze-blank")
|
.arg("--squeeze-blank")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 1\n\nline 5\n\nline 20\nline 21\n\nline 24\n\nline 26\n\nline 30\n");
|
.stdout(" 1 line 1\n 2 \n 5 line 5\n 6 \n 20 line 20\n 21 line 21\n 22 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 30 line 30\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -238,7 +238,7 @@ fn squeeze_limit() {
|
|||||||
.arg("--squeeze-limit=2")
|
.arg("--squeeze-limit=2")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 1\n\n\nline 5\n\n\nline 20\nline 21\n\n\nline 24\n\nline 26\n\n\nline 30\n");
|
.stdout(" 1 line 1\n 2 \n 3 \n 5 line 5\n 6 \n 7 \n 20 line 20\n 21 line 21\n 22 \n 23 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 28 \n 30 line 30\n");
|
||||||
|
|
||||||
bat()
|
bat()
|
||||||
.arg("empty_lines.txt")
|
.arg("empty_lines.txt")
|
||||||
@ -246,7 +246,7 @@ fn squeeze_limit() {
|
|||||||
.arg("--squeeze-limit=5")
|
.arg("--squeeze-limit=5")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("line 1\n\n\n\nline 5\n\n\n\n\n\nline 20\nline 21\n\n\nline 24\n\nline 26\n\n\n\nline 30\n");
|
.stdout(" 1 line 1\n 2 \n 3 \n 4 \n 5 line 5\n 6 \n 7 \n 8 \n 9 \n 10 \n 20 line 20\n 21 line 21\n 22 \n 23 \n 24 line 24\n 25 \n 26 line 26\n 27 \n 28 \n 29 \n 30 line 30\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -693,7 +693,7 @@ fn do_not_exit_directory() {
|
|||||||
.arg("sub_directory")
|
.arg("sub_directory")
|
||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.stdout("hello world\n")
|
.stdout(" 1 hello world\n")
|
||||||
.failure();
|
.failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -752,7 +752,7 @@ fn pager_disable() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -833,7 +833,7 @@ fn env_var_pager_value_bat() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -871,7 +871,7 @@ fn pager_most_from_pager_env_var() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -917,7 +917,7 @@ fn pager_most_with_arg() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -932,7 +932,7 @@ fn pager_more() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -944,7 +944,7 @@ fn alias_pager_disable() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -971,7 +971,7 @@ fn disable_pager_if_disable_paging_flag_comes_after_paging() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq(" 1 hello world\n").normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1043,7 +1043,7 @@ fn basic_set_terminal_title() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("\u{1b}]0;bat: test.txt\x07hello world\n")
|
.stdout("\u{1b}]0;bat: test.txt\x07 1 hello world\n")
|
||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1294,7 +1294,7 @@ fn can_print_file_named_cache() {
|
|||||||
.arg("cache")
|
.arg("cache")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("test\n")
|
.stdout(" 1 test\n")
|
||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1305,7 +1305,7 @@ fn can_print_file_named_cache_with_additional_argument() {
|
|||||||
.arg("test.txt")
|
.arg("test.txt")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("test\nhello world\n")
|
.stdout(" 1 test\n 1 hello world\n")
|
||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1315,7 +1315,7 @@ fn can_print_file_starting_with_cache() {
|
|||||||
.arg("cache.c")
|
.arg("cache.c")
|
||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout("test\n")
|
.stdout(" 1 test\n")
|
||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user