这是indexloc提供的服务,不要输入任何密码
Skip to content

Match center align to Rust's #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ You can use `{:spec}` to customize your output. See the Rust docs linked above f
- For strings, `?` (and thus `repr()`) has the effect of printing them with double quotes. For floats, this ensures a `.0` appears after it, even if it doesn't have decimal digits. For integers, this doesn't change anything. Finally, for labels, the `<label>` (with `?`) is printed as `<label>` instead of `label`.
- **TIP:** Prefer to always use `?` when you're inserting something that isn't a string, number or label, in order to ensure consistent results even if the library eventually changes the non-`?` representation.
- After the `:`, add e.g. `_<8` to align the string to the left, padding it with as many `_`s as necessary for it to be at least `8` characters long (for example). Replace `<` by `>` for right alignment, or `^` for center alignment. (If the `_` is omitted, it defaults to ' ' (aligns with spaces).)
- If you prefer to specify the minimum width (the `8` there) as a separate argument to `strfmt` instead, you can specify `argument$` in place of the width, which will extract it from the integer at `argument`. For example, `_^3$` will center align the output with `_`s, where the minimum width desired is specified by the fourth positional argument (index `3`), as an integer. This means that a call such as `strfmt("{:_^3$}", 1, 2, 3, 4)` would produce `"__1__"`, as `3$` would evaluate to `4` (the value at the fourth positional argument/index `3`). Similarly, `named$` would take the width from the argument with name `named`, if it is an integer (otherwise, error).
- If you prefer to specify the minimum width (the `8` there) as a separate argument to `strfmt` instead, you can specify `argument$` in place of the width, which will extract it from the integer at `argument`. For example, `_^3$` will center align the output with `_`s, where the minimum width desired is specified by the fourth positional argument (index `3`), as an integer. This means that a call such as `strfmt("{v:_^3$}", 1, 2, 3, 4, v: 88)` would produce `"_88_"`, as `3$` would evaluate to `4` (the value at the fourth positional argument/index `3`). Similarly, `named$` would take the width from the argument with name `named`, if it is an integer (otherwise, error).
- **For numbers:**
- Specify `+` after the `:` to ensure zero or positive numbers are prefixed with `+` before them (instead of having no sign). `-` is also accepted but ignored (negative numbers always specify their sign anyways).
- Use something like `:09` to add zeroes to the left of the number until it has at least 9 digits / characters.
Expand Down Expand Up @@ -147,7 +147,7 @@ Some examples:
#import "@preview/oxifmt:0.3.0": strfmt

#let s = strfmt("Left5 {:-<5}, Right6 {:=>6}, Center10 {centered: ^10?}, Left3 {tleft:_<3}", "xx", 539, tleft: "okay", centered: [a])
#assert.eq(s, "Left5 xx---, Right6 ===539, Center10 [a] , Left3 okay")
#assert.eq(s, "Left5 xx---, Right6 ===539, Center10 [a] , Left3 okay")
// note how 'okay' didn't suffer any padding at all (it already had at least the desired total width).
```

Expand Down
6 changes: 4 additions & 2 deletions oxifmt.typ
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,10 @@ parameter := argument '$'
} else if align == right {
replacement = (fill * width-diff) + replacement
} else if align == center {
let width-fill = fill * (calc.ceil(float(width-diff) / 2))
replacement = width-fill + replacement + width-fill
let half-width = calc.quo(width-diff, 2)
let left-fill = fill * half-width // floor div
let right-fill = fill * (half-width + calc.rem(width-diff, 2)) // ceil div (if adding odd fill, add 1 to the right)
replacement = left-fill + replacement + right-fill
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions tests/strfmt-tests.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// assert.eq(strfmt("a{b}}b}", ..("b}b": 5)), "a5")

// test 0 prefix with numbers, but also using 0 as a non-numeric affix
assert.eq(strfmt("{:08}|{0:0<8}|{0:0>8}|{0:0^8}", 120), "00000120|12000000|00000120|000120000")
assert.eq(strfmt("{:08}|{0:0<8}|{0:0>8}|{0:0^8}", 120), "00000120|12000000|00000120|00120000")

// test other kinds of affixes / fills and alignments
assert.eq(strfmt("{:a>8}, {:^20.10}, {:+05.4}, {:07}, {other:06?}", "b", 5.5, 11, -4, other: -30.0), "aaaaaaab, 5.5000000000 , +0011, -000004, -030.0")
Expand Down Expand Up @@ -182,15 +182,28 @@

// Issue #28: pad with {} inside :
assert.eq(strfmt("{:}>4}", "a"), "}}}a")
assert.eq(strfmt("{:}^4}", "a"), "}}a}}")
assert.eq(strfmt("{:}^4}", "a"), "}a}}")
assert.eq(strfmt("{:}<4}", "a"), "a}}}")
assert.eq(strfmt("{:{>4}", "a"), "{{{a")
assert.eq(strfmt("{:{^4}", "a"), "{{a{{")
assert.eq(strfmt("{:{^4}", "a"), "{a{{")
assert.eq(strfmt("{:{<4}", "a"), "a{{{")
assert.eq(strfmt("{:{^}", "a"), "a")
assert.eq(strfmt("{:}^}", "a"), "a")
assert.eq(strfmt("{:}}}", "a"), "a}")
}
// Issue #29: center alignment
#{
assert.eq(strfmt("{:}^1}", "a"), "a")
assert.eq(strfmt("{:}^2}", "a"), "a}")
assert.eq(strfmt("{:}^3}", "a"), "}a}")
assert.eq(strfmt("{:}^4}", "a"), "}a}}")
assert.eq(strfmt("{:}^5}", "a"), "}}a}}")

assert.eq(strfmt("{:}>4}", "a"), "}}}a")
assert.eq(strfmt("{:}<4}", "a"), "a}}}")
assert.eq(strfmt("{:}>5}", "a"), "}}}}a")
assert.eq(strfmt("{:}<5}", "a"), "a}}}}")
}
// DOC TESTS
#{
// --- Quick examples ---
Expand Down Expand Up @@ -262,7 +275,7 @@
}
{
let s = strfmt("Left5 {:_<5}, Right6 {:*>6}, Center10 {centered: ^10?}, Left3 {tleft:_<3}", "xx", 539, tleft: "okay", centered: [a])
assert.eq(s, "Left5 xx___, Right6 ***539, Center10 [a] , Left3 okay")
assert.eq(s, "Left5 xx___, Right6 ***539, Center10 [a] , Left3 okay")
}
{
let s = strfmt("Left-padded7 numbers: {:07} {:07} {:07} {3:07}", 123, -344, 44224059, 45.32)
Expand Down