-
Notifications
You must be signed in to change notification settings - Fork 140
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?
Conversation
01b4dd7
to
3bc6bc4
Compare
3bc6bc4
to
c5aa590
Compare
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.
Just some initial feedback to get things stared
@@ -57,6 +57,10 @@ w - match full words only | |||
*/ | |||
pub flags: Option<String>, | |||
|
|||
#[arg(short = 'l', long = "line-buffered", default_value_t = false)] |
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.
#[arg(short = 'l', long = "line-buffered", default_value_t = false)] | |
#[arg(short, long, default_value_t)] |
Nit: we can use clap
's "magic values" for all of these
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this silently ignores the --line-buffered
flag when running with files which seems pretty problematic
handle | ||
.write_all(replaced.as_ref()) | ||
.expect("Error writing to standard output"); | ||
handle.flush().expect("Error flushing output"); |
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.
handle | |
.write_all(replaced.as_ref()) | |
.expect("Error writing to standard output"); | |
handle.flush().expect("Error flushing output"); | |
handle.write_all(replaced.as_ref())?; | |
handle.flush()?; |
Failing to write is a pretty normal operation that we handle with the normal failure paths currently
.write_all(replaced.as_ref()) | ||
.expect("Error writing to standard output"); | ||
handle.flush().expect("Error flushing output"); | ||
buffer.clear(); |
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.
I'd prefer if this buffer.clear()
was moved right before the .read_line()
. Forgetting to clear a buffer that you're reusing is a super common mistake that's annoying to debug
2 cents:
|
Very likely people are, yes. Or anyone who wants to match over multiple lines
You can get a slice from it and wrap it in an |
Multi-line matching isn't tied to full reads, at least theoretically. Practically it might be, yes. But "line buffered" should be implementation detail. Something like I'm curious if a temp file would be useful here.
TIL |
Allowing for line buffering inputs opens the door for streaming
To support streaming reads from stdin. I was looking through |
Did a little test on (Excuse my "unificationism" XD) |
The major shortcoming of
sd
right now is that it doesn't support streaming stdin to stdout, instead, all input is read into memory which meanssd
can't be used for bigger-than-memory tasks.This PR is based on @vtronko's work in #100 but adapted for current main.
It's a bit of a graft and rough around the edges, no validation of multi-line mode switch off etc, but it's a proof of concept and already useful (roughly 3x faster than sed).
Resolves: