Read about our upcoming Code of Conduct on this issue

Commit a9119bff authored by Pulkit Goyal's avatar Pulkit Goyal
Browse files

rhg: try implement a `ui.warn()` replica [WIP]

parent e4b4a6ab5db7
Pipeline #23538 failed with stages
in 28 minutes and 13 seconds
use format_bytes::format_bytes;
use hg::config::Config;
use std::borrow::Cow;
use std::io;
use std::io::{ErrorKind, Write};
#[derive(Debug)]
pub struct Ui {
config: Option<&Config>,
stdout: std::io::Stdout,
stderr: std::io::Stderr,
}
......@@ -21,11 +22,16 @@ pub enum UiError {
impl Ui {
pub fn new() -> Self {
Ui {
config: None,
stdout: std::io::stdout(),
stderr: std::io::stderr(),
}
}
pub fn load_config(&self, config: Config) {
self.config = Some(&config);
}
/// Returns a buffered handle on stdout for faster batch printing
/// operations.
pub fn stdout_buffer(&self) -> StdoutBuffer<std::io::StdoutLock> {
......@@ -49,6 +55,24 @@ impl Ui {
stderr.flush().or_else(handle_stderr_error)
}
pub fn warn(&self, bytes: &[u8]) -> Result<(), UiError> {
match &self.config {
None => {
return Ok(());
}
Some(config) => match config.get_bool(b"ui", b"warn") {
Ok(val) => {
if val {
self.write_stderr(bytes)
} else {
Ok(())
}
}
Err(_) => Ok(()),
},
}
}
}
/// A buffered stdout writer for faster batch printing operations.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment