Debian Codegraph @

src/output/hint.rs

raw
1 //! Formats hints for users.
2
3 use std::fmt;
4 use std::io::IsTerminal;
5 use std::io::Write;
6
7 /// Formats a hint for the user.
8 pub struct Hint {
9 /// Whether to suppress printing the hint.
10 quiet: bool,
11
12 /// Whether this is the first hint in this hint block.
13 first: bool,
14 }
15
16 impl Hint {
17 /// Constructs a new hint, optionally suppressing it.
18 pub fn new(quiet: bool) -> Self {
19 Hint {
20 quiet,
21 first: true,
22 }
23 }
24
25 /// Displays a message to the user.
26 ///
27 /// It will be prefixed with "Hint: ", and should either end in a
28 /// full stop or colon, depending on whether or not a command hint
29 /// follows.
30 pub fn hint(mut self, msg: fmt::Arguments) -> Self {
31 if ! self.quiet {
32 weprintln!();
33 weprintln!(
34 initial_indent=if self.first { "Hint: " } else { " " },
35 subsequent_indent=" ",
36 "{}", msg);
37 self.first = false;
38 }
39 self
40 }
41
42 /// Suggests an `sq` command to the user.
43 pub fn sq(self) -> Command {
44 Command::new(self, "sq")
45 }
46
47 /// Suggests a free-form command to the user.
48 ///
49 /// Note: if you want to suggest an `sq` invocation, use
50 /// [`Hint::sq`] instead.
51 pub fn command(self, argv0: &str) -> Command {
52 Command::new(self, argv0)
53 }
54 }
55
56 /// A structured command hint.
57 pub struct Command {
58 hint: Hint,
59 args: Vec<(String, Option<String>)>,
60 }
61
62 impl Command {
63 fn new(hint: Hint, argv0: &str) -> Self {
64 Command {
65 hint,
66 args: vec![(argv0.into(), None)],
67 }
68 }
69
70 /// Adds `arg` to the command.
71 pub fn arg<S: ToString>(mut self, arg: S) -> Self {
72 self.args.push((arg.to_string(), None));
73 self
74 }
75
76 /// Adds `arg` to the command, but show the user the replacement.
77 pub fn arg_hidden<S: ToString, R: ToString>(
78 mut self, arg: S, replacement: R)
79 -> Self
80 {
81 self.args.push((arg.to_string(), Some(replacement.to_string())));
82 self
83 }
84
85 /// Adds an argument `arg` with value to the command.
86 pub fn arg_value<S: ToString, V: ToString>(mut self, arg: S, value: V)
87 -> Self
88 {
89 self.args.push(
90 (format!("{}={}", arg.to_string(), value.to_string()),
91 None));
92 self
93 }
94
95 /// Adds an argument `arg` with value to the command, but show the
96 /// user the replacement value.
97 pub fn arg_value_hidden<S: ToString, V: ToString, R: ToString>(
98 mut self, arg: S, value: V, replacement_value: R)
99 -> Self
100 {
101 self.args.push(
102 (format!("{}={}", arg.to_string(), value.to_string()),
103 Some(format!("{}={}",
104 arg.to_string(),
105 replacement_value.to_string()))));
106 self
107 }
108
109 /// Emits the command hint.
110 pub fn done(self) -> Hint {
111 if ! self.hint.quiet {
112 // If we're connected to a terminal, flush stdout to
113 // reduce the chance of incorrectly interleaving output
114 // and hints.
115 let mut stdout = std::io::stdout();
116 if stdout.is_terminal() {
117 // Best effort.
118 let _ = stdout.flush();
119 }
120
121 let width = crate::output::wrapping::stderr_terminal_width();
122
123 let args = self.args.iter()
124 .map(|(arg, replacement)| {
125 if let Some(replacement) = replacement {
126 replacement
127 } else {
128 arg
129 }
130 })
131 .collect::<Vec<_>>();
132
133 eprintln!();
134 eprintln!("{}", crate::cli::examples::wrap_command(
135 &args, &[], " ", width, " ", width));
136 }
137
138 if cfg!(debug_assertions) && self.args[0].0 == "sq" {
139 let cli = crate::cli::build(false);
140
141 if let Err(e)
142 = cli.try_get_matches_from(self.args.iter().map(|(a, _)| a))
143 {
144 panic!("bad hint, parsing {}", e);
145 }
146 }
147
148 self.hint
149 }
150 }