-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathdisplay.jl
182 lines (167 loc) · 4.85 KB
/
display.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Convenient dictionary for mapping powers of ten to an SI prefix.
const prefixdict = Dict(
-24 => "y",
-21 => "z",
-18 => "a",
-15 => "f",
-12 => "p",
-9 => "n",
-6 => "μ", # tab-complete \mu, not option-m on a Mac!
-3 => "m",
-2 => "c",
-1 => "d",
0 => "",
1 => "da",
2 => "h",
3 => "k",
6 => "M",
9 => "G",
12 => "T",
15 => "P",
18 => "E",
21 => "Z",
24 => "Y"
)
"""
`abbr(x)` provides abbreviations for units or dimensions. Since a method should
always be defined for each unit and dimension type, absence of a method for a
specific unit or dimension type is likely an error. Consequently, we return ❓
for generic arguments to flag unexpected behavior.
"""
abbr(x) = "❓" # Indicate missing abbreviations
"""
prefix(x::Unit)
Returns a string representing the SI prefix for the power-of-ten held by
this particular unit.
"""
function prefix(x::Unit)
if haskey(prefixdict, tens(x))
return prefixdict[tens(x)]
else
error("Invalid power-of-ten prefix.")
end
end
function show(io::IO, x::Unit{N,D}) where {N,D}
show(io, FreeUnits{(x,), D, nothing}())
end
# Space between numerical value and unit should always be included
# except for angular degress, minutes and seconds (° ′ ″)
# See SI 9th edition, section 5.4.3; "Formatting the value of a quantity"
# https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure-9.pdf
has_unit_spacing(u) = true
has_unit_spacing(u::Units{(Unit{:Degree, NoDims}(0, 1//1),), NoDims}) = false
"""
show(io::IO, x::Quantity)
Show a unitful quantity by calling `show` on the numeric value, appending a
space, and then calling `show` on a units object `U()`.
"""
function show(io::IO, x::Quantity)
show(io,x.val)
if !isunitless(unit(x))
has_unit_spacing(unit(x)) && print(io," ")
show(io, unit(x))
end
nothing
end
function show(io::IO, mime::MIME"text/plain", x::Quantity)
show(io, mime, x.val)
if !isunitless(unit(x))
has_unit_spacing(unit(x)) && print(io," ")
show(io, mime, unit(x))
end
end
function show(io::IO, x::Type{T}) where T<:Quantity
invoke(show, Tuple{IO, typeof(x)}, IOContext(io, :showoperators=>true), x)
end
function show(io::IO, x::Type{T}) where T<:Unitlike
invoke(show, Tuple{IO, typeof(x)}, IOContext(io, :showoperators=>true), x)
end
function show(io::IO, r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity
a,s,b = first(r), step(r), last(r)
U = unit(a)
print(io, '(')
if ustrip(U, s) == 1
show(io, ustrip(U, a):ustrip(U, b))
else
show(io, ustrip(U, a):ustrip(U, s):ustrip(U, b))
end
print(io, ')')
has_unit_spacing(U) && print(io,' ')
show(io, U)
end
function show(io::IO, x::typeof(NoDims))
print(io, "NoDims")
end
"""
show(io::IO, x::Unitlike)
Call [`Unitful.showrep`](@ref) on each object in the tuple that is the type
variable of a [`Unitful.Units`](@ref) or [`Unitful.Dimensions`](@ref) object.
"""
function show(io::IO, x::Unitlike)
showoperators = get(io, :showoperators, false)
first = ""
sep = showoperators ? "*" : " "
foreach(sortexp(typeof(x).parameters[1])) do y
print(io,first)
showrep(io,y)
first = sep
end
nothing
end
"""
sortexp(xs)
Sort units to show positive exponents first.
"""
function sortexp(xs)
vcat([x for x in xs if power(x) >= 0],
[x for x in xs if power(x) < 0])
end
"""
showrep(io::IO, x::Unit)
Show the unit, prefixing with any decimal prefix and appending the exponent as
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))))
nothing
end
"""
showrep(io::IO, x::Dimension)
Show the dimension, appending any exponent as formatted by
[`Unitful.superscript`](@ref).
"""
function showrep(io::IO, x::Dimension)
print(io, abbr(x))
print(io, (power(x) == 1//1 ? "" : superscript(power(x))))
end
"""
superscript(i::Rational)
Prints exponents.
"""
function superscript(i::Rational)
v = @eval get(ENV, "UNITFUL_FANCY_EXPONENTS", $(Sys.isapple() ? "true" : "false"))
t = tryparse(Bool, lowercase(v))
k = (t === nothing) ? false : t
if k
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), "//" => "/")
end
end
# Taken from SIUnits.jl
superscript(i::Integer) = map(repr(i)) do c
c == '-' ? '\u207b' :
c == '1' ? '\u00b9' :
c == '2' ? '\u00b2' :
c == '3' ? '\u00b3' :
c == '4' ? '\u2074' :
c == '5' ? '\u2075' :
c == '6' ? '\u2076' :
c == '7' ? '\u2077' :
c == '8' ? '\u2078' :
c == '9' ? '\u2079' :
c == '0' ? '\u2070' :
error("unexpected character")
end