Skip to content

Commit

Permalink
some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Aug 2, 2021
1 parent c3f72b7 commit 37d65ea
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/Crayons.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Crayons

export Crayon, CrayonStack, merge, @crayon_str
export Crayon, CrayonStack, @crayon_str

include("crayon.jl")
include("downcasts.jl")
Expand All @@ -10,6 +10,7 @@ include("test_prints.jl")
include("logo.jl")
include("consts.jl")
include("macro.jl")
include("precompile.jl")

end # module

44 changes: 22 additions & 22 deletions src/crayon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ function _torgb(hex::UInt32)::NTuple{3, UInt8}
(hex << 8 >> 24, hex << 16 >> 24, hex << 24 >> 24)
end

function _parse_color(c::Union{Integer,Symbol,NTuple{3,Integer},UInt32})
function _parse_color(c::Union{Integer,Symbol,NTuple{3,Integer},UInt32,Nothing})
ansicol = ANSIColor()
if c != :nothing
if c !== nothing
if isa(c, Symbol)
ansicol = ANSIColor(COLORS[c], COLORS_16)
elseif isa(c, UInt32)
Expand All @@ -155,17 +155,17 @@ function _parse_color(c::Union{Integer,Symbol,NTuple{3,Integer},UInt32})
return ansicol
end

function Crayon(;foreground::Union{Int,Symbol,NTuple{3,Integer},UInt32} = :nothing,
background::Union{Int,Symbol,NTuple{3,Integer},UInt32} = :nothing,
reset = :nothing,
bold = :nothing,
faint = :nothing,
italics = :nothing,
underline = :nothing,
blink = :nothing,
negative = :nothing,
conceal = :nothing,
strikethrough = :nothing)
function Crayon(;foreground::Union{Int,Symbol,NTuple{3,Integer},UInt32,Nothing} = nothing,
background::Union{Int,Symbol,NTuple{3,Integer},UInt32,Nothing} = nothing,
reset::Union{Bool,Nothing} = nothing,
bold::Union{Bool,Nothing} = nothing,
faint::Union{Bool,Nothing} = nothing,
italics::Union{Bool,Nothing} = nothing,
underline::Union{Bool,Nothing} = nothing,
blink::Union{Bool,Nothing} = nothing,
negative::Union{Bool,Nothing} = nothing,
conceal::Union{Bool,Nothing} = nothing,
strikethrough::Union{Bool,Nothing} = nothing)

fgcol = _parse_color(foreground)
bgcol = _parse_color(background)
Expand All @@ -180,15 +180,15 @@ function Crayon(;foreground::Union{Int,Symbol,NTuple{3,Integer},UInt32} = :nothi
_conceal = ANSIStyle()
_strikethrough = ANSIStyle()

reset != :nothing && (_reset = ANSIStyle(reset))
bold != :nothing && (_bold = ANSIStyle(bold))
faint != :nothing && (_faint = ANSIStyle(faint))
italics != :nothing && (_italics = ANSIStyle(italics))
underline != :nothing && (_underline = ANSIStyle(underline))
blink != :nothing && (_blink = ANSIStyle(blink))
negative != :nothing && (_negative = ANSIStyle(negative))
conceal != :nothing && (_conceal = ANSIStyle(conceal))
strikethrough != :nothing && (_strikethrough = ANSIStyle(strikethrough))
reset !== nothing && (_reset = ANSIStyle(reset))
bold !== nothing && (_bold = ANSIStyle(bold))
faint !== nothing && (_faint = ANSIStyle(faint))
italics !== nothing && (_italics = ANSIStyle(italics))
underline !== nothing && (_underline = ANSIStyle(underline))
blink !== nothing && (_blink = ANSIStyle(blink))
negative !== nothing && (_negative = ANSIStyle(negative))
conceal !== nothing && (_conceal = ANSIStyle(conceal))
strikethrough !== nothing && (_strikethrough = ANSIStyle(strikethrough))

return Crayon(fgcol,
bgcol,
Expand Down
1 change: 1 addition & 0 deletions src/crayon_stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ function Base.pop!(cs::CrayonStack)
# Return the currently active crayon so we can use print(pop!(crayonstack), "bla")
return cs
end

6 changes: 4 additions & 2 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ macro crayon_str(str::String)
end
@goto doparse
@label parse_err
return :(throw(ArgumentError("should have the format [fg/bg]:color")))
throw(ArgumentError("should have the format [fg/bg]:color"))
end

@label doparse
Expand Down Expand Up @@ -106,7 +106,9 @@ function _parse_color_string(token::AbstractString)
reg = r"\(([0-9]*),([0-9]*),([0-9]*)\)"
m = match(reg, token)
if m !== nothing
r, g, b = m.captures
r = m.captures[1]::SubString{String}
g = m.captures[2]::SubString{String}
b = m.captures[3]::SubString{String}
return _parse_color(parse.(Int, (r, g, b)))
end

Expand Down
8 changes: 8 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function _precompile_()
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
crayon"red"
print(IOBuffer(), Crayon(foreground = :red), "In red. ", Crayon(bold = true), "Red and bold")
precompile(Tuple{typeof(Base.print), Base.TTY, Crayons.Crayon})
end

_precompile_()

0 comments on commit 37d65ea

Please sign in to comment.