Skip to content

Commit 1aafc2f

Browse files
Kenotecosaur
authored andcommitted
Adapt to current_terminfo becoming OncePerProcess
Julia base is changing current_terminfo from a simple global variable to a OncePerProcess type. This commit updates all usage sites to call current_terminfo() as a function instead of accessing it directly. This is the counterpart to <JuliaLang/julia#58854>. For tests, we need to directly manipulate the OncePerProcess' internal value field, which relies on undocumented internals but is necessary for testing different terminal configurations without restarting the process.
1 parent 527a985 commit 1aafc2f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/io.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ function termstyle(io::IO, face::Face, lastface::Face=getface())
171171
print(io, if face.weight (:medium, :semibold, :bold, :extrabold, :black)
172172
ANSI_STYLE_CODES.bold_weight
173173
elseif face.weight (:semilight, :light, :extralight, :thin)
174-
get(Base.current_terminfo, :dim, "")
174+
get(Base.current_terminfo(), :dim, "")
175175
else # :normal
176176
ANSI_STYLE_CODES.normal_weight
177177
end)
178178
end
179179
face.slant == lastface.slant ||
180-
if haskey(Base.current_terminfo, :enter_italics_mode)
180+
if haskey(Base.current_terminfo(), :enter_italics_mode)
181181
print(io, ifelse(face.slant (:italic, :oblique),
182182
ANSI_STYLE_CODES.start_italics,
183183
ANSI_STYLE_CODES.end_italics))
@@ -189,8 +189,8 @@ function termstyle(io::IO, face::Face, lastface::Face=getface())
189189
# Kitty fancy underlines, see <https://sw.kovidgoyal.net/kitty/underlines>
190190
# Supported in Kitty, VTE, iTerm2, Alacritty, and Wezterm.
191191
face.underline == lastface.underline ||
192-
if haskey(Base.current_terminfo, :set_underline_style) ||
193-
get(Base.current_terminfo, :can_style_underline, false)
192+
if haskey(Base.current_terminfo(), :set_underline_style) ||
193+
get(Base.current_terminfo(), :can_style_underline, false)
194194
if face.underline isa Tuple # Color and style
195195
color, style = face.underline
196196
print(io, "\e[4:",
@@ -219,11 +219,11 @@ function termstyle(io::IO, face::Face, lastface::Face=getface())
219219
ANSI_STYLE_CODES.start_underline,
220220
ANSI_STYLE_CODES.end_underline))
221221
end
222-
face.strikethrough == lastface.strikethrough || !haskey(Base.current_terminfo, :smxx) ||
222+
face.strikethrough == lastface.strikethrough || !haskey(Base.current_terminfo(), :smxx) ||
223223
print(io, ifelse(face.strikethrough === true,
224224
ANSI_STYLE_CODES.start_strikethrough,
225225
ANSI_STYLE_CODES.end_strikethrough))
226-
face.inverse == lastface.inverse || !haskey(Base.current_terminfo, :enter_reverse_mode) ||
226+
face.inverse == lastface.inverse || !haskey(Base.current_terminfo(), :enter_reverse_mode) ||
227227
print(io, ifelse(face.inverse === true,
228228
ANSI_STYLE_CODES.start_reverse,
229229
ANSI_STYLE_CODES.end_reverse))

test/runtests.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ const vt100 = Base.TermInfo(read(joinpath(@__DIR__, "terminfos", "vt100"), Base.
2020
const fancy_term = Base.TermInfo(read(joinpath(@__DIR__, "terminfos", "fancy"), Base.TermInfoRaw))
2121

2222
function with_terminfo(fn::Function, tinfo::Base.TermInfo)
23-
prev_terminfo = getglobal(Base, :current_terminfo)
23+
# HACK: Directly modifying the value inside `Base.current_terminfo`
24+
# (a `OncePerProcess`) as we do here relies on private implementation
25+
# details, which is ill-advised outside of low-stakes testing scenarios.
26+
# This is a fragile shortcut to avoid modifying the environment and
27+
# starting a process.
28+
prev_terminfo = Base.current_terminfo()
2429
prev_truecolor = getglobal(Base, :have_truecolor)
25-
try
26-
setglobal!(Base, :current_terminfo, tinfo)
27-
setglobal!(Base, :have_truecolor, haskey(tinfo, :setrgbf))
30+
@lock Base.current_terminfo.lock try
31+
Base.current_terminfo.value = tinfo
32+
setglobal!(Base, :have_truecolor, haskey(tinfo, :setrgbf))
2833
fn()
2934
finally
30-
setglobal!(Base, :current_terminfo, prev_terminfo)
31-
setglobal!(Base, :have_truecolor, prev_truecolor)
35+
Base.current_terminfo.value = prev_terminfo
36+
setglobal!(Base, :have_truecolor, prev_truecolor)
3237
end
3338
end
3439

0 commit comments

Comments
 (0)