Mercurial > public > mercurial-scm > hg
annotate rust/rhg/src/ui.rs @ 45440:a6a000ab135b
rhg: print error message when argument parsing fails
Differential Revision: https://phab.mercurial-scm.org/D8956
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Mon, 17 Aug 2020 16:55:43 +0200 |
parents | fbc373b7cbc3 |
children | 66756b34c06e |
rev | line source |
---|---|
45049
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; |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
2 use std::io::{ErrorKind, Write}; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
3 |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
4 #[derive(Debug)] |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
5 pub struct Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
6 stdout: std::io::Stdout, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
7 stderr: std::io::Stderr, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
8 } |
45049
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 { |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
21 Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
22 stdout: std::io::stdout(), |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
23 stderr: std::io::stderr(), |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
24 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
25 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
26 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
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:
45049
diff
changeset
|
28 /// operations. |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
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:
45049
diff
changeset
|
30 StdoutBuffer::new(self.stdout.lock()) |
45049
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> { |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
35 let mut stdout = self.stdout.lock(); |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
36 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
37 stdout.write_all(bytes).or_else(handle_stdout_error)?; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
38 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
39 stdout.flush().or_else(handle_stdout_error) |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
40 } |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
41 |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
42 /// 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
|
43 pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> { |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
44 let mut stderr = self.stderr.lock(); |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
45 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
46 stderr.write_all(bytes).or_else(handle_stderr_error)?; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
47 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
48 stderr.flush().or_else(handle_stderr_error) |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
49 } |
45440
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
50 |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
51 /// Write string line to stderr |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
52 pub fn writeln_stderr_str(&self, s: &str) -> Result<(), UiError> { |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
53 self.write_stderr(&format!("{}\n", s).as_bytes()) |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
54 } |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
55 } |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
56 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
57 /// A buffered stdout writer for faster batch printing operations. |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
58 pub struct StdoutBuffer<W: Write> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
59 buf: io::BufWriter<W>, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
60 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
61 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
62 impl<W: Write> StdoutBuffer<W> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
63 pub fn new(writer: W) -> Self { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
64 let buf = io::BufWriter::new(writer); |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
65 Self { buf } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
66 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
67 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
68 /// Write bytes to stdout buffer |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
69 pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> { |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
70 self.buf.write_all(bytes).or_else(handle_stdout_error) |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
71 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
72 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
73 /// Flush bytes to stdout |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
74 pub fn flush(&mut self) -> Result<(), UiError> { |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
75 self.buf.flush().or_else(handle_stdout_error) |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
76 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
77 } |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
78 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
79 /// 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:
45362
diff
changeset
|
80 /// signal that failure, otherwise just bail. |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
81 fn handle_stdout_error(error: io::Error) -> Result<(), UiError> { |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
82 if let ErrorKind::BrokenPipe = error.kind() { |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
83 // This makes `| head` work for example |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
84 return Ok(()); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
85 } |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
86 let mut stderr = io::stderr(); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
87 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
88 stderr |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
89 .write_all(&[b"abort: ", error.to_string().as_bytes(), b"\n"].concat()) |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
90 .map_err(UiError::StderrError)?; |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
91 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
92 stderr.flush().map_err(UiError::StderrError)?; |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
93 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
94 Err(UiError::StdoutError(error)) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
95 } |
45367
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
96 |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
97 /// Sometimes writing to stderr is not possible. |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
98 fn handle_stderr_error(error: io::Error) -> Result<(), UiError> { |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
99 // A broken pipe should not result in a error |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
100 // like with `| head` for example |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
101 if let ErrorKind::BrokenPipe = error.kind() { |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
102 return Ok(()); |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
103 } |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
104 Err(UiError::StdoutError(error)) |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
105 } |