Skip to content

Commit 9cedb34

Browse files
committed
initial implementation using StyleStrings
1 parent f9184cf commit 9cedb34

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1919
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2020
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2121
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
22+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
2223

2324
[weakdeps]
2425
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
@@ -53,6 +54,7 @@ Printf = "1"
5354
SparseArrays = "1"
5455
StaticArrays = "1"
5556
StatsBase = "0.33 - 0.34"
57+
StyledStrings = "1"
5658
Term = "2"
5759
Unitful = "1"
5860
julia = "1.10"

src/UnicodePlots.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module UnicodePlots
22

33
using PrecompileTools
4+
using StyledStrings
45
using LinearAlgebra
56
using StaticArrays
67
using ColorTypes

src/common.jl

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,31 +454,44 @@ crayon_color(color::ColorType) =
454454
Crayons.ANSIColor(color - THRESHOLD, Crayons.COLORS_256)
455455
end
456456

457-
function print_crayons(io, c, args...)
458-
if CRAYONS_FAST[]
459-
if Crayons.anyactive(c) # bypass crayons checks (_have_color, _force_color)
460-
print(io, Crayons.CSI)
461-
Crayons._print(io, c)
462-
print(io, Crayons.END_ANSI, args..., CRAYONS_RESET)
463-
else
464-
print(io, args...)
465-
end
466-
else
467-
print(io, c, args..., CRAYONS_RESET)
457+
458+
ss_color(::Union{Missing,Nothing}) = nothing
459+
ss_color(color::ColorType) =
460+
if color INVALID_COLOR
461+
nothing
462+
elseif color < THRESHOLD # 24bit
463+
StyledStrings.SimpleColor(red(color), grn(color), blu(color))
464+
else # 8bit
465+
StyledStrings.Legacy.legacy_color(Int(color - THRESHOLD))
468466
end
469-
end
470467

471-
print_color(io::IO, color::Crayon, args...) = print_crayons(io, color, args...)
468+
# function print_crayons(io, c, args...)
469+
# if CRAYONS_FAST[]
470+
# if Crayons.anyactive(c) # bypass crayons checks (_have_color, _force_color)
471+
# print(io, Crayons.CSI)
472+
# Crayons._print(io, c)
473+
# print(io, Crayons.END_ANSI, args..., CRAYONS_RESET)
474+
# else
475+
# print(io, args...)
476+
# end
477+
# else
478+
# print(io, c, args..., CRAYONS_RESET)
479+
# end
480+
# end
481+
482+
# print_color(io::IO, color::Crayon, args...) = print_crayons(io, color, args...)
472483
print_color(io::IO, color::UserColorType, args...) =
473484
print_color(io, ansi_color(color), args...)
474485

475486
function print_color(io::IO, color::ColorType, args...; bgcol = missing)
476487
if get(io, :color, false)
477-
print_crayons(
478-
io,
479-
Crayon(crayon_color(color), crayon_color(bgcol), CRAYONS_EMPTY_STYLES...),
480-
args...,
481-
)
488+
face = StyledStrings.Face(; foreground = ss_color(color), background = ss_color(bgcol))
489+
print(io, StyledStrings.face!(Base.annotatedstring(args...), face))
490+
# print_crayons(
491+
# io,
492+
# Crayon(crayon_color(color), crayon_color(bgcol), CRAYONS_EMPTY_STYLES...),
493+
# args...,
494+
# )
482495
else
483496
print(io, args...)
484497
end

src/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function print_title(
8383
post_pad = blank^(max(0, p_width - length(pre_pad) - length(title)))
8484
print_nocol(io, post_pad, right_pad)
8585
(
86-
count(string('\n'), title) + 1, # NOTE: string(...) for compat with 1.6
86+
count('\n', title) + 1,
8787
length(strip(left_pad * pre_pad * title * post_pad * right_pad, '\n')),
8888
)
8989
end
@@ -145,7 +145,7 @@ end
145145
Base.show(io::IO, p::Plot) = _show(io, print, print_color, p)
146146

147147
function _show(end_io::IO, print_nocol, print_color, p::Plot)
148-
buf = PipeBuffer() # buffering, for performance
148+
buf = Base.AnnotatedIOBuffer() # buffering, for performance
149149
io_color = get(end_io, :color, false)
150150
io = IOContext(buf, :color => io_color, :displaysize => displaysize(end_io))
151151

@@ -385,7 +385,7 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
385385
end
386386

387387
# delayed print (buffering)
388-
print_nocol(end_io, read(buf, String))
388+
print(end_io, read(seekstart(buf), Base.AnnotatedString))
389389

390390
# return the approximate image size
391391
(

0 commit comments

Comments
 (0)