annotate rust/rhg/src/color.rs @ 50764:8ff187fbbfea

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