1
0
Fork 0

work on adding configuration.

This commit is contained in:
ChaotiCryptidz 2021-12-22 15:05:17 +00:00
parent 719ea5667d
commit 5f2d473ef8
4 changed files with 78 additions and 25 deletions
src
main.zig
widgets

View file

@ -46,6 +46,8 @@ pub fn log(
nosuspend stderr.print(prefix ++ format, args) catch return;
}
const default_bar_config = "cpu|mem|weather(London)";
pub fn main() !void {
std.log.info("Starting Bar.", .{});
var allocator: *std.mem.Allocator = undefined;
@ -66,15 +68,64 @@ pub fn main() !void {
var bar = barImpl.initBar(allocator);
var br = Bar.init(&bar);
const widgets = [_]*Widget{
//&Widget.init(&networkWidget.New(allocator, &br)), // 24.01KiB
&Widget.init(&cpuWidget.New(&br)), // 4.08KiB
&Widget.init(&memoryWidget.New(&br)), // 4.08KiB
&Widget.init(&weatherWidget.New(allocator, &br, @import("build_options").weather_location)), // 16.16KiB
&Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB
&Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
};
bar.widgets = widgets[0..];
var argIter = std.process.args();
allocator.free(try argIter.next(allocator).?);
var config: []const u8 = undefined;
if (argIter.next(allocator)) |conf| {
config = try conf;
} else {
config = try allocator.dupe(u8, default_bar_config);
}
defer allocator.free(config);
std.debug.print("Config: {s}\n", .{config});
var widgets = std.ArrayList(*Widget).init(allocator);
defer widgets.deinit();
var configIter = std.mem.split(config, "|");
while (configIter.next()) |barItemInfo| {
var barItemIter = std.mem.tokenize(barItemInfo, "()");
var widgetName = barItemIter.next().?;
var widgetConfig: ?[]const u8 = null;
if (barItemIter.next()) |wC| {
// i just assume that this will last the lifespan of everything
// TODO: check for leaks and stuff
widgetConfig = wC;
}
var widget: ?*Widget = null;
if (std.mem.eql(u8, widgetName, "cpu")) {
widget = &Widget.init(&cpuWidget.New(&br));
} else if (std.mem.eql(u8, widgetName, "mem")) {
widget = &Widget.init(&memoryWidget.New(&br));
} else if (std.mem.eql(u8, widgetName, "net")) {
widget = &Widget.init(&networkWidget.New(allocator, &br));
} else if (std.mem.eql(u8, widgetName, "bat")) {
widget = &Widget.init(&batteryWidget.New(allocator, &br));
} else if (std.mem.eql(u8, widgetName, "weather")) {
widget = &Widget.init(&weatherWidget.New(allocator, &br, widgetConfig.?));
} else if (std.mem.eql(u8, widgetName, "time")) {
widget = &Widget.init(&timeWidget.New(allocator, &br));
}
if (widget) |w| {
try widgets.append(w);
} else {
std.debug.print("Widget does not exist: {s}\n", .{widgetName});
@panic("Widget does not exist.");
}
}
//const widgets = [_]*Widget{
// &Widget.init(&networkWidget.New(allocator, &br)), // 24.01KiB
// &Widget.init(&cpuWidget.New(&br)), // 4.08KiB
// &Widget.init(&memoryWidget.New(&br)), // 4.08KiB
// &Widget.init(&weatherWidget.New(allocator, &br, @import("build_options").weather_location)), // 16.16KiB
// &Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB
// &Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
//};
bar.widgets = widgets.items;
try br.start();
if (debug_allocator) {
std.debug.print("Finished cleanup, last allocation info.\n", .{});

View file

@ -120,13 +120,13 @@ pub const BatteryWidget = struct {
}
if (std.mem.eql(u8, status, "Charging")) {
descriptor = comptimeColour("green", "(C)");
descriptor = comptimeColour("green", " (C)");
sign = "+";
} else if (std.mem.eql(u8, status, "Discharging")) {
descriptor = comptimeColour("red", "(D)");
descriptor = comptimeColour("red", " (D)");
sign = "-";
} else if (std.mem.eql(u8, status, "Unknown")) {
descriptor = comptimeColour("yellow", "(U)");
descriptor = comptimeColour("yellow", " (U)");
sign = "?";
}
@ -162,7 +162,7 @@ pub const BatteryWidget = struct {
self.allocator.free(capInfo);
defer self.allocator.free(colourCapInfo);
var bat_info = try std.fmt.allocPrint(self.allocator, "{s} {s} {s}{s}{s}", .{
var bat_info = try std.fmt.allocPrint(self.allocator, "{s}{s} {s}{s}{s}", .{
comptimeColour("accentlight", "bat"),
descriptor,
colourCapInfo,

View file

@ -49,6 +49,7 @@ pub const NetworkInfo = struct {
network_status: NetworkStatus = .Connected,
network_info: []const u8,
};
fn freeString(allocator: *std.mem.Allocator, string: []const u8) callconv(.Inline) void {
allocator.free(string);
}
@ -62,7 +63,7 @@ pub const NetworkWidget = struct {
network_infos: std.ArrayList(NetworkInfo),
num_interfaces: u8 = 0,
current_interface: u8 = 0,
update_mutex: std.Thread.Mutex = std.Mutex{},
update_mutex: std.Thread.Mutex = std.Thread.Mutex{},
pub fn name(self: *NetworkWidget) []const u8 {
return "network";
@ -85,14 +86,14 @@ pub const NetworkWidget = struct {
}
}
self.update_bar() catch |err| {
std.log.err(.network, "Error! {}\n", .{err});
std.log.err("Error! {any}\n", .{err});
};
}
pub fn update_network_infos(self: *NetworkWidget) anyerror!void {
const lock = self.update_mutex.acquire();
defer lock.release();
std.log.debug(.network, "Updating network info.\n", .{});
std.log.debug("Updating network info.\n", .{});
for (self.network_infos.items) |info| {
freeString(self.allocator, info.network_info);
}
@ -137,7 +138,7 @@ pub const NetworkWidget = struct {
for (self.network_infos.items) |info, i| {
if (i != self.current_interface) continue;
//std.log.debug(.network, "item! {} {}\n", .{ info, i });
const inner_text = try std.fmt.allocPrint(allocator, "{} {}", .{ @tagName(info.network_type), info.network_info });
const inner_text = try std.fmt.allocPrint(allocator, "{s} {s}", .{ @tagName(info.network_type), info.network_info });
const full_text = try colour(allocator, networkStatusToColour(info.network_status), inner_text);
defer allocator.free(full_text);
allocator.free(inner_text);

View file

@ -21,7 +21,7 @@ pub const WeatherWidget = struct {
allocator: *std.mem.Allocator,
bar: *Bar,
name: []const u8,
weather_api_url: []const u8,
location: []const u8,
pub fn name(self: *WeatherWidget) []const u8 {
return self.name;
@ -43,7 +43,11 @@ pub const WeatherWidget = struct {
var read_buffer: [512 * 512]u8 = undefined;
var client = hzzp.BaseClient.create(&read_buffer, &file.reader(), &file.writer());
try client.writeHead("GET", self.weather_api_url);
var weather_api_path = try std.fmt.allocPrint(allocator, "/data/2.5/weather?q={s}&appid=dcea3595afe693d1c17846141f58ea10&units=metric", .{
self.location
});
defer allocator.free(weather_api_path);
try client.writeHead("GET", weather_api_path);
try client.writeHeader("Host", "api.openweathermap.org");
try client.writeHeader("User-Agent", "uwu/1.2");
try client.writeHeader("Connection", "close");
@ -208,14 +212,11 @@ pub const WeatherWidget = struct {
}
}
};
pub fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: []const u8) callconv(.Inline) WeatherWidget {
pub fn New(allocator: *std.mem.Allocator, bar: *Bar, location: []const u8) callconv(.Inline) WeatherWidget {
return WeatherWidget{
.allocator = allocator,
.bar = bar,
.name = "weather " ++ location,
// Yeah I know I'm leaking a token here.
// So what? It ain't my token.
// It was the first result on github code search for "openweathermap appid"
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
.name = "weather",
.location = location,
};
}