1
0
Fork 0

Maybe fix some battery stuff.

This commit is contained in:
namedkitten 2020-07-27 13:41:28 +01:00
parent ed1df85bee
commit c22dd9f495
3 changed files with 46 additions and 9 deletions

View file

@ -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);
}

View file

@ -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",

View file

@ -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);
}
}
};