1
0
Fork 0

move stuff around

This commit is contained in:
namedkitten 2020-07-16 12:22:08 +01:00
parent 53985936a0
commit 7203cf1904
7 changed files with 141 additions and 355 deletions

View file

@ -9,13 +9,19 @@ pub fn build(b: *Builder) void {
"disable_colour",
"no colour",
) orelse false;
exe.addBuildOption(bool, "disable_colour", disable_colour);
const terminal_version = b.option(
bool,
"terminal_version",
"terminal-only version",
) orelse false;
exe.addBuildOption(bool, "terminal_version", terminal_version);
exe.addBuildOption(bool, "disable_colour", disable_colour);
const debug_allocator = b.option(
bool,
"debug_allocator",
"use debug allocator for testing",
) orelse false;
exe.addBuildOption(bool, "debug_allocator", debug_allocator);
//exe.strip = true;
exe.addPackage(.{

View file

@ -4,6 +4,7 @@ const Info = @import("../types/info.zig");
const MouseEvent = @import("../types/mouseevent.zig");
const os = std.os;
const terminal_version = @import("build_options").terminal_version;
const debug_allocator = @import("build_options").debug_allocator;
pub const Bar = struct {
allocator: *std.mem.Allocator,
@ -32,92 +33,99 @@ pub const Bar = struct {
self.infos.deinit();
}
inline fn print_i3bar_infos(self: *Bar) !void {
try self.out_file.writer().writeAll("[");
for (self.infos.items) |info, i| {
try std.json.stringify(info, .{}, self.out_file.writer());
if (i < self.infos.items.len - 1) {
try self.out_file.writer().writeAll(",");
}
}
try self.out_file.writer().writeAll("],\n");
}
inline fn print_terminal_infos(self: *Bar) !void {
for (self.infos.items) |info, i| {
try self.out_file.writer().writeAll(info.full_text);
if (i < self.infos.items.len - 1) {
try self.out_file.writer().writeAll("|");
}
}
try self.out_file.writer().writeAll("\n");
}
fn print_infos(self: *Bar, should_lock: bool) !void {
if (should_lock) {
const lock = self.items_mutex.acquire();
defer lock.release();
}
if (!terminal_version) try self.out_file.writer().writeAll("[");
for (self.infos.items) |info, i| {
if (!terminal_version) {
try std.json.stringify(info, .{}, self.out_file.writer());
if (i < self.infos.items.len - 1) {
try self.out_file.writer().writeAll(",");
}
} else {
try self.out_file.writer().writeAll(info.full_text);
if (i < self.infos.items.len - 1) {
try self.out_file.writer().writeAll("|");
}
}
}
if (!terminal_version) {
try self.out_file.writer().writeAll("],\n");
if (terminal_version) {
self.print_terminal_infos() catch {};
} else {
try self.out_file.writer().writeAll("\n");
self.print_i3bar_infos() catch {};
}
}
fn process(self: *Bar) !void {
if (terminal_version) {
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015h");
inline fn terminal_input_process(self: *Bar) !void {
try self.out_file.writer().writeAll("\u{001b}[?1000;1006;1015h");
var termios = try os.tcgetattr(0);
termios.iflag &= ~@as(
os.tcflag_t,
os.IGNBRK | os.BRKINT | os.PARMRK | os.ISTRIP |
os.INLCR | os.IGNCR | os.ICRNL | os.IXON,
);
termios.lflag |= ~@as(os.tcflag_t, (os.ECHO | os.ICANON));
termios.lflag &= os.ISIG;
var termios = try os.tcgetattr(0);
termios.iflag &= ~@as(
os.tcflag_t,
os.IGNBRK | os.BRKINT | os.PARMRK | os.ISTRIP |
os.INLCR | os.IGNCR | os.ICRNL | os.IXON,
);
termios.lflag |= ~@as(os.tcflag_t, (os.ECHO | os.ICANON));
termios.lflag &= os.ISIG;
try os.tcsetattr(0, .FLUSH, termios);
try os.tcsetattr(0, .FLUSH, termios);
while (true) {
var line_buffer: [128]u8 = undefined;
while (true) {
var line_buffer: [128]u8 = undefined;
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, 0x1b);
if (line_opt) |l| {
if (l.len < 2) continue;
var it = std.mem.tokenize(l, ";");
_ = it.next();
const n = try std.fmt.parseInt(u64, it.next().?, 10);
var y = it.next().?;
if (y[y.len - 1] == 'm') continue;
const line_opt = try std.io.getStdIn().inStream().readUntilDelimiterOrEof(&line_buffer, 0x1b);
if (line_opt) |l| {
if (l.len < 2) continue;
var it = std.mem.tokenize(l, ";");
_ = it.next();
const n = try std.fmt.parseInt(u64, it.next().?, 10);
var y = it.next().?;
if (y[y.len - 1] == 'm') continue;
var xe: u64 = 0;
for (self.infos.items) |infoItem, index| {
var isEscape: bool = false;
for (infoItem.full_text) |char| {
if (char == 0x1b) {
isEscape = true;
continue;
}
if (isEscape and char != 'm') {
continue;
}
if (char == 'm' and isEscape) {
isEscape = false;
continue;
}
xe = xe + 1;
var xe: u64 = 0;
for (self.infos.items) |infoItem, index| {
var isEscape: bool = false;
for (infoItem.full_text) |char| {
if (char == 0x1b) {
isEscape = true;
continue;
}
if (n <= xe) {
for (self.widgets) |w| {
if (std.mem.eql(u8, w.name(), infoItem.name)) {
w.mouse_event(.{ .button = .LeftClick }) catch {};
}
}
//std.debug.print("Info Item Clicky{}\n", .{infoItem.name});
break;
if (isEscape and char != 'm') {
continue;
}
if (char == 'm' and isEscape) {
isEscape = false;
continue;
}
xe = xe + 1;
}
if (n <= xe) {
for (self.widgets) |w| {
if (std.mem.eql(u8, w.name(), infoItem.name)) {
w.mouse_event(.{ .button = .LeftClick }) catch {};
}
}
//std.debug.print("Info Item Clicky{}\n", .{infoItem.name});
break;
}
xe = xe + 1;
}
}
}
}
inline fn i3bar_input_process(self: *Bar) !void {
var line_buffer: [512]u8 = undefined;
while (self.running) {
var arena = std.heap.ArenaAllocator.init(self.allocator);
@ -140,31 +148,45 @@ pub const Bar = struct {
}
}
}
fn process(self: *Bar) !void {
if (debug_allocator) {
std.time.sleep(std.time.ns_per_ms * 2000 * 5);
if (true) return;
}
if (terminal_version) {
self.terminal_input_process() catch {};
} else {
self.i3bar_input_process() catch {};
}
}
pub fn keep_running(self: *Bar) bool {
return self.running;
}
pub fn free_info(self: *Bar, info: Info) !void {
fn free_info(self: *Bar, info: Info) !void {
self.allocator.free(info.name);
self.allocator.free(info.full_text);
}
pub fn dupe_info(self: *Bar, info: Info) !Info {
/// In order to store the info and have Widgets not need to care about
/// memory lifetime, we duplicate the info fields.
fn dupe_info(self: *Bar, info: Info) !Info {
// TODO: name should be comptime known, rework.
const new_name = try self.allocator.alloc(u8, info.name.len);
std.mem.copy(u8, new_name, info.name);
const new_text = try self.allocator.alloc(u8, info.full_text.len);
std.mem.copy(u8, new_text, info.full_text);
var i = Info{
return Info{
.name = new_name,
.full_text = new_text,
.markup = "pango",
};
return i;
}
pub fn add(self: *Bar, info: Info) !void {
const lock = self.items_mutex.acquire();
defer lock.release();
//std.debug.warn("info: {}\n", .{info.name});
for (self.infos.items) |infoItem, index| {
if (std.mem.eql(u8, infoItem.name, info.name)) {
if (std.mem.eql(u8, infoItem.full_text, info.full_text)) {

View file

@ -85,12 +85,14 @@ allocation_strack_addresses: std.AutoHashMap(usize, [stack_addresses_size]usize)
// Interface implementation
allocator: std.mem.Allocator,
mutex: std.Mutex,
pub fn init(base_allocator: *std.mem.Allocator, max_bytes: usize) DebugAllocator {
return .{
.base_allocator = base_allocator,
.info = .{},
.max_bytes = max_bytes,
.mutex = std.Mutex.init(),
.allocation_strack_addresses = std.AutoHashMap(usize, [stack_addresses_size]usize).init(base_allocator),
.allocator = .{
.allocFn = alloc,
@ -105,6 +107,8 @@ pub fn deinit(self: *DebugAllocator) void {
fn alloc(allocator: *std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29) error{OutOfMemory}![]u8 {
const self = @fieldParentPtr(DebugAllocator, "allocator", allocator);
const lock = self.mutex.acquire();
defer lock.release();
const ptr = try self.base_allocator.callAllocFn(len, ptr_align, len_align);
self.info.allocation_stats.addSample(ptr.len);
@ -132,6 +136,8 @@ fn alloc(allocator: *std.mem.Allocator, len: usize, ptr_align: u29, len_align: u
fn resize(allocator: *std.mem.Allocator, old_mem: []u8, new_size: usize, len_align: u29) error{OutOfMemory}!usize {
const self = @fieldParentPtr(DebugAllocator, "allocator", allocator);
const lock = self.mutex.acquire();
defer lock.release();
if (old_mem.len == 0) {
std.log.debug(.debug_alloc, "Trying to resize empty slice\n", .{});

View file

@ -2,6 +2,8 @@ const std = @import("std");
const Bar = @import("types/bar.zig").Bar;
const Widget = @import("types/widget.zig").Widget;
const barImpl = @import("bar/bar.zig");
const terminalBar = @import("bar/bar.zig");
const textWidget = @import("widgets/text/text.zig");
const weatherWidget = @import("widgets/weather/weather.zig");
const timeWidget = @import("widgets/time/time.zig");
@ -10,11 +12,12 @@ const memoryWidget = @import("widgets/memory/memory.zig");
const DebugAllocator = @import("debug_allocator.zig");
const Info = @import("types/info.zig");
const debug_allocator = @import("build_options").debug_allocator;
pub fn main() !void {
const debug: bool = false;
var allocator: *std.mem.Allocator = undefined;
var dbgAlloc: *DebugAllocator = undefined;
if (debug) {
if (debug_allocator) {
dbgAlloc = &DebugAllocator.init(std.heap.page_allocator, 8192 * 8192);
allocator = &dbgAlloc.allocator;
} else {
@ -33,7 +36,7 @@ pub fn main() !void {
};
bar.widgets = widgets[0..];
try br.start();
if (debug) {
if (debug_allocator) {
std.debug.print("Finished cleanup, last allocation info.\n", .{});
std.debug.print("\n{}\n", .{dbgAlloc.info});
dbgAlloc.printRemainingStackTraces();

View file

@ -1,274 +0,0 @@
const std = @import("std");
const builtin = @import("builtin");
const os = std.os;
const linux = os.linux;
const assert = std.debug.assert;
const supported_os = switch (builtin.os) {
builtin.os.linux => true,
builtin.os.macosx => true,
else => @compileError("unsupported os"),
};
const supported_architecture = switch (builtin.arch) {
builtin.Arch.x86_64 => true,
builtin.Arch.i386 => true,
builtin.Arch.aarch64v8 => true,
else => @compileError("unsupported arch"), // NCCS can change
};
pub const pid_t = c_int;
pub const cc_t = u8;
pub const tcflag_t = c_uint;
pub const speed_t = c_uint;
const NCCS = 32;
pub const Termios = packed struct {
c_iflag: tcflag_t,
c_oflag: tcflag_t,
c_cflag: tcflag_t,
c_lflag: tcflag_t,
c_line: cc_t,
c_cc: [NCCS]cc_t,
__c_ispeed: speed_t,
__c_ospeed: speed_t,
};
pub fn cfgetospeed(tio: *const Termios) speed_t {
return tio.c_cflag & speed_t(CBAUD);
}
pub fn cfgetispeed(tio: *const Termios) speed_t {
return cfgetospeed(tio);
}
pub fn cfmakeraw(tio: *Termios) void {
tio.c_iflag &= ~tcflag_t(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tio.c_oflag &= ~tcflag_t(OPOST);
tio.c_lflag &= ~tcflag_t(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tio.c_cflag &= ~tcflag_t(CSIZE | PARENB);
tio.c_cflag |= tcflag_t(CS8);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
}
pub fn cfsetospeed(tio: *Termios, speed: speed_t) !void {
if (speed & ~speed_t(CBAUD) != 0) {
return error.UnexpectedBits;
}
tio.c_cflag &= ~speed_t(CBAUD);
tio.c_cflag |= speed;
}
pub fn cfsetispeed(tio: *Termios, speed: speed_t) !void {
if (speed != 0) return try cfsetospeed(tio, speed);
}
//TODO: weak linkage?
pub const cfsetspeed = cfsetospeed;
pub fn tcdrain(fd: i32) !void {
const rc = linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(fd)), linux.TCSBRK, 1);
const err = os.posix.getErrno(rc);
return switch (err) {
0 => {},
else => error.TCSBRK_Failed,
};
}
pub fn tcflow(fd: i32, action: i32) !void {
const rc = linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(fd)), linux.TCXONC, @bitCast(usize, isize(action)));
const err = os.posix.getErrno(rc);
return switch (err) {
0 => {},
else => error.TCXONC_Failed,
};
}
pub fn tcflush(fd: i32, queue: i32) !void {
_ = linux.ioctl(fd, linux.TCFLSH, queue);
}
pub fn tcgetattr(fd: i32, tio: *Termios) !void {
const tio_usize = @ptrToInt(tio);
_ = linux.ioctl(fd, linux.TCGETS, tio_usize);
}
pub fn tcgetsid(fd: i32) !pid_t {
var sid: pid_t = undefined;
const sid_usize = @ptrToInt(&sid);
const rc = linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(fd)), linux.TIOCGSID, sid_usize);
const err = os.posix.getErrno(rc);
return switch (err) {
0 => sid,
else => error.TIOCGSID_Failed,
};
}
pub fn tcsendbreak(fd: i32, dur: i32) !void {
// ignore dur, implementation defined, use 0 instead
const rc = linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(fd)), linux.TCSBRK, 0);
const err = os.posix.getErrno(rc);
return switch (err) {
0 => {},
else => error.TCSBRK_Failed,
};
}
pub fn tcsetattr(fd: i32, act: u32, tio: *const Termios) void {
const tio_usize = @ptrToInt(tio);
_ = linux.ioctl(fd, (linux.TCSETS + act), tio_usize);
return;
}
pub const VINTR = 0;
pub const VQUIT = 1;
pub const VERASE = 2;
pub const VKILL = 3;
pub const VEOF = 4;
pub const VTIME = 5;
pub const VMIN = 6;
pub const VSWTC = 7;
pub const VSTART = 8;
pub const VSTOP = 9;
pub const VSUSP = 10;
pub const VEOL = 11;
pub const VREPRINT = 12;
pub const VDISCARD = 13;
pub const VWERASE = 14;
pub const VLNEXT = 15;
pub const VEOL2 = 16;
pub const IGNBRK = 0o000001;
pub const BRKINT = 0o000002;
pub const IGNPAR = 0o000004;
pub const PARMRK = 0o000010;
pub const INPCK = 0o000020;
pub const ISTRIP = 0o000040;
pub const INLCR = 0o000100;
pub const IGNCR = 0o000200;
pub const ICRNL = 0o000400;
pub const IUCLC = 0o001000;
pub const IXON = 0o002000;
pub const IXANY = 0o004000;
pub const IXOFF = 0o010000;
pub const IMAXBEL = 0o020000;
pub const IUTF8 = 0o040000;
pub const OPOST = 0o000001;
pub const OLCUC = 0o000002;
pub const ONLCR = 0o000004;
pub const OCRNL = 0o000010;
pub const ONOCR = 0o000020;
pub const ONLRET = 0o000040;
pub const OFILL = 0o000100;
pub const OFDEL = 0o000200;
pub const NLDLY = 0o000400;
pub const NL0 = 0o000000;
pub const NL1 = 0o000400;
pub const CRDLY = 0o003000;
pub const CR0 = 0o000000;
pub const CR1 = 0o001000;
pub const CR2 = 0o002000;
pub const CR3 = 0o003000;
pub const TABDLY = 0o014000;
pub const TAB0 = 0o000000;
pub const TAB1 = 0o004000;
pub const TAB2 = 0o010000;
pub const TAB3 = 0o014000;
pub const BSDLY = 0o020000;
pub const BS0 = 0o000000;
pub const BS1 = 0o020000;
pub const FFDLY = 0o100000;
pub const FF0 = 0o000000;
pub const FF1 = 0o100000;
pub const VTDLY = 0o040000;
pub const VT0 = 0o000000;
pub const VT1 = 0o040000;
pub const B0 = 0o000000;
pub const B50 = 0o000001;
pub const B75 = 0o000002;
pub const B110 = 0o000003;
pub const B134 = 0o000004;
pub const B150 = 0o000005;
pub const B200 = 0o000006;
pub const B300 = 0o000007;
pub const B600 = 0o000010;
pub const B1200 = 0o000011;
pub const B1800 = 0o000012;
pub const B2400 = 0o000013;
pub const B4800 = 0o000014;
pub const B9600 = 0o000015;
pub const B19200 = 0o000016;
pub const B38400 = 0o000017;
pub const B57600 = 0o010001;
pub const B115200 = 0o010002;
pub const B230400 = 0o010003;
pub const B460800 = 0o010004;
pub const B500000 = 0o010005;
pub const B576000 = 0o010006;
pub const B921600 = 0o010007;
pub const B1000000 = 0o010010;
pub const B1152000 = 0o010011;
pub const B1500000 = 0o010012;
pub const B2000000 = 0o010013;
pub const B2500000 = 0o010014;
pub const B3000000 = 0o010015;
pub const B3500000 = 0o010016;
pub const B4000000 = 0o010017;
pub const CSIZE = 0o000060;
pub const CS5 = 0o000000;
pub const CS6 = 0o000020;
pub const CS7 = 0o000040;
pub const CS8 = 0o000060;
pub const CSTOPB = 0o000100;
pub const CREAD = 0o000200;
pub const PARENB = 0o000400;
pub const PARODD = 0o001000;
pub const HUPCL = 0o002000;
pub const CLOCAL = 0o004000;
pub const ISIG = 0o000001;
pub const ICANON = 0o000002;
pub const ECHO = 0o000010;
pub const ECHOE = 0o000020;
pub const ECHOK = 0o000040;
pub const ECHONL = 0o000100;
pub const NOFLSH = 0o000200;
pub const TOSTOP = 0o000400;
pub const IEXTEN = 0o100000;
pub const TCOOFF = 0;
pub const TCOON = 1;
pub const TCIOFF = 2;
pub const TCION = 3;
pub const TCIFLUSH = 0;
pub const TCOFLUSH = 1;
pub const TCIOFLUSH = 2;
pub const TCSANOW = 0;
pub const TCSADRAIN = 1;
pub const TCSAFLUSH = 2;
pub const EXTA = 0o000016;
pub const EXTB = 0o000017;
pub const CBAUD = 0o010017;
pub const CBAUDEX = 0o010000;
pub const CIBAUD = 0o02003600000;
pub const CMSPAR = 0o10000000000;
pub const CRTSCTS = 0o20000000000;
pub const XCASE = 0o000004;
pub const ECHOCTL = 0o001000;
pub const ECHOPRT = 0o002000;
pub const ECHOKE = 0o004000;
pub const FLUSHO = 0o010000;
pub const PENDIN = 0o040000;
pub const EXTPROC = 0o200000;
pub const XTABS = 0o014000;

View file

@ -9,6 +9,12 @@
},
{
"path": "/home/kitteh/zig/lib/std"
},
{
"path": "/home/kitteh/KittehSublimeTheme"
},
{
"path": "/home/kitteh/.config/sublime-text-3/Packages/Zig Language"
}
],
"settings":

View file

@ -88,6 +88,10 @@
"last_filter": "",
"selected_items":
[
[
"UI",
"UI: Select Theme"
],
[
"lsp",
"LSP: Disable Language Server in Project"
@ -179,25 +183,33 @@
},
"file_history":
[
"/home/kitteh/zar/src/widgets/time/time.zig",
"/home/kitteh/zar/src/widgets/memory/memory.zig",
"/home/kitteh/KittehSublimeTheme/Kitteh.sublime-theme",
"/home/kitteh/zar/src/bar/terminalbar.zig",
"/home/kitteh/zar/src/types/loopingcounter.zig",
"/home/kitteh/zig/lib/std/hash_map.zig",
"/home/kitteh/zig/lib/std/time.zig",
"/home/kitteh/zar/src/main.zig",
"/home/kitteh/zar/src/formatting/colour.zig",
"/home/kitteh/bar/memory.go",
"/home/kitteh/zar/src/bar/bar.zig",
"/home/kitteh/zar/src/widgets/weather/weather.zig",
"/home/kitteh/zar/src/widgets/battery/battery.zig",
"/home/kitteh/zar/src/widgets/text/text.zig",
"/home/kitteh/zar/src/widgets/time/time.zig",
"/home/kitteh/zar/src/widgets/memory/memory.zig",
"/home/kitteh/zar/src/types/mouseevent.zig",
"/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std/std.zig",
"/home/kitteh/zar/src/debug_allocator.zig",
"/home/kitteh/zar/src/bar/bar.zig",
"/home/kitteh/bar/battery.go",
"/home/kitteh/zar/build.zig",
"/home/kitteh/zar/deps/time/src/time.zig",
"/home/kitteh/bar/time.go",
"/home/kitteh/zar/src/widgets/weather/weather.zig",
"/home/kitteh/bar/main.go",
"/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std/valgrind/memcheck.zig",
"/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std/progress.zig",
"/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std/mutex.zig",
"/home/kitteh/zig-linux-x86_64-0.6.0+485231dea/lib/zig/std/meta.zig",
"/home/kitteh/zar/src/types/info.zig",
"/home/kitteh/zar/src/widgets/text/text.zig",
"/home/kitteh/bar/bar.go",
"/home/kitteh/zar/src/types/bar.zig",
"/home/kitteh/zar/src/types/widget.zig",
@ -220,9 +232,13 @@
},
"find_state":
{
"case_sensitive": false,
"case_sensitive": true,
"find_history":
[
"#fe4450",
"keyword",
"Bar",
"osICANON",
".mutex",
"else return",
") return",
@ -283,6 +299,7 @@
"regex": false,
"replace_history":
[
"TerminalBar",
".items_mutex",
"else",
")",
@ -303,7 +320,7 @@
},
"incremental_find":
{
"height": 28.0
"height": 54.0
},
"input":
{
@ -351,7 +368,7 @@
"pinned_build_system": "",
"replace":
{
"height": 52.0
"height": 94.0
},
"save_all_on_build": false,
"select_file":