stevee

My wayland statusbar
git clone git://gtms.dev/stevee
Log | Files | Refs | Submodules | README | LICENSE

commit 5401a84f3d52d1ba7b09e487a89827b63170d987
parent 801f9b70cf31ba704c5262273b0dfac308b043f4
Author: Andrea Feletto <andrea@andreafeletto.com>
Date:   Tue, 28 Jun 2022 17:05:27 +0200

replace zig-clap with posix args iterator

Diffstat:
M.gitmodules | 3---
Mbuild.zig | 5-----
Ddeps/zig-clap | 1-
Msrc/main.zig | 45+++++++++++++++++++++++++--------------------
4 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -7,9 +7,6 @@ [submodule "deps/zig-udev"] path = deps/zig-udev url = https://git.sr.ht/~andreafeletto/zig-udev -[submodule "deps/zig-clap"] - path = deps/zig-clap - url = https://github.com/Hejsil/zig-clap [submodule "deps/zig-fcft"] path = deps/zig-fcft url = https://git.sr.ht/~novakane/zig-fcft diff --git a/build.zig b/build.zig @@ -31,10 +31,6 @@ pub fn build(b: *std.build.Builder) void { exe.step.dependOn(&scanner.step); scanner.addCSource(exe); - const clap = Pkg{ - .name = "clap", - .path = .{ .path = "deps/zig-clap/clap.zig" }, - }; const wayland = Pkg{ .name = "wayland", .path = .{ .generated = &scanner.result }, @@ -53,7 +49,6 @@ pub fn build(b: *std.build.Builder) void { .path = .{ .path = "deps/zig-udev/udev.zig" }, }; - exe.addPackage(clap); exe.addPackage(fcft); exe.addPackage(pixman); exe.addPackage(udev); diff --git a/deps/zig-clap b/deps/zig-clap @@ -1 +0,0 @@ -Subproject commit 511b357b9fd6480f46cf2a52b1d168471d1ec015 diff --git a/src/main.zig b/src/main.zig @@ -4,8 +4,8 @@ const io = std.io; const log = std.log; const mem = std.mem; const os = std.os; +const process = std.process; -const clap = @import("clap"); const fcft = @import("fcft"); const Config = @import("Config.zig"); @@ -27,17 +27,6 @@ pub fn main() anyerror!void { _ = fcft.init(.auto, false, .warning); - // cli arguments - const params = comptime [_]clap.Param(clap.Help){ - try clap.parseParam("-h, --help Display this help and exit."), - try clap.parseParam("-m, --module <str>... Add module."), - }; - var args = try clap.parse(clap.Help, &params, .{}); - defer args.deinit(); - if (args.flag("--help")) { - return clap.help(io.getStdErr().writer(), &params); - } - // initialization var state: State = undefined; state.gpa = gpa.allocator(); @@ -49,25 +38,41 @@ pub fn main() anyerror!void { state.loop = try Loop.init(&state); // modules - for (args.options("--module")) |module_name| { - if (mem.eql(u8, module_name, "backlight")) { + var args = process.args(); + const program_name = args.nextPosix() orelse unreachable; + + while (args.nextPosix()) |arg| { + if (mem.eql(u8, arg, "backlight")) { try state.modules.register(Modules.Backlight); - } else if (mem.eql(u8, module_name, "battery")) { + } else if (mem.eql(u8, arg, "battery")) { try state.modules.register(Modules.Battery); - } else if (mem.eql(u8, module_name, "pulse")) { + } else if (mem.eql(u8, arg, "pulse")) { try state.modules.register(Modules.Pulse); } else { - log.err("unknown module: {s}", .{module_name}); - return clap.help(io.getStdErr().writer(), &params); + try help(program_name); + return; } } if (state.modules.modules.items.len == 0) { - log.err("having no modules is currently not supported", .{}); - return clap.help(io.getStdErr().writer(), &params); + try help(program_name); + return; } // event loop try state.wayland.registerGlobals(); try state.loop.run(); } + +fn help(program_name: []const u8) !void { + const help_text = + \\Usage: {s} [module]... + \\ + \\Available modules: + \\ backlight screen brightness + \\ battery battery capacity + \\ pulse speaker volume with pulseaudio + \\ + ; + try io.getStdErr().writer().print(help_text, .{program_name}); +}