地圖 (Jido) is a lightweight Unix TUI file explorer designed for speed and simplicity.
at main 1.2 kB view raw
1const std = @import("std"); 2const vaxis = @import("vaxis"); 3const Event = @import("app.zig").Event; 4 5const FileLogger = @import("file_logger.zig"); 6 7const Self = @This(); 8 9/// Seconds. 10pub const notification_timeout = 3; 11 12const Style = enum { 13 err, 14 info, 15 warn, 16}; 17 18var buf: [1024]u8 = undefined; 19 20style: Style = Style.info, 21fbs: std.io.FixedBufferStream([]u8) = std.io.fixedBufferStream(&buf), 22/// How long until the notification disappears in seconds. 23timer: i64 = 0, 24loop: ?*vaxis.Loop(Event) = null, 25 26pub fn write(self: *Self, text: []const u8, style: Style) !void { 27 self.fbs.reset(); 28 _ = try self.fbs.write(text); 29 self.timer = std.time.timestamp(); 30 self.style = style; 31 32 if (self.loop) |loop| { 33 loop.postEvent(.notification); 34 } 35} 36 37pub fn reset(self: *Self) void { 38 self.fbs.reset(); 39 self.style = Style.info; 40} 41 42pub fn slice(self: *Self) []const u8 { 43 return self.fbs.getWritten(); 44} 45 46pub fn clearIfEnded(self: *Self) bool { 47 if (std.time.timestamp() - self.timer > notification_timeout) { 48 self.reset(); 49 return true; 50 } 51 52 return false; 53} 54 55pub fn len(self: Self) usize { 56 return self.fbs.pos; 57}