From c07a963cfcd351d15dccfe131c1df91a231f3321 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 19 Jan 2022 01:33:31 -0800 Subject: [PATCH] cmd/init- properly wrap license content at 80 width, fixes #29 --- src/cmd/init.zig | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 2443bbfb77dd4033e03cecd5aa002984d0a64787..aa8e6f04592bc94df5ce2c178a45b62abc1185bc 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -97,7 +97,40 @@ pub fn execute(args: [][]u8) !void { const file = try cwd.createFile("LICENSE", .{}); defer file.close(); const w = file.writer(); - try w.writeAll(realtext); + + // properly format license text to fit within 80 columns + var start: usize = 0; + var end: usize = 0; + var run: u32 = 0; + var i: usize = 0; + while (i < realtext.len) { + const c = realtext[i]; + end += 1; + i += 1; + + if (c == '\n') { + try w.writeAll(realtext[start..end]); + std.log.info("{s}", .{realtext[start .. end - 1]}); + start = end; + run = 0; + i = start; + continue; + } + run += 1; + + if (run >= 79) { + const s_ind = (std.mem.lastIndexOfScalar(u8, realtext[start..end], ' ') orelse end) + start; + const n_ind = (std.mem.lastIndexOfScalar(u8, realtext[start .. end - 1], '\n') orelse end) + start; + const ind = std.math.min(s_ind, n_ind); + try w.print("{s}\n", .{realtext[start..ind]}); + std.log.debug("{s}", .{realtext[start..ind]}); + end = ind + 1; + start = end; + run = 0; + i = start; + } + } + try w.writeAll(realtext[start..realtext.len]); } } } -- 2.54.0