Skip to content

Commit 973e69f

Browse files
committed
ranges
1 parent 0aab8d7 commit 973e69f

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/InTheRed.jl

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This package overloads `Base.show` to change how numbers are printed:
88
* High- and low-precision numbers (like `Float32`, `Int16`, `BigInt`) are cyan, or magenta if negative.
99
1010
In addition, vectors of real numbers are displayed with a bar graph alongside their values.
11+
And ranges are printed with more detail.
1112
1213
# Examples
1314
@@ -43,6 +44,17 @@ julia> sort(vec(x))[2:2:end]
4344
2.0 # ┝━━━━━━━━━━━
4445
3.0 # ┝━━━━━━━━━━━━━━━━╸
4546
NaN # ╪
47+
48+
julia> 0:0.1:1
49+
11-element StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}:
50+
# range(0.0, 1.0, length=11) === 0.0:0.1:1.0
51+
0.0 # │
52+
0.1 # ┝━━━╸
53+
0.2 # ┝━━━━━━╸
54+
55+
0.8 # ┝━━━━━━━━━━━━━━━━━━━━━━━━━━╸
56+
0.9 # ┝━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
57+
1.0 # ┝━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4658
```
4759
"""
4860
module InTheRed
@@ -383,6 +395,97 @@ function _arrow(x::Complex)
383395
i = trunc(Int, mod(angle(x) - pi / 8, 2pi) * (4 / pi)) + 1
384396
ARROWS[mod1(i + 1, 8)]
385397
end
398+
399+
#####
400+
##### ranges
401+
#####
402+
403+
# show(io::IO, ::MIME"text/plain", r::AbstractRange) = show(io, r) # always use the compact form for printing ranges
404+
405+
# function show(io::IO, ::MIME"text/plain", r::LinRange)
406+
# isempty(r) && return show(io, r)
407+
# # show for LinRange, e.g.
408+
# # range(1, stop=3, length=7)
409+
# # 7-element LinRange{Float64}:
410+
# # 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
411+
# summary(io, r)
412+
# println(io, ":")
413+
# print_range(io, r)
414+
# end
415+
416+
# function show(io::IO, ::MIME"text/plain", r::LogRange) # display LogRange like LinRange
417+
# isempty(r) && return show(io, r)
418+
# summary(io, r)
419+
# println(io, ":")
420+
# print_range(io, r, " ", ", ", "", " \u2026 ")
421+
# end
422+
423+
Base.show(io::IO, ::MIME{Symbol("text/plain")}, r::AbstractRange{<:Real}) = _bigshowrange(io, r)
424+
Base.show(io::IO, ::MIME{Symbol("text/plain")}, r::AbstractRange{<:Complex}) = _bigshowrange(io, r)
425+
Base.show(io::IO, ::MIME{Symbol("text/plain")}, r::LinRange{<:Real}) = _bigshowrange(io, r)
426+
Base.show(io::IO, ::MIME{Symbol("text/plain")}, r::LinRange{<:Complex}) = _bigshowrange(io, r)
427+
Base.show(io::IO, ::MIME{Symbol("text/plain")}, r::Base.LogRange{<:AbstractFloat}) = _bigshowrange(io, r)
428+
429+
function _bigshowrange(io, r)
430+
summary(io, r)
431+
println(io, ":")
432+
_smallshowrange(io, r)
433+
isempty(r) && return
434+
435+
# From function show(io::IO, ::MIME"text/plain", X::AbstractArray)
436+
if get(io, :limit, false)::Bool && displaysize(io)[1] - 4 <= 0
437+
return print(io, "")
438+
else
439+
println(io)
440+
end
441+
442+
# vsize = min(13, displaysize(io)[1])
443+
vsize = min(11, displaysize(io)[1])
444+
io = IOContext(io, :typeinfo => eltype(r), :displaysize => (vsize, displaysize(io)[2]))
445+
# recur_io = IOContext(io, :SHOWN_SET => r)
446+
Base.print_array(io, r)
447+
end
448+
449+
function _smallshowrange(io, r)
450+
printstyled(io, "# ", repr(r), color=:light_black)
451+
end
452+
function _smallshowrange(io, r::StepRangeLen)
453+
printstyled(io, "# range(", repr(first(r)), ", ", repr(last(r)), ", length=", repr(length(r)), ")", color=:light_black)
454+
# print(io, "range(")
455+
# show(io, first(r))
456+
# print(io, ", ")
457+
# show(io, last(r))
458+
# print(io, ", length=", length(r), ")")
459+
if eltype(r) <: Real
460+
printstyled(io, " === ", r, color=:light_black)
461+
end
462+
end
463+
function _smallshowrange(io, r::StepRange)
464+
printstyled(io, "# range(", repr(first(r)), ", ", repr(last(r)), ", step=", repr(step(r)), ")", color=:light_black)
465+
# print(io, "range(")
466+
# show(io, first(r))
467+
# print(io, ", ")
468+
# show(io, last(r))
469+
# print(io, ", step=")
470+
# show(io, step(r))
471+
# print(io, ")")
472+
printstyled(io, " === ", r, color=:light_black)
473+
end
474+
function _smallshowrange(io, r::LinRange)
475+
printstyled(io, "# LinRange(", repr(first(r)), ", ", repr(last(r)), ", ", repr(length(r)), ")", color=:light_black)
476+
# print(io, "LinRange(")
477+
# show(io, first(r))
478+
# print(io, ", ")
479+
# show(io, last(r))
480+
# print(io, ", ", length(r), ")")
481+
end
482+
function _smallshowrange(io, r::Base.LogRange)
483+
printstyled(io, "# logrange(", repr(first(r)), ", ", repr(last(r)), ", length=", repr(length(r)), ")", color=:light_black)
484+
# print(io, "logrange(")
485+
# show(io, first(r))
486+
# print(io, ", ")
487+
# show(io, last(r))
488+
# print(io, ", length=", length(r), ")")
386489
end
387490

388491
end # module InTheRed

0 commit comments

Comments
 (0)