1
0
Fork 0

Delete info();

This commit is contained in:
namedkitten 2020-07-12 21:16:40 +01:00
parent ae3afea63f
commit f4bade27c4
9 changed files with 19 additions and 26 deletions

View file

@ -1,6 +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").Info; const Info = @import("../types/info.zig").Info;
const MouseEvent = @import("../types/mouseevent.zig").MouseEvent;
const terminal_version = @import("build_options").terminal_version; const terminal_version = @import("build_options").terminal_version;
@ -9,7 +10,7 @@ pub const Bar = struct {
widgets: []const *Widget, widgets: []const *Widget,
running: bool, running: bool,
infos: std.ArrayList(Info), infos: std.ArrayList(Info),
mutex: std.Mutex, items_mutex: std.Mutex,
out_file: std.fs.File, out_file: std.fs.File,
pub fn start(self: *Bar) !void { pub fn start(self: *Bar) !void {
self.running = true; self.running = true;
@ -35,7 +36,7 @@ pub const Bar = struct {
fn print_infos(self: *Bar, should_lock: bool) !void { fn print_infos(self: *Bar, should_lock: bool) !void {
if (should_lock) { if (should_lock) {
const lock = self.mutex.acquire(); const lock = self.items_mutex.acquire();
defer lock.release(); defer lock.release();
} }
if (!terminal_version) try self.out_file.writer().writeAll("["); if (!terminal_version) try self.out_file.writer().writeAll("[");
@ -63,12 +64,20 @@ pub const Bar = struct {
fn process(self: *Bar) !void { fn process(self: *Bar) !void {
var line_buffer: [512]u8 = undefined; var line_buffer: [512]u8 = undefined;
while (self.running) { while (self.running) {
var arena = std.heap.ArenaAllocator.init(self.allocator);
defer arena.deinit();
var allocator = &arena.allocator;
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, '\n'); const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, '\n');
if (line_opt) |l| { if (line_opt) |l| {
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}); 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});
std.json.parseFree(MouseEvent, data, parseOptions);
} }
//std.time.sleep(5000 * std.time.ns_per_ms); //std.time.sleep(5000 * std.time.ns_per_ms);
@ -97,7 +106,7 @@ pub const Bar = struct {
} }
pub fn add(self: *Bar, info: Info) !void { pub fn add(self: *Bar, info: Info) !void {
const lock = self.mutex.acquire(); const lock = self.items_mutex.acquire();
defer lock.release(); defer lock.release();
//std.debug.warn("info: {}\n", .{info.name}); //std.debug.warn("info: {}\n", .{info.name});
for (self.infos.items) |infoItem, index| { for (self.infos.items) |infoItem, index| {
@ -121,7 +130,7 @@ pub fn InitBar(allocator: *std.mem.Allocator) Bar {
.widgets = undefined, .widgets = undefined,
.running = false, .running = false,
.infos = std.ArrayList(Info).init(allocator), .infos = std.ArrayList(Info).init(allocator),
.mutex = std.Mutex.init(), .items_mutex = std.Mutex.init(),
.out_file = std.io.getStdOut(), .out_file = std.io.getStdOut(),
}; };
} }

View file

@ -28,7 +28,7 @@ pub fn main() !void {
&Widget.init(&textWidget.New("owo", "potato")), // 4KiB &Widget.init(&textWidget.New("owo", "potato")), // 4KiB
&Widget.init(&textWidget.New("uwu", "tomato")), // 4KiB &Widget.init(&textWidget.New("uwu", "tomato")), // 4KiB
&Widget.init(&memoryWidget.New(allocator, &br)), // 8.08KiB &Widget.init(&memoryWidget.New(allocator, &br)), // 8.08KiB
&Widget.init(&weatherWidget.New(allocator, &br, "London")), // 16KiB &Widget.init(&weatherWidget.New(allocator, &br, "Stockholm")), // 16KiB
&Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB &Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB
&Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB &Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
}; };

View file

@ -1,11 +1,15 @@
pub const MouseEvent = struct { pub const MouseEvent = struct {
name: []const u8, name: []const u8,
button: enum(u3) { button: enum(u4) {
LeftClick = 1, LeftClick = 1,
MiddleClick = 2, MiddleClick = 2,
RightClick = 3, RightClick = 3,
ScrollUp = 4, ScrollUp = 4,
ScrollDown = 5, ScrollDown = 5,
WheelLeft = 6,
WheelRight = 7,
Backwards = 8,
Forwards = 9,
}, },
event: u16, event: u16,
x: u16, x: u16,

View file

@ -7,7 +7,6 @@ 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,
info: fn (*SelfType) Info,
start: fn (*SelfType) anyerror!void, start: fn (*SelfType) anyerror!void,
}, interface.Storage.NonOwning); }, interface.Storage.NonOwning);
iface: IFace, iface: IFace,
@ -17,9 +16,6 @@ pub const Widget = struct {
pub fn name(self: *Widget) []const u8 { pub fn name(self: *Widget) []const u8 {
return self.iface.call("name", .{}); return self.iface.call("name", .{});
} }
pub fn info(self: *Widget) Info {
return self.iface.call("info", .{});
}
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", .{});
} }

View file

@ -43,9 +43,6 @@ pub const BatteryWidget = struct {
.markup = "pango", .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 { 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

@ -90,9 +90,6 @@ pub const MemoryWidget = struct {
.markup = "pango", .markup = "pango",
}; };
} }
pub fn info(self: *MemoryWidget) Info {
return self.initial_info();
}
fn update_bar(self: *MemoryWidget) !void { fn update_bar(self: *MemoryWidget) !void {
var arena = std.heap.ArenaAllocator.init(self.allocator); var arena = std.heap.ArenaAllocator.init(self.allocator);

View file

@ -15,9 +15,6 @@ pub const TextWidget = struct {
.markup = "pango", .markup = "pango",
}; };
} }
pub fn info(self: *TextWidget) Info {
return self.initial_info();
}
pub fn start(self: *TextWidget) anyerror!void {} pub fn start(self: *TextWidget) anyerror!void {}
}; };

View file

@ -19,10 +19,6 @@ pub const TimeWidget = struct {
}; };
} }
pub fn info(self: *TimeWidget) Info {
return self.initial_info();
}
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

@ -30,9 +30,6 @@ pub const WeatherWidget = struct {
.markup = "pango", .markup = "pango",
}; };
} }
pub fn info(self: *WeatherWidget) Info {
return self.initial_info();
}
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.