Skip to content

Commit ed884ac

Browse files
committed
in work
1 parent c17d4ee commit ed884ac

File tree

2 files changed

+220
-94
lines changed

2 files changed

+220
-94
lines changed

src/components_utils/table_format.jl

Lines changed: 110 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module TableFormat
2-
export Format
2+
export Format, Align, Group, Padding, Prefix, Scheme, Sign, DSymbol, Trim
33

44
struct NamedValue{Name, T}
55
value::T
@@ -90,75 +90,59 @@ module TableFormat
9090
yes = "~"
9191
))
9292

93-
abstract type FormatProperty end;
94-
95-
struct NamedProperty{Name, Keys} <: FormatProperty
96-
value::NamedValue{Name}
97-
function NamedProperty{Name, Keys}(value::NamedValue{ValueName}) where {Name, ValueName, Keys}
98-
Name != ValueName && throw(ArgumentError("expected value to be one of $(join(string.(string(Name), ".", string.(Keys)), ", "))"))
99-
return new{Name, Keys}(value)
100-
end
101-
end
102-
103-
property_by_tuple(::TupleWithNamedValues{Name, Keys}) where {Name, Keys} = NamedProperty{Name, Keys}
104-
105-
get_value(p::NamedProperty) = p.value.value
106-
107-
struct CharProperty <: FormatProperty
108-
value::Char
109-
CharProperty(value::Char) = new(value)
110-
function CharProperty(value::String)
111-
length(value) != 1 && throw(ArgumentError("expected char or string of length one"))
112-
return new(value[1])
113-
end
114-
CharProperty(value) = throw(ArgumentError("expected char or string of length one"))
115-
end
116-
117-
get_value(p::CharProperty) = p.value
118-
119-
struct StringProperty <: FormatProperty
120-
value::String
121-
StringProperty(value::String) = new(value)
122-
StringProperty(value) = throw(ArgumentError("expected value to be a string"))
123-
end
124-
125-
get_value(p::StringProperty) = p.value
126-
127-
struct UIntOrNothingProperty <: FormatProperty
128-
value::Union{UInt, Nothing}
129-
UIntOrNothingProperty(v::Nothing) = new(v)
130-
function UIntOrNothingProperty(v::Integer)
131-
v < 0 && throw(ArgumentError("expected value to be non-negative"))
132-
return new(v)
133-
end
134-
UIntOrNothingProperty(v) = throw(ArgumentError("expected value to be an integer"))
135-
end
136-
137-
get_value(p::UIntOrNothingProperty) = p.value
138-
13993
mutable struct Format
14094
locale
14195
nully
14296
prefix
14397
specifier
144-
function Format()
145-
return new(
98+
function Format(;
99+
align = Align.default,
100+
fill = nothing,
101+
group = Group.no,
102+
padding = Padding.no,
103+
padding_width = nothing,
104+
precision = nothing,
105+
scheme = Scheme.default,
106+
sign = Sign.default,
107+
symbol = DSymbol.no,
108+
trim = Trim.no,
109+
110+
symbol_prefix = nothing,
111+
symbol_suffix = nothing,
112+
decimal_delimiter = nothing,
113+
group_delimiter = nothing,
114+
groups = nothing,
115+
116+
nully = nothing,
117+
118+
si_prefix = Prefix.none
119+
)
120+
result = new(
146121
Dict(),
147122
"",
148-
Prefix.none,
149-
Dict(
150-
:align => Align.default,
151-
:fill => "",
152-
:group => Group.no,
153-
:width => "",
154-
:padding => Padding.no,
155-
:precision => "",
156-
:sign => Sign.default,
157-
:symbol => DSymbol.no,
158-
:trim => Trim.no,
159-
:type => Scheme.default
160-
)
123+
Prefix.none.value,
124+
Dict{Symbol, Any}()
161125
)
126+
align!(result, align)
127+
fill!(result, fill)
128+
group!(result, group)
129+
padding!(result, padding)
130+
padding_width!(result, padding_width)
131+
precision!(result, precision)
132+
scheme!(result, scheme)
133+
sign!(result, sign)
134+
symbol!(result, symbol)
135+
trim!(result, trim)
136+
137+
!isnothing(symbol_prefix) && symbol_prefix!(result, symbol_prefix)
138+
!isnothing(symbol_suffix) && symbol_suffix!(result, symbol_suffix)
139+
!isnothing(decimal_delimiter) && decimal_delimiter!(result, decimal_delimiter)
140+
!isnothing(group_delimiter) && group_delimiter!(result, group_delimiter)
141+
!isnothing(groups) && groups!(result, groups)
142+
!isnothing(nully) && nully!(result, nully)
143+
!isnothing(si_prefix) && si_prefix!(result, si_prefix)
144+
145+
return result
162146
end
163147
end
164148

@@ -170,10 +154,10 @@ module TableFormat
170154
throw(ArgumentError("expected value to be one of $(possible_values(t))"))
171155
end
172156

173-
check_char_value(v::Char) = v
157+
check_char_value(v::Char) = string(v)
174158
function check_char_value(v::String)
175-
length(v) != 1 && throw(ArgumentError("expected char or string of length one"))
176-
return v[1]
159+
length(v) > 1 && throw(ArgumentError("expected char or string of length one"))
160+
return v
177161
end
178162
function check_char_value(v)
179163
throw(ArgumentError("expected char or string of length one"))
@@ -189,10 +173,14 @@ module TableFormat
189173

190174
function align!(f::Format, value)
191175
check_named_value(Align, value)
192-
f.specifier[:align] = value
176+
f.specifier[:align] = value.value
193177
end
194178

195179
function fill!(f::Format, value)
180+
if isnothing(value)
181+
f.specifier[:fill] = ""
182+
return
183+
end
196184
v = check_char_value(value)
197185
f.specifier[:fill] = v
198186
end
@@ -203,15 +191,15 @@ module TableFormat
203191
value = value ? Group.yes : Group.no
204192
end
205193
check_named_value(Group, value)
206-
f.specifier[:group] = value
194+
f.specifier[:group] = value.value
207195
end
208196

209197
function padding!(f::Format, value)
210198
if value isa Bool
211199
value = value ? Padding.yes : Padding.no
212200
end
213201
check_named_value(Padding, value)
214-
f.specifier[:padding] = value
202+
f.specifier[:padding] = value.value
215203
end
216204

217205
function padding_width!(f::Format, value)
@@ -234,38 +222,38 @@ module TableFormat
234222

235223
function scheme!(f::Format, value)
236224
check_named_value(Scheme, value)
237-
f.specifier[:type] = value
225+
f.specifier[:type] = value.value
238226
end
239227

240228
function sign!(f::Format, value)
241229
check_named_value(Sign, value)
242-
f.specifier[:sign] = value
230+
f.specifier[:sign] = value.value
243231
end
244232

245233
function symbol!(f::Format, value)
246234
check_named_value(DSymbol, value)
247-
f.specifier[:symbol] = value
235+
f.specifier[:symbol] = value.value
248236
end
249237

250238
function trim!(f::Format, value)
251239
if value isa Bool
252240
value = value ? Trim.yes : Trim.no
253241
end
254242
check_named_value(Trim, value)
255-
f.specifier[:trim] = value
243+
f.specifier[:trim] = value.value
256244
end
257245

258246
# Locale
259247
function symbol_prefix!(f::Format, value::AbstractString)
260-
if haskey(f.locale, :symbol)
248+
if !haskey(f.locale, :symbol)
261249
f.locale[:symbol] = [value, ""]
262250
else
263251
f.locale[:symbol][1] = value
264252
end
265253
end
266254

267255
function symbol_suffix!(f::Format, value::AbstractString)
268-
if haskey(f.locale, :symbol)
256+
if !haskey(f.locale, :symbol)
269257
f.locale[:symbol] = ["", value]
270258
else
271259
f.locale[:symbol][2] = value
@@ -282,6 +270,54 @@ module TableFormat
282270
f.locale[:group] = v
283271
end
284272

273+
function groups!(f::Format, value::Union{Vector{<:Integer}, <:Integer})
274+
groups = value isa Integer ? [value] : value
275+
isempty(groups) && throw(ArgumentError("groups cannot be empty"))
276+
277+
for g in groups
278+
g < 0 && throw(ArgumentError("group entry must be non-negative integer"))
279+
end
280+
f.locale[:grouping] = groups
281+
end
282+
283+
# Nully
284+
function nully!(f::Format, value)
285+
f.nully = value
286+
end
287+
288+
# Prefix
289+
function si_prefix!(f::Format, value)
290+
check_named_value(Prefix, value)
291+
f.prefix = value.value
292+
end
293+
294+
JSON3.StructTypes.StructType(::Type{Format}) = JSON3.RawType()
295+
296+
function JSON3.rawbytes(f::Format)
297+
aligned = f.specifier[:align] != Align.default.value
298+
fill = aligned ? f.specifier[:fill] : ""
299+
spec_io = IOBuffer()
300+
print(spec_io,
301+
aligned ? f.specifier[:fill] : "",
302+
f.specifier[:align],
303+
f.specifier[:sign],
304+
f.specifier[:symbol],
305+
f.specifier[:padding],
306+
f.specifier[:width],
307+
f.specifier[:group],
308+
f.specifier[:precision],
309+
f.specifier[:trim],
310+
f.specifier[:type]
311+
)
312+
return JSON3.write(
313+
locale = deepcopy(f.locale),
314+
nully = f.nully,
315+
prefix = f.prefix,
316+
specifier = String(take!(spec_io))
317+
)
318+
end
319+
320+
285321
#=struct Format
286322
align ::Union{typeof(Align), Nothing}
287323
fill ::Union{Char, Nothing}
@@ -567,7 +603,6 @@ class Format:
567603
# Prefix
568604
def si_prefix(self, value):
569605
self._validate_named(value, Prefix)
570-
571606
self._prefix = value
572607
return self
573608

0 commit comments

Comments
 (0)