Skip to content

Commit 923fe2d

Browse files
authored
Add update mechanism for Terminfo, and common user-alias data (#53285)
Now that we take care of terminfo parsing ourselves, having a clear origin and processing method for arriving at our reference terminfo data seems somewhat important. The original form was acquired by re-purposing some pre-processed terminfo data from NCurses (I forget the exact source). I considered adding a separate ingestion/processing script, but it occurred to me that it would make sense to have the method for updating the data file be _in_ the data file, by turning it into a quine. This seems to work rather well, and so long as the NCurses source file format stays the same, updating the terminfo data is now dead simple. While working on the terminfo files, some minor refactors popped up as "probably nice to have". One of which makes the reported number of flags/numbers/strings actually accurate. Lastly, to support the ergonomic use of capability variable names instead of the short (read: uninformative) capname, we now also import the NCurses-recognised extended capabilities, and generate/specify some nice aliases for them. ----- If we separate out the terminfo parser/data into a small stdlib, the state here will be the initial state of the repo.
2 parents 6e1d062 + 1a76ed4 commit 923fe2d

File tree

3 files changed

+818
-533
lines changed

3 files changed

+818
-533
lines changed

base/terminfo.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ struct TermInfo
6666
numbers::Dict{Symbol, Int}
6767
strings::Dict{Symbol, String}
6868
extensions::Union{Nothing, Set{Symbol}}
69+
aliases::Dict{Symbol, Symbol}
6970
end
7071

71-
TermInfo() = TermInfo([], Dict(), Dict(), Dict(), nothing)
72+
TermInfo() = TermInfo([], Dict(), Dict(), Dict(), nothing, Dict())
7273

7374
function read(data::IO, ::Type{TermInfoRaw})
7475
# Parse according to `term(5)`
@@ -175,24 +176,28 @@ function TermInfo(raw::TermInfoRaw)
175176
flags = Dict{Symbol, Bool}()
176177
numbers = Dict{Symbol, Int}()
177178
strings = Dict{Symbol, String}()
179+
aliases = Dict{Symbol, Symbol}()
178180
extensions = nothing
179181
for (flag, value) in zip(TERM_FLAGS, raw.flags)
180-
flags[flag.short] = value
181-
flags[flag.long] = value
182+
flags[flag.name] = value
183+
aliases[flag.capname] = flag.name
182184
end
183185
for (num, value) in zip(TERM_NUMBERS, raw.numbers)
184-
numbers[num.short] = Int(value)
185-
numbers[num.long] = Int(value)
186+
numbers[num.name] = Int(value)
187+
aliases[num.capname] = num.name
186188
end
187189
for (str, value) in zip(TERM_STRINGS, raw.strings)
188190
if !isnothing(value)
189-
strings[str.short] = value
190-
strings[str.long] = value
191+
strings[str.name] = value
192+
aliases[str.capname] = str.name
191193
end
192194
end
193195
if !isnothing(raw.extended)
194196
extensions = Set{Symbol}()
195-
for (key, value) in raw.extended
197+
longalias(key, value) = first(get(TERM_USER, (typeof(value), key), (nothing, "")))
198+
for (short, value) in raw.extended
199+
long = longalias(short, value)
200+
key = something(long, short)
196201
push!(extensions, key)
197202
if value isa Bool
198203
flags[key] = value
@@ -201,26 +206,30 @@ function TermInfo(raw::TermInfoRaw)
201206
elseif value isa String
202207
strings[key] = value
203208
end
209+
if !isnothing(long)
210+
aliases[short] = long
211+
end
204212
end
205213
end
206-
TermInfo(raw.names, flags, numbers, strings, extensions)
214+
TermInfo(raw.names, flags, numbers, strings, extensions, aliases)
207215
end
208216

209-
get(ti::TermInfo, key::Symbol, default::Bool) = get(ti.flags, key, default)
210-
get(ti::TermInfo, key::Symbol, default::Int) = get(ti.numbers, key, default)
211-
get(ti::TermInfo, key::Symbol, default::String) = get(ti.strings, key, default)
217+
get(ti::TermInfo, key::Symbol, default::Bool) = get(ti.flags, get(ti.aliases, key, key), default)
218+
get(ti::TermInfo, key::Symbol, default::Int) = get(ti.numbers, get(ti.aliases, key, key), default)
219+
get(ti::TermInfo, key::Symbol, default::String) = get(ti.strings, get(ti.aliases, key, key), default)
212220

213221
haskey(ti::TermInfo, key::Symbol) =
214-
haskey(ti.flags, key) || haskey(ti.numbers, key) || haskey(ti.strings, key)
222+
haskey(ti.flags, key) || haskey(ti.numbers, key) || haskey(ti.strings, key) || haskey(ti.aliases, key)
215223

216224
function getindex(ti::TermInfo, key::Symbol)
217225
haskey(ti.flags, key) && return ti.flags[key]
218226
haskey(ti.numbers, key) && return ti.numbers[key]
219227
haskey(ti.strings, key) && return ti.strings[key]
228+
haskey(ti.aliases, key) && return getindex(ti, ti.aliases[key])
220229
throw(KeyError(key))
221230
end
222231

223-
keys(ti::TermInfo) = keys(ti.flags) keys(ti.numbers) keys(ti.strings)
232+
keys(ti::TermInfo) = keys(ti.flags) keys(ti.numbers) keys(ti.strings) keys(ti.aliases)
224233

225234
function show(io::IO, ::MIME"text/plain", ti::TermInfo)
226235
print(io, "TermInfo(", ti.names, "; ", length(ti.flags), " flags, ",

0 commit comments

Comments
 (0)