1
0
Fork 0

Add looping counter and more memory stats.

This commit is contained in:
namedkitten 2020-07-15 14:37:00 +01:00
parent b0f1418887
commit 84f2b91d7f
11 changed files with 162 additions and 37 deletions

View file

@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const Widget = @import("../types/widget.zig").Widget; const Widget = @import("../types/widget.zig").Widget;
const Info = @import("../types/info.zig"); const Info = @import("../types/info.zig");
const MouseEvent = @import("../types/mouseevent.zig").MouseEvent; const MouseEvent = @import("../types/mouseevent.zig");
const terminal_version = @import("build_options").terminal_version; const terminal_version = @import("build_options").terminal_version;
@ -72,16 +72,15 @@ pub const Bar = struct {
var line = l; var line = l;
if (std.mem.eql(u8, line, "[")) continue; if (std.mem.eql(u8, line, "[")) continue;
if (line[0] == ',') line = line[1..line.len]; if (line[0] == ',') line = line[1..line.len];
std.debug.print("{}\n", .{line});
const parseOptions = std.json.ParseOptions{ .allocator = allocator }; const parseOptions = std.json.ParseOptions{ .allocator = allocator };
const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions); const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions);
std.debug.print("{}\n", .{data}); for (self.widgets) |w| {
if (std.mem.eql(u8, w.name(), data.name)) {
w.mouse_event(data) catch {};
}
}
std.json.parseFree(MouseEvent, data, parseOptions); std.json.parseFree(MouseEvent, data, parseOptions);
} }
//std.time.sleep(5000 * std.time.ns_per_ms);
//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"); const Info = @import("types/info.zig");
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) {

View file

@ -0,0 +1,39 @@
const std = @import("std");
const testing = std.testing;
pub fn LoopingCounter(
comptime max_number: comptime_int,
) type {
return struct {
const Self = @This();
i: std.math.IntFittingRange(0, max_number) = 0,
n: std.math.IntFittingRange(0, max_number) = max_number,
pub fn init() Self {
return .{};
}
pub fn get(s: *Self) std.math.IntFittingRange(0, max_number) {
return s.i;
}
pub fn next(s: *Self) void {
if (s.i == s.n) {
s.i = 0;
} else {
s.i = s.i + 1;
}
}
};
}
test "looping test" {
var lc = LoopingCounter(3).init();
testing.expect(lc.get() == 0);
lc.next();
testing.expect(lc.get() != 0);
testing.expect(lc.get() == 1);
lc.next();
testing.expect(lc.get() == 2);
lc.next();
testing.expect(lc.get() == 3);
lc.next();
testing.expect(lc.get() == 0);
}

View file

@ -1,6 +1,5 @@
pub const MouseEvent = struct { name: []const u8,
name: []const u8, button: enum(u4) {
button: enum(u4) {
LeftClick = 1, LeftClick = 1,
MiddleClick = 2, MiddleClick = 2,
RightClick = 3, RightClick = 3,
@ -10,12 +9,11 @@ pub const MouseEvent = struct {
WheelRight = 7, WheelRight = 7,
Backwards = 8, Backwards = 8,
Forwards = 9, Forwards = 9,
}, },
event: u16, event: u16,
x: u16, x: u16,
y: u16, y: u16,
relative_x: u16, relative_x: u16,
relative_y: u16, relative_y: u16,
height: u16, height: u16,
width: u16, width: u16,
};

View file

@ -2,11 +2,14 @@ const interface = @import("interfaces");
const Interface = interface.Interface; const Interface = interface.Interface;
const SelfType = interface.SelfType; const SelfType = interface.SelfType;
const Info = @import("info.zig"); const Info = @import("info.zig");
const MouseEvent = @import("mouseevent.zig");
pub const Widget = struct { pub const Widget = struct {
const IFace = Interface(struct { const IFace = Interface(struct {
name: fn (*SelfType) []const u8, name: fn (*SelfType) []const u8,
initial_info: fn (*SelfType) Info, initial_info: fn (*SelfType) Info,
mouse_event: fn (*SelfType, MouseEvent) anyerror!void,
start: fn (*SelfType) anyerror!void, start: fn (*SelfType) anyerror!void,
}, interface.Storage.NonOwning); }, interface.Storage.NonOwning);
iface: IFace, iface: IFace,
@ -19,6 +22,9 @@ pub const Widget = struct {
pub fn initial_info(self: *Widget) Info { pub fn initial_info(self: *Widget) Info {
return self.iface.call("initial_info", .{}); return self.iface.call("initial_info", .{});
} }
pub fn mouse_event(self: *Widget, event: MouseEvent) anyerror!void {
return self.iface.call("mouse_event", .{event});
}
pub fn start(self: *Widget) anyerror!void { pub fn start(self: *Widget) anyerror!void {
return self.iface.call("start", .{}); return self.iface.call("start", .{});
} }

View file

@ -4,6 +4,7 @@ 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; const colour = @import("../../formatting/colour.zig").colour;
const MouseEvent = @import("../../types/mouseevent.zig");
pub const PowerPaths = struct { pub const PowerPaths = struct {
status_path: []const u8 = "", status_path: []const u8 = "",
@ -43,6 +44,9 @@ pub const BatteryWidget = struct {
.markup = "pango", .markup = "pango",
}; };
} }
pub fn mouse_event(self: *BatteryWidget, event: MouseEvent) void {}
pub fn get_power_paths(self: *BatteryWidget, provided_allocator: *std.mem.Allocator) anyerror!PowerPaths { pub fn get_power_paths(self: *BatteryWidget, provided_allocator: *std.mem.Allocator) anyerror!PowerPaths {
var arena = std.heap.ArenaAllocator.init(provided_allocator); var arena = std.heap.ArenaAllocator.init(provided_allocator);
defer arena.deinit(); defer arena.deinit();

View file

@ -3,21 +3,25 @@ const Info = @import("../../types/info.zig");
const Bar = @import("../../types/bar.zig").Bar; const Bar = @import("../../types/bar.zig").Bar;
const colour = @import("../../formatting/colour.zig").colour; const colour = @import("../../formatting/colour.zig").colour;
const comptimeColour = @import("../../formatting/colour.zig").colour; const comptimeColour = @import("../../formatting/colour.zig").colour;
const MouseEvent = @import("../../types/mouseevent.zig");
const LoopingCounter = @import("../../types/loopingcounter.zig").LoopingCounter;
const MemInfo = struct { const MemInfo = struct {
memTotal: u64, memTotal: u64,
memFree: u64, memFree: u64,
buffers: u64, buffers: u64,
cached: u64, cached: u64,
swapTotal: u64,
swapFree: u64,
swapCached: u64,
}; };
fn parseKibibytes(buf: []const u8) !u64 { fn parseKibibytes(buf: []const u8) !u64 {
return try std.fmt.parseInt(u64, buf, 10); return try std.fmt.parseInt(u64, buf, 10);
} }
fn parseKibibytesToMegabytes(buf: []const u8) !u64 { fn kibibytesToMegabytes(i: u64) u64 {
const kilobytes = try std.fmt.parseInt(u64, buf, 10); return (i * 1024) / 1000 / 1000;
return (kilobytes * 1024) / 1000 / 1000;
} }
fn formatMemoryPercent(allocator: *std.mem.Allocator, percent: f64) ![]const u8 { fn formatMemoryPercent(allocator: *std.mem.Allocator, percent: f64) ![]const u8 {
@ -45,6 +49,9 @@ fn fetchTotalMemory() !MemInfo {
.memFree = 0, .memFree = 0,
.buffers = 0, .buffers = 0,
.cached = 0, .cached = 0,
.swapTotal = 0,
.swapFree = 0,
.swapCached = 0,
}; };
while (true) { while (true) {
@ -69,6 +76,18 @@ fn fetchTotalMemory() !MemInfo {
meminfo.cached = try parseKibibytes(it.next().?); meminfo.cached = try parseKibibytes(it.next().?);
continue; continue;
} }
if (std.mem.eql(u8, line_header, "SwapTotal:")) {
meminfo.swapTotal = try parseKibibytes(it.next().?);
continue;
}
if (std.mem.eql(u8, line_header, "SwapFree:")) {
meminfo.swapFree = try parseKibibytes(it.next().?);
continue;
}
if (std.mem.eql(u8, line_header, "SwapCached:")) {
meminfo.swapCached = try parseKibibytes(it.next().?);
continue;
}
} else { } else {
// reached eof // reached eof
break; break;
@ -80,6 +99,7 @@ fn fetchTotalMemory() !MemInfo {
pub const MemoryWidget = struct { pub const MemoryWidget = struct {
bar: *Bar, bar: *Bar,
lc: *LoopingCounter(8),
pub fn name(self: *MemoryWidget) []const u8 { pub fn name(self: *MemoryWidget) []const u8 {
return "mem"; return "mem";
} }
@ -91,17 +111,67 @@ pub const MemoryWidget = struct {
}; };
} }
pub fn mouse_event(self: *MemoryWidget, event: MouseEvent) void {
self.lc.next();
self.update_bar() catch {};
}
fn update_bar(self: *MemoryWidget) !void { fn update_bar(self: *MemoryWidget) !void {
var buffer: [512]u8 = undefined; var buffer: [512]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer); var fba = std.heap.FixedBufferAllocator.init(&buffer);
var allocator = &fba.allocator; var allocator = &fba.allocator;
const memInfo = try fetchTotalMemory(); const memInfo = try fetchTotalMemory();
try self.bar.add(Info{ var text: []const u8 = " ";
.name = "mem", if (self.lc.get() == 0) {
.full_text = try std.fmt.allocPrint(allocator, "{} {}", .{ text = try std.fmt.allocPrint(allocator, "{} {}", .{
colour(allocator, "accentlight", "mem"), colour(allocator, "accentlight", "mem"),
formatMemoryPercent(allocator, (@intToFloat(f64, memInfo.memTotal - memInfo.memFree - memInfo.buffers - memInfo.cached) / @intToFloat(f64, memInfo.memTotal)) * 100), formatMemoryPercent(allocator, (@intToFloat(f64, memInfo.memTotal - memInfo.memFree - memInfo.buffers - memInfo.cached) / @intToFloat(f64, memInfo.memTotal)) * 100),
}), });
} else if (self.lc.get() == 1) {
text = try std.fmt.allocPrint(allocator, "{} {}", .{
colour(allocator, "accentlight", "swap"),
formatMemoryPercent(allocator, (@intToFloat(f64, memInfo.swapTotal - memInfo.swapFree) / @intToFloat(f64, memInfo.swapTotal)) * 100),
});
} else if (self.lc.get() == 2) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "mem free"),
kibibytesToMegabytes(memInfo.memFree),
});
} else if (self.lc.get() == 3) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "swap free"),
kibibytesToMegabytes(memInfo.swapFree),
});
} else if (self.lc.get() == 4) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "mem used"),
kibibytesToMegabytes(memInfo.memTotal - memInfo.memFree - memInfo.buffers - memInfo.cached),
});
} else if (self.lc.get() == 5) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "swap used"),
kibibytesToMegabytes(memInfo.swapTotal - memInfo.swapFree),
});
} else if (self.lc.get() == 6) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "mem cache"),
kibibytesToMegabytes(memInfo.cached),
});
} else if (self.lc.get() == 7) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "swap cache"),
kibibytesToMegabytes(memInfo.swapCached),
});
} else if (self.lc.get() == 8) {
text = try std.fmt.allocPrint(allocator, "{} {d:0<2} MB", .{
colour(allocator, "accentlight", "mem buf"),
kibibytesToMegabytes(memInfo.buffers),
});
}
try self.bar.add(Info{
.name = "mem",
.full_text = text,
.markup = "pango", .markup = "pango",
}); });
} }
@ -117,5 +187,6 @@ pub const MemoryWidget = struct {
pub inline fn New(bar: *Bar) MemoryWidget { pub inline fn New(bar: *Bar) MemoryWidget {
return MemoryWidget{ return MemoryWidget{
.bar = bar, .bar = bar,
.lc = &LoopingCounter(8).init(),
}; };
} }

View file

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const Info = @import("../../types/info.zig"); const Info = @import("../../types/info.zig");
const MouseEvent = @import("../../types/mouseevent.zig");
pub const TextWidget = struct { pub const TextWidget = struct {
name: []const u8, name: []const u8,
@ -15,6 +16,7 @@ pub const TextWidget = struct {
.markup = "pango", .markup = "pango",
}; };
} }
pub fn mouse_event(self: *TextWidget, event: MouseEvent) void {}
pub fn start(self: *TextWidget) anyerror!void {} pub fn start(self: *TextWidget) anyerror!void {}
}; };

