Skip to content

Commit 6c03c19

Browse files
committed
add std.fmt format string
1 parent 3586ccd commit 6c03c19

File tree

4 files changed

+147
-20
lines changed

4 files changed

+147
-20
lines changed

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"myriad-dreamin.tinymist"
4+
]
5+
}

_lib/cram-snap.typ

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
left: none,
66
right: none,
77
top: none,
8-
bottom: if y == 0 {
9-
color
10-
} else {
11-
0pt
12-
},
8+
bottom: none,
139
)
1410
)
1511

@@ -18,18 +14,29 @@
1814
if calc.odd(y) {
1915
rgb(color)
2016
} else {
21-
none
17+
// none
18+
white
2219
}
2320
}
2421
)
2522

26-
#let theader(..cells, colspan: 2) = table.header(
27-
..cells
28-
.pos()
29-
.map(x => if type(x) == content and x.func() == table.cell {
30-
x
31-
} else {
32-
table.cell(colspan: colspan, x)
33-
}),
34-
..cells.named(),
35-
)
23+
#let stroke-color = "21222C"
24+
#let theader(..cells, colspan: 2) = {
25+
return table.header(
26+
..cells
27+
.pos()
28+
.map(x => if type(x) == content and x.func() == table.cell {
29+
x.with(bottom: rgb(stroke-color), body: x.body.with(weight: "bold", size: 1.2em))
30+
} else {
31+
table.cell(
32+
colspan: colspan,
33+
stroke: (
34+
bottom: rgb(stroke-color),
35+
),
36+
text(x, weight: "bold", size: 1.2em),
37+
)
38+
}),
39+
..cells.named(),
40+
)
41+
}
42+

fmt.typ

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#import "_lib/cram-snap.typ": theader
2+
3+
#{
4+
set table(inset: (x: 1.5mm, y: 1.7mm))
5+
6+
table(
7+
columns: (30mm, 1fr),
8+
9+
theader[std.fmt format string],
10+
11+
table.cell(
12+
colspan: 2,
13+
14+
grid(
15+
columns: (1fr, 124mm),
16+
[
17+
format string must be comptime-known and may contain placeholders in this format:\
18+
`{[argument][specifier]:[fill][alignment][width].[precision]}`\
19+
To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
20+
],
21+
text(size: 8pt)[
22+
*Note*: most of the parameters are optional and may be omitted.
23+
Also you can leave out separators like `:` and `.` when all parameters after the separator are omitted.
24+
Only exception is the *fill* parameter. If a non-zero fill character is required at the same time as width is specified, one has to specify alignment as well, as otherwise the digit following `:` is interpreted as width, not fill.
25+
],
26+
),
27+
),
28+
29+
[`argument`], [
30+
`[score]` - field name (an identifier) enclose in square brackets\
31+
`2` - numeric index (omit means use next argument)
32+
],
33+
34+
[`specifier`], [
35+
#box(inset: (top: 2mm))[
36+
a type-dependent formatting option that determines how a type should formatted
37+
]
38+
39+
#show table.cell.where(x: 0): it => [
40+
#align(center)[
41+
#it.body
42+
]
43+
]
44+
45+
#table(
46+
columns: (14mm, 1fr),
47+
[`x`, `X`], [output numeric value in hexadecimal notation],
48+
[`s`],
49+
[
50+
for pointer-to-many and C pointers of u8, print as a C-string using zero-termination\
51+
for slices of u8, print the entire slice as a string without zero-termination
52+
],
53+
54+
[`e`], [ output floating point value in scientific notation ],
55+
[`d`], [ output numeric value in decimal notation ],
56+
[`b`], [ output integer value in binary notation ],
57+
[`o`], [ output integer value in octal notation ],
58+
[`c`], [ output integer as an ASCII character. Integer type must have 8 bits at max. ],
59+
[`u`], [ output integer as an UTF-8 sequence. Integer type must have 21 bits at max. ],
60+
[`?`],
61+
[
62+
output optional value as either the unwrapped value, or null;\
63+
may be followed by a format specifier for the underlying value.
64+
],
65+
66+
[`!`],
67+
[
68+
output error union value as either the unwrapped value, or the formatted error value;\
69+
may be followed by a format specifier for the underlying value.
70+
],
71+
72+
[`*`], [ output the address of the value instead of the value itself. ],
73+
[`any`], [ output a value of any type using its default format. ],
74+
)
75+
],
76+
77+
78+
[`fill`], [
79+
is a single unicode codepoint which is used to pad the formatted text
80+
],
81+
82+
[`alignment`], [
83+
is one of the three bytes `<`, `^`, or `>` to make the text left-, center-, or right-aligned, respectively
84+
],
85+
86+
[`width`], [
87+
is the total width of the field in unicode codepoints
88+
],
89+
90+
[`precision`], [
91+
specifies how many decimals a formatted number should have
92+
],
93+
94+
table.cell(
95+
colspan: 2,
96+
stroke: (top: rgb("#333")),
97+
block(
98+
inset: (x: 0mm, y: 0mm, left: 6mm, top: 1mm),
99+
outset: 0mm,
100+
)[
101+
value with `struct`, `vector`, `unioni` or `enum` type can have a custom formatting function defined as:\
102+
103+
```zig
104+
pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void
105+
```
106+
107+
with ? being the type formatted, this function will be called instead of the default implementation.
108+
],
109+
)
110+
)
111+
}

main.typ

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
],
2222
)
2323

24+
#show heading.where(level: 1): it => { }
25+
2426
#set text(font: "Arial", size: 11pt)
2527
#let fill-color = "F6F6F6"
26-
#let stroke-color = "21222C"
2728
#set table(
2829
align: left + horizon,
2930
columns: (2fr, 3fr),
30-
inset: (x: 1.5mm, y: 1mm),
31+
inset: (x: 1.5mm, y: 1.3mm),
3132
fill: table_fill(rgb(fill-color)),
32-
stroke: table_stroke(rgb(stroke-color)),
33+
stroke: table_stroke(none),
3334
)
3435
#set table.header(repeat: false)
35-
#show table.cell.where(y: 0): set text(weight: "bold", size: 1.2em)
3636

3737
#show raw.where(block: false): it => {
3838
box(fill: rgb("#a9fca530"), inset: 0.5mm, radius: 0.2mm, outset: (y: 0mm), it)
@@ -237,9 +237,13 @@ const Empty = struct {
237237
[*packed struct*], [TBD],
238238
)
239239

240+
#pagebreak()
241+
= std.fmt
242+
#include "fmt.typ";
240243

241244

242245
#pagebreak()
246+
= std.builtin.Type
243247
#include "type.typ";
244248

245249

0 commit comments

Comments
 (0)