Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert crayon based color scheme #6

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ version = "0.4.0"

[deps]
Configurations = "5218b696-f38b-4ac9-8b61-a12ec717816d"
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"

[compat]
Configurations = "0.16"
Crayons = "4"
Preferences = "1"
julia = "1"

Expand Down
3 changes: 0 additions & 3 deletions src/GarishPrint.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module GarishPrint

export pprint, pprint_struct

using Crayons
using Configurations

# 1.0 Compat
Expand All @@ -21,7 +19,6 @@ else
end

include("color.jl")
include("colorschemes.jl")
include("io.jl")
include("pprint.jl")
include("struct.jl")
Expand Down
3 changes: 1 addition & 2 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ end
# NOTE: modified based on base/arrayshow.jl:show_zero_dim
function pprint_zero_dim(io::GarishIO, X::AbstractArray{T, 0}) where T
if isassigned(X)
print_token(io, :call, "fill")
print(io, "(")
print(io, "fill(")
pprint(io, X[])
else
print_token(io, :type, "Array{", T, ", 0}(")
Expand Down
234 changes: 80 additions & 154 deletions src/color.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,182 +26,108 @@ function supports_color256()
end
end

# NOTE: this type immutable because each object maps to
# an preference file, thus it shouldn't be changed once
# created
const ColorType = Union{Int, Symbol}

"""
ColorScheme
ColorPreference

The color scheme type.
The color preference type.
"""
@option struct ColorScheme
# struct components
fieldname::Crayon
type::Crayon

# keyword-like
keyword::Crayon
call::Crayon

struct ColorPreference
fieldname::ColorType
type::ColorType
operator::ColorType

# literal-like
text::Crayon
number::Crayon
string::Crayon
symbol::Crayon
literal::Crayon
constant::Crayon
op::Crayon
literal::ColorType
constant::ColorType
number::ColorType
string::ColorType

# comment-like
comment::Crayon
undef::Crayon
lineno::Crayon
comment::ColorType
undef::ColorType
linenumber::ColorType
end

# fancy color names
const semantic_crayons = Dict{Crayon, String}()
const semantic_colornames = [
"default", "black", "blue",
"cyan", "green", "light_blue", "light_cyan", "light_green",
"light_magenta", "light_red", "light_yellow", "magenta", "nothing", "red",
"white"
]

const COLORS = Dict(
0 => "black",
1 => "red",
2 => "green",
3 => "yellow",
4 => "blue",
5 => "magenta",
6 => "cyan",
7 => "light_gray",
9 => "default",
60 => "dark_gray",
61 => "light_red",
62 => "light_green",
63 => "light_yellow",
64 => "light_blue",
65 => "light_magenta",
66 => "light_cyan",
67 => "white"
)

for name in semantic_colornames
semantic_crayons[Crayon(foreground = Symbol(name))] = name
struct PreferenceInvalid <: Exception
key::String
type
got
end

function Configurations.convert_to_option(::Type{ColorScheme}, ::Type{Crayon}, x::String)
x in semantic_colornames || throw(ArgumentError("invalid color $x"))
return Crayon(foreground = Symbol(x))
function Base.showerror(io::IO, x::PreferenceInvalid)
print(io, "preference for ")
printstyled(io, x.key; color=:light_blue)
print(io, " is invalid, expect ")
printstyled(io, x.type; color=:green)
print(io, " got: ", repr(x.got))
end

function Configurations.convert_to_option(::Type{ColorScheme}, ::Type{Crayon}, d::Dict)
kwargs = []
if haskey(d, "foreground")
push!(kwargs, :foreground => parse_color(d["foreground"]))
end
Base.show(io::IO, x::ColorPreference) = pprint_struct(io, x)

if haskey(d, "background")
push!(kwargs, :background => parse_color(d["background"]))
end
"""
ColorPreference(;kw...)

for key in [
:reset,
:bold,
:faint,
:italics,
:underline,
:blink,
:negative,
:conceal,
:strikethrough,
]

if get(d, string(key), false)
push!(kwargs, key => true)
end
See [`pprint`](@ref) for available keyword configurations.
"""
function ColorPreference(;kw...)
colors = supports_color256() ? default_colors_256() : default_colors_ansi()
if color_prefs_toml !== nothing
merge!(colors, color_prefs_toml)
end
return Crayon(;kwargs...)
end

function Configurations.to_dict(::Type{ColorScheme}, x::Crayon)
if haskey(semantic_crayons, x)
return semantic_crayons[x]
else
d = Dict{String, Any}()
if x.fg.active
d["foreground"] = ansi_color_to_dict(x.fg)
end

if x.bg.active
d["background"] = ansi_color_to_dict(x.bg)
colors = merge!(colors, kw)

args = map(fieldnames(ColorPreference)) do name
val = colors[string(name)]
if val isa String
return Symbol(val)
elseif val isa Int
return val
else
throw(PreferenceInvalid("GarishPrint.color.$name", Union{String, Int}, val))
end

for f in fieldnames(Crayon)
if f === :fg || f === :bg
continue
end

if getfield(x, f).active
d[string(each)] = true
end
end
return d
end
return ColorPreference(args...)
end

parse_color(s::String) = Symbol(s)

function parse_color(d::Dict)
haskey(d, "style") || error("field `style` is required for color")
haskey(d, "color") || error("field `color` is required for color")

if d["style"] == "256"
return Int(d["color"])
elseif d["style"] == "24bit"
return parse(UInt32, "0x" * d["color"])
else
error("invalid style: $(d["style"])")
end
end

function ansi_color_to_dict(x::Crayons.ANSIColor)
if x.style == Crayons.COLORS_16
return COLORS[x.r]
elseif x.style == Crayons.COLORS_256
Dict{String, Any}(
"color" => x.r,
"style" => "256",
)
elseif x.style == Crayons.COLORS_24BIT
Dict{String, Any}(
"color" => rgb_to_hex(x.r, x.g, x.b),
"style" => "24bit",
)
else
error("invalid color encoding RESET")
end
end
"""
default_colors_ansi()

function rgb_to_hex(r, g, b)
r, g, b = UInt32(r), UInt32(g), UInt32(b)
hex = r << 16 | g << 8 | b
return uppercase(string(hex; base=16))
The default ANSI color theme.
"""
function default_colors_ansi()
Dict{String, Any}(
"fieldname" => "light_blue",
"type" => "green",
"operator" => "normal",
"literal" => "yellow",
"constant" => "yellow",
"number" => "normal",
"string" => "yellow",
"comment" => "light_black",
"undef" => "normal",
"linenumber" => "light_black",
)
end

Base.show(io::IO, x::ColorScheme) = pprint_struct(io, x)

function color_scheme(;kw...)
colors = supports_color256() ? monokai_256() : monokai()
d = to_dict(colors, TOMLStyle)
if color_prefs_toml !== nothing
merge!(d, color_prefs_toml)
end
"""
default_colors_256()

if !isempty(kw)
merge!(d, kw)
end
return from_dict(ColorScheme, d)
The default color 256 theme.
"""
function default_colors_256()
Dict{String, Any}(
"fieldname" => 039,
"type" => 037,
"operator" => 196,
"literal" => 140,
"constant" => 099,
"number" => 140,
"string" => 180,
"comment" => 240,
# undef is actually a constant
"undef" => 099,
"linenumber" => 240,
)
end
43 changes: 0 additions & 43 deletions src/colorschemes.jl

This file was deleted.

Loading