dotfiles/scripts/playtwitch/src/config.zig

83 lines
2.5 KiB
Zig
Raw Normal View History

2022-10-29 21:49:44 +02:00
const std = @import("std");
const c = @import("ffi.zig").c;
const State = @import("State.zig");
2022-11-02 22:35:51 +01:00
const log = std.log.scoped(.config);
2022-10-29 21:49:44 +02:00
pub fn configLoaderThread(state: *State) !void {
const home = std.os.getenv("HOME") orelse return error.HomeNotSet;
const channels_path = try std.fs.path.join(
std.heap.c_allocator,
2022-11-02 22:35:51 +01:00
&.{ home, ".config", "playtwitch", "channels.cfg" },
2022-10-29 21:49:44 +02:00
);
defer std.heap.c_allocator.free(channels_path);
2022-11-02 22:35:51 +01:00
log.info("reading config from '{s}'", .{channels_path});
const start_time = std.time.milliTimestamp();
2022-10-29 21:49:44 +02:00
const file = std.fs.cwd().openFile(channels_path, .{}) catch |e| {
switch (e) {
error.FileNotFound => {
2022-11-02 22:35:51 +01:00
log.warn("channels config file not found at {s}, skipping.", .{channels_path});
2022-10-29 21:49:44 +02:00
return;
},
else => return e,
}
};
defer file.close();
const channels_data = try file.readToEndAlloc(std.heap.c_allocator, std.math.maxInt(usize));
2022-11-22 23:58:49 +01:00
var channels = std.ArrayList(State.Entry).init(std.heap.c_allocator);
2022-10-29 21:49:44 +02:00
2022-11-22 23:58:49 +01:00
var channels_iter = std.mem.tokenize(u8, channels_data, "\n");
2022-11-02 22:35:51 +01:00
while (channels_iter.next()) |line| {
2022-11-22 23:58:49 +01:00
var line_iter = std.mem.tokenize(u8, line, ":");
2022-11-02 22:35:51 +01:00
const channel = line_iter.next() orelse continue;
const channel_trimmed = std.mem.trim(u8, channel, " \n\r");
2022-11-22 23:58:49 +01:00
if (channel_trimmed.len <= 0 or channel_trimmed[0] == '#')
2022-11-02 22:35:51 +01:00
continue;
const comment_trimmed = blk: {
const comment = line_iter.next() orelse break :blk null;
const comment_trimmed = std.mem.trim(u8, comment, " \n\r");
2022-11-02 22:35:51 +01:00
if (comment_trimmed.len == 0)
break :blk null;
break :blk comment_trimmed;
};
2022-11-22 23:58:49 +01:00
// dashes act as separator
if (std.mem.allEqual(u8, channel_trimmed, '-')) {
// separators can have comments to act as headings
try channels.append(.{ .separator = comment_trimmed });
continue;
}
try channels.append(.{ .channel = .{
2022-11-02 22:35:51 +01:00
.name = channel_trimmed,
.comment = comment_trimmed,
2022-11-22 23:58:49 +01:00
} });
2022-10-29 21:49:44 +02:00
}
2022-11-02 22:35:51 +01:00
const end_time = std.time.milliTimestamp();
log.info(
2022-11-22 23:58:49 +01:00
"Loaded {d} channel items in {d}ms",
2022-11-02 22:35:51 +01:00
.{ channels.items.len, end_time - start_time },
);
2022-11-05 13:02:30 +01:00
{
state.mutex.lock();
defer state.mutex.unlock();
2022-10-29 21:49:44 +02:00
2022-11-05 13:02:30 +01:00
state.channels_file_data = channels_data;
2022-12-11 20:55:29 +01:00
state.channels = try channels.toOwnedSlice();
2022-11-05 13:02:30 +01:00
}
@import("live.zig").tryFetchChannelsLive(state);
2022-10-29 21:49:44 +02:00
}