work on adding configuration.
This commit is contained in:
parent
719ea5667d
commit
5f2d473ef8
src
69
src/main.zig
69
src/main.zig
|
@ -46,6 +46,8 @@ pub fn log(
|
||||||
nosuspend stderr.print(prefix ++ format, args) catch return;
|
nosuspend stderr.print(prefix ++ format, args) catch return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const default_bar_config = "cpu|mem|weather(London)";
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
std.log.info("Starting Bar.", .{});
|
std.log.info("Starting Bar.", .{});
|
||||||
var allocator: *std.mem.Allocator = undefined;
|
var allocator: *std.mem.Allocator = undefined;
|
||||||
|
@ -66,15 +68,64 @@ pub fn main() !void {
|
||||||
var bar = barImpl.initBar(allocator);
|
var bar = barImpl.initBar(allocator);
|
||||||
var br = Bar.init(&bar);
|
var br = Bar.init(&bar);
|
||||||
|
|
||||||
const widgets = [_]*Widget{
|
var argIter = std.process.args();
|
||||||
//&Widget.init(&networkWidget.New(allocator, &br)), // 24.01KiB
|
allocator.free(try argIter.next(allocator).?);
|
||||||
&Widget.init(&cpuWidget.New(&br)), // 4.08KiB
|
|
||||||
&Widget.init(&memoryWidget.New(&br)), // 4.08KiB
|
var config: []const u8 = undefined;
|
||||||
&Widget.init(&weatherWidget.New(allocator, &br, @import("build_options").weather_location)), // 16.16KiB
|
if (argIter.next(allocator)) |conf| {
|
||||||
&Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB
|
config = try conf;
|
||||||
&Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
|
} else {
|
||||||
};
|
config = try allocator.dupe(u8, default_bar_config);
|
||||||
bar.widgets = widgets[0..];
|
}
|
||||||
|
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();
|
try br.start();
|
||||||
if (debug_allocator) {
|
if (debug_allocator) {
|
||||||
std.debug.print("Finished cleanup, last allocation info.\n", .{});
|
std.debug.print("Finished cleanup, last allocation info.\n", .{});
|
||||||
|
|
|
@ -120,13 +120,13 @@ pub const BatteryWidget = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.eql(u8, status, "Charging")) {
|
if (std.mem.eql(u8, status, "Charging")) {
|
||||||
descriptor = comptimeColour("green", "(C)");
|
descriptor = comptimeColour("green", " (C)");
|
||||||
sign = "+";
|
sign = "+";
|
||||||
} else if (std.mem.eql(u8, status, "Discharging")) {
|
} else if (std.mem.eql(u8, status, "Discharging")) {
|
||||||
descriptor = comptimeColour("red", "(D)");
|
descriptor = comptimeColour("red", " (D)");
|
||||||
sign = "-";
|
sign = "-";
|
||||||
} else if (std.mem.eql(u8, status, "Unknown")) {
|
} else if (std.mem.eql(u8, status, "Unknown")) {
|
||||||
descriptor = comptimeColour("yellow", "(U)");
|
descriptor = comptimeColour("yellow", " (U)");
|
||||||
sign = "?";
|
sign = "?";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ pub const BatteryWidget = struct {
|
||||||
self.allocator.free(capInfo);
|
self.allocator.free(capInfo);
|
||||||
defer self.allocator.free(colourCapInfo);
|
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"),
|
comptimeColour("accentlight", "bat"),
|
||||||
descriptor,
|
descriptor,
|
||||||
colourCapInfo,
|
colourCapInfo,
|
||||||
|
|
|
@ -49,6 +49,7 @@ pub const NetworkInfo = struct {
|
||||||
network_status: NetworkStatus = .Connected,
|
network_status: NetworkStatus = .Connected,
|
||||||
network_info: []const u8,
|
network_info: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn freeString(allocator: *std.mem.Allocator, string: []const u8) callconv(.Inline) void {
|
fn freeString(allocator: *std.mem.Allocator, string: []const u8) callconv(.Inline) void {
|
||||||
allocator.free(string);
|
allocator.free(string);
|
||||||
}
|
}
|
||||||
|
@ -62,7 +63,7 @@ pub const NetworkWidget = struct {
|
||||||
network_infos: std.ArrayList(NetworkInfo),
|
network_infos: std.ArrayList(NetworkInfo),
|
||||||
num_interfaces: u8 = 0,
|
num_interfaces: u8 = 0,
|
||||||
current_interface: 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 {
|
pub fn name(self: *NetworkWidget) []const u8 {
|
||||||
return "network";
|
return "network";
|
||||||
|
@ -85,14 +86,14 @@ pub const NetworkWidget = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.update_bar() catch |err| {
|
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 {
|
pub fn update_network_infos(self: *NetworkWidget) anyerror!void {
|
||||||
const lock = self.update_mutex.acquire();
|
const lock = self.update_mutex.acquire();
|
||||||
defer lock.release();
|
defer lock.release();
|
||||||
std.log.debug(.network, "Updating network info.\n", .{});
|
std.log.debug("Updating network info.\n", .{});
|
||||||
for (self.network_infos.items) |info| {
|
for (self.network_infos.items) |info| {
|
||||||
freeString(self.allocator, info.network_info);
|
freeString(self.allocator, info.network_info);
|
||||||
}
|
}
|
||||||
|
@ -137,7 +138,7 @@ pub const NetworkWidget = struct {
|
||||||
for (self.network_infos.items) |info, i| {
|
for (self.network_infos.items) |info, i| {
|
||||||
if (i != self.current_interface) continue;
|
if (i != self.current_interface) continue;
|
||||||
//std.log.debug(.network, "item! {} {}\n", .{ info, i });
|
//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);
|
const full_text = try colour(allocator, networkStatusToColour(info.network_status), inner_text);
|
||||||
defer allocator.free(full_text);
|
defer allocator.free(full_text);
|
||||||
allocator.free(inner_text);
|
allocator.free(inner_text);
|
||||||
|
|
|
@ -21,7 +21,7 @@ pub const WeatherWidget = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
bar: *Bar,
|
bar: *Bar,
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
weather_api_url: []const u8,
|
location: []const u8,
|
||||||
|
|
||||||
pub fn name(self: *WeatherWidget) []const u8 {
|
pub fn name(self: *WeatherWidget) []const u8 {
|
||||||
return self.name;
|
return self.name;
|
||||||
|
@ -43,7 +43,11 @@ pub const WeatherWidget = struct {
|
||||||
var read_buffer: [512 * 512]u8 = undefined;
|
var read_buffer: [512 * 512]u8 = undefined;
|
||||||
var client = hzzp.BaseClient.create(&read_buffer, &file.reader(), &file.writer());
|
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("Host", "api.openweathermap.org");
|
||||||
try client.writeHeader("User-Agent", "uwu/1.2");
|
try client.writeHeader("User-Agent", "uwu/1.2");
|
||||||
try client.writeHeader("Connection", "close");
|
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{
|
return WeatherWidget{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.bar = bar,
|
.bar = bar,
|
||||||
.name = "weather " ++ location,
|
.name = "weather",
|
||||||
// Yeah I know I'm leaking a token here.
|
.location = location,
|
||||||
// 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",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue