diff --git a/Project.toml b/Project.toml index cfb4ee66..e8be0cf1 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/docs/src/display.md b/docs/src/display.md index 57d7c0c9..a1f016d8 100644 --- a/docs/src/display.md +++ b/docs/src/display.md @@ -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 diff --git a/src/display.jl b/src/display.jl index e51ae640..249ad93f 100644 --- a/src/display.jl +++ b/src/display.jl @@ -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 @@ -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), "//" => "/") diff --git a/test/runtests.jl b/test/runtests.jl index aec5a66f..c0803185 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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