Skip to content

Commit

Permalink
Use the :fancy_exponent IO context property to override the behavio…
Browse files Browse the repository at this point in the history
…r of fancy exponents
  • Loading branch information
DilumAluthge committed May 20, 2021
1 parent cc3adef commit 664bc52
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Unitful"
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.7.0"
version = "1.8.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
3 changes: 2 additions & 1 deletion docs/src/display.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
By default, exponents on units or dimensions are indicated using Unicode superscripts on
macOS and without superscripts on other operating systems. You can set the environment
variable `UNITFUL_FANCY_EXPONENTS` to either `true` or `false` to force using or not using
the exponents.
the exponents. You can also set the `:fancy_exponent` IO context property to either `true`
or `false` to force using or not using the exponents.

```@docs
Unitful.BracketStyle
Expand Down
32 changes: 23 additions & 9 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ formatted by [`Unitful.superscript`](@ref).
function showrep(io::IO, x::Unit)
print(io, prefix(x))
print(io, abbr(x))
print(io, (power(x) == 1//1 ? "" : superscript(power(x))))
print(io, (power(x) == 1//1 ? "" : superscript(power(x); io=io)))
nothing
end

Expand All @@ -194,18 +194,32 @@ Show the dimension, appending any exponent as formatted by
"""
function showrep(io::IO, x::Dimension)
print(io, abbr(x))
print(io, (power(x) == 1//1 ? "" : superscript(power(x))))
print(io, (power(x) == 1//1 ? "" : superscript(power(x); io=io)))
end

"""
superscript(i::Rational)
Prints exponents.
superscript(i::Rational; io::Union{IO, Nothing} = nothing)
Returns exponents as a string.
This function returns the value as a string. It does not print to `io`. `io` is
only used for IO context values. If `io` contains the `:fancy_exponent`
property and the value is a `Bool`, this value will override the behavior of
fancy exponents.
"""
function superscript(i::Rational)
v = get(ENV, "UNITFUL_FANCY_EXPONENTS", Sys.isapple() ? "true" : "false")
t = tryparse(Bool, lowercase(v))
k = (t === nothing) ? false : t
if k
function superscript(i::Rational; io::Union{IO, Nothing} = nothing)
if io === nothing
iocontext_value = nothing
else
iocontext_value = get(io, :fancy_exponent, nothing)
end
if iocontext_value isa Bool
fancy_exponent = iocontext_value
else
v = get(ENV, "UNITFUL_FANCY_EXPONENTS", Sys.isapple() ? "true" : "false")
t = tryparse(Bool, lowercase(v))
fancy_exponent = (t === nothing) ? false : t
end
if fancy_exponent
return i.den == 1 ? superscript(i.num) : string(superscript(i.num), '\u141F', superscript(i.den))
else
i.den == 1 ? "^" * string(i.num) : "^" * replace(string(i), "//" => "/")
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,10 @@ end
@test string(dimension(1u"m/s")) == "𝐋 𝐓^-1"
@test string(NoDims) == "NoDims"
end
@testset ":fancy_exponent IOContext property" begin
@test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "m s⁻¹"
@test sprint(io -> show(IOContext(io, :fancy_exponent => false), u"m/s")) == "m s^-1"
end
end

struct Foo <: Number end
Expand Down

0 comments on commit 664bc52

Please sign in to comment.