Skip to content

Commit

Permalink
Use PrecompileTools for reduced latency
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur committed Aug 14, 2024
1 parent 52b01db commit d37f043
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["TEC <[email protected]>"]
version = "1.0.2"

[deps]
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[compat]
Expand Down
5 changes: 2 additions & 3 deletions src/StyledStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module StyledStrings


export @styled_str

include("compat.jl")
Expand Down Expand Up @@ -54,8 +55,6 @@ function __init__()
load_customisations!()
end

if generating_output()
include("precompile.jl")
end
include("precompile.jl")

end
89 changes: 49 additions & 40 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
colorio = IOContext(IOBuffer(), :color => true)

print(colorio, styled"just a string")
Base.escape_string(colorio, styled"just a string")
print(colorio, styled"{red:with color}")
print(colorio, styled"{face=$(Face(foreground=:red)):with color}")
print(colorio, styled"{(foreground=red):with color}")
show(colorio, MIME("text/plain"), styled"{red:with color}")
show(colorio, MIME("text/html"), styled"{red:with color}")
print(colorio, styled"{red:with color}"[1])
print(colorio, styled"{underline=(blue,curly):more styling}")

convert(StyledStrings.SimpleColor, (r = 0x01, g = 0x02, b = 0x03))
convert(StyledStrings.SimpleColor, 0x010203)
convert(StyledStrings.SimpleColor, :namedcolor)
tryparse(StyledStrings.SimpleColor, "blue")
parse(StyledStrings.SimpleColor, "#010203")

StyledStrings.Face(nothing, nothing, nothing, nothing, nothing,
nothing, nothing, nothing, nothing, [:default])
StyledStrings.Face(height=2)
merge(StyledStrings.Face(inherit=:blue), StyledStrings.Face(foreground=:white))
StyledStrings.Face(height=2) == StyledStrings.Face(height=3)

show(colorio, MIME("text/plain"), StyledStrings.Face(foreground=:green))
show(colorio, StyledStrings.Face(foreground=:green))

StyledStrings.getface()
StyledStrings.getface(:red)
StyledStrings.getface(styled"{red:red}", 1)

StyledStrings.addface!(:_precompile => Face(font="precompile"))
StyledStrings.loadface!(:_precompile => Face(inverse=true))
StyledStrings.loaduserfaces!(Dict{String, Any}("_precompile" =>
Dict{String, Any}("strikethough" => true)))
StyledStrings.resetfaces!(:_precompile)
StyledStrings.resetfaces!()

StyledStrings.withfaces(:yellow => StyledStrings.Face(foreground=:red), :green => :blue) do
println(colorio, styled"{yellow:red} and {green:blue} mixed make {magenta:purple}")
using PrecompileTools: @compile_workload, @setup_workload

@setup_workload begin
@compile_workload begin
colorio = IOContext(IOBuffer(), :color => true)

print(colorio, styled"just a string")
Base.escape_string(colorio, styled"just a string")
print(colorio, styled"{red:with color}")
print(colorio, styled"{face=$(Face(foreground=:red)):with color}")
print(colorio, styled"{(foreground=red):with color}")
show(colorio, MIME("text/plain"), styled"{red:with color}")
show(colorio, MIME("text/html"), styled"{red:with color}")
print(colorio, styled"{red:with color}"[1])
print(colorio, styled"{underline=(blue,curly):more styling}")

convert(StyledStrings.SimpleColor, (r = 0x01, g = 0x02, b = 0x03))
convert(StyledStrings.SimpleColor, 0x010203)
convert(StyledStrings.SimpleColor, :namedcolor)
tryparse(StyledStrings.SimpleColor, "blue")
parse(StyledStrings.SimpleColor, "#010203")

StyledStrings.Face(nothing, nothing, nothing, nothing, nothing,
nothing, nothing, nothing, nothing, [:default])
StyledStrings.Face(height=2)
merge(StyledStrings.Face(inherit=:blue), StyledStrings.Face(foreground=:white))
StyledStrings.Face(height=2) == StyledStrings.Face(height=3)

show(colorio, MIME("text/plain"), StyledStrings.Face(foreground=:green))
show(colorio, StyledStrings.Face(foreground=:green))

StyledStrings.getface()
StyledStrings.getface(:red)
StyledStrings.getface(styled"{red:red}", 1)

StyledStrings.addface!(:_precompile => Face(font="precompile"))
StyledStrings.loadface!(:_precompile => Face(inverse=true))
StyledStrings.loaduserfaces!(Dict{String, Any}("_precompile" =>
Dict{String, Any}("strikethough" => true)))
StyledStrings.resetfaces!(:_precompile)
StyledStrings.resetfaces!()

StyledStrings.withfaces(:yellow => StyledStrings.Face(foreground=:red), :green => :blue) do
println(colorio, styled"{yellow:red} and {green:blue} mixed make {magenta:purple}")
end

__init__()
end
StyledStrings.FACES.current[] = copy(StyledStrings.FACES.default)
end

0 comments on commit d37f043

Please sign in to comment.