Mercurial > hg
annotate rust/rhg/src/ui.rs @ 45528:66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Differential Revision: https://phab.mercurial-scm.org/D8960
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Wed, 09 Sep 2020 12:07:05 +0200 |
parents | a6a000ab135b |
children | fada33872b5b |
rev | line source |
---|---|
45528
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
1 use std::borrow::Cow; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
2 use std::io; |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
3 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
|
4 |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
5 #[derive(Debug)] |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
6 pub struct Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
7 stdout: std::io::Stdout, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
8 stderr: std::io::Stderr, |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
9 } |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
10 |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
11 /// 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
|
12 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
|
13 /// 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
|
14 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
|
15 /// 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
|
16 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
|
17 } |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
18 |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
19 /// 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
|
20 impl Ui { |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
21 pub fn new() -> Self { |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
22 Ui { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
23 stdout: std::io::stdout(), |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
24 stderr: std::io::stderr(), |
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 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
28 /// 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
|
29 /// operations. |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
30 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
|
31 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
|
32 } |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
33 |
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
34 /// 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
|
35 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
|
36 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
|
37 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
38 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
|
39 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
40 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
|
41 } |
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 /// 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
|
44 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
|
45 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
|
46 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
47 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
|
48 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
49 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
|
50 } |
45440
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
51 |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
52 /// Write string line to stderr |
a6a000ab135b
rhg: print error message when argument parsing fails
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45439
diff
changeset
|
53 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
|
54 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
|
55 } |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff
changeset
|
56 } |
45362
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
57 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
58 /// 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
|
59 pub struct StdoutBuffer<W: Write> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
60 buf: io::BufWriter<W>, |
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 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
63 impl<W: Write> StdoutBuffer<W> { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
64 pub fn new(writer: W) -> Self { |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
65 let buf = io::BufWriter::new(writer); |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
66 Self { buf } |
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 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
69 /// Write bytes to stdout buffer |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
70 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
|
71 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
|
72 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
73 |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
74 /// Flush bytes to stdout |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
75 pub fn flush(&mut self) -> Result<(), UiError> { |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
76 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
|
77 } |
eb55274d3650
rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
78 } |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
79 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
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:
45362
diff
changeset
|
81 /// signal that failure, otherwise just bail. |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
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:
45362
diff
changeset
|
83 if let ErrorKind::BrokenPipe = error.kind() { |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
84 // This makes `| head` work for example |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
85 return Ok(()); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
86 } |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
87 let mut stderr = io::stderr(); |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
88 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
89 stderr |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
90 .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
|
91 .map_err(UiError::StderrError)?; |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
92 |
45439
fbc373b7cbc3
rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45367
diff
changeset
|
93 stderr.flush().map_err(UiError::StderrError)?; |
45366
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
94 |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
95 Err(UiError::StdoutError(error)) |
10c36ead86f8
rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45362
diff
changeset
|
96 } |
45367
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
97 |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
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:
45366
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:
45366
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:
45366
diff
changeset
|
101 // like with `| head` for example |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
102 if let ErrorKind::BrokenPipe = error.kind() { |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
103 return Ok(()); |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
104 } |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
105 Err(UiError::StdoutError(error)) |
53af26aa5951
rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45366
diff
changeset
|
106 } |
45528
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
107 |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
108 /// Encode rust strings according to the user system. |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
109 pub fn utf8_to_local(s: &str) -> Cow<[u8]> { |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
110 // TODO encode for the user's system // |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
111 let bytes = s.as_bytes(); |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
112 Cow::Borrowed(bytes) |
66756b34c06e
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45440
diff
changeset
|
113 } |