author | Antoine Cezar <antoine.cezar@octobus.net> |
Fri, 24 Jul 2020 10:34:04 +0200 | |
changeset 45387 | 53af26aa5951 |
parent 45386 | 10c36ead86f8 |
child 45450 | fbc373b7cbc3 |
permissions | -rw-r--r-- |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
1 |
use std::io; |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
2 |
use std::io::{ErrorKind, Write}; |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
3 |
|
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
4 |
#[derive(Debug)] |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
5 |
pub struct Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
6 |
stdout: std::io::Stdout, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
7 |
stderr: std::io::Stderr, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
8 |
} |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
9 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
10 |
/// The kind of user interface error |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
11 |
pub enum UiError { |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
12 |
/// The standard output stream cannot be written to |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
13 |
StdoutError(io::Error), |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
14 |
/// The standard error stream cannot be written to |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
15 |
StderrError(io::Error), |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
16 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
17 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 |
/// The commandline user interface |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
19 |
impl Ui { |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
20 |
pub fn new() -> Self { |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
21 |
Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
22 |
stdout: std::io::stdout(), |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
23 |
stderr: std::io::stderr(), |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
24 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
25 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
26 |
|
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
27 |
/// Returns a buffered handle on stdout for faster batch printing |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
28 |
/// operations. |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
29 |
pub fn stdout_buffer(&self) -> StdoutBuffer<std::io::StdoutLock> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
30 |
StdoutBuffer::new(self.stdout.lock()) |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
31 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
32 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
33 |
/// Write bytes to stdout |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
34 |
pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> { |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
35 |
let mut stdout = self.stdout.lock(); |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
36 |
|
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
37 |
stdout |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
38 |
.write_all(bytes) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
39 |
.or_else(|e| handle_stdout_error(e))?; |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
40 |
|
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
41 |
stdout.flush().or_else(|e| handle_stdout_error(e)) |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
42 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
43 |
|
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
44 |
/// Write bytes to stderr |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 |
pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> { |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
46 |
let mut stderr = self.stderr.lock(); |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
47 |
|
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
48 |
stderr |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
49 |
.write_all(bytes) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
50 |
.or_else(|e| handle_stderr_error(e))?; |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
51 |
|
45387
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
52 |
stderr.flush().or_else(|e| handle_stderr_error(e)) |
45050
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
53 |
} |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
54 |
} |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
55 |
|
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
56 |
/// A buffered stdout writer for faster batch printing operations. |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
57 |
pub struct StdoutBuffer<W: Write> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
58 |
buf: io::BufWriter<W>, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
59 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
60 |
|
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
61 |
impl<W: Write> StdoutBuffer<W> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
62 |
pub fn new(writer: W) -> Self { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
63 |
let buf = io::BufWriter::new(writer); |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
64 |
Self { buf } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
65 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
66 |
|
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
67 |
/// Write bytes to stdout buffer |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
68 |
pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> { |
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
69 |
self.buf |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
70 |
.write_all(bytes) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
71 |
.or_else(|e| handle_stdout_error(e)) |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
72 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
73 |
|
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
74 |
/// Flush bytes to stdout |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
75 |
pub fn flush(&mut self) -> Result<(), UiError> { |
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
76 |
self.buf.flush().or_else(|e| handle_stdout_error(e)) |
45382
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
77 |
} |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
78 |
} |
45386
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
79 |
|
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
80 |
/// Sometimes writing to stdout is not possible, try writing to stderr to |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
81 |
/// signal that failure, otherwise just bail. |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
82 |
fn handle_stdout_error(error: io::Error) -> Result<(), UiError> { |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
83 |
if let ErrorKind::BrokenPipe = error.kind() { |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
84 |
// This makes `| head` work for example |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
85 |
return Ok(()); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
86 |
} |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
87 |
let mut stderr = io::stderr(); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
88 |
|
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
89 |
stderr |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
90 |
.write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat()) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
91 |
.map_err(|e| UiError::StderrError(e))?; |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
92 |
|
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
93 |
stderr.flush().map_err(|e| UiError::StderrError(e))?; |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
94 |
|
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
95 |
Err(UiError::StdoutError(error)) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45382
diff
changeset
|
96 |
} |
45387
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
97 |
|
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
98 |
/// Sometimes writing to stderr is not possible. |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
99 |
fn handle_stderr_error(error: io::Error) -> Result<(), UiError> { |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
100 |
// A broken pipe should not result in a error |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
101 |
// like with `| head` for example |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
102 |
if let ErrorKind::BrokenPipe = error.kind() { |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
103 |
return Ok(()); |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
104 |
} |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
105 |
Err(UiError::StdoutError(error)) |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45386
diff
changeset
|
106 |
} |