Skip to content

Commit

Permalink
Fix #100: allow line-buffered input
Browse files Browse the repository at this point in the history
  • Loading branch information
vtronko authored and corneliusroemer committed Dec 23, 2023
1 parent 2d287b9 commit 01b4dd7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ w - match full words only
*/
pub flags: Option<String>,

#[arg(short = 'l', long = "line-buffered", default_value_t = false)]
/// Buffered mode. Doesn't support multiline matching
pub line_buffered: bool,

/// The regexp or string (if using `-F`) to search for.
pub find: String,

Expand Down
3 changes: 1 addition & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::Result;
use memmap2::{Mmap, MmapOptions};
use std::{
fs::File,
io::{stdin, Read},
path::PathBuf,
};

use crate::error::Result;

#[derive(Debug, PartialEq)]
pub(crate) enum Source {
Stdin,
Expand Down
30 changes: 29 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,6 +43,34 @@ fn try_main() -> Result<()> {
Source::from_stdin()
};

if options.line_buffered && sources == Source::from_stdin() {
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");
buffer.clear();
}

return Ok(());
}

let mut mmaps = Vec::new();
for source in sources.iter() {
let mmap = match source {
Expand Down

0 comments on commit 01b4dd7

Please sign in to comment.