Skip to content

Commit 50762f0

Browse files
authored
add Printf support (#772)
1 parent 2effe2e commit 50762f0

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1313
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
1414
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1515
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
16+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1617

1718
[extensions]
1819
ConstructionBaseUnitfulExt = "ConstructionBase"
1920
ForwardDiffExt = "ForwardDiff"
2021
InverseFunctionsUnitfulExt = "InverseFunctions"
22+
PrintfExt = "Printf"
2123

2224
[compat]
2325
Aqua = "0.8"
@@ -26,6 +28,7 @@ Dates = "<0.0.1, 1"
2628
ForwardDiff = "0.10, 1"
2729
InverseFunctions = "0.1"
2830
LinearAlgebra = "<0.0.1, 1"
31+
Printf = "<0.0.1, 1"
2932
REPL = "<0.0.1, 1"
3033
Random = "<0.0.1, 1"
3134
Test = "<0.0.1, 1"
@@ -37,9 +40,10 @@ ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
3740
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
3841
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
3942
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
43+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
4044
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
4145
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
4246
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4347

4448
[targets]
45-
test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL"]
49+
test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL", "Printf"]

ext/PrintfExt.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module PrintfExt
2+
3+
using Printf
4+
using Unitful
5+
using Unitful: AbstractQuantity
6+
7+
Printf.plength(f::Printf.Spec{<:Printf.Ints}, x::AbstractQuantity{<:Real}) = Printf.plength(f, ustrip(x)) + length(string(unit(x))) + Unitful.has_unit_spacing(unit(x))
8+
9+
# separate methods for disambiguation
10+
Printf.fmt(buf, pos, arg::AbstractQuantity{<:Real}, spec::Printf.Spec{<:Printf.Floats}) = _fmt(buf, pos, arg, spec)
11+
Printf.fmt(buf, pos, arg::AbstractQuantity{<:Real}, spec::Printf.Spec{<:Printf.Ints}) = _fmt(buf, pos, arg, spec)
12+
13+
function _fmt(buf, pos, arg, spec)
14+
pos = Printf.fmt(buf, pos, ustrip(arg), spec)
15+
if Unitful.has_unit_spacing(unit(arg))
16+
pos = Printf.fmt(buf, pos, ' ', only((Printf.format"%c").formats))
17+
end
18+
pos = Printf.fmt(buf, pos, string(unit(arg)), only((Printf.format"%s").formats))
19+
return pos
20+
end
21+
22+
end

test/runtests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Unitful
2-
using Test, LinearAlgebra, Random, ConstructionBase, InverseFunctions
2+
using Test, LinearAlgebra, Random, ConstructionBase, InverseFunctions, Printf
33
import Unitful: DimensionError, AffineError
44
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
55
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
@@ -1739,6 +1739,10 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0")
17391739
@test Base.alignment(stdout, 3.0dBm*s) == Base.alignment(stdout, 3.0) .+ (1, 7)
17401740
end
17411741

1742+
VERSION v"1.9.0" && @testset "printf" begin
1743+
@test (@sprintf "%f %d %.2f %05d" 1.23u"m" 123.4u"°" 0.1234u"W" 12.34u"km") == "1.230000 m 123° 0.12 W 00012 km"
1744+
end
1745+
17421746
@testset "DimensionError message" begin
17431747
function errorstr(e)
17441748
b = IOBuffer()

0 commit comments

Comments
 (0)