Skip to content

Commit

Permalink
Add an option to force writing to stdout (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin D. Howard <gavin@gavinhoward.com>
  • Loading branch information
gavinhoward authored Jan 1, 2024
1 parent ec953eb commit f183315
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ environment variable REP_PAGER can be used to override the pager.
/// Disable color
pub no_color: bool,

#[structopt(long = "stdout")]
/// Force printing to standard output without using a pager
pub stdout: bool,

#[structopt(short = "f", long = "flags", verbatim_doc_comment)]
#[rustfmt::skip]
/** Regex flags. May be combined (like `-f mc`)
Expand Down
12 changes: 8 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ impl App {
Self { replacer }
}

pub(crate) fn run(&self, preview: bool, delete: bool, color: bool, pager: Option<String>) -> Result<()> {
pub(crate) fn run(&self, preview: bool, delete: bool, color: bool, stdout: bool, pager: Option<String>) -> Result<()> {
{
let stdin = std::io::stdin();
let handle = stdin.lock();

// FIXME: Instantiating `output_type` and `write` should only happen if `preview` is true
let mut output_type = match OutputType::for_pager(pager, true) {
Ok(output_type) => output_type,
Err(_) => return Ok(()), // FIXME:
let mut output_type = if stdout {
OutputType::stdout()
} else {
match OutputType::for_pager(pager, true) {
Ok(output_type) => output_type,
Err(_) => return Ok(()), // FIXME:
}
};

let write = match output_type.handle() {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ fn main() -> Result<()> {
options.replacements,
)?),
)
.run(!options.write, options.delete, color, pager)?;
.run(!options.write, options.delete, color, options.stdout, pager)?;
} else {
App::new(None).run(!options.write, options.delete, color, pager)?;
App::new(None).run(!options.write, options.delete, color, options.stdout, pager)?;
}

process::exit(0);
Expand Down
2 changes: 1 addition & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl OutputType {
})
}

fn stdout() -> Self {
pub fn stdout() -> Self {
OutputType::Stdout(io::stdout())
}

Expand Down

0 comments on commit f183315

Please sign in to comment.