main.zig (9653B)
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 4 const options = @import("options"); 5 const ziggy = @import("ziggy"); 6 7 const environment = @import("environment.zig"); 8 const stats = @import("stats.zig"); 9 const sys_info = @import("sys_info.zig"); 10 11 const padding: u8 = 9; 12 13 const Widgets = enum { 14 distro, 15 uptime, 16 kernel, 17 desktop, 18 shell, 19 memory, 20 battery, 21 cpu, 22 disk, 23 locale, 24 colours, 25 }; 26 27 const Config = struct { 28 widgets: []const Widgets, 29 }; 30 31 const Options = struct { 32 help: bool = false, 33 version: bool = false, 34 35 fn optKind(a: []const u8) enum { short, long, positional } { 36 if (std.mem.startsWith(u8, a, "--")) return .long; 37 if (std.mem.startsWith(u8, a, "-")) return .short; 38 return .positional; 39 } 40 }; 41 42 const CONFIG_NAME = "config.ziggy"; 43 const HOME_DIR_NAME = ".lens"; 44 const XDG_CONFIG_HOME_DIR_NAME = "lens"; 45 const default_config = Config{ 46 .widgets = &[_]Widgets{ 47 .distro, 48 .uptime, 49 .kernel, 50 .desktop, 51 .shell, 52 .memory, 53 .battery, 54 .cpu, 55 .disk, 56 .locale, 57 .colours, 58 }, 59 }; 60 const help_menu = 61 \\Usage: lens 62 \\ 63 \\a minimal posix system fetch tool 64 \\ 65 \\Flags: 66 \\ -h, --help Show help information and exit. 67 \\ -v, --version Print version information and exit. 68 \\ 69 ; 70 71 pub fn main(init: std.process.Init) !void { 72 const gpa = init.gpa; 73 const io = init.io; 74 75 var stdout_buffer: [1024]u8 = undefined; 76 var stdout_file_writer: std.Io.File.Writer = .init(.stdout(), io, &stdout_buffer); 77 const stdout_writer = &stdout_file_writer.interface; 78 79 var opts = Options{}; 80 var args = init.minimal.args.iterate(); 81 _ = args.skip(); 82 while (args.next()) |arg| { 83 switch (Options.optKind(arg)) { 84 .short => { 85 const str = arg[1..]; 86 for (str) |b| { 87 switch (b) { 88 'v' => opts.version = true, 89 'h' => opts.help = true, 90 else => { 91 std.log.err("Invalid option: '{c}'", .{b}); 92 std.process.exit(1); 93 }, 94 } 95 } 96 }, 97 .long => { 98 var split = std.mem.splitScalar(u8, arg[2..], '='); 99 const opt = split.first(); 100 if (std.mem.eql(u8, opt, "version")) { 101 opts.version = true; 102 } else if (std.mem.eql(u8, opt, "help")) { 103 opts.help = true; 104 } 105 }, 106 .positional => { 107 std.log.err("Invalid opt: '{s}'. lens does not take positional arguments.", .{arg}); 108 std.process.exit(1); 109 }, 110 } 111 } 112 113 if (opts.help) { 114 try stdout_writer.writeAll(help_menu); 115 try stdout_writer.flush(); 116 return; 117 } 118 119 if (opts.version) { 120 try stdout_writer.print("lens v{f}\n", .{options.version}); 121 try stdout_writer.flush(); 122 return; 123 } 124 125 var arena = std.heap.ArenaAllocator.init(gpa); 126 defer _ = arena.deinit(); 127 var allocator = arena.allocator(); 128 129 // Read config 130 const config = lbl: { 131 var config_dir = dir: { 132 var dir_env: struct { dir: std.Io.Dir, name: []const u8 } = env: { 133 if (try environment.getXdgConfigHomeDir(io, init.environ_map)) |home_dir| { 134 break :env .{ .dir = home_dir, .name = XDG_CONFIG_HOME_DIR_NAME }; 135 } 136 137 if (try environment.getHomeDir(io, init.environ_map)) |home_dir| { 138 break :env .{ .dir = home_dir, .name = HOME_DIR_NAME }; 139 } 140 141 break :lbl default_config; 142 }; 143 defer dir_env.dir.close(io); 144 145 if (!environment.dirExists(dir_env.dir, io, dir_env.name)) { 146 break :lbl default_config; 147 } 148 149 const dir = try dir_env.dir.openDir(io, dir_env.name, .{ .iterate = true }); 150 if (!environment.fileExists(dir, io, CONFIG_NAME)) { 151 break :lbl default_config; 152 } 153 154 break :dir dir; 155 }; 156 157 const contents = config_dir.readFileAlloc(io, CONFIG_NAME, allocator, .limited(4096)) catch break :lbl default_config; 158 defer allocator.free(contents); 159 const contentsZ = try allocator.dupeSentinel(u8, contents, 0); 160 defer allocator.free(contentsZ); 161 162 var meta: ziggy.Deserializer.Meta = undefined; 163 break :lbl ziggy.deserializeLeaky(Config, allocator, contentsZ, &meta, .{}) catch default_config; 164 }; 165 166 // Print header 167 const user_env = try sys_info.getUser(allocator, init.environ_map); 168 defer allocator.free(user_env); 169 const distro_id = try sys_info.getDistroId(io, allocator); 170 defer allocator.free(distro_id); 171 172 var header_buf: [1024]u8 = undefined; 173 const header = try std.fmt.bufPrint(&header_buf, "{s}@{s}\n", .{ user_env, distro_id }); 174 175 try stdout_writer.writeAll(header); 176 try stdout_writer.print("{[str]s:-<[count]}\n", .{ .str = "-", .count = header.len + 1 }); 177 178 // Print widgets 179 for (config.widgets) |widget| { 180 switch (widget) { 181 .distro => { 182 const distro = sys_info.getDistro(io, allocator) catch continue; 183 defer allocator.free(distro); 184 185 try stdout_writer.print( 186 "{[header]s: <[padding]}{[stat]s}\n", 187 .{ .header = "distro", .stat = distro, .padding = padding }, 188 ); 189 }, 190 .uptime => { 191 const uptime = stats.getUptime() catch continue; 192 193 try stdout_writer.print( 194 "{[header]s: <[padding]}{[days]d}d {[hours]d}h {[minutes]d}m\n", 195 .{ 196 .header = "uptime", 197 .days = uptime.days, 198 .hours = uptime.hours, 199 .minutes = uptime.minutes, 200 .padding = padding, 201 }, 202 ); 203 }, 204 .kernel => { 205 const utsname = std.posix.uname(); 206 const release = std.mem.sliceTo(&utsname.release, 0); 207 208 try stdout_writer.print( 209 "{[header]s: <[padding]}{[stat]s}\n", 210 .{ .header = "kernel", .stat = release, .padding = padding }, 211 ); 212 }, 213 .desktop => { 214 const desktop = try sys_info.getDesktop(allocator, init.environ_map); 215 defer allocator.free(desktop); 216 217 try stdout_writer.print( 218 "{[header]s: <[padding]}{[stat]s}\n", 219 .{ .header = "desktop", .stat = desktop, .padding = padding }, 220 ); 221 }, 222 .shell => { 223 const shell = sys_info.getShell(io, allocator, init.environ_map) catch continue; 224 defer allocator.free(shell); 225 226 try stdout_writer.print( 227 "{[header]s: <[padding]}{[stat]s}\n", 228 .{ .header = "shell", .stat = shell, .padding = padding }, 229 ); 230 }, 231 .memory => { 232 const mem = stats.getMemory(io, allocator, .{ .mb = true }) catch continue; 233 defer allocator.free(mem); 234 235 try stdout_writer.print( 236 "{[header]s: <[padding]}{[stat]s}\n", 237 .{ .header = "memory", .stat = mem, .padding = padding }, 238 ); 239 }, 240 .battery => { 241 const battery = stats.getBattery(io, allocator) catch continue; 242 defer allocator.free(battery); 243 244 try stdout_writer.print( 245 "{[header]s: <[padding]}{[stat]s}\n", 246 .{ .header = "battery", .stat = battery, .padding = padding }, 247 ); 248 }, 249 .cpu => { 250 const cpu = sys_info.getCpu(io, allocator) catch continue; 251 defer allocator.free(cpu); 252 253 try stdout_writer.print( 254 "{[header]s: <[padding]}{[stat]s}\n", 255 .{ .header = "cpu", .stat = cpu, .padding = padding }, 256 ); 257 }, 258 .disk => { 259 const disk = sys_info.getDisk(io, allocator) catch continue; 260 defer allocator.free(disk); 261 262 try stdout_writer.print( 263 "{[header]s: <[padding]}{[stat]s}\n", 264 .{ .header = "disk", .stat = disk, .padding = padding }, 265 ); 266 }, 267 .locale => { 268 if (environment.getLocale(init.environ_map)) |locale| { 269 try stdout_writer.print( 270 "{[header]s: <[padding]}{[stat]s}\n", 271 .{ .header = "locale", .stat = locale, .padding = padding }, 272 ); 273 } 274 }, 275 .colours => { 276 try stdout_writer.writeAll("\n"); 277 for (0..16) |i| { 278 const colour = if (i < 8) 40 + i else 100 + (i - 8); 279 if (i == 8) { 280 try stdout_writer.writeAll("\x1b[0m\n"); 281 } 282 try stdout_writer.print("\x1b[{d}m ", .{colour}); 283 } 284 try stdout_writer.writeAll("\x1b[0m\n"); 285 }, 286 } 287 } 288 289 try stdout_writer.flush(); 290 }