-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathdisplay.jl
More file actions
229 lines (205 loc) · 6.92 KB
/
display.jl
File metadata and controls
229 lines (205 loc) · 6.92 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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
abstract type BracketStyle end
struct NoBrackets <: BracketStyle end
print_opening_bracket(io::IO, ::NoBrackets) = nothing
print_closing_bracket(io::IO, ::NoBrackets) = nothing
struct RoundBrackets <: BracketStyle end
print_opening_bracket(io::IO, ::RoundBrackets) = print(io, '(')
print_closing_bracket(io::IO, ::RoundBrackets) = print(io, ')')
struct SquareBrackets <: BracketStyle end
print_opening_bracket(io::IO, ::SquareBrackets) = print(io, '[')
print_closing_bracket(io::IO, ::SquareBrackets) = print(io, ']')
print_opening_bracket(io::IO, x) = print_opening_bracket(io, BracketStyle(x))
print_closing_bracket(io::IO, x) = print_closing_bracket(io, BracketStyle(x))
"""
BracketStyle(x)
BracketStyle(typeof(x))
`BracketStyle` specifies whether the numeric value of a `Quantity` is printed in brackets
(and what kind of brackets). Three styles are defined:
* `NoBrackets()`: this is the default, for example used for real numbers: `1.2 m`
* `RoundBrackets()`: used for complex numbers: `(2.5 + 1.0im) V`
* `SquareBrackets()`: used for [`Level`](@ref)/[`Gain`](@ref): `[3 dB] Hz^-1`
"""
BracketStyle(x) = BracketStyle(typeof(x))
BracketStyle(::Type) = NoBrackets()
BracketStyle(::Type{<:Complex}) = RoundBrackets()
"""
showval(io::IO, x::Number, brackets::Bool=true)
Show the numeric value `x` of a quantity. Depending on the type of `x`, the value may be
enclosed in brackets (see [`BracketStyle`](@ref)). If `brackets` is set to `false`, the
brackets are not printed.
"""
function showval(io::IO, x::Number, brackets::Bool=true)
brackets && print_opening_bracket(io, x)
show(io, MIME"text/plain"(), x)
brackets && print_closing_bracket(io, x)
end
function showval(io::IO, mime::MIME, x::Number, brackets::Bool=true)
brackets && print_opening_bracket(io, x)
show(io, mime, x)
brackets && print_closing_bracket(io, x)
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, mime::MIME"text/plain", x::Quantity)
Show a unitful quantity by calling [`showval`](@ref) on the numeric value, appending a
space, and then calling `show` on a units object `U()`.
"""
function show(io::IO, mime::MIME"text/plain", x::Quantity)
if isunitless(unit(x))
showval(io, mime, x.val, false)
else
showval(io, mime, x.val, true)
has_unit_spacing(unit(x)) && print(io, ' ')
show(io, mime, unit(x))
end
end
function show(io::IO, mime::MIME"text/plain", 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, mime, ustrip(U, a):ustrip(U, b))
else
show(io, mime, ustrip(U, a):ustrip(U, s):ustrip(U, b))
end
print(io, ')')
has_unit_spacing(U) && print(io,' ')
show(io, mime, U)
end
function show(io::IO, ::MIME"text/plain", x::typeof(NoDims))
print(io, "NoDims")
end
"""
show(io::IO, ::MIME"text/plain", 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, ::MIME"text/plain", 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); io=io)))
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); io=io)))
end
"""
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; 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), "//" => "/")
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