get zigged idiot

This commit is contained in:
ptrcnull 2023-10-10 20:03:39 +02:00
parent d230d84e91
commit 4e80ad4617
6 changed files with 144 additions and 69 deletions

View file

@ -1,18 +0,0 @@
---
kind: pipeline
steps:
- name: build
image: "golang:alpine"
commands:
- go build
- name: release
image: plugins/gitea-release
settings:
api_key:
from_secret: gitea_api_key
base_url: https://git.ddd.rip
files: bat-alert
when:
event: tag

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
zig-cache/
zig-out/

70
build.zig Normal file
View file

@ -0,0 +1,70 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "bat-alert",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}

3
go.mod
View file

@ -1,3 +0,0 @@
module git.ddd.rip/ptrcnull/bat-alert
go 1.16

48
main.go
View file

@ -1,48 +0,0 @@
package main
import (
"flag"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
func notify(msg string) {
cmd := exec.Command("notify-send", "-t", "5000", "-u", "critical", "bat-alert", msg)
err := cmd.Run()
if err != nil {
log.Println(err)
}
}
var targetFlag = flag.Int("target", 15, "when to send the alert")
func main() {
flag.Parse()
sent := false
for {
capacity, err := os.ReadFile("/sys/class/power_supply/BAT0/capacity")
if err != nil {
notify("error: " + err.Error())
}
numCap, err := strconv.Atoi(strings.Trim(string(capacity), "\n"))
if err != nil {
notify("error: " + err.Error())
}
if numCap <= *targetFlag {
if !sent {
sent = true
notify("battery running low!")
}
} else {
sent = false
}
time.Sleep(time.Second * 10)
}
}

72
src/main.zig Normal file
View file

@ -0,0 +1,72 @@
const std = @import("std");
pub const std_options = struct {
pub const log_level = .info;
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
var target: u8 = 15;
for (args, 0..) |arg, i| {
if (std.mem.eql(u8, arg, "--help")) {
const stdout = std.io.getStdOut().writer();
try stdout.print(
\\Usage: bat-alert [options]
\\
\\Options:
\\ -t, --target NUM Target battery level
\\
, .{});
std.process.exit(0);
}
if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) {
if (i + 1 == args.len) {
std.log.err("--target requires an argument", .{});
std.process.exit(1);
}
const parsedTarget = std.fmt.parseInt(u8, args[i + 1], 10) catch |err| {
std.log.err("invalid target '{s}': {}", .{ args[i + 1], err });
std.process.exit(1);
};
target = parsedTarget;
}
}
var sent = false;
while (true) {
const capacityFile = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/capacity", .{});
var capacityStr = std.ArrayList(u8).init(allocator);
try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null);
capacityFile.close();
// std.log.info("battery: {s}", .{capacityStr.items});
const capacity = try std.fmt.parseInt(u8, capacityStr.items, 10);
// std.log.info("battery (but parsed): {}", .{capacity});
if (capacity <= target) {
if (!sent) {
sent = true;
_ = try std.process.Child.exec(.{
.allocator = allocator,
.argv = &[_][]const u8{ "notify-send", "-t", "5000", "-u", "critical", "bat-alert", "battery running low!" },
});
std.log.info("[NOTIFY] battery running low!", .{});
}
} else {
sent = false;
}
std.time.sleep(10 * 1e9);
}
}