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.
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2021-04-07 06:05:16 +00:00
|
|
|
use std::io;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::io::{Error, Read, Write};
|
2022-10-07 03:52:01 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu_lib::settings::UserSettings;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
// Lets the caller label strings and translates the labels to colors
|
2021-06-02 22:50:08 +00:00
|
|
|
pub trait Formatter: Write {
|
2021-04-07 06:05:16 +00:00
|
|
|
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
|
|
|
|
self.write_all(data)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
fn write_str(&mut self, text: &str) -> io::Result<()> {
|
|
|
|
self.write_all(text.as_bytes())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
fn write_from_reader(&mut self, reader: &mut dyn Read) -> io::Result<()> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut buffer = vec![];
|
|
|
|
reader.read_to_end(&mut buffer).unwrap();
|
2021-11-10 18:46:10 +00:00
|
|
|
self.write_all(&buffer)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 11:16:41 +00:00
|
|
|
fn add_label(&mut self, label: &str) -> io::Result<()>;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
fn remove_label(&mut self) -> io::Result<()>;
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 03:52:01 +00:00
|
|
|
/// Creates `Formatter` instances with preconfigured parameters.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct FormatterFactory {
|
|
|
|
kind: FormatterFactoryKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum FormatterFactoryKind {
|
|
|
|
PlainText,
|
|
|
|
Color {
|
|
|
|
colors: Arc<HashMap<String, String>>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FormatterFactory {
|
|
|
|
pub fn prepare(settings: &UserSettings, color: bool) -> Self {
|
|
|
|
let kind = if color {
|
|
|
|
let colors = Arc::new(config_colors(settings));
|
|
|
|
FormatterFactoryKind::Color { colors }
|
|
|
|
} else {
|
|
|
|
FormatterFactoryKind::PlainText
|
|
|
|
};
|
|
|
|
FormatterFactory { kind }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_formatter<'output>(
|
|
|
|
&self,
|
|
|
|
output: Box<dyn Write + 'output>,
|
|
|
|
) -> Box<dyn Formatter + 'output> {
|
|
|
|
match &self.kind {
|
|
|
|
FormatterFactoryKind::PlainText => Box::new(PlainTextFormatter::new(output)),
|
|
|
|
FormatterFactoryKind::Color { colors } => {
|
|
|
|
Box::new(ColorFormatter::new(output, colors.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_color(&self) -> bool {
|
|
|
|
matches!(&self.kind, FormatterFactoryKind::Color { .. })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
pub struct PlainTextFormatter<'output> {
|
|
|
|
output: Box<dyn Write + 'output>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl<'output> PlainTextFormatter<'output> {
|
|
|
|
pub fn new(output: Box<dyn Write + 'output>) -> PlainTextFormatter<'output> {
|
2020-12-12 08:00:42 +00:00
|
|
|
Self { output }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl Write for PlainTextFormatter<'_> {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
|
|
|
|
self.output.write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Error> {
|
|
|
|
self.output.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl Formatter for PlainTextFormatter<'_> {
|
2022-10-06 11:16:41 +00:00
|
|
|
fn add_label(&mut self, _label: &str) -> io::Result<()> {
|
2021-04-07 06:05:16 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
fn remove_label(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
pub struct ColorFormatter<'output> {
|
|
|
|
output: Box<dyn Write + 'output>,
|
2022-10-07 03:52:01 +00:00
|
|
|
colors: Arc<HashMap<String, String>>,
|
2020-12-12 08:00:42 +00:00
|
|
|
labels: Vec<String>,
|
|
|
|
cached_colors: HashMap<Vec<String>, Vec<u8>>,
|
|
|
|
current_color: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config_colors(user_settings: &UserSettings) -> HashMap<String, String> {
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
result.insert(String::from("error"), String::from("red"));
|
2022-05-02 19:58:32 +00:00
|
|
|
result.insert(String::from("warning"), String::from("yellow"));
|
|
|
|
result.insert(String::from("hint"), String::from("blue"));
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
result.insert(String::from("commit_id"), String::from("blue"));
|
|
|
|
result.insert(String::from("commit_id open"), String::from("green"));
|
|
|
|
result.insert(String::from("change_id"), String::from("magenta"));
|
|
|
|
result.insert(String::from("author"), String::from("yellow"));
|
2021-04-26 21:54:35 +00:00
|
|
|
result.insert(String::from("author timestamp"), String::from("cyan"));
|
2020-12-12 08:00:42 +00:00
|
|
|
result.insert(String::from("committer"), String::from("yellow"));
|
2021-04-26 21:54:35 +00:00
|
|
|
result.insert(String::from("committer timestamp"), String::from("cyan"));
|
2022-09-18 21:46:12 +00:00
|
|
|
result.insert(String::from("working_copies"), String::from("magenta"));
|
2021-08-11 15:46:57 +00:00
|
|
|
result.insert(String::from("branch"), String::from("magenta"));
|
2021-07-15 08:31:48 +00:00
|
|
|
result.insert(String::from("branches"), String::from("magenta"));
|
|
|
|
result.insert(String::from("tags"), String::from("magenta"));
|
2021-01-03 08:26:57 +00:00
|
|
|
result.insert(String::from("git_refs"), String::from("magenta"));
|
2021-11-29 05:33:37 +00:00
|
|
|
result.insert(String::from("git_head"), String::from("magenta"));
|
2020-12-12 08:00:42 +00:00
|
|
|
result.insert(String::from("divergent"), String::from("red"));
|
|
|
|
result.insert(String::from("conflict"), String::from("red"));
|
|
|
|
|
2021-10-27 20:40:58 +00:00
|
|
|
// TODO: This near-duplication of the lines above is unfortunate. Should we
|
|
|
|
// allow adding and clearing the "bright" bit somehow? Or should we instead
|
|
|
|
// use a different background color? (We don't have support for background
|
|
|
|
// colors yet.)
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy commit_id"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright blue"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy commit_id open"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright green"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy change_id"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy author"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright yellow"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy author timestamp"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright cyan"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy committer"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright yellow"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy committer timestamp"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright cyan"),
|
|
|
|
);
|
2022-02-03 01:00:05 +00:00
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy working_copies"),
|
2022-02-03 01:00:05 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
2021-10-27 20:40:58 +00:00
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy branch"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy branches"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy tags"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy git_refs"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright magenta"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy divergent"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright red"),
|
|
|
|
);
|
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy conflict"),
|
2021-10-27 20:40:58 +00:00
|
|
|
String::from("bright red"),
|
|
|
|
);
|
2021-10-27 23:28:07 +00:00
|
|
|
result.insert(
|
2022-09-18 21:46:12 +00:00
|
|
|
String::from("working_copy description"),
|
2021-10-27 23:28:07 +00:00
|
|
|
String::from("bright white"),
|
|
|
|
);
|
2021-10-27 20:40:58 +00:00
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
result.insert(String::from("diff header"), String::from("yellow"));
|
2021-10-09 16:49:51 +00:00
|
|
|
result.insert(
|
|
|
|
String::from("diff file_header"),
|
|
|
|
String::from("bright white"),
|
|
|
|
);
|
|
|
|
result.insert(String::from("diff hunk_header"), String::from("cyan"));
|
2021-10-27 20:38:29 +00:00
|
|
|
result.insert(String::from("diff removed"), String::from("red"));
|
|
|
|
result.insert(String::from("diff added"), String::from("green"));
|
2021-10-27 19:13:54 +00:00
|
|
|
result.insert(String::from("diff modified"), String::from("cyan"));
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
result.insert(String::from("op-log id"), String::from("blue"));
|
|
|
|
result.insert(String::from("op-log user"), String::from("yellow"));
|
2021-11-08 05:02:32 +00:00
|
|
|
result.insert(String::from("op-log time"), String::from("cyan"));
|
2021-10-27 23:09:05 +00:00
|
|
|
result.insert(String::from("op-log tags"), String::from("white"));
|
|
|
|
|
|
|
|
result.insert(String::from("op-log head id"), String::from("bright blue"));
|
2021-10-28 04:25:45 +00:00
|
|
|
result.insert(
|
|
|
|
String::from("op-log head user"),
|
|
|
|
String::from("bright yellow"),
|
|
|
|
);
|
|
|
|
result.insert(
|
|
|
|
String::from("op-log head time"),
|
2021-11-08 05:02:32 +00:00
|
|
|
String::from("bright cyan"),
|
2021-10-28 04:25:45 +00:00
|
|
|
);
|
|
|
|
result.insert(
|
|
|
|
String::from("op-log head description"),
|
|
|
|
String::from("bright white"),
|
|
|
|
);
|
|
|
|
result.insert(
|
|
|
|
String::from("op-log head tags"),
|
|
|
|
String::from("bright white"),
|
|
|
|
);
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
if let Ok(table) = user_settings.config().get_table("colors") {
|
|
|
|
for (key, value) in table {
|
|
|
|
result.insert(key, value.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl<'output> ColorFormatter<'output> {
|
|
|
|
pub fn new(
|
|
|
|
output: Box<dyn Write + 'output>,
|
2022-10-07 03:52:01 +00:00
|
|
|
colors: Arc<HashMap<String, String>>,
|
2021-06-02 22:50:08 +00:00
|
|
|
) -> ColorFormatter<'output> {
|
|
|
|
ColorFormatter {
|
2020-12-12 08:00:42 +00:00
|
|
|
output,
|
2022-10-07 03:52:01 +00:00
|
|
|
colors,
|
2020-12-12 08:00:42 +00:00
|
|
|
labels: vec![],
|
|
|
|
cached_colors: HashMap::new(),
|
|
|
|
current_color: b"\x1b[0m".to_vec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_color(&mut self) -> Vec<u8> {
|
|
|
|
if let Some(cached) = self.cached_colors.get(&self.labels) {
|
|
|
|
cached.clone()
|
|
|
|
} else {
|
|
|
|
let mut best_match = (-1, "");
|
2022-10-07 03:52:01 +00:00
|
|
|
for (key, value) in self.colors.as_ref() {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut num_matching = 0;
|
|
|
|
let mut valid = true;
|
|
|
|
for label in key.split_whitespace() {
|
|
|
|
if !self.labels.contains(&label.to_string()) {
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
num_matching += 1;
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if num_matching >= best_match.0 {
|
|
|
|
best_match = (num_matching, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:18:38 +00:00
|
|
|
let color = self.color_for_name(best_match.1);
|
2020-12-12 08:00:42 +00:00
|
|
|
self.cached_colors
|
|
|
|
.insert(self.labels.clone(), color.clone());
|
|
|
|
color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn color_for_name(&self, color_name: &str) -> Vec<u8> {
|
|
|
|
match color_name {
|
|
|
|
"black" => b"\x1b[30m".to_vec(),
|
|
|
|
"red" => b"\x1b[31m".to_vec(),
|
|
|
|
"green" => b"\x1b[32m".to_vec(),
|
|
|
|
"yellow" => b"\x1b[33m".to_vec(),
|
|
|
|
"blue" => b"\x1b[34m".to_vec(),
|
|
|
|
"magenta" => b"\x1b[35m".to_vec(),
|
|
|
|
"cyan" => b"\x1b[36m".to_vec(),
|
|
|
|
"white" => b"\x1b[37m".to_vec(),
|
|
|
|
"bright black" => b"\x1b[1;30m".to_vec(),
|
|
|
|
"bright red" => b"\x1b[1;31m".to_vec(),
|
|
|
|
"bright green" => b"\x1b[1;32m".to_vec(),
|
|
|
|
"bright yellow" => b"\x1b[1;33m".to_vec(),
|
|
|
|
"bright blue" => b"\x1b[1;34m".to_vec(),
|
|
|
|
"bright magenta" => b"\x1b[1;35m".to_vec(),
|
|
|
|
"bright cyan" => b"\x1b[1;36m".to_vec(),
|
|
|
|
"bright white" => b"\x1b[1;37m".to_vec(),
|
|
|
|
_ => b"\x1b[0m".to_vec(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl Write for ColorFormatter<'_> {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
|
|
|
|
self.output.write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), Error> {
|
|
|
|
self.output.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 22:50:08 +00:00
|
|
|
impl Formatter for ColorFormatter<'_> {
|
2022-10-06 11:16:41 +00:00
|
|
|
fn add_label(&mut self, label: &str) -> io::Result<()> {
|
|
|
|
self.labels.push(label.to_owned());
|
2020-12-12 08:00:42 +00:00
|
|
|
let new_color = self.current_color();
|
|
|
|
if new_color != self.current_color {
|
2021-04-07 06:05:16 +00:00
|
|
|
self.output.write_all(&new_color)?;
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
self.current_color = new_color;
|
2021-04-07 06:05:16 +00:00
|
|
|
Ok(())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:05:16 +00:00
|
|
|
fn remove_label(&mut self) -> io::Result<()> {
|
2020-12-12 08:00:42 +00:00
|
|
|
self.labels.pop();
|
|
|
|
let new_color = self.current_color();
|
|
|
|
if new_color != self.current_color {
|
2021-04-07 06:05:16 +00:00
|
|
|
self.output.write_all(&new_color)?;
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
self.current_color = new_color;
|
2021-04-07 06:05:16 +00:00
|
|
|
Ok(())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|