32 |
32 |
33 /// Write bytes to stdout |
33 /// Write bytes to stdout |
34 pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> { |
34 pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> { |
35 let mut stdout = self.stdout.lock(); |
35 let mut stdout = self.stdout.lock(); |
36 |
36 |
37 stdout |
37 stdout.write_all(bytes).or_else(handle_stdout_error)?; |
38 .write_all(bytes) |
|
39 .or_else(|e| handle_stdout_error(e))?; |
|
40 |
38 |
41 stdout.flush().or_else(|e| handle_stdout_error(e)) |
39 stdout.flush().or_else(handle_stdout_error) |
42 } |
40 } |
43 |
41 |
44 /// Write bytes to stderr |
42 /// Write bytes to stderr |
45 pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> { |
43 pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> { |
46 let mut stderr = self.stderr.lock(); |
44 let mut stderr = self.stderr.lock(); |
47 |
45 |
48 stderr |
46 stderr.write_all(bytes).or_else(handle_stderr_error)?; |
49 .write_all(bytes) |
|
50 .or_else(|e| handle_stderr_error(e))?; |
|
51 |
47 |
52 stderr.flush().or_else(|e| handle_stderr_error(e)) |
48 stderr.flush().or_else(handle_stderr_error) |
53 } |
49 } |
54 } |
50 } |
55 |
51 |
56 /// A buffered stdout writer for faster batch printing operations. |
52 /// A buffered stdout writer for faster batch printing operations. |
57 pub struct StdoutBuffer<W: Write> { |
53 pub struct StdoutBuffer<W: Write> { |
64 Self { buf } |
60 Self { buf } |
65 } |
61 } |
66 |
62 |
67 /// Write bytes to stdout buffer |
63 /// Write bytes to stdout buffer |
68 pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> { |
64 pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> { |
69 self.buf |
65 self.buf.write_all(bytes).or_else(handle_stdout_error) |
70 .write_all(bytes) |
|
71 .or_else(|e| handle_stdout_error(e)) |
|
72 } |
66 } |
73 |
67 |
74 /// Flush bytes to stdout |
68 /// Flush bytes to stdout |
75 pub fn flush(&mut self) -> Result<(), UiError> { |
69 pub fn flush(&mut self) -> Result<(), UiError> { |
76 self.buf.flush().or_else(|e| handle_stdout_error(e)) |
70 self.buf.flush().or_else(handle_stdout_error) |
77 } |
71 } |
78 } |
72 } |
79 |
73 |
80 /// Sometimes writing to stdout is not possible, try writing to stderr to |
74 /// Sometimes writing to stdout is not possible, try writing to stderr to |
81 /// signal that failure, otherwise just bail. |
75 /// signal that failure, otherwise just bail. |
86 } |
80 } |
87 let mut stderr = io::stderr(); |
81 let mut stderr = io::stderr(); |
88 |
82 |
89 stderr |
83 stderr |
90 .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat()) |
84 .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat()) |
91 .map_err(|e| UiError::StderrError(e))?; |
85 .map_err(UiError::StderrError)?; |
92 |
86 |
93 stderr.flush().map_err(|e| UiError::StderrError(e))?; |
87 stderr.flush().map_err(UiError::StderrError)?; |
94 |
88 |
95 Err(UiError::StdoutError(error)) |
89 Err(UiError::StdoutError(error)) |
96 } |
90 } |
97 |
91 |
98 /// Sometimes writing to stderr is not possible. |
92 /// Sometimes writing to stderr is not possible. |