1
0
Fork 0

b a t t e r y

This commit is contained in:
namedkitten 2020-07-10 02:53:15 +01:00
parent 43980feb61
commit 8d6a7df569
4 changed files with 95 additions and 15 deletions

View file

@ -49,7 +49,7 @@ pub const Bar = struct {
fn process(self: *Bar) !void { fn process(self: *Bar) !void {
while (self.running) { while (self.running) {
std.time.sleep(5000 * std.time.ns_per_ms); std.time.sleep(5000 * std.time.ns_per_ms);
return; //return;
} }
} }
pub fn keep_running(self: *Bar) bool { pub fn keep_running(self: *Bar) bool {

View file

@ -11,7 +11,7 @@ const DebugAllocator = @import("debug_allocator.zig");
const Info = @import("types/info.zig").Info; const Info = @import("types/info.zig").Info;
pub fn main() !void { pub fn main() !void {
const debug: bool = true; const debug: bool = false;
var allocator: *std.mem.Allocator = undefined; var allocator: *std.mem.Allocator = undefined;
var dbgAlloc: *DebugAllocator = undefined; var dbgAlloc: *DebugAllocator = undefined;
if (debug) { if (debug) {
@ -28,8 +28,8 @@ pub fn main() !void {
&Widget.init(&textWidget.New("owo", "potato")), &Widget.init(&textWidget.New("owo", "potato")),
&Widget.init(&textWidget.New("uwu", "tomato")), &Widget.init(&textWidget.New("uwu", "tomato")),
&Widget.init(&weatherWidget.New(allocator, &br, "London")), &Widget.init(&weatherWidget.New(allocator, &br, "London")),
&Widget.init(&timeWidget.New(allocator, &br)),
&Widget.init(&batteryWidget.New(allocator, &br)), &Widget.init(&batteryWidget.New(allocator, &br)),
&Widget.init(&timeWidget.New(allocator, &br)),
//&Widget.init(&weatherWidget.New(allocator, &br, "Oxford")), //&Widget.init(&weatherWidget.New(allocator, &br, "Oxford")),
//&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")), //&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")),

View file

@ -3,6 +3,7 @@ const Info = @import("../../types/info.zig").Info;
const Bar = @import("../../types/bar.zig").Bar; const Bar = @import("../../types/bar.zig").Bar;
const fs = std.fs; const fs = std.fs;
const cwd = fs.cwd; const cwd = fs.cwd;
const colour = @import("../../formatting/colour.zig").colour;
pub fn compare_from_walker(allocator: *std.mem.Allocator, path: []const u8, start_path: []const u8, required_filename: []const u8) !bool { pub fn compare_from_walker(allocator: *std.mem.Allocator, path: []const u8, start_path: []const u8, required_filename: []const u8) !bool {
var full_path = try std.fmt.allocPrint(allocator, "{}/{}", .{ start_path, required_filename }); var full_path = try std.fmt.allocPrint(allocator, "{}/{}", .{ start_path, required_filename });
@ -18,6 +19,22 @@ pub const PowerPaths = struct {
voltage_now_path: []const u8 = "", voltage_now_path: []const u8 = "",
}; };
pub fn read_file_to_unsigned_int64(path: []const u8) u64 {
var buffer: [std.math.log10(std.math.maxInt(u64)) + 2]u8 = undefined;
var file = fs.cwd().openFile(path, .{}) catch return 0;
defer file.close();
const siz = file.read(&buffer) catch return 0;
return std.fmt.parseInt(u64, buffer[0 .. siz - 1], 10) catch return 0;
}
pub fn read_file(path: []const u8) ![]const u8 {
var buffer: [128]u8 = undefined;
var file = try fs.cwd().openFile(path, .{});
defer file.close();
const siz = try file.readAll(&buffer);
return if (buffer[siz - 1] == '\n') buffer[0 .. siz - 1] else buffer[0..siz];
}
pub const BatteryWidget = struct { pub const BatteryWidget = struct {
bar: *Bar, bar: *Bar,
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
@ -106,18 +123,81 @@ pub const BatteryWidget = struct {
} }
pub fn start(self: *BatteryWidget) anyerror!void { pub fn start(self: *BatteryWidget) anyerror!void {
var arena = std.heap.ArenaAllocator.init(self.allocator); var pparena = std.heap.ArenaAllocator.init(self.allocator);
defer arena.deinit(); defer pparena.deinit();
var allocator = &arena.allocator; var ppallocator = &pparena.allocator;
const pp = try self.get_power_paths(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;
std.debug.print("{} {} {} {} {}\n", .{ var can_get_watts: bool = false;
pp.status_path,
pp.power_now_path, var watts: f64 = 0;
pp.capacity_path, var descriptor: []const u8 = "";
pp.current_now_path, var sign: []const u8 = "?";
pp.voltage_now_path, var power_colour: []const u8 = "#ffffff";
});
const capacity = @intToFloat(f64, read_file_to_unsigned_int64(pp.capacity_path));
const status = try read_file(pp.status_path);
if (capacity > 80) {
power_colour = "green";
} else if (capacity > 60) {
power_colour = "yellow";
} else if (capacity > 40) {
power_colour = "orange";
} else {
power_colour = "red";
}
if (std.mem.eql(u8, status, "Charging")) {
descriptor = try colour(allocator, "green", "(C)");
sign = "+";
} else if (std.mem.eql(u8, status, "Discharging")) {
descriptor = try colour(allocator, power_colour, "(D)");
sign = "-";
} else if (std.mem.eql(u8, status, "Unknown")) {
descriptor = try colour(allocator, "yellow", "(U)");
sign = "?";
}
if (pp.power_now_path.len != 0) {
watts = @intToFloat(f64, read_file_to_unsigned_int64(pp.power_now_path)) / 1000000;
can_get_watts = true;
} else if (pp.current_now_path.len != 0 and pp.voltage_now_path.len != 0) {
const current_now = @intToFloat(f64, read_file_to_unsigned_int64(pp.current_now_path)) / 1000000;
const voltage_now = @intToFloat(f64, read_file_to_unsigned_int64(pp.voltage_now_path)) / 1000000;
if (current_now == 0 or voltage_now == 0) {
can_get_watts = false;
} else {
watts = (current_now * voltage_now);
can_get_watts = true;
}
}
var watts_info: []const u8 = "";
if (can_get_watts) {
watts_info = try colour(allocator, "purple", try std.fmt.allocPrint(allocator, " {}{d:0<2}W", .{ sign, watts }));
}
var bat_info = try std.fmt.allocPrint(allocator, "{} {} {}{}{}", .{
colour(allocator, "accentlight", "bat"),
descriptor,
colour(allocator, power_colour, try std.fmt.allocPrint(allocator, "{d:0<2}", .{capacity})),
colour(allocator, "accentdark", "%"),
watts_info,
});
try self.bar.add(Info{
.name = "battery",
.full_text = bat_info,
.markup = "pango",
});
std.time.sleep(std.time.ns_per_s);
}
} }
}; };

View file

@ -191,7 +191,7 @@ pub const WeatherWidget = struct {
pub fn start(self: *WeatherWidget) anyerror!void { pub fn start(self: *WeatherWidget) anyerror!void {
while (self.bar.keep_running()) { while (self.bar.keep_running()) {
try self.update_info(); try self.update_info();
std.time.sleep(1000 * std.time.ns_per_ms); std.time.sleep(30 * std.time.ns_per_min);
} }
} }
}; };