h
This commit is contained in:
parent
2ab5c8e019
commit
43980feb61
|
@ -49,7 +49,7 @@ pub const Bar = struct {
|
|||
fn process(self: *Bar) !void {
|
||||
while (self.running) {
|
||||
std.time.sleep(5000 * std.time.ns_per_ms);
|
||||
//return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
pub fn keep_running(self: *Bar) bool {
|
||||
|
|
|
@ -5,13 +5,12 @@ const barImpl = @import("bar/bar.zig");
|
|||
const textWidget = @import("widgets/text/text.zig");
|
||||
const weatherWidget = @import("widgets/weather/weather.zig");
|
||||
const timeWidget = @import("widgets/time/time.zig");
|
||||
const batteryWidget = @import("widgets/battery/battery.zig");
|
||||
|
||||
const DebugAllocator = @import("debug_allocator.zig");
|
||||
const Info = @import("types/info.zig").Info;
|
||||
|
||||
pub fn main() !void {
|
||||
std.debug.print("{}\n", .{@import("builtin").link_libc});
|
||||
|
||||
const debug: bool = true;
|
||||
var allocator: *std.mem.Allocator = undefined;
|
||||
var dbgAlloc: *DebugAllocator = undefined;
|
||||
|
@ -30,6 +29,7 @@ pub fn main() !void {
|
|||
&Widget.init(&textWidget.New("uwu", "tomato")),
|
||||
&Widget.init(&weatherWidget.New(allocator, &br, "London")),
|
||||
&Widget.init(&timeWidget.New(allocator, &br)),
|
||||
&Widget.init(&batteryWidget.New(allocator, &br)),
|
||||
|
||||
//&Widget.init(&weatherWidget.New(allocator, &br, "Oxford")),
|
||||
//&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")),
|
||||
|
|
129
src/widgets/battery/battery.zig
Normal file
129
src/widgets/battery/battery.zig
Normal file
|
@ -0,0 +1,129 @@
|
|||
const std = @import("std");
|
||||
const Info = @import("../../types/info.zig").Info;
|
||||
const Bar = @import("../../types/bar.zig").Bar;
|
||||
const fs = std.fs;
|
||||
const cwd = fs.cwd;
|
||||
|
||||
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 });
|
||||
defer allocator.free(full_path);
|
||||
return std.mem.eql(u8, path, full_path);
|
||||
}
|
||||
|
||||
pub const PowerPaths = struct {
|
||||
status_path: []const u8 = "",
|
||||
power_now_path: []const u8 = "",
|
||||
capacity_path: []const u8 = "",
|
||||
current_now_path: []const u8 = "",
|
||||
voltage_now_path: []const u8 = "",
|
||||
};
|
||||
|
||||
pub const BatteryWidget = struct {
|
||||
bar: *Bar,
|
||||
allocator: *std.mem.Allocator,
|
||||
|
||||
pub fn name(self: *BatteryWidget) []const u8 {
|
||||
return "battery";
|
||||
}
|
||||
pub fn initial_info(self: *BatteryWidget) Info {
|
||||
return Info{
|
||||
.name = "battery",
|
||||
.full_text = "bat",
|
||||
.markup = "pango",
|
||||
};
|
||||
}
|
||||
pub fn info(self: *BatteryWidget) Info {
|
||||
return self.initial_info();
|
||||
}
|
||||
pub fn get_power_paths(self: *BatteryWidget, provided_allocator: *std.mem.Allocator) anyerror!PowerPaths {
|
||||
// remember that PowerPaths fields are allocated with self.allocator and will need to be freed seporately
|
||||
var arena = std.heap.ArenaAllocator.init(provided_allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
var power_supply_walker = try fs.walkPath(allocator, "/sys/class/power_supply");
|
||||
defer power_supply_walker.deinit();
|
||||
var power_supply_dirs = std.ArrayList([]const u8).init(self.allocator);
|
||||
defer power_supply_dirs.deinit();
|
||||
|
||||
var pp = PowerPaths{};
|
||||
|
||||
while (try power_supply_walker.next()) |entry| {
|
||||
switch (entry.kind) {
|
||||
.SymLink => {
|
||||
try power_supply_dirs.append(entry.path);
|
||||
},
|
||||
.File,
|
||||
.BlockDevice,
|
||||
.CharacterDevice,
|
||||
.Directory,
|
||||
.NamedPipe,
|
||||
.UnixDomainSocket,
|
||||
.Whiteout,
|
||||
.Unknown,
|
||||
=> continue,
|
||||
}
|
||||
}
|
||||
for (power_supply_dirs.items) |filepath| {
|
||||
var power_supply_file_walker = try fs.walkPath(allocator, filepath);
|
||||
defer power_supply_file_walker.deinit();
|
||||
while (try power_supply_file_walker.next()) |entry| {
|
||||
switch (entry.kind) {
|
||||
.File => {
|
||||
if (try compare_from_walker(allocator, entry.path, filepath, "status")) {
|
||||
pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path});
|
||||
continue;
|
||||
}
|
||||
if (try compare_from_walker(allocator, entry.path, filepath, "power_now")) {
|
||||
pp.power_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path});
|
||||
continue;
|
||||
}
|
||||
if (try compare_from_walker(allocator, entry.path, filepath, "capacity")) {
|
||||
pp.capacity_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path});
|
||||
continue;
|
||||
}
|
||||
if (try compare_from_walker(allocator, entry.path, filepath, "current_now")) {
|
||||
pp.current_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path});
|
||||
continue;
|
||||
}
|
||||
if (try compare_from_walker(allocator, entry.path, filepath, "voltage_now")) {
|
||||
pp.voltage_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path});
|
||||
continue;
|
||||
}
|
||||
},
|
||||
.SymLink,
|
||||
.BlockDevice,
|
||||
.CharacterDevice,
|
||||
.Directory,
|
||||
.NamedPipe,
|
||||
.UnixDomainSocket,
|
||||
.Whiteout,
|
||||
.Unknown,
|
||||
=> continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
return pp;
|
||||
}
|
||||
|
||||
pub fn start(self: *BatteryWidget) anyerror!void {
|
||||
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||
defer arena.deinit();
|
||||
var allocator = &arena.allocator;
|
||||
const pp = try self.get_power_paths(allocator);
|
||||
|
||||
std.debug.print("{} {} {} {} {}\n", .{
|
||||
pp.status_path,
|
||||
pp.power_now_path,
|
||||
pp.capacity_path,
|
||||
pp.current_now_path,
|
||||
pp.voltage_now_path,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) BatteryWidget {
|
||||
return BatteryWidget{
|
||||
.allocator = allocator,
|
||||
.bar = bar,
|
||||
};
|
||||
}
|
|
@ -130,19 +130,27 @@ pub const WeatherWidget = struct {
|
|||
error.TemporaryNameServerFailure => {
|
||||
try self.bar.add(Info{
|
||||
.name = self.name,
|
||||
.full_text = "weather DNS Error with a chance of WiFi",
|
||||
.full_text = "DNS Error Fetching Weather",
|
||||
.markup = "pango",
|
||||
});
|
||||
},
|
||||
error.InvalidIPAddressFormat => {
|
||||
try self.bar.add(Info{
|
||||
.name = self.name,
|
||||
.full_text = "invalid IP",
|
||||
.full_text = "Invalid Weather IP",
|
||||
.markup = "pango",
|
||||
});
|
||||
},
|
||||
error.ConnectionResetByPeer => {
|
||||
try self.bar.add(Info{
|
||||
.name = self.name,
|
||||
.full_text = "Weather Reset",
|
||||
.markup = "pango",
|
||||
});
|
||||
},
|
||||
else => |e| {
|
||||
std.debug.print("\n\n\n\n\nError!: {}\n\n\n\n\n", .{@errorName(e)});
|
||||
return;
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue