| 1 |
use std::collections::HashMap; |
| 2 |
|
| 3 |
use format_bytes::write_bytes; |
| 4 |
use hg::config::Config; |
| 5 |
use hg::config::ConfigOrigin; |
| 6 |
use hg::errors::HgError; |
| 7 |
|
| 8 |
use crate::ui::formatted; |
| 9 |
use crate::ui::plain; |
| 10 |
|
| 11 |
pub type Effect = u32; |
| 12 |
|
| 13 |
pub type EffectsMap = HashMap<Vec<u8>, Vec<Effect>>; |
| 14 |
|
| 15 |
macro_rules! effects { |
| 16 |
($( $name: ident: $value: expr ,)+) => { |
| 17 |
|
| 18 |
#[allow(non_upper_case_globals)] |
| 19 |
mod effects { |
| 20 |
$( |
| 21 |
pub const $name: super::Effect = $value; |
| 22 |
)+ |
| 23 |
} |
| 24 |
|
| 25 |
fn effect(name: &[u8]) -> Option<Effect> { |
| 26 |
$( |
| 27 |
if name == stringify!($name).as_bytes() { |
| 28 |
Some(effects::$name) |
| 29 |
} else |
| 30 |
)+ |
| 31 |
{ |
| 32 |
None |
| 33 |
} |
| 34 |
} |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
effects! { |
| 39 |
none: 0, |
| 40 |
black: 30, |
| 41 |
red: 31, |
| 42 |
green: 32, |
| 43 |
yellow: 33, |
| 44 |
blue: 34, |
| 45 |
magenta: 35, |
| 46 |
cyan: 36, |
| 47 |
white: 37, |
| 48 |
bold: 1, |
| 49 |
italic: 3, |
| 50 |
underline: 4, |
| 51 |
inverse: 7, |
| 52 |
dim: 2, |
| 53 |
black_background: 40, |
| 54 |
red_background: 41, |
| 55 |
green_background: 42, |
| 56 |
yellow_background: 43, |
| 57 |
blue_background: 44, |
| 58 |
purple_background: 45, |
| 59 |
cyan_background: 46, |
| 60 |
white_background: 47, |
| 61 |
} |
| 62 |
|
| 63 |
macro_rules! default_styles { |
| 64 |
($( $key: expr => [$($value: expr),*],)+) => { |
| 65 |
fn default_styles() -> EffectsMap { |
| 66 |
use effects::*; |
| 67 |
let mut map = HashMap::new(); |
| 68 |
$( |
| 69 |
map.insert($key[..].to_owned(), vec![$( $value ),*]); |
| 70 |
)+ |
| 71 |
map |
| 72 |
} |
| 73 |
}; |
| 74 |
} |
| 75 |
|
| 76 |
default_styles! { |
| 77 |
b"grep.match" => [red, bold], |
| 78 |
b"grep.linenumber" => [green], |
| 79 |
b"grep.rev" => [blue], |
| 80 |
b"grep.sep" => [cyan], |
| 81 |
b"grep.filename" => [magenta], |
| 82 |
b"grep.user" => [magenta], |
| 83 |
b"grep.date" => [magenta], |
| 84 |
b"grep.inserted" => [green, bold], |
| 85 |
b"grep.deleted" => [red, bold], |
| 86 |
b"bookmarks.active" => [green], |
| 87 |
b"branches.active" => [none], |
| 88 |
b"branches.closed" => [black, bold], |
| 89 |
b"branches.current" => [green], |
| 90 |
b"branches.inactive" => [none], |
| 91 |
b"diff.changed" => [white], |
| 92 |
b"diff.deleted" => [red], |
| 93 |
b"diff.deleted.changed" => [red, bold, underline], |
| 94 |
b"diff.deleted.unchanged" => [red], |
| 95 |
b"diff.diffline" => [bold], |
| 96 |
b"diff.extended" => [cyan, bold], |
| 97 |
b"diff.file_a" => [red, bold], |
| 98 |
b"diff.file_b" => [green, bold], |
| 99 |
b"diff.hunk" => [magenta], |
| 100 |
b"diff.inserted" => [green], |
| 101 |
b"diff.inserted.changed" => [green, bold, underline], |
| 102 |
b"diff.inserted.unchanged" => [green], |
| 103 |
b"diff.tab" => [], |
| 104 |
b"diff.trailingwhitespace" => [bold, red_background], |
| 105 |
b"changeset.public" => [], |
| 106 |
b"changeset.draft" => [], |
| 107 |
b"changeset.secret" => [], |
| 108 |
b"diffstat.deleted" => [red], |
| 109 |
b"diffstat.inserted" => [green], |
| 110 |
b"formatvariant.name.mismatchconfig" => [red], |
| 111 |
b"formatvariant.name.mismatchdefault" => [yellow], |
| 112 |
b"formatvariant.name.uptodate" => [green], |
| 113 |
b"formatvariant.repo.mismatchconfig" => [red], |
| 114 |
b"formatvariant.repo.mismatchdefault" => [yellow], |
| 115 |
b"formatvariant.repo.uptodate" => [green], |
| 116 |
b"formatvariant.config.special" => [yellow], |
| 117 |
b"formatvariant.config.default" => [green], |
| 118 |
b"formatvariant.default" => [], |
| 119 |
b"histedit.remaining" => [red, bold], |
| 120 |
b"ui.addremove.added" => [green], |
| 121 |
b"ui.addremove.removed" => [red], |
| 122 |
b"ui.error" => [red], |
| 123 |
b"ui.prompt" => [yellow], |
| 124 |
b"log.changeset" => [yellow], |
| 125 |
b"patchbomb.finalsummary" => [], |
| 126 |
b"patchbomb.from" => [magenta], |
| 127 |
b"patchbomb.to" => [cyan], |
| 128 |
b"patchbomb.subject" => [green], |
| 129 |
b"patchbomb.diffstats" => [], |
| 130 |
b"rebase.rebased" => [blue], |
| 131 |
b"rebase.remaining" => [red, bold], |
| 132 |
b"resolve.resolved" => [green, bold], |
| 133 |
b"resolve.unresolved" => [red, bold], |
| 134 |
b"shelve.age" => [cyan], |
| 135 |
b"shelve.newest" => [green, bold], |
| 136 |
b"shelve.name" => [blue, bold], |
| 137 |
b"status.added" => [green, bold], |
| 138 |
b"status.clean" => [none], |
| 139 |
b"status.copied" => [none], |
| 140 |
b"status.deleted" => [cyan, bold, underline], |
| 141 |
b"status.ignored" => [black, bold], |
| 142 |
b"status.modified" => [blue, bold], |
| 143 |
b"status.removed" => [red, bold], |
| 144 |
b"status.unknown" => [magenta, bold, underline], |
| 145 |
b"tags.normal" => [green], |
| 146 |
b"tags.local" => [black, bold], |
| 147 |
b"upgrade-repo.requirement.preserved" => [cyan], |
| 148 |
b"upgrade-repo.requirement.added" => [green], |
| 149 |
b"upgrade-repo.requirement.removed" => [red], |
| 150 |
} |
| 151 |
|
| 152 |
fn parse_effect(config_key: &[u8], effect_name: &[u8]) -> Option<Effect> { |
| 153 |
let found = effect(effect_name); |
| 154 |
if found.is_none() { |
| 155 |
// TODO: have some API for warnings |
| 156 |
// TODO: handle IO errors during warnings |
| 157 |
let stderr = std::io::stderr(); |
| 158 |
let _ = write_bytes!( |
| 159 |
&mut stderr.lock(), |
| 160 |
b"ignoring unknown color/effect '{}' \ |
| 161 |
(configured in color.{})\n", |
| 162 |
effect_name, |
| 163 |
config_key, |
| 164 |
); |
| 165 |
} |
| 166 |
found |
| 167 |
} |
| 168 |
|
| 169 |
fn effects_from_config(config: &Config) -> EffectsMap { |
| 170 |
let mut styles = default_styles(); |
| 171 |
for (key, _value) in config.iter_section(b"color") { |
| 172 |
if !key.contains(&b'.') |
| 173 |
|| key.starts_with(b"color.") |
| 174 |
|| key.starts_with(b"terminfo.") |
| 175 |
{ |
| 176 |
continue; |
| 177 |
} |
| 178 |
// `unwrap` shouldn’t panic since we just got this key from |
| 179 |
// iteration |
| 180 |
let list = config.get_list(b"color", key).unwrap(); |
| 181 |
let parsed = |
| 182 |
list.iter().filter_map(|name| parse_effect(key, name)).collect(); |
| 183 |
styles.insert(key.to_owned(), parsed); |
| 184 |
} |
| 185 |
styles |
| 186 |
} |
| 187 |
|
| 188 |
enum ColorMode { |
| 189 |
// TODO: support other modes |
| 190 |
Ansi, |
| 191 |
} |
| 192 |
|
| 193 |
impl ColorMode { |
| 194 |
// Similar to _modesetup in mercurial/color.py |
| 195 |
fn get(config: &Config) -> Result<Option<Self>, HgError> { |
| 196 |
if plain(Some("color")) { |
| 197 |
return Ok(None); |
| 198 |
} |
| 199 |
let enabled_default = b"auto"; |
| 200 |
// `origin` is only used when `!auto`, so its default doesn’t matter |
| 201 |
let (enabled, origin) = config |
| 202 |
.get_with_origin(b"ui", b"color") |
| 203 |
.unwrap_or((enabled_default, &ConfigOrigin::CommandLineColor)); |
| 204 |
if enabled == b"debug" { |
| 205 |
return Err(HgError::unsupported("debug color mode")); |
| 206 |
} |
| 207 |
let auto = enabled == b"auto"; |
| 208 |
let always = if !auto { |
| 209 |
let enabled_bool = config.get_bool(b"ui", b"color")?; |
| 210 |
if !enabled_bool { |
| 211 |
return Ok(None); |
| 212 |
} |
| 213 |
enabled == b"always" || *origin == ConfigOrigin::CommandLineColor |
| 214 |
} else { |
| 215 |
false |
| 216 |
}; |
| 217 |
let formatted = always |
| 218 |
|| (std::env::var_os("TERM").unwrap_or_default() != "dumb" |
| 219 |
&& formatted(config)?); |
| 220 |
|
| 221 |
let mode_default = b"auto"; |
| 222 |
let mode = config.get(b"color", b"mode").unwrap_or(mode_default); |
| 223 |
|
| 224 |
if formatted { |
| 225 |
match mode { |
| 226 |
b"ansi" | b"auto" => Ok(Some(ColorMode::Ansi)), |
| 227 |
// TODO: support other modes |
| 228 |
_ => Err(HgError::unsupported(format!( |
| 229 |
"color mode {}", |
| 230 |
String::from_utf8_lossy(mode) |
| 231 |
))), |
| 232 |
} |
| 233 |
} else { |
| 234 |
Ok(None) |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
pub struct ColorConfig { |
| 240 |
pub styles: EffectsMap, |
| 241 |
} |
| 242 |
|
| 243 |
impl ColorConfig { |
| 244 |
// Similar to _modesetup in mercurial/color.py |
| 245 |
pub fn new(config: &Config) -> Result<Option<Self>, HgError> { |
| 246 |
Ok(ColorMode::get(config)?.map(|ColorMode::Ansi| ColorConfig { |
| 247 |
styles: effects_from_config(config), |
| 248 |
})) |
| 249 |
} |
| 250 |
} |