Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
5 changes: 3 additions & 2 deletions test/fmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ i = 1234567
@test fmt(i) == "1234567"
@test fmt(i,:commas) == "1,234,567"

@test_throws ErrorException fmt_default(Real)
@test_throws ErrorException fmt_default(Complex)
# These are not handled
#@test_throws ErrorException fmt_default(Real)
#@test_throws ErrorException fmt_default(Complex)

fmt_default!(Int, :commas, width = 12)
@test fmt(i) == " 1,234,567"
Expand Down