2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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};
|
2021-06-09 23:45:47 +00:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2022-06-08 02:43:12 +00:00
|
|
|
use std::str::FromStr;
|
2022-10-21 04:08:23 +00:00
|
|
|
use std::{fmt, io};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-06-03 05:02:06 +00:00
|
|
|
use atty::Stream;
|
2022-10-21 04:08:23 +00:00
|
|
|
use jujutsu_lib::file_util;
|
2022-10-20 08:57:03 +00:00
|
|
|
use jujutsu_lib::repo_path::{RepoPath, RepoPathComponent};
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu_lib::settings::UserSettings;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-10-07 03:52:01 +00:00
|
|
|
use crate::formatter::{Formatter, FormatterFactory};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-10-19 00:44:10 +00:00
|
|
|
pub struct Ui {
|
2020-12-12 08:00:42 +00:00
|
|
|
cwd: PathBuf,
|
2022-10-07 03:52:01 +00:00
|
|
|
formatter_factory: FormatterFactory,
|
2022-10-19 00:44:10 +00:00
|
|
|
output_pair: UiOutputPair,
|
2020-12-12 08:00:42 +00:00
|
|
|
settings: UserSettings,
|
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_setting(settings: &UserSettings) -> ColorChoice {
|
|
|
|
settings
|
2022-03-19 17:00:13 +00:00
|
|
|
.config()
|
|
|
|
.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-10-20 00:33:51 +00:00
|
|
|
ColorChoice::Auto => atty::is(Stream::Stdout),
|
2022-03-19 17:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:44:10 +00:00
|
|
|
impl Ui {
|
|
|
|
pub fn for_terminal(settings: UserSettings) -> Ui {
|
2020-12-12 08:00:42 +00:00
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2022-10-19 08:31:28 +00:00
|
|
|
Self::with_cwd(cwd, settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_cwd(cwd: PathBuf, settings: UserSettings) -> Ui {
|
2022-10-20 00:33:51 +00:00
|
|
|
let color = use_color(color_setting(&settings));
|
2022-10-07 05:24:35 +00:00
|
|
|
let formatter_factory = FormatterFactory::prepare(&settings, color);
|
|
|
|
Ui {
|
|
|
|
cwd,
|
|
|
|
formatter_factory,
|
|
|
|
output_pair: UiOutputPair::Terminal {
|
|
|
|
stdout: io::stdout(),
|
|
|
|
stderr: io::stderr(),
|
|
|
|
},
|
|
|
|
settings,
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 03:11:02 +00:00
|
|
|
/// Reconfigures the underlying outputs with the new color choice.
|
2022-10-08 06:37:04 +00:00
|
|
|
pub fn reset_color(&mut self, choice: ColorChoice) {
|
2022-10-20 00:33:51 +00:00
|
|
|
let color = use_color(choice);
|
2022-10-07 03:52:01 +00:00
|
|
|
if self.formatter_factory.is_color() != color {
|
2022-10-07 04:24:44 +00:00
|
|
|
self.formatter_factory = FormatterFactory::prepare(&self.settings, color);
|
2022-06-09 03:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn cwd(&self) -> &Path {
|
|
|
|
&self.cwd
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn settings(&self) -> &UserSettings {
|
|
|
|
&self.settings
|
|
|
|
}
|
|
|
|
|
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-07 05:24:35 +00:00
|
|
|
match &self.output_pair {
|
|
|
|
UiOutputPair::Terminal { stdout, .. } => self.new_formatter(stdout.lock()),
|
|
|
|
}
|
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-07 05:24:35 +00:00
|
|
|
match &self.output_pair {
|
|
|
|
UiOutputPair::Terminal { stderr, .. } => self.new_formatter(stderr.lock()),
|
|
|
|
}
|
2020-12-12 08:00:42 +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();
|
|
|
|
match &mut self.output_pair {
|
|
|
|
UiOutputPair::Terminal { stdout, .. } => stdout.write_all(data),
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
pub fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
|
2022-10-07 05:24:35 +00:00
|
|
|
match &mut self.output_pair {
|
|
|
|
UiOutputPair::Terminal { stdout, .. } => stdout.write_fmt(fmt),
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 15:06:44 +00:00
|
|
|
pub fn write_hint(&mut self, text: impl AsRef<str>) -> io::Result<()> {
|
|
|
|
let mut formatter = self.stderr_formatter();
|
2022-10-06 11:16:41 +00:00
|
|
|
formatter.add_label("hint")?;
|
2022-05-02 15:06:44 +00:00
|
|
|
formatter.write_str(text.as_ref())?;
|
|
|
|
formatter.remove_label()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-02 19:58:32 +00:00
|
|
|
pub fn write_warn(&mut self, text: impl AsRef<str>) -> io::Result<()> {
|
|
|
|
let mut formatter = self.stderr_formatter();
|
2022-10-06 11:16:41 +00:00
|
|
|
formatter.add_label("warning")?;
|
2022-05-02 19:58:32 +00:00
|
|
|
formatter.write_str(text.as_ref())?;
|
|
|
|
formatter.remove_label()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
pub fn write_error(&mut self, text: &str) -> io::Result<()> {
|
2022-04-07 06:25:01 +00:00
|
|
|
let mut formatter = self.stderr_formatter();
|
2022-10-06 11:16:41 +00:00
|
|
|
formatter.add_label("error")?;
|
2021-06-02 22:50:08 +00:00
|
|
|
formatter.write_str(text)?;
|
|
|
|
formatter.remove_label()?;
|
2021-04-07 06:05:16 +00:00
|
|
|
Ok(())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 23:45:47 +00:00
|
|
|
/// Parses a path relative to cwd into a RepoPath relative to wc_path
|
|
|
|
pub fn parse_file_path(
|
|
|
|
&self,
|
|
|
|
wc_path: &Path,
|
|
|
|
input: &str,
|
|
|
|
) -> Result<RepoPath, FilePathParseError> {
|
2022-10-21 04:08:23 +00:00
|
|
|
let abs_input_path = file_util::normalize_path(&self.cwd.join(input));
|
|
|
|
let repo_relative_path = file_util::relative_path(wc_path, &abs_input_path);
|
2022-10-20 08:57:03 +00:00
|
|
|
if repo_relative_path == Path::new(".") {
|
|
|
|
return Ok(RepoPath::root());
|
2021-06-09 23:45:47 +00:00
|
|
|
}
|
2022-10-20 08:57:03 +00:00
|
|
|
let components = repo_relative_path
|
|
|
|
.components()
|
|
|
|
.map(|c| match c {
|
|
|
|
Component::Normal(a) => Ok(RepoPathComponent::from(a.to_str().unwrap())),
|
|
|
|
_ => Err(FilePathParseError::InputNotInRepo(input.to_string())),
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
Ok(RepoPath::from_components(components))
|
2021-06-09 23:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:44:10 +00:00
|
|
|
enum UiOutputPair {
|
2022-10-19 08:31:28 +00:00
|
|
|
Terminal { stdout: Stdout, stderr: Stderr },
|
2022-10-07 04:24:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 23:45:47 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
|
|
|
pub enum FilePathParseError {
|
|
|
|
InputNotInRepo(String),
|
2021-05-16 18:06:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 04:36:56 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-07 03:29:39 +00:00
|
|
|
use jujutsu_lib::testutils;
|
|
|
|
|
2022-04-22 04:36:56 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_file_path_wc_in_cwd() {
|
2022-09-07 03:29:39 +00:00
|
|
|
let temp_dir = testutils::new_temp_dir();
|
2022-04-22 04:36:56 +00:00
|
|
|
let cwd_path = temp_dir.path().join("repo");
|
|
|
|
let wc_path = cwd_path.clone();
|
2022-10-20 08:57:03 +00:00
|
|
|
let ui = Ui::with_cwd(cwd_path.clone(), UserSettings::default());
|
2022-04-22 04:36:56 +00:00
|
|
|
|
|
|
|
assert_eq!(ui.parse_file_path(&wc_path, ""), Ok(RepoPath::root()));
|
|
|
|
assert_eq!(ui.parse_file_path(&wc_path, "."), Ok(RepoPath::root()));
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "file"),
|
|
|
|
Ok(RepoPath::from_internal_string("file"))
|
|
|
|
);
|
|
|
|
// Both slash and the platform's separator are allowed
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, &format!("dir{}file", std::path::MAIN_SEPARATOR)),
|
|
|
|
Ok(RepoPath::from_internal_string("dir/file"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "dir/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("dir/file"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, ".."),
|
|
|
|
Err(FilePathParseError::InputNotInRepo("..".to_string()))
|
|
|
|
);
|
2022-10-20 08:57:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&cwd_path, "../repo"),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&cwd_path, "../repo/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("file"))
|
|
|
|
);
|
|
|
|
// Input may be absolute path with ".."
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&cwd_path, cwd_path.join("../repo").to_str().unwrap()),
|
|
|
|
Ok(RepoPath::root())
|
|
|
|
);
|
2022-04-22 04:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_file_path_wc_in_cwd_parent() {
|
2022-09-07 03:29:39 +00:00
|
|
|
let temp_dir = testutils::new_temp_dir();
|
2022-04-22 04:36:56 +00:00
|
|
|
let cwd_path = temp_dir.path().join("dir");
|
|
|
|
let wc_path = cwd_path.parent().unwrap().to_path_buf();
|
2022-10-19 08:31:28 +00:00
|
|
|
let ui = Ui::with_cwd(cwd_path, UserSettings::default());
|
2022-04-22 04:36:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, ""),
|
|
|
|
Ok(RepoPath::from_internal_string("dir"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "."),
|
|
|
|
Ok(RepoPath::from_internal_string("dir"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "file"),
|
|
|
|
Ok(RepoPath::from_internal_string("dir/file"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "subdir/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("dir/subdir/file"))
|
|
|
|
);
|
|
|
|
assert_eq!(ui.parse_file_path(&wc_path, ".."), Ok(RepoPath::root()));
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "../.."),
|
|
|
|
Err(FilePathParseError::InputNotInRepo("../..".to_string()))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "../other-dir/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("other-dir/file"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_file_path_wc_in_cwd_child() {
|
2022-09-07 03:29:39 +00:00
|
|
|
let temp_dir = testutils::new_temp_dir();
|
2022-04-22 04:36:56 +00:00
|
|
|
let cwd_path = temp_dir.path().join("cwd");
|
|
|
|
let wc_path = cwd_path.join("repo");
|
2022-10-19 08:31:28 +00:00
|
|
|
let ui = Ui::with_cwd(cwd_path, UserSettings::default());
|
2022-04-22 04:36:56 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, ""),
|
|
|
|
Err(FilePathParseError::InputNotInRepo("".to_string()))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "not-repo"),
|
|
|
|
Err(FilePathParseError::InputNotInRepo("not-repo".to_string()))
|
|
|
|
);
|
|
|
|
assert_eq!(ui.parse_file_path(&wc_path, "repo"), Ok(RepoPath::root()));
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "repo/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("file"))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ui.parse_file_path(&wc_path, "repo/dir/file"),
|
|
|
|
Ok(RepoPath::from_internal_string("dir/file"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|