srctree

Gregory Mullen parent 6335af98 985b28f6
add some nice colors to battery status

src/battery.zig added: 101, removed: 22, total 79
@@ -1,4 +1,5 @@
const std = @import("std");
const Pango = @import("pango.zig");
 
const DELAY = 10;
 
@@ -28,6 +29,12 @@ pub fn update(self: *Battery, i: i64) !void {
try self.read();
}
 
pub fn format(self: Battery, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
return out.print("Battery {}%", .{self.capacity});
pub fn format(self: Battery, comptime fmt: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
if (std.mem.eql(u8, fmt, "pango")) {
if (self.capacity == 69) return out.print("Battery NICE!", .{});
return out.print("Battery {}%", .{self.capacity});
}
const color: ?Pango.Color = if (self.capacity < 20) Pango.Color.Red else null;
var p = Pango.Pango(Battery).init(self, color);
return out.print("{}", .{p});
}
 
src/main.zig added: 101, removed: 22, total 79
@@ -11,7 +11,7 @@ const Header = struct {
};
 
const Body = struct {
full_text: ?[]u8 = null,
full_text: ?[]const u8 = null,
short_text: ?[]u8 = null,
color: ?[]u8 = null,
background: ?[]u8 = null,
@@ -30,17 +30,21 @@ const Body = struct {
markup: ?[]const u8 = null,
};
 
fn dateOffset(os: i16) DateTime {
return DateTime.nowOffset(@as(isize, os) * 60 * 60);
}
 
var date_buffer: [1024]u8 = undefined;
fn date() Body {
fn date() anyerror!Body {
return Body{
.full_text = std.fmt.bufPrint(&date_buffer, "{}", .{DateTime.nowOffset(-28800)}) catch unreachable,
.full_text = try std.fmt.bufPrint(&date_buffer, "{}", .{dateOffset(-8)}),
};
}
 
var bl_buffer: [1024]u8 = undefined;
fn bl() !Body {
return Body{
.full_text = std.fmt.bufPrint(&bl_buffer, "{}", .{try Video.Backlight.init()}) catch unreachable,
.full_text = try std.fmt.bufPrint(&bl_buffer, "{}", .{try Video.Backlight.init()}),
};
}
 
@@ -49,22 +53,19 @@ fn battery() !Body {
var bat = try Battery.init();
//try bat.update(std.time.timestamp());
return Body{
.full_text = std.fmt.bufPrint(&bat_buffer, "{}", .{bat}) catch unreachable,
.full_text = try std.fmt.bufPrint(&bat_buffer, "{}", .{bat}),
.markup = "pango",
};
}
 
fn build(a: std.mem.Allocator) ![]Body {
const list = try a.alloc(Body, 3);
for (list) |*l|
l.* = Body{};
list[0] = try bl();
list[1] = try battery();
list[2] = date();
return list;
}
const build_error = Body{
.full_text = "error building this complication",
};
 
const Builder = *const fn () anyerror!Body;
 
var buffer: [0xffffff]u8 = undefined;
pub fn main() !void {
var buffer: [0xffff]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
var a = fba.allocator();
var header = Header{};
@@ -78,9 +79,21 @@ pub fn main() !void {
 
_ = try bw.write("\n[");
 
const builders = [_]Builder{
bl,
battery,
date,
};
const list = try a.alloc(Body, builders.len);
defer a.free(list);
 
while (true) {
var list = try build(a);
defer a.free(list);
for (list, builders) |*l, func| {
l.* = func() catch |err| backup: {
std.debug.print("Error {} when attempting to try {}\n", .{ err, func });
break :backup build_error;
};
}
 
try std.json.stringify(list, opt, stdout);
_ = try bw.write(",\n");
 
filename was Deleted added: 101, removed: 22, total 79
@@ -0,0 +1,59 @@
const std = @import("std");
const hexL = std.fmt.fmtSliceHexLower;
const bPrint = std.fmt.bufPrint;
 
//const Pango = @This();
 
pub const Color = struct {
red: u8 = 0x00,
blue: u8 = 0x00,
green: u8 = 0x00,
alpha: ?u8 = null,
buffer: [9]u8 = [_]u8{'#'} ++ [_]u8{'0'} ** 8,
len: usize = 7,
 
pub const Red = Color{
.red = 0xff,
.buffer = [_]u8{'#'} ++ [_]u8{'f'} ** 2 ++ [_]u8{'0'} ** 6,
};
 
pub fn red(self: *Color, r: u8) !void {
self.red = r;
try bPrint(self.buffer[1..3], "{}", hexL(self.red));
}
 
pub fn green(self: *Color, g: u8) !void {
self.green = g;
try bPrint(self.buffer[3..5], "{}", hexL(self.green));
}
 
pub fn blue(self: *Color, b: u8) !void {
self.blue = b;
try bPrint(self.buffer[5..7], "{}", hexL(self.blue));
}
 
pub fn format(self: Color, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
_ = try out.write(self.buffer[0..self.len]);
}
};
 
pub fn Pango(comptime other: type) type {
return struct {
color: ?Color,
other: other,
 
const Self = @This();
 
pub fn init(thing: other, color: ?Color) Self {
return Self{
.color = color,
.other = thing,
};
}
 
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
if (self.color == null) return try out.print("{pango}", .{self.other});
return try out.print("<span color=\"{}\">{pango}</span>", .{ self.color.?, self.other });
}
};
}