Category talk:Wren-fmt: Difference between revisions
Content added Content deleted
(→Source code: Bug fix.) |
(→Source code: Several improvements including support for formatting a sequence of values.) |
||
Line 249: | Line 249: | ||
// Centers a string or value 'v' within a field of minimum width 'w'. Pads with spaces. |
// Centers a string or value 'v' within a field of minimum width 'w'. Pads with spaces. |
||
static c(w, v) { cjust(w, v) } |
static c(w, v) { cjust(w, v) } |
||
// Embeds a string or value 'v' in 'cc', a string with no more than two characters. |
|||
// If it has none, no embedding takes place. If has one, it's doubled. |
|||
// The first character is added at the left and the second at the right. |
|||
static q(v, cc) { |
|||
var len |
|||
if (!(cc is String && (len = cc.count) < 3)) { |
|||
Fiber.abort("Second argument must be a string with no more than 2 characters.") |
|||
} |
|||
if (len == 0) return (v is String) ? v : "%(v)" |
|||
if (len == 1) cc = cc + cc |
|||
return "%(cc[0])%(v)%(cc[1])" |
|||
} |
|||
// |
// Convenience version of the above which uses double quotes as the embedding characters. |
||
static q(v) { "\"%(v)\"" } |
static q(v) { "\"%(v)\"" } |
||
Line 303: | Line 316: | ||
static fz(w, n, p) { (w >= 0) ? zfill(w, f(w, n, p).trimStart()) : f(w, n, p) } |
static fz(w, n, p) { (w >= 0) ? zfill(w, f(w, n, p).trimStart()) : f(w, n, p) } |
||
static gz(w, n, p) { (w >= 0) ? zfill(w, g(w, n, p).trimStart()) : g(w, n, p) } |
static gz(w, n, p) { (w >= 0) ? zfill(w, g(w, n, p).trimStart()) : g(w, n, p) } |
||
// Formats the integer part of 'n' in commatized form, space padded, |
|||
// using ',' as the separator. The decimal part is not affected. |
|||
static fc(w, n, p) { |
|||
var f = f(w, n, p) |
|||
if (f.contains("infinity") || f == "nan" || f.contains("e")) return f |
|||
var ix = f.indexOf(".") |
|||
var dp = (ix >= 0) ? f[ix..-1] : "" |
|||
var c = dp.count |
|||
w = (w >= 0) ? w - c : w + c |
|||
if (w < 0) w = 0 |
|||
return dc(w, n.truncate) + dp |
|||
} |
|||
// Works like 'fc' except replaces any trailing zeros after the decimal point with spaces. |
|||
// If the resulting string would end with a decimal point, a zero is first added back. |
|||
static gc(w, n, p) { |
|||
var f = fc(w, n, p) |
|||
if (f.contains(".") && f[-1] == "0") { |
|||
var l1 = f.count |
|||
f = f.trimEnd("0") |
|||
if (f[-1] == ".") f = f + "0" |
|||
f = f + (" " * (l1 - f.count)) |
|||
} |
|||
return f |
|||
} |
|||
// Convenience versions of the above methods which use the default precision. |
// Convenience versions of the above methods which use the default precision. |
||
Line 309: | Line 348: | ||
static fz(w, n) { fz(w, n, precision) } |
static fz(w, n) { fz(w, n, precision) } |
||
static gz(w, n) { gz(w, n, precision) } |
static gz(w, n) { gz(w, n, precision) } |
||
static fc(w, n) { fc(w, n, precision) } |
|||
static gc(w, n) { gc(w, n, precision) } |
|||
// Applies a 'short' formatting method to each element of a list or sequence 'seq'. |
|||
// The method to be applied is specified (as a string) in 'fn'. |
|||
// The parameters to be passed to the method are specified in 'w' and 'p' |
|||
// 'p' is needed for 'f', 'g', 'fz' or 'gz' but is ignored otherwise. |
|||
// The resulting strings are then joined together using the separator 'sep'. |
|||
// having first applied the 'q' method, with parameter 'cc', to each of them. |
|||
// Finally, the 'q' method is applied again, with parameter 'bb', to the whole |
|||
// string, if a prefix/suffix is needed. |
|||
static v(fn, w, seq, p, sep, bb, cc) { |
|||
var l = List.filled(seq.count, "") |
|||
for (i in 0...seq.count) { |
|||
var e = seq[i] |
|||
l[i] = (fn == "d") ? d(w, e) : |
|||
(fn == "b") ? b(w, e) : |
|||
(fn == "o") ? o(w, e) : |
|||
(fn == "x") ? x(w, e) : |
|||
(fn == "X") ? Fmt.X(w, e) : |
|||
(fn == "s") ? s(w, e) : |
|||
(fn == "c") ? c(w, e) : |
|||
(fn == "f") ? f(w, e, p) : |
|||
(fn == "g") ? g(w, e, p) : |
|||
(fn == "dz") ? dz(w, e) : |
|||
(fn == "bz") ? bz(w, e) : |
|||
(fn == "oz") ? oz(w, e) : |
|||
(fn == "xz") ? xz(w, e) : |
|||
(fn == "Xz") ? Fmt.Xz(w, e) : |
|||
(fn == "fz") ? fz(w, e, p) : |
|||
(fn == "gz") ? gz(w, e, p) : |
|||
(fn == "dp") ? dp(w, e) : |
|||
(fn == "dm") ? dm(w, e) : |
|||
(fn == "dc") ? dc(w, e) : |
|||
(fn == "fc") ? fc(w, e, p) : |
|||
(gn == "gc") ? gc(w, e, p) : Fiber.abort("Method not recognized.") |
|||
l[i] = q(l[i], cc) |
|||
} |
|||
return q(l.join(sep), bb) |
|||
} |
|||
// Convenience versions of the above method which use default values |
|||
// for some parameters. |
|||
static v(fn, w, seq, p, sep, bb) { v(fn, w, seq, p, sep, bb, "") } |
|||
static v(fn, w, seq, p) { v(fn, w, seq, p, ", ", "[]", "") } |
|||
static v(fn, w, seq) { v(fn, w, seq, precision, ", ", "[]", "") } |
|||
} |
} |
||
// Type aliases for classes in case of any name clashes with other modules. |
// Type aliases for classes in case of any name clashes with other modules. |
||
var Fmt_Conv = Conv |
var Fmt_Conv = Conv |