handwritten code:
const REPORT =
\\User time : {} s, {} us
\\System time : {} s, {} us
\\Time : {} ms ({d:.3} ms/per)
\\Max RSS : {} kB
\\Page reclaims : {}
\\Page faults : {}
\\Block inputs : {}
\\Block outputs : {}
\\vol ctx switches : {}
\\invol ctx switches : {}
\\
;
fn report(count: i32) !void {
var usage: rusage.Usage = undefined;
try usage.Get(rusage.Who.CHILDREN);
const time = @divTrunc((usage.utime.usec + usage.stime.usec), 1000)
+ @divTrunc((usage.utime.sec + usage.stime.sec), 1000);
const ptime = (@intToFloat(f64, usage.utime.usec + usage.stime.usec)/1000.0
+ (@intToFloat(f64, usage.utime.sec + usage.stime.sec)*1000.0))/@intToFloat(f64, count);
warn(REPORT,
usage.utime.sec, usage.utime.usec,
usage.stime.sec, usage.stime.usec,
time, ptime,
usage.maxrss,
usage.minflt,
usage.majflt,
usage.inblock,
usage.oublock,
usage.nvcsw,
usage.nivcsw);
}
current zig fmt treatment:
fn report(count: i32) !void {
var usage: rusage.Usage = undefined;
try usage.Get(rusage.Who.CHILDREN);
const time = @divTrunc((usage.utime.usec + usage.stime.usec), 1000) + @divTrunc((usage.utime.sec + usage.stime.sec), 1000);
const ptime = (@intToFloat(f64, usage.utime.usec + usage.stime.usec) / 1000.0 + (@intToFloat(f64, usage.utime.sec + usage.stime.sec) * 1000.0)) / @intToFloat(f64, count);
warn(REPORT, usage.utime.sec, usage.utime.usec, usage.stime.sec, usage.stime.usec, time, ptime, usage.maxrss, usage.minflt, usage.majflt, usage.inblock, usage.oublock, usage.nvcsw, usage.nivcsw);
}
What I'd prefer is a "making this change will result in heuristically ugly code, so I'll defer to the provided formatting" behavior, but if zig fmt needs to be only formatter of code, maybe this would do:
fn report(count: i32) !void {
var usage: rusage.Usage = undefined;
try usage.Get(rusage.Who.CHILDREN);
const time = @divTrunc((usage.utime.usec + usage.stime.usec), 1000)
+ @divTrunc((usage.utime.sec + usage.stime.sec), 1000);
const ptime = (@intToFloat(f64, usage.utime.usec + usage.stime.usec)
/ 1000.0 + (@intToFloat(f64, usage.utime.sec
+ usage.stime.sec)
* 1000.0))
/ @intToFloat(f64, count);
warn(REPORT, usage.utime.sec, usage.utime.usec, usage.stime.sec,
usage.stime.usec, time, ptime, usage.maxrss, usage.minflt, usage.majflt,
usage.inblock, usage.oublock, usage.nvcsw, usage.nivcsw);
}
applying the rules:
I think the ptime output is still pretty ugly, but the coder could reasonably work around a result like that by adding variables:
const secs = @intToFloat(f64, usage.utime.sec + usage.stime.sec);
const usecs = @intToFloat(f64, usage.utime.usec + usage.stime.usec);
const ptime = (usecs / 1000.0 + secs * 1000.0) / @intToFloat(f64, count);
Formatting is quite complicated. Just take a look at all the options and styles provided by clang-format:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
Maybe you can describe your wish in terms of a .clang-format file?
A workaround for "manual" formatting: put a // at the end of a line, and the formatter will preserve the line break (see https://github.com/ziglang/zig/issues/2748 for example)
However, I think in most cases it's just better not write long statements. Defining a couple functions would make your code much more readable and the lines much smaller, to no longer require wrapping. In your code, specifically, I think a Time.sum(Time) Time and a timeToFloat(Time) f64 could help a lot. Your current long lines make a lot of details harder to notice -- why is time adds seconds to microseconds -- is this intentional or a bug? Why is ptime computing in milliseconds but dividing by a count? Do you think that everyone reading the code will get all that without skimming and incorrectly assuming they've understood is correctly the way that it's currently composed?
To that end, having no wrapping by default is useful since it discourages writing code which hasn't been appropriately broken up.
Having a predictable, less-configurable formatter is nice. Where would the right-margin be placed? Is that a hard-limit or a soft-limit? If it's a hard-limit, what does that mean for "necessarily" long lines which can't be reasonably fit inside? I think this also forces programmers to make unnecessary choices. Changing a variable name could now change how wrapping occurs, which means that aesthetic preferences could make someone avoid a particular programming structure, which seems wrong.
To that end, having no wrapping by default is useful since it discourages writing code which hasn't been appropriately broken up.
The goal of zig fmt is to discourage style debates.
zig fmt however produces objectively ugly code.
If the solution is "this is fine--desirable even!", then the style debates are just moved to debates on how to change code so that it will be automatically styled in a pleasing way, or whether to do that, and the goal is defeated.
Putting an extra comma at the end of the arguments in the function call gives you the desired formatting.
foo(bar, baz,);
zig fmt turns the above into:
foo(
bar,
baz,
);
There were 2 things to discover here, which were:
With those 2 things in mind, I think the result is reasonable:
fn report(count: i32) !void {
var usage: rusage.Usage = undefined;
try usage.Get(rusage.Who.CHILDREN);
const time = @divTrunc((usage.utime.usec + usage.stime.usec), 1000) +
@divTrunc((usage.utime.sec + usage.stime.sec), 1000);
const ptime = (@intToFloat(f64, usage.utime.usec + usage.stime.usec) / 1000.0 +
(@intToFloat(f64, usage.utime.sec + usage.stime.sec) * 1000.0)) / @intToFloat(f64, count);
warn(
REPORT,
usage.utime.sec,
usage.utime.usec,
usage.stime.sec,
usage.stime.usec,
time,
ptime,
usage.maxrss,
usage.minflt,
usage.majflt,
usage.inblock,
usage.oublock,
usage.nvcsw,
usage.nivcsw,
);
}
Given this, plus the fact that it is an intentional design decision that zig fmt has no understanding of column count / line length, I feel confident in leaving things as status quo.
I agree that it's annoying when you don't know the 2 things above, and zig fmt creates long lines.
Most helpful comment
Putting an extra comma at the end of the arguments in the function call gives you the desired formatting.
zig fmtturns the above into: