2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-10-07 05:24:35 +00:00
|
|
|
use std::io::{Stderr, Stdout, Write};
|
2022-12-01 12:39:34 +00:00
|
|
|
use std::process::{Child, ChildStdin, Stdio};
|
2022-06-08 02:43:12 +00:00
|
|
|
use std::str::FromStr;
|
2022-10-18 18:07:35 +00:00
|
|
|
use std::{fmt, io, mem};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-11-23 16:14:13 +00:00
|
|
|
use crossterm::tty::IsTty;
|
2023-02-11 22:44:17 +00:00
|
|
|
use maplit::hashmap;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-02-11 22:44:17 +00:00
|
|
|
use crate::config::{CommandNameAndArgs, NonEmptyCommandArgsVec};
|
2023-01-12 06:25:17 +00:00
|
|
|
use crate::formatter::{Formatter, FormatterFactory, LabeledWriter};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-10-19 00:44:10 +00:00
|
|
|
pub struct Ui {
|
2022-10-31 17:49:53 +00:00
|
|
|
color: bool,
|
2023-02-06 04:13:44 +00:00
|
|
|
pager_cmd: CommandNameAndArgs,
|
2022-11-01 00:31:30 +00:00
|
|
|
paginate: PaginationChoice,
|
2022-11-01 23:28:45 +00:00
|
|
|
progress_indicator: bool,
|
2022-10-07 03:52:01 +00:00
|
|
|
formatter_factory: FormatterFactory,
|
2022-10-21 01:05:40 +00:00
|
|
|
output: UiOutput,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:58:24 +00:00
|
|
|
fn progress_indicator_setting(config: &config::Config) -> bool {
|
|
|
|
config.get_bool("ui.progress-indicator").unwrap_or(true)
|
2022-11-01 23:28:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 02:43:12 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum ColorChoice {
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
Auto,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ColorChoice {
|
|
|
|
fn default() -> Self {
|
|
|
|
ColorChoice::Auto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ColorChoice {
|
|
|
|
type Err = &'static str;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"always" => Ok(ColorChoice::Always),
|
|
|
|
"never" => Ok(ColorChoice::Never),
|
|
|
|
"auto" => Ok(ColorChoice::Auto),
|
|
|
|
_ => Err("must be one of always, never, or auto"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 04:38:54 +00:00
|
|
|
impl fmt::Display for ColorChoice {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let s = match self {
|
2022-10-31 01:29:26 +00:00
|
|
|
ColorChoice::Always => "always",
|
|
|
|
ColorChoice::Never => "never",
|
|
|
|
ColorChoice::Auto => "auto",
|
2023-01-02 04:38:54 +00:00
|
|
|
};
|
|
|
|
write!(f, "{s}")
|
2022-10-31 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:58:24 +00:00
|
|
|
fn color_setting(config: &config::Config) -> ColorChoice {
|
|
|
|
config
|
2022-03-19 17:00:13 +00:00
|
|
|
.get_string("ui.color")
|
2022-06-08 02:43:12 +00:00
|
|
|
.ok()
|
|
|
|
.and_then(|s| s.parse().ok())
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:33:51 +00:00
|
|
|
fn use_color(choice: ColorChoice) -> bool {
|
2022-06-08 02:43:12 +00:00
|
|
|
match choice {
|
|
|
|
ColorChoice::Always => true,
|
|
|
|
ColorChoice::Never => false,
|
2022-11-23 16:14:13 +00:00
|
|
|
ColorChoice::Auto => io::stdout().is_tty(),
|
2022-03-19 17:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 00:31:30 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum PaginationChoice {
|
|
|
|
No,
|
|
|
|
Auto,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PaginationChoice {
|
|
|
|
fn default() -> Self {
|
|
|
|
PaginationChoice::Auto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:13:44 +00:00
|
|
|
fn pager_setting(config: &config::Config) -> CommandNameAndArgs {
|
2023-01-04 12:58:24 +00:00
|
|
|
config
|
2022-12-01 12:39:34 +00:00
|
|
|
.get("ui.pager")
|
2023-02-11 22:44:17 +00:00
|
|
|
.unwrap_or_else(|_| CommandNameAndArgs::Structured {
|
|
|
|
command: NonEmptyCommandArgsVec::try_from(vec!["less".to_string(), "-FRX".to_string()])
|
|
|
|
.unwrap(),
|
|
|
|
env: hashmap! { "LESSCHARSET".to_string() => "utf-8".to_string() },
|
|
|
|
})
|
2022-10-18 18:07:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 00:44:10 +00:00
|
|
|
impl Ui {
|
2023-01-18 01:20:27 +00:00
|
|
|
pub fn with_config(config: &config::Config) -> Result<Ui, config::ConfigError> {
|
2023-01-05 06:04:54 +00:00
|
|
|
let color = use_color(color_setting(config));
|
2023-01-21 21:55:34 +00:00
|
|
|
// Sanitize ANSI escape codes if we're printing to a terminal. Doesn't affect
|
|
|
|
// ANSI escape codes that originate from the formatter itself.
|
|
|
|
let sanitize = io::stdout().is_tty();
|
2023-01-18 01:20:27 +00:00
|
|
|
let formatter_factory = FormatterFactory::prepare(config, color, sanitize)?;
|
2023-01-05 06:04:54 +00:00
|
|
|
let progress_indicator = progress_indicator_setting(config);
|
2023-01-18 01:20:27 +00:00
|
|
|
Ok(Ui {
|
2022-10-31 17:49:53 +00:00
|
|
|
color,
|
2022-10-07 05:24:35 +00:00
|
|
|
formatter_factory,
|
2023-01-05 06:04:54 +00:00
|
|
|
pager_cmd: pager_setting(config),
|
2022-11-01 00:31:30 +00:00
|
|
|
paginate: PaginationChoice::Auto,
|
2022-11-01 23:28:45 +00:00
|
|
|
progress_indicator,
|
2022-10-21 01:05:40 +00:00
|
|
|
output: UiOutput::new_terminal(),
|
2023-01-18 01:20:27 +00:00
|
|
|
})
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 01:20:27 +00:00
|
|
|
pub fn reset(&mut self, config: &config::Config) -> Result<(), config::ConfigError> {
|
2023-01-04 12:58:24 +00:00
|
|
|
self.color = use_color(color_setting(config));
|
|
|
|
self.pager_cmd = pager_setting(config);
|
|
|
|
self.progress_indicator = progress_indicator_setting(config);
|
2023-01-21 21:55:34 +00:00
|
|
|
let sanitize = io::stdout().is_tty();
|
2023-01-18 01:20:27 +00:00
|
|
|
self.formatter_factory = FormatterFactory::prepare(config, self.color, sanitize)?;
|
|
|
|
Ok(())
|
2023-01-01 07:00:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 00:31:30 +00:00
|
|
|
/// Sets the pagination value.
|
|
|
|
pub fn set_pagination(&mut self, choice: PaginationChoice) {
|
|
|
|
self.paginate = choice;
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:07:35 +00:00
|
|
|
/// Switches the output to use the pager, if allowed.
|
|
|
|
pub fn request_pager(&mut self) {
|
2022-11-01 00:31:30 +00:00
|
|
|
if self.paginate == PaginationChoice::No {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-18 18:07:35 +00:00
|
|
|
match self.output {
|
2022-12-01 14:50:51 +00:00
|
|
|
UiOutput::Terminal { .. } if io::stdout().is_tty() => {
|
2023-01-04 12:38:21 +00:00
|
|
|
match UiOutput::new_paged(&self.pager_cmd) {
|
2022-12-01 14:50:51 +00:00
|
|
|
Ok(new_output) => {
|
|
|
|
self.output = new_output;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-01-12 06:36:59 +00:00
|
|
|
writeln!(
|
|
|
|
self.warning(),
|
|
|
|
"Failed to spawn pager '{cmd}': {e}",
|
2023-01-05 01:57:35 +00:00
|
|
|
cmd = self.pager_cmd,
|
2023-01-12 06:36:59 +00:00
|
|
|
)
|
2023-01-05 01:57:35 +00:00
|
|
|
.ok();
|
2022-12-01 14:50:51 +00:00
|
|
|
}
|
2022-10-18 18:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 14:50:51 +00:00
|
|
|
UiOutput::Terminal { .. } | UiOutput::Paged { .. } => {}
|
2022-10-18 18:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 17:49:53 +00:00
|
|
|
pub fn color(&self) -> bool {
|
|
|
|
self.color
|
|
|
|
}
|
|
|
|
|
2022-10-07 11:57:35 +00:00
|
|
|
pub fn new_formatter<'output, W: Write + 'output>(
|
2021-06-03 04:43:00 +00:00
|
|
|
&self,
|
2022-10-07 11:57:35 +00:00
|
|
|
output: W,
|
2021-06-03 04:43:00 +00:00
|
|
|
) -> Box<dyn Formatter + 'output> {
|
2022-10-07 03:52:01 +00:00
|
|
|
self.formatter_factory.new_formatter(output)
|
2021-06-03 04:43:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 04:24:44 +00:00
|
|
|
/// Creates a formatter for the locked stdout stream.
|
|
|
|
///
|
|
|
|
/// Labels added to the returned formatter should be removed by caller.
|
|
|
|
/// Otherwise the last color would persist.
|
|
|
|
pub fn stdout_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> {
|
2022-10-21 01:05:40 +00:00
|
|
|
match &self.output {
|
|
|
|
UiOutput::Terminal { stdout, .. } => self.new_formatter(stdout.lock()),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => self.new_formatter(child_stdin),
|
2022-10-07 05:24:35 +00:00
|
|
|
}
|
2022-04-07 06:25:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 04:24:44 +00:00
|
|
|
/// Creates a formatter for the locked stderr stream.
|
|
|
|
pub fn stderr_formatter<'a>(&'a self) -> Box<dyn Formatter + 'a> {
|
2022-10-21 01:05:40 +00:00
|
|
|
match &self.output {
|
|
|
|
UiOutput::Terminal { stderr, .. } => self.new_formatter(stderr.lock()),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => self.new_formatter(child_stdin),
|
2022-10-07 05:24:35 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 23:38:25 +00:00
|
|
|
/// Whether continuous feedback should be displayed for long-running
|
|
|
|
/// operations
|
|
|
|
pub fn use_progress_indicator(&self) -> bool {
|
2022-11-01 23:28:45 +00:00
|
|
|
self.progress_indicator && io::stdout().is_tty()
|
2022-10-21 23:38:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
pub fn write(&mut self, text: &str) -> io::Result<()> {
|
2022-10-07 05:24:35 +00:00
|
|
|
let data = text.as_bytes();
|
2022-10-21 01:05:40 +00:00
|
|
|
match &mut self.output {
|
|
|
|
UiOutput::Terminal { stdout, .. } => stdout.write_all(data),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => child_stdin.write_all(data),
|
2022-10-07 05:24:35 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 23:06:42 +00:00
|
|
|
pub fn write_stderr(&mut self, text: &str) -> io::Result<()> {
|
|
|
|
let data = text.as_bytes();
|
2022-10-21 01:05:40 +00:00
|
|
|
match &mut self.output {
|
|
|
|
UiOutput::Terminal { stderr, .. } => stderr.write_all(data),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => child_stdin.write_all(data),
|
2022-10-31 23:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
pub fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
|
2022-10-21 01:05:40 +00:00
|
|
|
match &mut self.output {
|
|
|
|
UiOutput::Terminal { stdout, .. } => stdout.write_fmt(fmt),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => child_stdin.write_fmt(fmt),
|
2022-10-07 05:24:35 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 06:25:17 +00:00
|
|
|
pub fn hint(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
|
|
|
|
LabeledWriter::new(self.stderr_formatter(), "hint")
|
2022-05-02 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 06:36:59 +00:00
|
|
|
pub fn warning(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
|
|
|
|
LabeledWriter::new(self.stderr_formatter(), "warning")
|
2022-05-02 19:58:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 06:41:38 +00:00
|
|
|
pub fn error(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
|
|
|
|
LabeledWriter::new(self.stderr_formatter(), "error")
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-10-23 20:20:02 +00:00
|
|
|
|
|
|
|
pub fn flush(&mut self) -> io::Result<()> {
|
2022-10-21 01:05:40 +00:00
|
|
|
match &mut self.output {
|
|
|
|
UiOutput::Terminal { stdout, .. } => stdout.flush(),
|
2022-10-18 18:07:35 +00:00
|
|
|
UiOutput::Paged { child_stdin, .. } => child_stdin.flush(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 10:18:53 +00:00
|
|
|
/// Waits for the pager exits. Returns true if the pager exits successfully
|
|
|
|
/// or the output isn't paged.
|
|
|
|
pub fn finalize_pager(&mut self) -> bool {
|
2022-10-18 18:07:35 +00:00
|
|
|
if let UiOutput::Paged {
|
|
|
|
mut child,
|
|
|
|
child_stdin,
|
|
|
|
} = mem::replace(&mut self.output, UiOutput::new_terminal())
|
|
|
|
{
|
|
|
|
drop(child_stdin);
|
2023-02-11 10:18:53 +00:00
|
|
|
match child.wait() {
|
|
|
|
Ok(status) => status.success(),
|
|
|
|
Err(e) => {
|
|
|
|
// It's possible (though unlikely) that this write fails, but
|
|
|
|
// this function gets called so late that there's not much we
|
|
|
|
// can do about it.
|
|
|
|
writeln!(self.error(), "Failed to wait on pager: {e}").ok();
|
|
|
|
false
|
|
|
|
}
|
2022-10-18 18:07:35 +00:00
|
|
|
}
|
2023-02-11 10:18:53 +00:00
|
|
|
} else {
|
|
|
|
true
|
2022-10-23 20:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-23 20:50:03 +00:00
|
|
|
|
2022-11-06 18:15:44 +00:00
|
|
|
pub fn prompt(&mut self, prompt: &str) -> io::Result<String> {
|
2022-11-23 16:14:13 +00:00
|
|
|
if !io::stdout().is_tty() {
|
2022-11-06 18:15:44 +00:00
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::Unsupported,
|
|
|
|
"Cannot prompt for input since the output is not connected to a terminal",
|
|
|
|
));
|
|
|
|
}
|
2022-12-15 02:30:06 +00:00
|
|
|
write!(self, "{prompt}: ")?;
|
2022-11-06 18:15:44 +00:00
|
|
|
self.flush()?;
|
|
|
|
let mut buf = String::new();
|
|
|
|
io::stdin().read_line(&mut buf)?;
|
|
|
|
Ok(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prompt_password(&mut self, prompt: &str) -> io::Result<String> {
|
2022-11-23 16:14:13 +00:00
|
|
|
if !io::stdout().is_tty() {
|
2022-11-06 18:15:44 +00:00
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::Unsupported,
|
|
|
|
"Cannot prompt for input since the output is not connected to a terminal",
|
|
|
|
));
|
|
|
|
}
|
2022-11-23 04:47:53 +00:00
|
|
|
rpassword::prompt_password(format!("{prompt}: "))
|
2022-11-06 18:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 20:50:03 +00:00
|
|
|
pub fn size(&self) -> Option<(u16, u16)> {
|
|
|
|
crossterm::terminal::size().ok()
|
|
|
|
}
|
2022-10-30 02:34:17 +00:00
|
|
|
|
|
|
|
/// Construct a guard object which writes `data` when dropped. Useful for
|
|
|
|
/// restoring terminal state.
|
|
|
|
pub fn output_guard(&self, text: String) -> OutputGuard {
|
|
|
|
OutputGuard {
|
|
|
|
text,
|
2022-10-21 01:05:40 +00:00
|
|
|
output: match self.output {
|
|
|
|
UiOutput::Terminal { .. } => io::stdout(),
|
2022-10-18 18:07:35 +00:00
|
|
|
// TODO we don't actually need to write in this case, so it
|
|
|
|
// might be better to no-op
|
|
|
|
UiOutput::Paged { .. } => io::stdout(),
|
2022-10-30 02:34:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 23:45:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 01:05:40 +00:00
|
|
|
enum UiOutput {
|
2022-10-18 18:07:35 +00:00
|
|
|
Terminal {
|
|
|
|
stdout: Stdout,
|
|
|
|
stderr: Stderr,
|
|
|
|
},
|
|
|
|
Paged {
|
|
|
|
child: Child,
|
|
|
|
child_stdin: ChildStdin,
|
|
|
|
},
|
2022-10-07 04:24:44 +00:00
|
|
|
}
|
2022-10-30 02:34:17 +00:00
|
|
|
|
2022-10-21 01:05:40 +00:00
|
|
|
impl UiOutput {
|
|
|
|
fn new_terminal() -> UiOutput {
|
|
|
|
UiOutput::Terminal {
|
|
|
|
stdout: io::stdout(),
|
|
|
|
stderr: io::stderr(),
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 18:07:35 +00:00
|
|
|
|
2023-02-06 04:13:44 +00:00
|
|
|
fn new_paged(pager_cmd: &CommandNameAndArgs) -> io::Result<UiOutput> {
|
2022-12-01 12:39:34 +00:00
|
|
|
let mut child = pager_cmd.to_command().stdin(Stdio::piped()).spawn()?;
|
2022-12-01 14:50:51 +00:00
|
|
|
let child_stdin = child.stdin.take().unwrap();
|
|
|
|
Ok(UiOutput::Paged { child, child_stdin })
|
2022-10-18 18:07:35 +00:00
|
|
|
}
|
2022-10-21 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:34:17 +00:00
|
|
|
pub struct OutputGuard {
|
|
|
|
text: String,
|
|
|
|
output: Stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for OutputGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
_ = self.output.write_all(self.text.as_bytes());
|
|
|
|
}
|
|
|
|
}
|