cli: rename UiOutputPair to UiOutput

We'll add a variant that isn't a pair. Also add a function to create a
new UiOutput::Terminal, we will create this variant in a few places
because we want to fall back to it.
This commit is contained in:
Glen Choo 2022-10-20 18:05:40 -07:00
parent babbdc4e72
commit d622656deb

View file

@ -26,7 +26,7 @@ pub struct Ui {
color: bool, color: bool,
cwd: PathBuf, cwd: PathBuf,
formatter_factory: FormatterFactory, formatter_factory: FormatterFactory,
output_pair: UiOutputPair, output: UiOutput,
settings: UserSettings, settings: UserSettings,
} }
@ -93,10 +93,7 @@ impl Ui {
color, color,
cwd, cwd,
formatter_factory, formatter_factory,
output_pair: UiOutputPair::Terminal { output: UiOutput::new_terminal(),
stdout: io::stdout(),
stderr: io::stderr(),
},
settings, settings,
} }
} }
@ -139,15 +136,15 @@ impl Ui {
/// Labels added to the returned formatter should be removed by caller. /// Labels added to the returned formatter should be removed by caller.
/// Otherwise the last color would persist. /// Otherwise the last color would persist.
pub fn stdout_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> { pub fn stdout_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> {
match &self.output_pair { match &self.output {
UiOutputPair::Terminal { stdout, .. } => self.new_formatter(stdout.lock()), UiOutput::Terminal { stdout, .. } => self.new_formatter(stdout.lock()),
} }
} }
/// Creates a formatter for the locked stderr stream. /// Creates a formatter for the locked stderr stream.
pub fn stderr_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> { pub fn stderr_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> {
match &self.output_pair { match &self.output {
UiOutputPair::Terminal { stderr, .. } => self.new_formatter(stderr.lock()), UiOutput::Terminal { stderr, .. } => self.new_formatter(stderr.lock()),
} }
} }
@ -159,21 +156,21 @@ impl Ui {
pub fn write(&mut self, text: &str) -> io::Result<()> { pub fn write(&mut self, text: &str) -> io::Result<()> {
let data = text.as_bytes(); let data = text.as_bytes();
match &mut self.output_pair { match &mut self.output {
UiOutputPair::Terminal { stdout, .. } => stdout.write_all(data), UiOutput::Terminal { stdout, .. } => stdout.write_all(data),
} }
} }
pub fn write_stderr(&mut self, text: &str) -> io::Result<()> { pub fn write_stderr(&mut self, text: &str) -> io::Result<()> {
let data = text.as_bytes(); let data = text.as_bytes();
match &mut self.output_pair { match &mut self.output {
UiOutputPair::Terminal { stderr, .. } => stderr.write_all(data), UiOutput::Terminal { stderr, .. } => stderr.write_all(data),
} }
} }
pub fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { pub fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
match &mut self.output_pair { match &mut self.output {
UiOutputPair::Terminal { stdout, .. } => stdout.write_fmt(fmt), UiOutput::Terminal { stdout, .. } => stdout.write_fmt(fmt),
} }
} }
@ -202,8 +199,8 @@ impl Ui {
} }
pub fn flush(&mut self) -> io::Result<()> { pub fn flush(&mut self) -> io::Result<()> {
match &mut self.output_pair { match &mut self.output {
UiOutputPair::Terminal { stdout, .. } => stdout.flush(), UiOutput::Terminal { stdout, .. } => stdout.flush(),
} }
} }
@ -240,17 +237,26 @@ impl Ui {
pub fn output_guard(&self, text: String) -> OutputGuard { pub fn output_guard(&self, text: String) -> OutputGuard {
OutputGuard { OutputGuard {
text, text,
output: match self.output_pair { output: match self.output {
UiOutputPair::Terminal { .. } => io::stdout(), UiOutput::Terminal { .. } => io::stdout(),
}, },
} }
} }
} }
enum UiOutputPair { enum UiOutput {
Terminal { stdout: Stdout, stderr: Stderr }, Terminal { stdout: Stdout, stderr: Stderr },
} }
impl UiOutput {
fn new_terminal() -> UiOutput {
UiOutput::Terminal {
stdout: io::stdout(),
stderr: io::stderr(),
}
}
}
pub struct OutputGuard { pub struct OutputGuard {
text: String, text: String,
output: Stdout, output: Stdout,