Skip to content

Commit c3f7c7d

Browse files
committed
Support Irrational, Complex, and other types for default formatting
1 parent c4d2f61 commit c3f7c7d

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
2323
license = "MIT"
2424
name = "Format"
2525
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
26-
version = "1.0.1"
26+
version = "1.1.0"
2727

2828
[deps]
2929
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

src/fmt.jl

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@
1414

1515
mutable struct DefaultSpec
1616
typechar::Char
17+
kwargs::Any
1718
fspec::FormatSpec
18-
DefaultSpec(c::AbstractChar) = new(Char(c), FormatSpec(c))
19+
end
20+
21+
DefaultSpec(c::AbstractChar) = DefaultSpec(Char(c), (), FormatSpec(c))
22+
function DefaultSpec(c::AbstractChar, syms...; kwargs...)
23+
# otherwise update the spec
24+
if isempty(syms)
25+
DefaultSpec(Char(c), kwargs, FormatSpec(c; kwargs...))
26+
else
27+
kw = _add_kwargs_from_symbols(kwargs, syms...)
28+
DefaultSpec(Char(c), tuple(kw...), FormatSpec(c; kw...))
29+
end
1930
end
2031

2132
const DEFAULT_FORMATTERS = Dict{DataType, DefaultSpec}()
@@ -28,17 +39,17 @@ default_spec!(::Type{T}, c::AbstractChar) where {T} =
2839
default_spec!(::Type{T}, ::Type{K}) where {T,K} =
2940
(DEFAULT_FORMATTERS[T] = DEFAULT_FORMATTERS[K]; nothing)
3041

31-
# seed it with some basic default formatters
32-
for (t, c) in [(Integer,'d'), (AbstractFloat,'f'), (AbstractChar,'c'), (AbstractString,'s')]
33-
default_spec!(t, c)
34-
end
42+
default_spec!(::Type{T}, c::AbstractChar, syms...; kwargs...) where {T} =
43+
(DEFAULT_FORMATTERS[T] = DefaultSpec(c, syms...; kwargs...); nothing)
3544

36-
reset!(::Type{T}) where {T} =
37-
(dspec = default_spec(T); dspec.fspec = FormatSpec(dspec.typechar); nothing)
45+
function reset!(::Type{T}) where {T}
46+
dspec = default_spec(T)
47+
dspec.fspec = FormatSpec(dspec.typechar; dspec.kwargs...)
48+
nothing
49+
end
3850

3951
# --------------------------------------------------------------------------------------------------
4052

41-
4253
function _add_kwargs_from_symbols(kwargs, syms::Symbol...)
4354
d = Dict{Symbol, Any}(kwargs)
4455
for s in syms
@@ -66,6 +77,8 @@ default_spec(::Type{<:Integer}) = DEFAULT_FORMATTERS[Integer]
6677
default_spec(::Type{<:AbstractFloat}) = DEFAULT_FORMATTERS[AbstractFloat]
6778
default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString]
6879
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar]
80+
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational]
81+
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number]
6982

7083
default_spec(::Type{T}) where {T} =
7184
get(DEFAULT_FORMATTERS, T) do
@@ -200,3 +213,16 @@ function fmt(x, syms::Symbol...; kwargs...)
200213
d = _add_kwargs_from_symbols(kwargs, syms...)
201214
fmt(x; d...)
202215
end
216+
217+
# --------------------------------------------------------------------------------------------------
218+
219+
# seed it with some basic default formatters
220+
for (t, c) in [(Integer,'d'),
221+
(AbstractFloat,'f'),
222+
(AbstractChar,'c'),
223+
(AbstractString,'s')]
224+
default_spec!(t, c)
225+
end
226+
227+
default_spec!(Number, 's', :right)
228+
default_spec!(AbstractIrrational, 's', :right)

0 commit comments

Comments
 (0)