From 2aa90c04ffc17deb28601be69d61184102d8c62b Mon Sep 17 00:00:00 2001 From: utox39 Date: Fri, 1 Aug 2025 03:51:11 +0200 Subject: [PATCH] feat: add a utility function to get the length of the longest sys info string --- src/utils.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/utils.zig b/src/utils.zig index 0520d6c..a15816d 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -49,3 +49,29 @@ test "getTerminalSize" { try std.testing.expect((terminal_size.height > 0) and (terminal_size.width > 0)); } + +pub fn getLongestSysInfoStringLen(strings: []const []const u8) usize { + const ansi_reset = "\x1b[0m"; + var longest_len: usize = 0; + + // Ignore the username@host and the separator + for (strings[2..]) |s| { + const ansi_restet_index = std.mem.indexOf(u8, s, ansi_reset); + var start: usize = 0; + + if (ansi_restet_index != null) { + // `start` is the index of the last character of the ANSI reset escape sequence + 1 + start = ansi_restet_index.? + ansi_reset.len + 1; + } + + longest_len = @max(longest_len, s[start..].len); + } + + return longest_len; +} + +test "getLongestSysInfoStringLen" { + const strings = [_][]const u8{ "", "", "test", "test-test", "test1" }; + + try std.testing.expectEqual(strings[3].len, getLongestSysInfoStringLen(strings[0..])); +}