diff --git a/src/bar/bar.zig b/src/bar/bar.zig index 8de5012..8c65dc1 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -1,7 +1,7 @@ const std = @import("std"); const Widget = @import("../types/widget.zig").Widget; 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; @@ -72,16 +72,15 @@ pub const Bar = struct { var line = l; if (std.mem.eql(u8, line, "[")) continue; if (line[0] == ',') line = line[1..line.len]; - std.debug.print("{}\n", .{line}); - const parseOptions = std.json.ParseOptions{ .allocator = allocator }; 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.time.sleep(5000 * std.time.ns_per_ms); - //return; } } pub fn keep_running(self: *Bar) bool { diff --git a/src/main.zig b/src/main.zig index 2db35e2..71a78a5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,7 +11,7 @@ const DebugAllocator = @import("debug_allocator.zig"); const Info = @import("types/info.zig"); pub fn main() !void { - const debug: bool = true; + const debug: bool = false; var allocator: *std.mem.Allocator = undefined; var dbgAlloc: *DebugAllocator = undefined; if (debug) { diff --git a/src/types/loopingcounter.zig b/src/types/loopingcounter.zig new file mode 100644 index 0000000..fcd8f36 --- /dev/null +++ b/src/types/loopingcounter.zig @@ -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); +} diff --git a/src/types/mouseevent.zig b/src/types/mouseevent.zig index 618ada3..7d79453 100644 --- a/src/types/mouseevent.zig +++ b/src/types/mouseevent.zig @@ -1,21 +1,19 @@ -pub const MouseEvent = struct { - name: []const u8, - button: enum(u4) { - LeftClick = 1, - MiddleClick = 2, - RightClick = 3, - ScrollUp = 4, - ScrollDown = 5, - WheelLeft = 6, - WheelRight = 7, - Backwards = 8, - Forwards = 9, - }, - event: u16, - x: u16, - y: u16, - relative_x: u16, - relative_y: u16, - height: u16, - width: u16, -}; +name: []const u8, +button: enum(u4) { + LeftClick = 1, + MiddleClick = 2, + RightClick = 3, + ScrollUp = 4, + ScrollDown = 5, + WheelLeft = 6, + WheelRight = 7, + Backwards = 8, + Forwards = 9, +}, +event: u16, +x: u16, +y: u16, +relative_x: u16, +relative_y: u16, +height: u16, +width: u16, diff --git a/src/types/widget.zig b/src/types/widget.zig index f5a44de..9e977e0 100644 --- a/src/types/widget.zig +++ b/src/types/widget.zig @@ -2,11 +2,14 @@ const interface = @import("interfaces"); const Interface = interface.Interface; const SelfType = interface.SelfType; const Info = @import("info.zig"); +const MouseEvent = @import("mouseevent.zig"); pub const Widget = struct { const IFace = Interface(struct { name: fn (*SelfType) []const u8, initial_info: fn (*SelfType) Info, + mouse_event: fn (*SelfType, MouseEvent) anyerror!void, + start: fn (*SelfType) anyerror!void, }, interface.Storage.NonOwning); iface: IFace, @@ -19,6 +22,9 @@ pub const Widget = struct { pub fn initial_info(self: *Widget) 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 { return self.iface.call("start", .{}); } diff --git a/src/widgets/battery/battery.zig b/src/widgets/battery/battery.zig index e7abc8d..e33b7a2 100644 --- a/src/widgets/battery/battery.zig +++ b/src/widgets/battery/battery.zig @@ -4,6 +4,7 @@ const Bar = @import("../../types/bar.zig").Bar; const fs = std.fs; const cwd = fs.cwd; const colour = @import("../../formatting/colour.zig").colour; +const MouseEvent = @import("../../types/mouseevent.zig"); pub const PowerPaths = struct { status_path: []const u8 = "", @@ -43,6 +44,9 @@ pub const BatteryWidget = struct { .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 { var arena = std.heap.ArenaAllocator.init(provided_allocator); defer arena.deinit(); diff --git a/src/widgets/memory/memory.zig b/src/widgets/memory/memory.zig index 2514e0e..fc482c1 100644 --- a/src/widgets/memory/memory.zig +++ b/src/widgets/memory/memory.zig @@ -3,21 +3,25 @@ const Info = @import("../../types/info.zig"); const Bar = @import("../../types/bar.zig").Bar; const colour = @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 { memTotal: u64, memFree: u64, buffers: u64, cached: u64, + swapTotal: u64, + swapFree: u64, + swapCached: u64, }; fn parseKibibytes(buf: []const u8) !u64 { return try std.fmt.parseInt(u64, buf, 10); } -fn parseKibibytesToMegabytes(buf: []const u8) !u64 { - const kilobytes = try std.fmt.parseInt(u64, buf, 10); - return (kilobytes * 1024) / 1000 / 1000; +fn kibibytesToMegabytes(i: u64) u64 { + return (i * 1024) / 1000 / 1000; } fn formatMemoryPercent(allocator: *std.mem.Allocator, percent: f64) ![]const u8 { @@ -45,6 +49,9 @@ fn fetchTotalMemory() !MemInfo { .memFree = 0, .buffers = 0, .cached = 0, + .swapTotal = 0, + .swapFree = 0, + .swapCached = 0, }; while (true) { @@ -69,6 +76,18 @@ fn fetchTotalMemory() !MemInfo { meminfo.cached = try parseKibibytes(it.next().?); 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 { // reached eof break; @@ -80,6 +99,7 @@ fn fetchTotalMemory() !MemInfo { pub const MemoryWidget = struct { bar: *Bar, + lc: *LoopingCounter(8), pub fn name(self: *MemoryWidget) []const u8 { 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 { var buffer: [512]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&buffer); var allocator = &fba.allocator; const memInfo = try fetchTotalMemory(); - try self.bar.add(Info{ - .name = "mem", - .full_text = try std.fmt.allocPrint(allocator, "{} {}", .{ + var text: []const u8 = " "; + if (self.lc.get() == 0) { + text = try std.fmt.allocPrint(allocator, "{} {}", .{ colour(allocator, "accentlight", "mem"), 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", }); } @@ -117,5 +187,6 @@ pub const MemoryWidget = struct { pub inline fn New(bar: *Bar) MemoryWidget { return MemoryWidget{ .bar = bar, + .lc = &LoopingCounter(8).init(), }; } diff --git a/src/widgets/text/text.zig b/src/widgets/text/text.zig index 0065fca..42f0067 100644 --- a/src/widgets/text/text.zig +++ b/src/widgets/text/text.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Info = @import("../../types/info.zig"); +const MouseEvent = @import("../../types/mouseevent.zig"); pub const TextWidget = struct { name: []const u8, @@ -15,6 +16,7 @@ pub const TextWidget = struct { .markup = "pango", }; } + pub fn mouse_event(self: *TextWidget, event: MouseEvent) void {} pub fn start(self: *TextWidget) anyerror!void {} }; diff --git a/src/widgets/time/time.zig b/src/widgets/time/time.zig index f6ac513..72b245b 100644 --- a/src/widgets/time/time.zig +++ b/src/widgets/time/time.zig @@ -3,6 +3,7 @@ const Info = @import("../../types/info.zig"); const Bar = @import("../../types/bar.zig").Bar; const time = @import("time"); const colour = @import("../../formatting/colour.zig").colour; +const MouseEvent = @import("../../types/mouseevent.zig"); pub const TimeWidget = struct { 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 { while (self.bar.keep_running()) { var arena = std.heap.ArenaAllocator.init(self.allocator); diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 5e69049..f7e4ea0 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -6,6 +6,7 @@ const Info = @import("../../types/info.zig"); const Bar = @import("../../types/bar.zig").Bar; const colour = @import("../../formatting/colour.zig").colour; const DebugAllocator = @import("../../debug_allocator.zig"); +const MouseEvent = @import("../../types/mouseevent.zig"); const WeatherData = struct { 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 { // 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); diff --git a/untitled.sublime-project b/untitled.sublime-project index 5885f7e..75e081f 100644 --- a/untitled.sublime-project +++ b/untitled.sublime-project @@ -8,7 +8,7 @@ "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":