From c22dd9f495e8ca5317dd00ef2fa4917bbc14d644 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Mon, 27 Jul 2020 13:41:28 +0100 Subject: [PATCH] Maybe fix some battery stuff. --- src/bar/bar.zig | 28 ++++++++++++++++++++++++++++ src/widgets/battery/battery.zig | 25 +++++++++++++++++-------- src/widgets/memory/memory.zig | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/bar/bar.zig b/src/bar/bar.zig index d16ac8b..851dbc4 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -14,6 +14,31 @@ fn readFromSignalFd(signal_fd: std.os.fd_t) !void { return error.Shutdown; } +pub fn utf8ValidateSlice(s: []const u8) bool { + var i: usize = 0; + while (i < s.len) { + if (std.unicode.utf8ByteSequenceLength(s[i])) |cp_len| { + if (i + cp_len > s.len) { + log.err(.uni, "oh nos: {} {} {}\n", .{i + cp_len, i, s.len}); + log.err(.uni, "oh no: {}\n", .{s[i .. i + cp_len]}); + return false; + } + + if (std.unicode.utf8Decode(s[i .. i + cp_len])) |_| {} else |_| { + log.err(.uni, "oh no: {}\n", .{s[i .. i + cp_len]}); + return false; + } + i += cp_len; + } else |err| { + log.err(.uni, "oh noz: {} {} {} {} \"{}\" \n", .{s[i], s.len, i, err, s[0..i+1]}); + + return false; + } + } + return true; +} + + fn sigemptyset(set: *std.os.sigset_t) void { for (set) |*val| { val.* = 0; @@ -291,6 +316,9 @@ pub const Bar = struct { } // If we reach here then it changed. try self.free_info(infoItem); +if (!utf8ValidateSlice(info.full_text)) { +std.log.err(.barerr, "Oh No: {}\n", .{info.full_text}); +} self.infos.items[index] = try self.dupe_info(info); try self.print_infos(false); } diff --git a/src/widgets/battery/battery.zig b/src/widgets/battery/battery.zig index d0c83b1..09b946a 100644 --- a/src/widgets/battery/battery.zig +++ b/src/widgets/battery/battery.zig @@ -99,10 +99,6 @@ pub const BatteryWidget = struct { var ppallocator = &fba.allocator; const pp = try self.get_power_paths(ppallocator); while (self.bar.keep_running()) { - var arena = std.heap.ArenaAllocator.init(self.allocator); - defer arena.deinit(); - var allocator = &arena.allocator; - var can_get_watts: bool = false; var watts: f64 = 0; @@ -127,7 +123,7 @@ pub const BatteryWidget = struct { descriptor = comptimeColour("green", "(C)"); sign = "+"; } else if (std.mem.eql(u8, status, "Discharging")) { - descriptor = try colour(allocator, power_colour, "(D)"); + descriptor = comptimeColour("red", "(D)"); sign = "-"; } else if (std.mem.eql(u8, status, "Unknown")) { descriptor = comptimeColour("yellow", "(U)"); @@ -151,16 +147,29 @@ pub const BatteryWidget = struct { var watts_info: []const u8 = ""; if (can_get_watts) { - watts_info = try colour(allocator, "purple", try std.fmt.allocPrint(allocator, " {}{d:.2}W", .{ sign, watts })); + const watts_str = try std.fmt.allocPrint(self.allocator, " {}{d:.2}W", .{ sign, watts }); + watts_info = try colour(self.allocator, "purple", watts_str); + self.allocator.free(watts_str); + } + defer { + if (can_get_watts) { + self.allocator.free(watts_info); + } } - var bat_info = try std.fmt.allocPrint(allocator, "{} {} {}{}{}", .{ + const capInfo = try std.fmt.allocPrint(self.allocator, "{d:.2}", .{capacity}); + const colourCapInfo = try colour(self.allocator, power_colour, capInfo); + self.allocator.free(capInfo); + defer self.allocator.free(colourCapInfo); + + var bat_info = try std.fmt.allocPrint(self.allocator, "{} {} {}{}{}", .{ comptimeColour("accentlight", "bat"), descriptor, - colour(allocator, power_colour, try std.fmt.allocPrint(allocator, "{d:.2}", .{capacity})), + colourCapInfo, comptimeColour("accentdark", "%"), watts_info, }); + defer self.allocator.free(bat_info); try self.bar.add(Info{ .name = "battery", diff --git a/src/widgets/memory/memory.zig b/src/widgets/memory/memory.zig index 19b4a2c..81f088b 100644 --- a/src/widgets/memory/memory.zig +++ b/src/widgets/memory/memory.zig @@ -205,7 +205,7 @@ pub const MemoryWidget = struct { while (self.bar.keep_running()) { self.update_bar() catch {}; - //std.time.sleep(500 * std.time.ns_per_ms); + std.time.sleep(500 * std.time.ns_per_ms); } } };