Define some custom faces

Hardcoding colours is so `printstyled` 😛
This commit is contained in:
TEC 2024-04-08 19:05:49 +08:00
parent d67e9a3bce
commit 19fe198ba4
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
4 changed files with 15 additions and 10 deletions

View File

@ -1,7 +1,7 @@
module About
using Base: AnnotatedString, AnnotatedIOBuffer
using StyledStrings: @styled_str, Face, face!
using StyledStrings: @styled_str, Face, face!, addface!
using JuliaSyntaxHighlighting: highlight
using InteractiveUtils
@ -31,4 +31,11 @@ function about(xs...)
end
end
const ABOUT_FACES = [
:about_module => Face(foreground=:bright_red),
:about_pointer => Face(foreground=:cyan),
]
__init__() = foreach(addface!, ABOUT_FACES)
end

View File

@ -3,11 +3,11 @@ function about(io::IO, fn::Function)
methodmodules = getproperty.(methods(fn).ms, :module)
others = setdiff(methodmodules, [source])
fn_name, fn_extra = split(Base.summary(fn), ' ', limit=2)
print(io, styled"{julia_funcall:$fn_name} $fn_extra\n Defined in {bright_red:$source}")
print(io, styled"{julia_funcall:$fn_name} $fn_extra\n Defined in {about_module:$source}")
if length(others) > 0
print(io, styled"{shadow:({emphasis:$(sum(Ref(source) .=== methodmodules))})} extended in ")
for (i, oth) in enumerate(others)
print(io, styled"{bright_red:$oth}{shadow:({emphasis:$(sum(Ref(oth) .=== methodmodules))})}")
print(io, styled"{about_module:$oth}{shadow:({emphasis:$(sum(Ref(oth) .=== methodmodules))})}")
if length(others) == 2 && i == 1
print(io, " and ")
elseif length(others) > 2 && i < length(others)-1

View File

@ -1,5 +1,3 @@
const POINTER_FACE = :cyan # should not appear in `FACE_CYCLE`
struct FieldInfo
i::Int
face::Union{Symbol, Face}
@ -54,7 +52,7 @@ function about(io::IO, type::Type)
print(io, "singleton ")
end
print(Base.summary(type))
print(io, styled" defined in {bright_red:$(parentmodule(type))}, ")
print(io, styled" defined in {about_module:$(parentmodule(type))}, ")
hassizeof(type) && print(io, "$(join(humansize(sizeof(type))))")
println(io, "\n ", supertypestr(type))
(!isstructtype(type) || fieldcount(type) == 0) && return
@ -64,7 +62,7 @@ function about(io::IO, type::Type)
sinfo = structinfo(type)
namepad = maximum(fi -> textwidth(string(fi.name)), sinfo) + 1
for (; face, name, type, ispointer) in sinfo
push!(fieldinfo, rpad(styled"{$face:$name}", namepad) * styled"{$POINTER_FACE:$(ifelse(ispointer, \"*\", \" \"))}$type")
push!(fieldinfo, rpad(styled"{$face:$name}", namepad) * styled"{about_pointer:$(ifelse(ispointer, \"*\", \" \"))}$type")
end
else
for (; name, type) in structinfo(type)
@ -121,7 +119,7 @@ function memorylayout(io::IO, type::DataType)
end
multirow_wrap(io, permutedims(hcat(bars, descs)))
if any(getfield.(si, :ispointer))
print(io, styled"\n {$POINTER_FACE:*} = {$POINTER_FACE:Pointer} {light:(8B)}")
print(io, styled"\n {about_pointer:*} = {about_pointer:Pointer} {light:(8B)}")
end
println(io)
end

View File

@ -53,9 +53,9 @@ function memorylayout(io::IO, value::T) where {T}
elseif ispointer
try
pt = pointer(getfield(value, name))
push!(freprs, styled"{$POINTER_FACE,light:$pt}")
push!(freprs, styled"{about_pointer,light:$pt}")
catch
push!(freprs, styled"{$POINTER_FACE:Ptr?}")
push!(freprs, styled"{about_pointer:Ptr?}")
end
else
memorylayout(IOContext(aio, :compact => true), getfield(value, name))