Skip to content

Commit a682673

Browse files
committed
Load the JULIA_*_COLOR env vars for compat
As we change more code to use the StyledStrings approach, it would be nice to try to maintain compatibility with old user customisations as much as possible.
1 parent 61e7b10 commit a682673

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/StyledStrings.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ include("faces.jl")
1212
include("regioniterator.jl")
1313
include("io.jl")
1414
include("stylemacro.jl")
15+
include("legacy.jl")
1516

1617
function __init__()
1718
userfaces = joinpath(first(DEPOT_PATH), "config", "faces.toml")
1819
isfile(userfaces) && loaduserfaces!(userfaces)
20+
Legacy.load_env_colors!()
1921
end
2022

2123
if Base.generating_output()

src/legacy.jl

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
# StyledStrings provides a new way of handling colored output, however the
4+
# ~decade of colored printing in Julia before now has left us with a legacy,
5+
# and we should try to maintain compatibility as much as reasonable possible.
6+
7+
module Legacy
8+
9+
using ..StyledStrings: SimpleColor, Face, loadface!
10+
11+
"""
12+
A mapping from 256-color codes to 8-bit colours, represented with
13+
`SimpleColor`-compatible `UInt32`s.
14+
"""
15+
const ANSI_256_COLORS = Dict{Int, UInt32}(
16+
0 => 0x000000, 1 => 0x800000, 2 => 0x008000, 3 => 0x808000, 4 => 0x000080,
17+
5 => 0x800080, 6 => 0x008080, 7 => 0xc0c0c0, 8 => 0x808080, 9 => 0xff0000,
18+
10 => 0x00ff00, 11 => 0xffff00, 12 => 0x0000ff, 13 => 0xff00ff, 14 => 0x00ffff,
19+
15 => 0xffffff, 16 => 0x000000, 17 => 0x00005f, 18 => 0x000087, 19 => 0x0000af,
20+
20 => 0x0000d7, 21 => 0x0000ff, 22 => 0x005f00, 23 => 0x005f5f, 24 => 0x005f87,
21+
25 => 0x005faf, 26 => 0x005fd7, 27 => 0x005fff, 28 => 0x008700, 29 => 0x00875f,
22+
30 => 0x008787, 31 => 0x0087af, 32 => 0x0087d7, 33 => 0x0087ff, 34 => 0x00af00,
23+
35 => 0x00af5f, 36 => 0x00af87, 37 => 0x00afaf, 38 => 0x00afd7, 39 => 0x00afff,
24+
40 => 0x00d700, 41 => 0x00d75f, 42 => 0x00d787, 43 => 0x00d7af, 44 => 0x00d7d7,
25+
45 => 0x00d7ff, 46 => 0x00ff00, 47 => 0x00ff5f, 48 => 0x00ff87, 49 => 0x00ffaf,
26+
50 => 0x00ffd7, 51 => 0x00ffff, 52 => 0x5f0000, 53 => 0x5f005f, 54 => 0x5f0087,
27+
55 => 0x5f00af, 56 => 0x5f00d7, 57 => 0x5f00ff, 58 => 0x5f5f00, 59 => 0x5f5f5f,
28+
60 => 0x5f5f87, 61 => 0x5f5faf, 62 => 0x5f5fd7, 63 => 0x5f5fff, 64 => 0x5f8700,
29+
65 => 0x5f875f, 66 => 0x5f8787, 67 => 0x5f87af, 68 => 0x5f87d7, 69 => 0x5f87ff,
30+
70 => 0x5faf00, 71 => 0x5faf5f, 72 => 0x5faf87, 73 => 0x5fafaf, 74 => 0x5fafd7,
31+
75 => 0x5fafff, 76 => 0x5fd700, 77 => 0x5fd75f, 78 => 0x5fd787, 79 => 0x5fd7af,
32+
80 => 0x5fd7d7, 81 => 0x5fd7ff, 82 => 0x5fff00, 83 => 0x5fff5f, 84 => 0x5fff87,
33+
85 => 0x5fffaf, 86 => 0x5fffd7, 87 => 0x5fffff, 88 => 0x870000, 89 => 0x87005f,
34+
90 => 0x870087, 91 => 0x8700af, 92 => 0x8700d7, 93 => 0x8700ff, 94 => 0x875f00,
35+
95 => 0x875f5f, 96 => 0x875f87, 97 => 0x875faf, 98 => 0x875fd7, 99 => 0x875fff,
36+
100 => 0x878700, 101 => 0x87875f, 102 => 0x878787, 103 => 0x8787af, 104 => 0x8787d7,
37+
105 => 0x8787ff, 106 => 0x87af00, 107 => 0x87af5f, 108 => 0x87af87, 109 => 0x87afaf,
38+
110 => 0x87afd7, 111 => 0x87afff, 112 => 0x87d700, 113 => 0x87d75f, 114 => 0x87d787,
39+
115 => 0x87d7af, 116 => 0x87d7d7, 117 => 0x87d7ff, 118 => 0x87ff00, 119 => 0x87ff5f,
40+
120 => 0x87ff87, 121 => 0x87ffaf, 122 => 0x87ffd7, 123 => 0x87ffff, 124 => 0xaf0000,
41+
125 => 0xaf005f, 126 => 0xaf0087, 127 => 0xaf00af, 128 => 0xaf00d7, 129 => 0xaf00ff,
42+
130 => 0xaf5f00, 131 => 0xaf5f5f, 132 => 0xaf5f87, 133 => 0xaf5faf, 134 => 0xaf5fd7,
43+
135 => 0xaf5fff, 136 => 0xaf8700, 137 => 0xaf875f, 138 => 0xaf8787, 139 => 0xaf87af,
44+
140 => 0xaf87d7, 141 => 0xaf87ff, 142 => 0xafaf00, 143 => 0xafaf5f, 144 => 0xafaf87,
45+
145 => 0xafafaf, 146 => 0xafafd7, 147 => 0xafafff, 148 => 0xafd700, 149 => 0xafd75f,
46+
150 => 0xafd787, 151 => 0xafd7af, 152 => 0xafd7d7, 153 => 0xafd7ff, 154 => 0xafff00,
47+
155 => 0xafff5f, 156 => 0xafff87, 157 => 0xafffaf, 158 => 0xafffd7, 159 => 0xafffff,
48+
160 => 0xd70000, 161 => 0xd7005f, 162 => 0xd70087, 163 => 0xd700af, 164 => 0xd700d7,
49+
165 => 0xd700ff, 166 => 0xd75f00, 167 => 0xd75f5f, 168 => 0xd75f87, 169 => 0xd75faf,
50+
170 => 0xd75fd7, 171 => 0xd75fff, 172 => 0xd78700, 173 => 0xd7875f, 174 => 0xd78787,
51+
175 => 0xd787af, 176 => 0xd787d7, 177 => 0xd787ff, 178 => 0xd7af00, 179 => 0xd7af5f,
52+
180 => 0xd7af87, 181 => 0xd7afaf, 182 => 0xd7afd7, 183 => 0xd7afff, 184 => 0xd7d700,
53+
185 => 0xd7d75f, 186 => 0xd7d787, 187 => 0xd7d7af, 188 => 0xd7d7d7, 189 => 0xd7d7ff,
54+
190 => 0xd7ff00, 191 => 0xd7ff5f, 192 => 0xd7ff87, 193 => 0xd7ffaf, 194 => 0xd7ffd7,
55+
195 => 0xd7ffff, 196 => 0xff0000, 197 => 0xff005f, 198 => 0xff0087, 199 => 0xff00af,
56+
200 => 0xff00d7, 201 => 0xff00ff, 202 => 0xff5f00, 203 => 0xff5f5f, 204 => 0xff5f87,
57+
205 => 0xff5faf, 206 => 0xff5fd7, 207 => 0xff5fff, 208 => 0xff8700, 209 => 0xff875f,
58+
210 => 0xff8787, 211 => 0xff87af, 212 => 0xff87d7, 213 => 0xff87ff, 214 => 0xffaf00,
59+
215 => 0xffaf5f, 216 => 0xffaf87, 217 => 0xffafaf, 218 => 0xffafd7, 219 => 0xffafff,
60+
220 => 0xffd700, 221 => 0xffd75f, 222 => 0xffd787, 223 => 0xffd7af, 224 => 0xffd7d7,
61+
225 => 0xffd7ff, 226 => 0xffff00, 227 => 0xffff5f, 228 => 0xffff87, 229 => 0xffffaf,
62+
230 => 0xffffd7, 231 => 0xffffff, 232 => 0x080808, 233 => 0x121212, 234 => 0x1c1c1c,
63+
235 => 0x262626, 236 => 0x303030, 237 => 0x3a3a3a, 238 => 0x444444, 239 => 0x4e4e4e,
64+
240 => 0x585858, 241 => 0x626262, 242 => 0x6c6c6c, 243 => 0x767676, 244 => 0x808080,
65+
245 => 0x8a8a8a, 246 => 0x949494, 247 => 0x9e9e9e, 248 => 0xa8a8a8, 249 => 0xb2b2b2,
66+
250 => 0xbcbcbc, 251 => 0xc6c6c6, 252 => 0xd0d0d0, 253 => 0xdadada, 254 => 0xe4e4e4,
67+
255 => 0xeeeeee)
68+
69+
"""
70+
A mapping from old named colours to the new names, specifically from `light_*`
71+
to `bright_*`.
72+
"""
73+
const RENAMED_COLORS = Dict{Symbol, Symbol}(
74+
:light_black => :bright_black,
75+
:light_red => :bright_red,
76+
:light_green => :bright_green,
77+
:light_yellow => :bright_yellow,
78+
:light_blue => :bright_blue,
79+
:light_magenta => :bright_magenta,
80+
:light_cyan => :bright_cyan,
81+
:light_white => :bright_white)
82+
83+
legacy_color(color::Symbol) = SimpleColor(get(RENAMED_COLORS, color, color))
84+
function legacy_color(color256::Int)
85+
if 0 <= color256 <= 256
86+
SimpleColor(ANSI_256_COLORS[color256])
87+
else
88+
nothing
89+
end
90+
end
91+
92+
"""
93+
legacy_color(color::Union{String, Symbol, Int})
94+
95+
Attempt to obtain a `SimpleColor` for a "legacy" color value `color`.
96+
97+
When this is not possible, `nothing` is returned.
98+
"""
99+
function legacy_color(color::String)
100+
namedcolours = ("black", "red", "green", "yellow", "blue", "magenta", "cyan", "white",
101+
"bright_black", "grey", "gray", "bright_red", "bright_green", "bright_yellow",
102+
"bright_blue", "bright_magenta", "bright_cyan", "bright_white",
103+
"light_black", "light_red", "light_green", "light_yellow",
104+
"light_blue", "light_magenta", "light_cyan", "light_white")
105+
if color in namedcolours
106+
legacy_color(Symbol(namedcolours))
107+
elseif 0 <= (color256 = something(tryparse(Int, ecolor), -1)) <= 255
108+
legacy_color(color256)
109+
else
110+
nothing
111+
end
112+
end
113+
114+
"""
115+
load_env_colors!()
116+
117+
Try to emulate the effect of the various `*_color()` functions of `Base`, by
118+
loading any specified colours as foregrounds of the relevant faces.
119+
"""
120+
function load_env_colors!()
121+
for (fname, envkey) in ((:error, "JULIA_ERROR_COLOR"),
122+
(:warn, "JULIA_WARN_COLOR"),
123+
(:info, "JULIA_INFO_COLOR"),
124+
(:log_debug, "JULIA_DEBUG_COLOR"))
125+
if haskey(ENV, envkey)
126+
ecolor = legacy_color(ENV[envkey])
127+
if !isnothing(ecolor)
128+
loadface!(fname => Face(foreground = ecolor))
129+
end
130+
end
131+
end
132+
end
133+
134+
end

0 commit comments

Comments
 (0)