spdlog-rs icon indicating copy to clipboard operation
spdlog-rs copied to clipboard

[Feature Request] Support formatting parameters in pattern placeholders

Open Lancern opened this issue 1 year ago • 0 comments

spdlog supports specifying text alignment in its pattern flags, see https://github.com/gabime/spdlog/wiki/3.-Custom-formatting#aligning:

  • %8l requires right alignment and will expand to info;
  • %-8l requires left alignment and will expand to info ;
  • %=8l requires center alignment and will expand to info .

Besides, an additional ! can be used to truncate the expanded result if it's too wide:

  • %3!l, %-3!l, and %=3!l will expand to inf.

This feature can be neat sometimes and maybe we could also support this. We may follow the convention of format! and propose the following new pattern placeholder syntax:

placeholder := '{' NAME [ ':' format_spec ] '}'
format_spec := [ [fill] align ] [ ['.'] width ]
fill := CHAR
align := INT
width := INT

Some examples:

  • {level:8} and {level:<8} will expand to info (left alignment, pad with spaces);
  • {level:-<8} will expand to info---- (left alignment, pad with -);
  • {level:>8} will expand to info (right alignment, pad with spaces);
  • {level:->8} will expand to ----info (right alignment, pad with -);
  • {level:^8} will expand to info (center alignment, pad with spaces);
  • {level:-^8} will expand to --info-- (center alignment, pad with -);
  • {level:.3} will expand to inf (left alignment, truncate field to 3 characters long).

Lancern avatar Dec 27 '23 10:12 Lancern