-
Notifications
You must be signed in to change notification settings - Fork 141
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
feat: allow streaming, line-buffered input and output #287
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ use clap::Parser; | |||||||||||||
use memmap2::MmapMut; | ||||||||||||||
use std::{ | ||||||||||||||
fs, | ||||||||||||||
io::{stdout, Write}, | ||||||||||||||
io::{stdout, BufRead, Write}, | ||||||||||||||
ops::DerefMut, | ||||||||||||||
path::PathBuf, | ||||||||||||||
process, | ||||||||||||||
|
@@ -43,6 +43,34 @@ fn try_main() -> Result<()> { | |||||||||||||
Source::from_stdin() | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
if options.line_buffered && sources == Source::from_stdin() { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently this silently ignores the |
||||||||||||||
let stdin = std::io::stdin(); | ||||||||||||||
let stdout = std::io::stdout(); | ||||||||||||||
let mut handle = stdout.lock(); | ||||||||||||||
|
||||||||||||||
let mut buffer = String::new(); | ||||||||||||||
|
||||||||||||||
loop { | ||||||||||||||
let mut read_handle = stdin.lock(); | ||||||||||||||
let bytes_read = read_handle.read_line(&mut buffer)?; | ||||||||||||||
|
||||||||||||||
// .lock() | ||||||||||||||
// .read_line(&mut buffer) | ||||||||||||||
// .expect("Error reading from standard input"); | ||||||||||||||
if bytes_read == 0 { | ||||||||||||||
break; | ||||||||||||||
} | ||||||||||||||
let replaced = replacer.replace(buffer.as_bytes()); | ||||||||||||||
handle | ||||||||||||||
.write_all(replaced.as_ref()) | ||||||||||||||
.expect("Error writing to standard output"); | ||||||||||||||
handle.flush().expect("Error flushing output"); | ||||||||||||||
Comment on lines
+64
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Failing to write is a pretty normal operation that we handle with the normal failure paths currently |
||||||||||||||
buffer.clear(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if this |
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return Ok(()); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
let mut mmaps = Vec::new(); | ||||||||||||||
for source in sources.iter() { | ||||||||||||||
let mmap = match source { | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we can use
clap
's "magic values" for all of these