Skip to content

feat: output overhaul stdlib primitives (chrono format_duration + format left-align)#328

Merged
octalide merged 2 commits into
devfrom
fix/output-primitives
Jun 30, 2026
Merged

feat: output overhaul stdlib primitives (chrono format_duration + format left-align)#328
octalide merged 2 commits into
devfrom
fix/output-primitives

Conversation

@octalide

@octalide octalide commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Part of the output-overhaul epic (briar-systems/mach#1773), implementing briar-systems/mach#1774.

Two shared multiplatform stdlib primitives the terminal-output overhaul needs. ASCII-only, no platform branches, no allocation.

1. chrono format_duration

chrono.Duration had component accessors but no human string. Adds format_duration(d: Duration, buf: *u8, cap: usize) str — renders a Duration into a caller-provided buffer, returning the null-terminated string (aliasing buf), or nil if cap is insufficient. A 24-byte buffer holds every i64 duration. Unit chosen by magnitude:

range output
zero 0ms
sub-microsecond <1ms
microseconds Nus
milliseconds Nms
seconds N.Ns (tenths truncate)
minutes and up NmNs

Negative durations carry a leading -. Minutes is the top band (no hours).

2. format {:<N} left-align

Extends the hole-spec grammar from '{' [':' ['0'] [width] [x|X|c] '}' to '{' [':' ['<'] ['0'] [width] [x|X|c] '}'. A < immediately after : (before the optional 0/width) sets a new Spec.left flag; emit_field/fmt_str_field then write the value first and space-pad on the right. The zero flag is ignored for left-align (trailing zeros would corrupt the value). Right-align specs ({:N}, {:0N}, {:08x}) are byte-identical.

Tests

mach-std suite green (567 passed, 0 failed). New unit tests:

  • format_duration across ns/us/ms/s/m ranges + sign, band boundaries (1s, 1m, >1h), and insufficient-capacity cases.
  • {:<N} left-align for str + int (and hex, negative, zero-flag-ignored, value-over-width), asserting exact padded bytes, with {:N}/{:0N} right-align re-asserted unchanged.

Note: the consuming mach repo's mach.lock bump is coordinated centrally by the epic lead, not in this PR.

🤖 Generated with Claude Code

octalide added 2 commits June 29, 2026 23:47
renders a Duration to a compact ASCII string into a caller buffer with a
unit chosen by magnitude (<1ms / Nus / Nms / N.Ns / NmNs). no allocation,
no platform branches. covers ns/us/ms/s/m ranges, unit-tested.
extends the hole grammar to '{' [':' ['<'] ['0'] [width] [x|X|c] '}'. a '<'
after ':' (before the optional 0/width) left-aligns: the value is written
first and space-padded on the right. the zero flag is ignored for left-align.
right-align specs ({:N}/{:0N}/{:08x}) stay byte-identical. unit-tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant