Skip to content

Commit

Permalink
unify print_color
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Aug 19, 2021
1 parent ff44a47 commit 7d8dc95
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
18 changes: 6 additions & 12 deletions src/canvas/heatmapcanvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,19 @@ function printcolorbarrow(io::IO, c::HeatmapCanvas, row::Int, colormap::Any, bor
max_z = lim[2]
if row == 1
# print top border and maximum z value
printstyled(io, b[:tl]; color = :light_black)
printstyled(io, b[:t]; color = :light_black)
printstyled(io, b[:t]; color = :light_black)
printstyled(io, b[:tr]; color = :light_black)
print_color(:light_black, io, b[:tl], b[:t], b[:t], b[:tr])
max_z_str = isinteger(max_z) ? max_z : float_round_log10(max_z)
print(io, plot_padding)
printstyled(io, max_z_str; color = :light_black)
print_color(:light_black, io, max_z_str)
elseif row == nrows(c)
# print bottom border and minimum z value
printstyled(io, b[:bl]; color = :light_black)
printstyled(io, b[:b]; color = :light_black)
printstyled(io, b[:b]; color = :light_black)
printstyled(io, b[:br]; color = :light_black)
print_color(:light_black, io, b[:bl], b[:b], b[:b], b[:br])
min_z_str = isinteger(min_z) ? min_z : float_round_log10(min_z)
print(io, plot_padding)
printstyled(io, min_z_str; color = :light_black)
print_color(:light_black, io, min_z_str)
else
# print gradient
printstyled(io, b[:l]; color = :light_black)
print_color(:light_black, io, b[:l])
# if min and max are the same, single color
if min_z == max_z
bgcol = colormap(1, 1, 1)
Expand All @@ -96,7 +90,7 @@ function printcolorbarrow(io::IO, c::HeatmapCanvas, row::Int, colormap::Any, bor
print(io, Crayon(foreground=fgcol, background=bgcol), HALF_BLOCK)
print(io, HALF_BLOCK)
print(io, Crayon(reset=true))
printstyled(io, b[:r]; color = :light_black)
print_color(:light_black, io, b[:r])

# print z label
if row == div(nrows(c), 2) + 1
Expand Down
4 changes: 2 additions & 2 deletions src/graphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function Base.show(io::IO, c::GraphicsArea)
print_border_top(io, "", border_length, :solid, :light_black)
print(io, '\n')
for row in 1:nrows(c)
printstyled(io, b[:l]; color = :light_black)
print_color(:light_black, io, b[:l])
printrow(io, c, row)
printstyled(io, b[:r]; color = :light_black)
print_color(:light_black, io, b[:r])
print(io, '\n')
end
print_border_bottom(io, "", border_length, :solid, :light_black)
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/bargraphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function printrow(io::IO, g::BarplotGraphics, row::Int)
bar_len = max_freq > 0 ? round(Int, max(val, zero(val))/max_freq * max_bar_width, RoundNearestTiesUp) : 0
bar_str = max_freq > 0 ? repeat(g.symb, bar_len) : ""
bar_lbl = string(bar)
printstyled(io, bar_str; color = g.color)
printstyled(io, " ", bar_lbl; color = :normal)
print_color(g.color, io, bar_str)
print_color(:normal, io, " ", bar_lbl)
pan_len = max(max_bar_width + 1 + g.max_len - bar_len - length(bar_lbl), 0)
pad = repeat(" ", round(Int, pan_len))
print(io, pad)
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/boxgraphics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ function printrow(io::IO, c::BoxplotGraphics, row::Int)
line[i] = line_char
end

printstyled(io, join(line), color = c.color)
print_color(c.color, io, join(line))
end
28 changes: 14 additions & 14 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,18 @@ function print_title(io::IO, padding::AbstractString, title::AbstractString; p_w
offset = round(Int, p_width / 2 - length(title) / 2, RoundNearestTiesUp)
offset = offset > 0 ? offset : 0
tpad = repeat(" ", offset)
printstyled(io, padding, tpad, title; color = color)
print_color(color, io, padding, tpad, title)
end
end

function print_border_top(io::IO, padding::AbstractString, length::Int, border::Symbol = :solid, color::UserColorType = :light_black)
b = bordermap[border]
border == :none || printstyled(io, padding, b[:tl], repeat(b[:t], length), b[:tr]; color = color)
border == :none || print_color(color, io, padding, b[:tl], repeat(b[:t], length), b[:tr])
end

function print_border_bottom(io::IO, padding::AbstractString, length::Int, border::Symbol = :solid, color::UserColorType = :light_black)
b = bordermap[border]
border == :none || printstyled(io, padding, b[:bl], repeat(b[:b], length), b[:br]; color = color)
border == :none || print_color(color, io, padding, b[:bl], repeat(b[:b], length), b[:br])
end

_nocolor_string(str) = replace(string(str), r"\e\[[0-9]+m" => "")
Expand Down Expand Up @@ -415,13 +415,13 @@ function Base.show(io::IO, p::Plot)
topleft_len = length(topleft_str)
topmid_len = length(topmid_str)
topright_len = length(topright_str)
printstyled(io, border_padding, topleft_str; color = topleft_col)
print_color(topleft_col, io, border_padding, topleft_str)
cnt = round(Int, border_length / 2 - topmid_len / 2 - topleft_len, RoundNearestTiesUp)
pad = cnt > 0 ? repeat(" ", cnt) : ""
printstyled(io, pad, topmid_str; color = topmid_col)
print_color(topmid_col, io, pad, topmid_str)
cnt = border_length - topright_len - topleft_len - topmid_len + 2 - cnt
pad = cnt > 0 ? repeat(" ", cnt) : ""
printstyled(io, pad, topright_str, "\n"; color = topright_col)
print_color(topright_col, io, pad, topright_str, "\n")
end
end
print_border_top(io, border_padding, border_length, p.border)
Expand All @@ -448,24 +448,24 @@ function Base.show(io::IO, p::Plot)
if p.show_labels
if row == y_lab_row
# print ylabel
printstyled(io, p.ylabel; color = :normal)
print_color(:normal, io, p.ylabel)
print(io, repeat(" ", max_len_l - length(p.ylabel) - left_len))
else
# print padding to fill ylabel length
print(io, repeat(" ", max_len_l - left_len))
end
# print the left annotation
printstyled(io, left_str; color = left_col)
print_color(left_col, io, left_str)
end
# print left border
printstyled(io, plot_padding, b[:l]; color = :light_black)
print_color(:light_black, io, plot_padding, b[:l])
# print canvas row
printrow(io, c, row)
# print right label and padding
printstyled(io, b[:r]; color = :light_black)
print_color(:light_black, io, b[:r])
if p.show_labels
print(io, plot_padding)
printstyled(io, right_str; color = right_col)
print_color(right_col, io, right_str)
print(io, repeat(" ", max_len_r - right_len))
end
# print colorbar
Expand All @@ -491,13 +491,13 @@ function Base.show(io::IO, p::Plot)
botleft_len = length(botleft_str)
botmid_len = length(botmid_str)
botright_len = length(botright_str)
printstyled(io, border_padding, botleft_str; color = botleft_col)
print_color(botleft_col, io, border_padding, botleft_str)
cnt = round(Int, border_length / 2 - botmid_len / 2 - botleft_len, RoundNearestTiesUp)
pad = cnt > 0 ? repeat(" ", cnt) : ""
printstyled(io, pad, botmid_str; color = botmid_col)
print_color(botmid_col, io, pad, botmid_str)
cnt = border_length - botright_len - botleft_len - botmid_len + 2 - cnt
pad = cnt > 0 ? repeat(" ", cnt) : ""
printstyled(io, pad, botright_str; color = botright_col)
print_color(botright_col, io, pad, botright_str)
end
# abuse the print_title function to print the xlabel. maybe refactor this
p.xlabel != "" && println(io)
Expand Down

0 comments on commit 7d8dc95

Please sign in to comment.