ui.rs
113 lines
| 3.1 KiB
| application/rls-services+xml
|
RustLexer
|
r46099 | use std::borrow::Cow; | ||
|
r45592 | use std::io; | ||
|
r45921 | use std::io::{ErrorKind, Write}; | ||
|
r45592 | |||
|
r45921 | #[derive(Debug)] | ||
pub struct Ui { | ||||
stdout: std::io::Stdout, | ||||
stderr: std::io::Stderr, | ||||
} | ||||
|
r45592 | |||
/// The kind of user interface error | ||||
pub enum UiError { | ||||
/// The standard output stream cannot be written to | ||||
StdoutError(io::Error), | ||||
/// The standard error stream cannot be written to | ||||
StderrError(io::Error), | ||||
} | ||||
/// The commandline user interface | ||||
impl Ui { | ||||
pub fn new() -> Self { | ||||
|
r45921 | Ui { | ||
stdout: std::io::stdout(), | ||||
stderr: std::io::stderr(), | ||||
} | ||||
} | ||||
/// Returns a buffered handle on stdout for faster batch printing | ||||
/// operations. | ||||
pub fn stdout_buffer(&self) -> StdoutBuffer<std::io::StdoutLock> { | ||||
StdoutBuffer::new(self.stdout.lock()) | ||||
|
r45592 | } | ||
/// Write bytes to stdout | ||||
pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> { | ||||
|
r45921 | let mut stdout = self.stdout.lock(); | ||
|
r45592 | |||
|
r46010 | stdout.write_all(bytes).or_else(handle_stdout_error)?; | ||
|
r45592 | |||
|
r46010 | stdout.flush().or_else(handle_stdout_error) | ||
|
r45592 | } | ||
/// Write bytes to stderr | ||||
pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> { | ||||
|
r45921 | let mut stderr = self.stderr.lock(); | ||
|
r45592 | |||
|
r46010 | stderr.write_all(bytes).or_else(handle_stderr_error)?; | ||
|
r45592 | |||
|
r46010 | stderr.flush().or_else(handle_stderr_error) | ||
|
r45592 | } | ||
|
r46011 | |||
/// Write string line to stderr | ||||
pub fn writeln_stderr_str(&self, s: &str) -> Result<(), UiError> { | ||||
self.write_stderr(&format!("{}\n", s).as_bytes()) | ||||
} | ||||
|
r45592 | } | ||
|
r45921 | |||
/// A buffered stdout writer for faster batch printing operations. | ||||
pub struct StdoutBuffer<W: Write> { | ||||
buf: io::BufWriter<W>, | ||||
} | ||||
impl<W: Write> StdoutBuffer<W> { | ||||
pub fn new(writer: W) -> Self { | ||||
let buf = io::BufWriter::new(writer); | ||||
Self { buf } | ||||
} | ||||
/// Write bytes to stdout buffer | ||||
pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> { | ||||
|
r46010 | self.buf.write_all(bytes).or_else(handle_stdout_error) | ||
|
r45921 | } | ||
/// Flush bytes to stdout | ||||
pub fn flush(&mut self) -> Result<(), UiError> { | ||||
|
r46010 | self.buf.flush().or_else(handle_stdout_error) | ||
|
r45921 | } | ||
} | ||||
|
r45925 | |||
/// Sometimes writing to stdout is not possible, try writing to stderr to | ||||
/// signal that failure, otherwise just bail. | ||||
fn handle_stdout_error(error: io::Error) -> Result<(), UiError> { | ||||
if let ErrorKind::BrokenPipe = error.kind() { | ||||
// This makes `| head` work for example | ||||
return Ok(()); | ||||
} | ||||
let mut stderr = io::stderr(); | ||||
stderr | ||||
.write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat()) | ||||
|
r46010 | .map_err(UiError::StderrError)?; | ||
|
r45925 | |||
|
r46010 | stderr.flush().map_err(UiError::StderrError)?; | ||
|
r45925 | |||
Err(UiError::StdoutError(error)) | ||||
} | ||||
|
r45926 | |||
/// Sometimes writing to stderr is not possible. | ||||
fn handle_stderr_error(error: io::Error) -> Result<(), UiError> { | ||||
// A broken pipe should not result in a error | ||||
// like with `| head` for example | ||||
if let ErrorKind::BrokenPipe = error.kind() { | ||||
return Ok(()); | ||||
} | ||||
Err(UiError::StdoutError(error)) | ||||
} | ||||
|
r46099 | |||
/// Encode rust strings according to the user system. | ||||
pub fn utf8_to_local(s: &str) -> Cow<[u8]> { | ||||
// TODO encode for the user's system // | ||||
let bytes = s.as_bytes(); | ||||
Cow::Borrowed(bytes) | ||||
} | ||||