-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: accept text through pipes in chat (#259)
* fix: accept text through pipes in chat For the `q chat` command, also allow piping text into the q chat command. * Update mod.rs * Print to stdout if input is piped * Combine the 2 try_chat functions * code format --------- Co-authored-by: Rahel Ahmed <[email protected]>
- Loading branch information
Showing
2 changed files
with
169 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::io::{ | ||
Stderr, | ||
Stdout, | ||
Write, | ||
}; | ||
|
||
pub enum WriteOutput { | ||
Interactive(Stderr), | ||
NonInteractive(Stdout), | ||
} | ||
|
||
// wraps the stderr/stdout handle with a enum type. | ||
pub fn new(is_interactive: bool) -> WriteOutput { | ||
if is_interactive { | ||
WriteOutput::Interactive(std::io::stderr()) | ||
} else { | ||
WriteOutput::NonInteractive(std::io::stdout()) | ||
} | ||
} | ||
|
||
impl Write for WriteOutput { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
match self { | ||
WriteOutput::Interactive(stderr) => stderr.write(buf), | ||
WriteOutput::NonInteractive(stdout) => stdout.write(buf), | ||
} | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
match self { | ||
WriteOutput::Interactive(stderr) => stderr.flush(), | ||
WriteOutput::NonInteractive(stdout) => stdout.flush(), | ||
} | ||
} | ||
} |