mirror of
https://github.com/sharkdp/bat
synced 2026-07-31 18:21:43 +00:00
feat: implement --unbuffered mode for streaming input (#3555)
Repurpose the existing --unbuffered/-u flag (previously a POSIX no-op) to enable unbuffered input reading using fill_buf()/consume() instead of read_until(b'\n'). This allows partial lines to display immediately when piping streaming input like `tail -f` into bat. - Add unbuffered field to Config and InputReader - Add read_line_unbuffered() using BufRead::fill_buf()/consume() - Add flush() to OutputHandle, called after each line in unbuffered mode - Auto-disable line numbers in unbuffered mode to avoid partial line confusion - Update help text, man page, and shell completions - Add unit tests and integration tests
This commit is contained in:
@@ -462,6 +462,7 @@ impl App {
|
||||
_ => unreachable!("other values for --strip-ansi are not allowed"),
|
||||
},
|
||||
quiet_empty: self.matches.get_flag("quiet-empty"),
|
||||
unbuffered: self.matches.get_flag("unbuffered"),
|
||||
theme: theme(self.theme_options()).to_string(),
|
||||
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
|
||||
&& self.matches.get_flag("diff")
|
||||
@@ -619,6 +620,11 @@ impl App {
|
||||
bat_warning!("Style 'rule' is a subset of style 'grid', 'rule' will not be visible.");
|
||||
}
|
||||
|
||||
// Auto-disable line numbers in unbuffered mode to avoid confusion with partial lines
|
||||
if self.matches.get_flag("unbuffered") {
|
||||
styled_components.0.remove(&StyleComponent::LineNumbers);
|
||||
}
|
||||
|
||||
Ok(styled_components)
|
||||
}
|
||||
|
||||
|
||||
@@ -548,11 +548,14 @@ pub fn build_app(interactive_output: bool) -> Command {
|
||||
.short('u')
|
||||
.long("unbuffered")
|
||||
.action(ArgAction::SetTrue)
|
||||
.hide_short_help(true)
|
||||
.help("Enable unbuffered input reading for streaming use cases.")
|
||||
.long_help(
|
||||
"This option exists for POSIX-compliance reasons ('u' is for \
|
||||
'unbuffered'). The output is always unbuffered - this option \
|
||||
is simply ignored.",
|
||||
"Enable unbuffered input reading. When this flag is set, bat will \
|
||||
display data as soon as it is available, without waiting for a \
|
||||
complete line. This is useful for streaming use cases like \
|
||||
'tail -f logfile | bat -u --paging=never'. Note that line numbers \
|
||||
are automatically disabled in unbuffered mode, and syntax \
|
||||
highlighting may be imperfect on partial lines.",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
|
||||
Reference in New Issue
Block a user