From ef7434d3da758a3ef2134cf7514d36ea70fb2f8e Mon Sep 17 00:00:00 2001 From: himcraft <1014harry@gmail.com> Date: Wed, 29 Jan 2025 11:21:15 +0800 Subject: [PATCH] Added an option `export_raw` for showoff() to export unprocessed data to Makie for further customization. --- src/Showoff.jl | 6 +++--- src/ryu.jl | 42 +++++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Showoff.jl b/src/Showoff.jl index 0001f5b..2e224de 100644 --- a/src/Showoff.jl +++ b/src/Showoff.jl @@ -80,7 +80,7 @@ function scientific_precision_heuristic(xs::AbstractArray{<:AbstractFloat}) end -function showoff(xs::AbstractArray{<:AbstractFloat}, style=:auto) +function showoff(xs::AbstractArray{<:AbstractFloat}, style=:auto; export_raw=false) x_min = concrete_minimum(xs) x_max = concrete_maximum(xs) x_min = Float64(x_min) @@ -103,11 +103,11 @@ function showoff(xs::AbstractArray{<:AbstractFloat}, style=:auto) return String[format_fixed(x, precision) for x in xs] elseif style == :scientific precision = scientific_precision_heuristic(xs) - return String[format_fixed_scientific(x, precision, false) + return String[format_fixed_scientific(x, precision, false, export_raw) for x in xs] elseif style == :engineering precision = scientific_precision_heuristic(xs) - return String[format_fixed_scientific(x, precision, true) + return String[format_fixed_scientific(x, precision, true, export_raw) for x in xs] else throw(ArgumentError("$(style) is not a recongnized number format")) diff --git a/src/ryu.jl b/src/ryu.jl index 8b074af..1341a03 100644 --- a/src/ryu.jl +++ b/src/ryu.jl @@ -34,7 +34,7 @@ end # Print a floating point number in scientific notation at fixed precision. Sort of equivalent # to @sprintf("%0.$(precision)e", x), but prettier printing. function format_fixed_scientific(x::AbstractFloat, precision::Integer, - engineering::Bool) + engineering::Bool, export_raw::Bool=false) if iszero(x) return "0" elseif isinf(x) @@ -53,31 +53,35 @@ function format_fixed_scientific(x::AbstractFloat, precision::Integer, end - buf = IOBuffer() + if export_raw #if export base_digits and power separately. + return e_format_number + else + buf = IOBuffer() - print(buf, base_digits) - print(buf, "×10") + print(buf, base_digits) + print(buf, "×10") - if power[1] == '-' - print(buf, '⁻') - end - leading_index = findfirst(c -> '1' <= c <= '9', power) + if power[1] == '-' + print(buf, '⁻') + end + leading_index = findfirst(c -> '1' <= c <= '9', power) - if leading_index === nothing - print(buf, superscript_numerals[1]) - return String(take!(buf)) - end + if leading_index === nothing + print(buf, superscript_numerals[1]) + return String(take!(buf)) + end + + for digit in power[leading_index:end] + if digit == '-' + print(buf, '⁻') + elseif '0' <= digit <= '9' + print(buf, superscript_numerals[digit - '0' + 1]) + end - for digit in power[leading_index:end] - if digit == '-' - print(buf, '⁻') - elseif '0' <= digit <= '9' - print(buf, superscript_numerals[digit - '0' + 1]) end + return String(take!(buf)) end - - return String(take!(buf)) end