Skip to content

Commit

Permalink
Support Irrational, Complex, and other types for default formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Dec 17, 2019
1 parent c4d2f61 commit c3f7c7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
license = "MIT"
name = "Format"
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
version = "1.0.1"
version = "1.1.0"

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
42 changes: 34 additions & 8 deletions src/fmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@

mutable struct DefaultSpec
typechar::Char
kwargs::Any
fspec::FormatSpec
DefaultSpec(c::AbstractChar) = new(Char(c), FormatSpec(c))
end

DefaultSpec(c::AbstractChar) = DefaultSpec(Char(c), (), FormatSpec(c))
function DefaultSpec(c::AbstractChar, syms...; kwargs...)
# otherwise update the spec
if isempty(syms)
DefaultSpec(Char(c), kwargs, FormatSpec(c; kwargs...))
else
kw = _add_kwargs_from_symbols(kwargs, syms...)
DefaultSpec(Char(c), tuple(kw...), FormatSpec(c; kw...))
end
end

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

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

reset!(::Type{T}) where {T} =
(dspec = default_spec(T); dspec.fspec = FormatSpec(dspec.typechar); nothing)
function reset!(::Type{T}) where {T}
dspec = default_spec(T)
dspec.fspec = FormatSpec(dspec.typechar; dspec.kwargs...)
nothing
end

# --------------------------------------------------------------------------------------------------


function _add_kwargs_from_symbols(kwargs, syms::Symbol...)
d = Dict{Symbol, Any}(kwargs)
for s in syms
Expand Down Expand Up @@ -66,6 +77,8 @@ default_spec(::Type{<:Integer}) = DEFAULT_FORMATTERS[Integer]
default_spec(::Type{<:AbstractFloat}) = DEFAULT_FORMATTERS[AbstractFloat]
default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString]
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar]
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational]
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number]

default_spec(::Type{T}) where {T} =
get(DEFAULT_FORMATTERS, T) do
Expand Down Expand Up @@ -200,3 +213,16 @@ function fmt(x, syms::Symbol...; kwargs...)
d = _add_kwargs_from_symbols(kwargs, syms...)
fmt(x; d...)
end

# --------------------------------------------------------------------------------------------------

# seed it with some basic default formatters
for (t, c) in [(Integer,'d'),
(AbstractFloat,'f'),
(AbstractChar,'c'),
(AbstractString,'s')]
default_spec!(t, c)
end

default_spec!(Number, 's', :right)
default_spec!(AbstractIrrational, 's', :right)

0 comments on commit c3f7c7d

Please sign in to comment.