1
1
module TableFormat
2
- export Format
2
+ export Format, Align, Group, Padding, Prefix, Scheme, Sign, DSymbol, Trim
3
3
4
4
struct NamedValue{Name, T}
5
5
value:: T
@@ -90,75 +90,59 @@ module TableFormat
90
90
yes = " ~"
91
91
))
92
92
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
-
139
93
mutable struct Format
140
94
locale
141
95
nully
142
96
prefix
143
97
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 (
146
121
Dict (),
147
122
" " ,
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} ()
161
125
)
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
162
146
end
163
147
end
164
148
@@ -170,10 +154,10 @@ module TableFormat
170
154
throw (ArgumentError (" expected value to be one of $(possible_values (t)) " ))
171
155
end
172
156
173
- check_char_value (v:: Char ) = v
157
+ check_char_value (v:: Char ) = string (v)
174
158
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
177
161
end
178
162
function check_char_value (v)
179
163
throw (ArgumentError (" expected char or string of length one" ))
@@ -189,10 +173,14 @@ module TableFormat
189
173
190
174
function align! (f:: Format , value)
191
175
check_named_value (Align, value)
192
- f. specifier[:align ] = value
176
+ f. specifier[:align ] = value. value
193
177
end
194
178
195
179
function fill! (f:: Format , value)
180
+ if isnothing (value)
181
+ f. specifier[:fill ] = " "
182
+ return
183
+ end
196
184
v = check_char_value (value)
197
185
f. specifier[:fill ] = v
198
186
end
@@ -203,15 +191,15 @@ module TableFormat
203
191
value = value ? Group. yes : Group. no
204
192
end
205
193
check_named_value (Group, value)
206
- f. specifier[:group ] = value
194
+ f. specifier[:group ] = value. value
207
195
end
208
196
209
197
function padding! (f:: Format , value)
210
198
if value isa Bool
211
199
value = value ? Padding. yes : Padding. no
212
200
end
213
201
check_named_value (Padding, value)
214
- f. specifier[:padding ] = value
202
+ f. specifier[:padding ] = value. value
215
203
end
216
204
217
205
function padding_width! (f:: Format , value)
@@ -234,38 +222,38 @@ module TableFormat
234
222
235
223
function scheme! (f:: Format , value)
236
224
check_named_value (Scheme, value)
237
- f. specifier[:type ] = value
225
+ f. specifier[:type ] = value. value
238
226
end
239
227
240
228
function sign! (f:: Format , value)
241
229
check_named_value (Sign, value)
242
- f. specifier[:sign ] = value
230
+ f. specifier[:sign ] = value. value
243
231
end
244
232
245
233
function symbol! (f:: Format , value)
246
234
check_named_value (DSymbol, value)
247
- f. specifier[:symbol ] = value
235
+ f. specifier[:symbol ] = value. value
248
236
end
249
237
250
238
function trim! (f:: Format , value)
251
239
if value isa Bool
252
240
value = value ? Trim. yes : Trim. no
253
241
end
254
242
check_named_value (Trim, value)
255
- f. specifier[:trim ] = value
243
+ f. specifier[:trim ] = value. value
256
244
end
257
245
258
246
# Locale
259
247
function symbol_prefix! (f:: Format , value:: AbstractString )
260
- if haskey (f. locale, :symbol )
248
+ if ! haskey (f. locale, :symbol )
261
249
f. locale[:symbol ] = [value, " " ]
262
250
else
263
251
f. locale[:symbol ][1 ] = value
264
252
end
265
253
end
266
254
267
255
function symbol_suffix! (f:: Format , value:: AbstractString )
268
- if haskey (f. locale, :symbol )
256
+ if ! haskey (f. locale, :symbol )
269
257
f. locale[:symbol ] = [" " , value]
270
258
else
271
259
f. locale[:symbol ][2 ] = value
@@ -282,6 +270,54 @@ module TableFormat
282
270
f. locale[:group ] = v
283
271
end
284
272
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
+
285
321
#= struct Format
286
322
align ::Union{typeof(Align), Nothing}
287
323
fill ::Union{Char, Nothing}
@@ -567,7 +603,6 @@ class Format:
567
603
# Prefix
568
604
def si_prefix(self, value):
569
605
self._validate_named(value, Prefix)
570
-
571
606
self._prefix = value
572
607
return self
573
608
0 commit comments