View file

@ -3,6 +3,7 @@ const Info = @import("../../types/info.zig");
const Bar = @import("../../types/bar.zig").Bar; const Bar = @import("../../types/bar.zig").Bar;
const time = @import("time"); const time = @import("time");
const colour = @import("../../formatting/colour.zig").colour; const colour = @import("../../formatting/colour.zig").colour;
const MouseEvent = @import("../../types/mouseevent.zig");
pub const TimeWidget = struct { pub const TimeWidget = struct {
bar: *Bar, bar: *Bar,
@ -19,6 +20,8 @@ pub const TimeWidget = struct {
}; };
} }
pub fn mouse_event(self: *TimeWidget, event: MouseEvent) void {}
pub fn start(self: *TimeWidget) anyerror!void { pub fn start(self: *TimeWidget) anyerror!void {
while (self.bar.keep_running()) { while (self.bar.keep_running()) {
var arena = std.heap.ArenaAllocator.init(self.allocator); var arena = std.heap.ArenaAllocator.init(self.allocator);

View file

@ -6,6 +6,7 @@ const Info = @import("../../types/info.zig");
const Bar = @import("../../types/bar.zig").Bar; const Bar = @import("../../types/bar.zig").Bar;
const colour = @import("../../formatting/colour.zig").colour; const colour = @import("../../formatting/colour.zig").colour;
const DebugAllocator = @import("../../debug_allocator.zig"); const DebugAllocator = @import("../../debug_allocator.zig");
const MouseEvent = @import("../../types/mouseevent.zig");
const WeatherData = struct { const WeatherData = struct {
temp: u16, temp: u16,
@ -31,6 +32,8 @@ pub const WeatherWidget = struct {
}; };
} }
pub fn mouse_event(self: *WeatherWidget, event: MouseEvent) void {}
fn get_weather_info(self: *WeatherWidget, allocator: *std.mem.Allocator) !WeatherData { fn get_weather_info(self: *WeatherWidget, allocator: *std.mem.Allocator) !WeatherData {
// this will allocate some memory but it will be freed by the time it is returned. // this will allocate some memory but it will be freed by the time it is returned.
var file = try net.tcpConnectToHost(allocator, "api.openweathermap.org", 80); var file = try net.tcpConnectToHost(allocator, "api.openweathermap.org", 80);

View file

@ -8,7 +8,7 @@
"path": "/home/kitteh/bar" "path": "/home/kitteh/bar"
}, },
{ {
"path": "/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std" "path": "/home/kitteh/zig/lib/std"
} }
], ],
"settings": "settings":