1
0
Fork 0

Updated to zig v0.8.0.

This commit is contained in:
purringChaos 2021-03-31 14:43:35 +01:00
parent 36b3429c2c
commit 8bb45d6553
14 changed files with 60 additions and 73 deletions

View file

@ -6,7 +6,7 @@ const assert = std.debug.assert;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
pub const SelfType = @Type(.Opaque);
pub const SelfType = opaque{};
fn makeSelfPtr(ptr: anytype) *SelfType {
if (comptime !trait.isSingleItemPtr(@TypeOf(ptr))) {

View file

@ -27,7 +27,7 @@ pub const Bar = struct {
widgets: []const *Widget,
running: bool,
infos: std.ArrayList(Info),
items_mutex: std.Mutex,
items_mutex: std.Thread.Mutex,
out_file: std.fs.File,
pub fn start(self: *Bar) !void {
self.running = true;
@ -48,9 +48,9 @@ pub const Bar = struct {
log.debug("Spawning threads.\n", .{});
for (self.widgets) |w| {
log.debug("Spawning thread=\"{}\"\n", .{w.name()});
var thread = try std.Thread.spawn(w, Widget.start);
var thread = try std.Thread.spawn(Widget.start, w);
}
_ = try std.Thread.spawn(self, Bar.process);
_ = try std.Thread.spawn(Bar.process, self);
log.info("Waiting for kill signal.\n", .{});
while (true) {
@ -75,8 +75,7 @@ pub const Bar = struct {
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015l");
}
}
inline fn print_i3bar_infos(self: *Bar) !void {
fn print_i3bar_infos(self: *Bar) callconv(.Inline) !void {
// Serialize all bar items and put on stdout.
try self.out_file.writer().writeAll("[");
for (self.infos.items) |info, i| {
@ -88,8 +87,7 @@ pub const Bar = struct {
}
try self.out_file.writer().writeAll("],\n");
}
inline fn print_terminal_infos(self: *Bar) !void {
fn print_terminal_infos(self: *Bar) callconv(.Inline) !void {
// For terminal we just need to directly print.
for (self.infos.items) |info, i| {
try self.out_file.writer().writeAll(info.full_text);
@ -121,8 +119,7 @@ pub const Bar = struct {
}
}
}
inline fn terminal_input_process(self: *Bar) !void {
fn terminal_input_process(self: *Bar) callconv(.Inline) !void {
// TODO: make work on other OSes other than xterm compatable terminals.
// Write to stdout that we want to recieve all terminal click events.
@ -145,7 +142,7 @@ pub const Bar = struct {
while (self.running) {
var line_buffer: [128]u8 = undefined;
// 0x1b is the ESC key which is used for sending and recieving events to xterm terminals.
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, 0x1b);
const line_opt = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&line_buffer, 0x1b);
if (line_opt) |l| {
// I honestly have no idea what this does but I assume that it checks
// that this is the right event?
@ -200,11 +197,10 @@ pub const Bar = struct {
}
}
}
inline fn i3bar_input_process(self: *Bar) !void {
fn i3bar_input_process(self: *Bar) callconv(.Inline) !void {
var line_buffer: [512]u8 = undefined;
while (self.running) {
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, '\n');
const line_opt = try std.io.getStdIn().reader().readUntilDelimiterOrEof(&line_buffer, '\n');
if (line_opt) |l| {
var line = l;
if (std.mem.eql(u8, line, "[")) continue;
@ -217,6 +213,7 @@ pub const Bar = struct {
// Anyway this just strips off the prefix of ',' so I can parse the json.
if (line[0] == ',') line = line[1..line.len];
const parseOptions = std.json.ParseOptions{ .allocator = self.allocator };
@setEvalBranchQuota(9999999);
const data = try std.json.parse(MouseEvent, &std.json.TokenStream.init(line), parseOptions);
defer std.json.parseFree(MouseEvent, data, parseOptions);
@ -306,7 +303,7 @@ pub fn initBar(allocator: *std.mem.Allocator) Bar {
.widgets = undefined,
.running = false,
.infos = std.ArrayList(Info).init(allocator),
.items_mutex = std.Mutex{},
.items_mutex = std.Thread.Mutex{},
.out_file = std.io.getStdOut(),
};
}

View file

@ -85,14 +85,14 @@ allocation_strack_addresses: std.AutoHashMap(usize, [stack_addresses_size]usize)
// Interface implementation
allocator: std.mem.Allocator,
mutex: std.Mutex,
mutex: std.Thread.Mutex,
pub fn init(base_allocator: *std.mem.Allocator, max_bytes: usize) DebugAllocator {
return .{
.base_allocator = base_allocator,
.info = .{},
.max_bytes = max_bytes,
.mutex = std.Mutex.init(),
.mutex = std.Thread.Mutex.init(),
.allocation_strack_addresses = std.AutoHashMap(usize, [stack_addresses_size]usize).init(base_allocator),
.allocator = .{
.allocFn = alloc,

View file

@ -27,8 +27,7 @@ const TerminalOrangeColour = "\u{001b}[31;1m";
const TerminalYellowColour = "\u{001b}[33m";
const TerminalGreenColour = "\u{001b}[32m";
const TerminalPurpleColour = "\u{001b}[35m";
inline fn getColourFromColour(clr: []const u8) []const u8 {
fn getColourFromColour(clr: []const u8) callconv(.Inline) []const u8 {
if (clr[0] == '#' or clr[0] == '\u{001b}') {
return clr;
} else if (eql(u8, clr, "text")) {
@ -74,8 +73,8 @@ pub fn colour(alloc: *std.mem.Allocator, clr: []const u8, str: []const u8) ![]co
if (disable_colour) return str;
const proper_colour = getColourFromColour(clr);
if (terminal_version) {
return try std.fmt.allocPrint(alloc, "{}{}" ++ TerminalResetColour, .{ proper_colour, str });
return try std.fmt.allocPrint(alloc, "{s}{s}" ++ TerminalResetColour, .{ proper_colour, str });
} else {
return try std.fmt.allocPrint(alloc, "<span color=\"{}\">{}</span>", .{ proper_colour, str });
return try std.fmt.allocPrint(alloc, "<span color=\"{s}\">{s}</span>", .{ proper_colour, str });
}
}

View file

@ -71,7 +71,7 @@ pub fn main() !void {
&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(&batteryWidget.New(allocator, &br)), // 12.11KiB
&Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
};
bar.widgets = widgets[0..];

View file

@ -62,29 +62,29 @@ pub const BatteryWidget = struct {
var iterate = dir.iterate();
defer dir.close();
while (try iterate.next()) |ent| {
var ps_dir = try std.fmt.allocPrint(provided_allocator, "/sys/class/power_supply/{}", .{ent.name});
var ps_dir = try std.fmt.allocPrint(provided_allocator, "/sys/class/power_supply/{s}", .{ent.name});
var supply_dir = try fs.cwd().openDir(ps_dir, .{ .iterate = true });
var supply_iterate = supply_dir.iterate();
defer supply_dir.close();
while (try supply_iterate.next()) |entry| {
if (std.mem.eql(u8, entry.name, "status")) {
pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.status_path = try std.fmt.allocPrint(provided_allocator, "{s}/{s}", .{ ps_dir, entry.name });
continue;
}
if (std.mem.eql(u8, entry.name, "power_now")) {
pp.power_now_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.power_now_path = try std.fmt.allocPrint(provided_allocator, "{s}/{s}", .{ ps_dir, entry.name });
continue;
}
if (std.mem.eql(u8, entry.name, "capacity")) {
pp.capacity_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.capacity_path = try std.fmt.allocPrint(provided_allocator, "{s}/{s}", .{ ps_dir, entry.name });
continue;
}
if (std.mem.eql(u8, entry.name, "current_now")) {
pp.current_now_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.current_now_path = try std.fmt.allocPrint(provided_allocator, "{s}/{s}", .{ ps_dir, entry.name });
continue;
}
if (std.mem.eql(u8, entry.name, "voltage_now")) {
pp.voltage_now_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.voltage_now_path = try std.fmt.allocPrint(provided_allocator, "{s}/{s}", .{ ps_dir, entry.name });
continue;
}
}
@ -147,7 +147,7 @@ pub const BatteryWidget = struct {
var watts_info: []const u8 = "";
if (can_get_watts) {
const watts_str = try std.fmt.allocPrint(self.allocator, " {}{d:.2}W", .{ sign, watts });
const watts_str = try std.fmt.allocPrint(self.allocator, " {s}{d:.2}W", .{ sign, watts });
watts_info = try colour(self.allocator, "purple", watts_str);
self.allocator.free(watts_str);
}
@ -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, "{} {} {}{}{}", .{
var bat_info = try std.fmt.allocPrint(self.allocator, "{s} {s} {s}{s}{s}", .{
comptimeColour("accentlight", "bat"),
descriptor,
colourCapInfo,
@ -180,8 +180,7 @@ pub const BatteryWidget = struct {
}
}
};
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) BatteryWidget {
pub fn New(allocator: *std.mem.Allocator, bar: *Bar) callconv(.Inline) BatteryWidget {
return BatteryWidget{
.allocator = allocator,
.bar = bar,

View file

@ -32,7 +32,7 @@ fn fetchCPU() ![2]f64 {
var data: [2]f64 = [2]f64{ 0.0, 0.0 };
var line_buffer: [128]u8 = undefined;
const line_opt = try stat_file.inStream().readUntilDelimiterOrEof(&line_buffer, '\n');
const line_opt = try stat_file.reader().readUntilDelimiterOrEof(&line_buffer, '\n');
if (line_opt) |line| {
var it = std.mem.tokenize(line, " ");
const stat_type = it.next().?;
@ -98,7 +98,7 @@ pub const CPUWidget = struct {
var allocator = &fba.allocator;
try self.bar.add(Info{
.name = "cpu",
.full_text = try std.fmt.allocPrint(allocator, "{} {}", .{
.full_text = try std.fmt.allocPrint(allocator, "{s} {s}", .{
comptimeColour("accentlight", "cpu"),
formatCPUPercent(allocator, percentage),
}),
@ -120,8 +120,7 @@ pub const CPUWidget = struct {
}
}
};
pub inline fn New(bar: *Bar) CPUWidget {
pub fn New(bar: *Bar) callconv(.Inline) CPUWidget {
return CPUWidget{
.bar = bar,
};

View file

@ -66,7 +66,7 @@ fn fetchTotalMemory() !MemInfo {
while (true) {
var line_buffer: [128]u8 = undefined;
const line_opt = try meminfo_file.inStream().readUntilDelimiterOrEof(&line_buffer, '\n');
const line_opt = try meminfo_file.reader().readUntilDelimiterOrEof(&line_buffer, '\n');
if (line_opt) |line| {
var it = std.mem.tokenize(line, " ");
const line_header = it.next().?;
@ -152,47 +152,47 @@ pub const MemoryWidget = struct {
// And this is why I love the looping counter.
if (self.lc.get() == 0) {
text = try std.fmt.allocPrint(allocator, "{} {}", .{
text = try std.fmt.allocPrint(allocator, "{s} {s}", .{
comptimeColour("accentlight", "mem"),
formatMemoryPercent(allocator, memInfo.memPercent),
});
} else if (self.lc.get() == 1) {
text = try std.fmt.allocPrint(allocator, "{} {}", .{
text = try std.fmt.allocPrint(allocator, "{s} {s}", .{
comptimeColour("accentlight", "swap"),
formatMemoryPercent(allocator, memInfo.swapPercent),
});
} else if (self.lc.get() == 2) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "mem free"),
kibibytesToMegabytes(memInfo.memFree),
});
} else if (self.lc.get() == 3) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "swap free"),
kibibytesToMegabytes(memInfo.swapFree),
});
} else if (self.lc.get() == 4) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "mem used"),
kibibytesToMegabytes(memInfo.memUsed),
});
} else if (self.lc.get() == 5) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "swap used"),
kibibytesToMegabytes(memInfo.swapUsed),
});
} else if (self.lc.get() == 6) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "mem cache"),
kibibytesToMegabytes(memInfo.cached),
});
} else if (self.lc.get() == 7) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "swap cache"),
kibibytesToMegabytes(memInfo.swapCached),
});
} else if (self.lc.get() == 8) {
text = try std.fmt.allocPrint(allocator, "{} {d:0>2} MB", .{
text = try std.fmt.allocPrint(allocator, "{s} {d:0>2} MB", .{
comptimeColour("accentlight", "mem buf"),
kibibytesToMegabytes(memInfo.buffers),
});
@ -207,7 +207,7 @@ pub const MemoryWidget = struct {
});
if (kibibytesToMegabytes(memInfo.cached) > 1000) {
self.clear_cache() catch |err| {
std.log.err("Can't clear cache {}.\n", .{err});
std.log.err("Can't clear cache {any}.\n", .{err});
};
}
}
@ -228,8 +228,7 @@ pub const MemoryWidget = struct {
}
}
};
pub inline fn New(bar: *Bar) MemoryWidget {
pub fn New(bar: *Bar) callconv(.Inline) MemoryWidget {
return MemoryWidget{
.bar = bar,
.lc = LoopingCounter(8).init(),

View file

@ -49,12 +49,10 @@ pub const NetworkInfo = struct {
network_status: NetworkStatus = .Connected,
network_info: []const u8,
};
inline fn freeString(allocator: *std.mem.Allocator, string: []const u8) void {
fn freeString(allocator: *std.mem.Allocator, string: []const u8) callconv(.Inline) void {
allocator.free(string);
}
inline fn dupeString(allocator: *std.mem.Allocator, string: []const u8) ![]const u8 {
fn dupeString(allocator: *std.mem.Allocator, string: []const u8) callconv(.Inline) ![]const u8 {
return try allocator.dupe(u8, string);
}
@ -64,7 +62,7 @@ pub const NetworkWidget = struct {
network_infos: std.ArrayList(NetworkInfo),
num_interfaces: u8 = 0,
current_interface: u8 = 0,
update_mutex: std.Mutex = std.Mutex{},
update_mutex: std.Thread.Mutex = std.Mutex{},
pub fn name(self: *NetworkWidget) []const u8 {
return "network";
@ -167,8 +165,7 @@ pub const NetworkWidget = struct {
}
}
};
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) NetworkWidget {
pub fn New(allocator: *std.mem.Allocator, bar: *Bar) callconv(.Inline) NetworkWidget {
return NetworkWidget{
.allocator = allocator,
.bar = bar,

View file

@ -20,8 +20,7 @@ pub const TextWidget = struct {
pub fn start(self: *TextWidget) anyerror!void {}
};
pub inline fn New(name: []const u8, text: []const u8) TextWidget {
pub fn New(name: []const u8, text: []const u8) callconv(.Inline) TextWidget {
return TextWidget{
.name = name,
.text = text,

View file

@ -45,7 +45,7 @@ pub const TimeWidget = struct {
end = "am";
}
var timeStr = try std.fmt.allocPrint(allocator, "{}{}{}{}{}{}", .{
var timeStr = try std.fmt.allocPrint(allocator, "{s}{s}{s}{s}{s}{s}", .{
colour(allocator, "red", try std.fmt.allocPrint(allocator, "{d:0>2}", .{@intCast(u7, hour)})),
comptimeColour("accentlight", ":"),
colour(allocator, "orange", try std.fmt.allocPrint(allocator, "{d:0>2}", .{@intCast(u7, clock.min)})),
@ -69,15 +69,15 @@ pub const TimeWidget = struct {
}
}
var h = try std.fmt.allocPrint(allocator, "{} {} {}{} {} {} {} {} {} {}", .{
var h = try std.fmt.allocPrint(allocator, "{s} {s} {s}{s} {s} {s} {s} {s} {s} {s}", .{
colour(allocator, "green", now.weekday().string()),
comptimeColour("purple", "the"),
colour(allocator, "yellow", try std.fmt.allocPrint(allocator, "{}", .{date.day})),
colour(allocator, "yellow", try std.fmt.allocPrint(allocator, "{d}", .{date.day})),
colour(allocator, "accentmedium", suffix),
comptimeColour("purple", "of"),
colour(allocator, "red", date.month.string()),
comptimeColour("purple", "in"),
colour(allocator, "accentlight", try std.fmt.allocPrint(allocator, "{}", .{date.year})),
colour(allocator, "accentlight", try std.fmt.allocPrint(allocator, "{d}", .{date.year})),
comptimeColour("purple", "at"),
timeStr,
});
@ -92,8 +92,7 @@ pub const TimeWidget = struct {
}
}
};
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) TimeWidget {
pub fn New(allocator: *std.mem.Allocator, bar: *Bar) callconv(.Inline) TimeWidget {
return TimeWidget{
.allocator = allocator,
.bar = bar,

View file

@ -155,7 +155,7 @@ pub const WeatherWidget = struct {
return;
},
else => |e| {
std.debug.print("\n\n\n\n\nError!: {}\n\n\n\n\n", .{@errorName(e)});
std.debug.print("\n\n\n\n\nError!: {s}\n\n\n\n\n", .{@errorName(e)});
return;
},
}
@ -163,7 +163,7 @@ pub const WeatherWidget = struct {
if (inf.code != 200) {
try self.bar.add(Info{
.name = self.name,
.full_text = try std.fmt.allocPrint(arenacator, "Weather API Failed: {}", .{inf.message}),
.full_text = try std.fmt.allocPrint(arenacator, "Weather API Failed: {s}", .{inf.message}),
.markup = "pango",
});
return;
@ -189,9 +189,9 @@ pub const WeatherWidget = struct {
var i = Info{
.name = self.name,
.full_text = try std.fmt.allocPrint(arenacator, "{} {}{}{} {}", .{
.full_text = try std.fmt.allocPrint(arenacator, "{s} {s}{s}{s} {s}", .{
comptimeColour("accentlight", "weather"),
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})),
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{d}", .{temp})),
comptimeColour("accentlight", "°"),
comptimeColour("accentdark", "C"),
colour(arenacator, "green", main),
@ -208,8 +208,7 @@ pub const WeatherWidget = struct {
}
}
};
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: []const u8) WeatherWidget {
pub fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: []const u8) callconv(.Inline) WeatherWidget {
return WeatherWidget{
.allocator = allocator,
.bar = bar,