feat: improve styling of minilog and logview
This commit is contained in:
		
							parent
							
								
									b56290d640
								
							
						
					
					
						commit
						54ee8b437b
					
				
					 2 changed files with 36 additions and 17 deletions
				
			
		| 
						 | 
				
			
			@ -30,9 +30,15 @@ const Entry = struct {
 | 
			
		|||
    msg: []u8,
 | 
			
		||||
    time: i64,
 | 
			
		||||
    tdiff: i64,
 | 
			
		||||
    level: Level,
 | 
			
		||||
};
 | 
			
		||||
const Buffer = ArrayList(Entry);
 | 
			
		||||
 | 
			
		||||
const Level = enum {
 | 
			
		||||
    info,
 | 
			
		||||
    err,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub fn create(a: Allocator, parent: Plane) !Widget {
 | 
			
		||||
    const self: *Self = try a.create(Self);
 | 
			
		||||
    self.* = .{ .plane = try Plane.init(&(Widget.Box{}).opts_vscroll(name), parent) };
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +51,10 @@ pub fn deinit(self: *Self, a: Allocator) void {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
 | 
			
		||||
    self.plane.set_base_style(" ", theme.panel);
 | 
			
		||||
    const style_normal = theme.panel;
 | 
			
		||||
    const style_info: Widget.Theme.Style = .{ .fg = theme.editor_information.fg, .fs = theme.editor_information.fs };
 | 
			
		||||
    const style_error: Widget.Theme.Style = .{ .fg = theme.editor_error.fg, .fs = theme.editor_error.fs };
 | 
			
		||||
    self.plane.set_base_style(" ", style_normal);
 | 
			
		||||
    self.plane.erase();
 | 
			
		||||
    self.plane.home();
 | 
			
		||||
    const height = self.plane.dim_y();
 | 
			
		||||
| 
						 | 
				
			
			@ -56,7 +65,9 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
 | 
			
		|||
    for (buffer.items[begin_at..]) |item| {
 | 
			
		||||
        if (first) first = false else _ = self.plane.putstr("\n") catch return false;
 | 
			
		||||
        self.output_tdiff(item.tdiff) catch return false;
 | 
			
		||||
        self.plane.set_style(if (item.level == .err) style_error else style_info);
 | 
			
		||||
        _ = self.plane.print("{s}: {s}", .{ escape(item.src), escape(item.msg) }) catch {};
 | 
			
		||||
        self.plane.set_style(style_normal);
 | 
			
		||||
    }
 | 
			
		||||
    if (last_count > 0)
 | 
			
		||||
        _ = self.plane.print(" ({})", .{last_count}) catch {};
 | 
			
		||||
| 
						 | 
				
			
			@ -81,7 +92,7 @@ pub fn process_log(m: tp.message) !void {
 | 
			
		|||
    var msg: []const u8 = undefined;
 | 
			
		||||
    const buffer = get_buffer();
 | 
			
		||||
    if (try m.match(.{ "log", tp.extract(&src), tp.extract(&msg) })) {
 | 
			
		||||
        try append(buffer, src, msg);
 | 
			
		||||
        try append(buffer, src, msg, .info);
 | 
			
		||||
    } else if (try m.match(.{ "log", "error", tp.extract(&src), tp.extract(&context), "->", tp.extract(&msg) })) {
 | 
			
		||||
        try append_error(buffer, src, context, msg);
 | 
			
		||||
    } else if (try m.match(.{ "log", tp.extract(&src), tp.more })) {
 | 
			
		||||
| 
						 | 
				
			
			@ -89,7 +100,7 @@ pub fn process_log(m: tp.message) !void {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn append(buffer: *Buffer, src: []const u8, msg: []const u8) !void {
 | 
			
		||||
fn append(buffer: *Buffer, src: []const u8, msg: []const u8, level: Level) !void {
 | 
			
		||||
    const ts = time.microTimestamp();
 | 
			
		||||
    const tdiff = if (buffer.getLastOrNull()) |last| ret: {
 | 
			
		||||
        if (eql(u8, msg, last.src) and eql(u8, msg, last.msg)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -104,19 +115,20 @@ fn append(buffer: *Buffer, src: []const u8, msg: []const u8) !void {
 | 
			
		|||
        .tdiff = tdiff,
 | 
			
		||||
        .src = try buffer.allocator.dupeZ(u8, src),
 | 
			
		||||
        .msg = try buffer.allocator.dupeZ(u8, msg),
 | 
			
		||||
        .level = level,
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn append_error(buffer: *Buffer, src: []const u8, context: []const u8, msg_: []const u8) !void {
 | 
			
		||||
    var buf: [4096]u8 = undefined;
 | 
			
		||||
    const msg = try fmt.bufPrint(&buf, "error in {s}: {s}", .{ context, msg_ });
 | 
			
		||||
    try append(buffer, src, msg);
 | 
			
		||||
    try append(buffer, src, msg, .err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn append_json(buffer: *Buffer, src: []const u8, m: tp.message) !void {
 | 
			
		||||
    var buf: [4096]u8 = undefined;
 | 
			
		||||
    const json = try m.to_json(&buf);
 | 
			
		||||
    try append(buffer, src, json);
 | 
			
		||||
    try append(buffer, src, json, .err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn get_buffer() *Buffer {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,13 +13,18 @@ const logview = @import("../logview.zig");
 | 
			
		|||
parent: Plane,
 | 
			
		||||
plane: Plane,
 | 
			
		||||
msg: std.ArrayList(u8),
 | 
			
		||||
is_error: bool = false,
 | 
			
		||||
clear_time: i64 = 0,
 | 
			
		||||
level: Level = .info,
 | 
			
		||||
 | 
			
		||||
const message_display_time_seconds = 2;
 | 
			
		||||
const error_display_time_seconds = 4;
 | 
			
		||||
const Self = @This();
 | 
			
		||||
 | 
			
		||||
const Level = enum {
 | 
			
		||||
    info,
 | 
			
		||||
    err,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub fn create(a: std.mem.Allocator, parent: Plane) !Widget {
 | 
			
		||||
    const self: *Self = try a.create(Self);
 | 
			
		||||
    self.* = .{
 | 
			
		||||
| 
						 | 
				
			
			@ -46,11 +51,13 @@ pub fn layout(self: *Self) Widget.Layout {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
 | 
			
		||||
    self.plane.set_base_style(" ", if (self.msg.items.len > 0) theme.sidebar else theme.statusbar);
 | 
			
		||||
    const style_normal = theme.statusbar;
 | 
			
		||||
    const style_info: Widget.Theme.Style = .{ .fg = theme.statusbar.fg, .fs = theme.editor_information.fs };
 | 
			
		||||
    const style_error: Widget.Theme.Style = .{ .fg = theme.editor_error.fg, .fs = theme.editor_error.fs };
 | 
			
		||||
    self.plane.set_base_style(" ", style_normal);
 | 
			
		||||
    self.plane.erase();
 | 
			
		||||
    self.plane.home();
 | 
			
		||||
    if (self.is_error)
 | 
			
		||||
        self.plane.set_base_style(" ", theme.editor_error);
 | 
			
		||||
    self.plane.set_style(if (self.level == .err) style_error else style_info);
 | 
			
		||||
    _ = self.plane.print(" {s} ", .{self.msg.items}) catch {};
 | 
			
		||||
 | 
			
		||||
    const curr_time = std.time.milliTimestamp();
 | 
			
		||||
| 
						 | 
				
			
			@ -75,14 +82,13 @@ fn process_log(self: *Self, m: tp.message) !void {
 | 
			
		|||
    var context: []const u8 = undefined;
 | 
			
		||||
    var msg: []const u8 = undefined;
 | 
			
		||||
    if (try m.match(.{ "log", tp.extract(&src), tp.extract(&msg) })) {
 | 
			
		||||
        if (self.is_error) return;
 | 
			
		||||
        try self.set(msg, false);
 | 
			
		||||
        try self.set(msg, .info);
 | 
			
		||||
    } else if (try m.match(.{ "log", "error", tp.extract(&src), tp.extract(&context), "->", tp.extract(&msg) })) {
 | 
			
		||||
        if (std.mem.eql(u8, msg, "error.Stop"))
 | 
			
		||||
            return;
 | 
			
		||||
        try self.set(msg, true);
 | 
			
		||||
        try self.set(msg, .err);
 | 
			
		||||
    } else if (try m.match(.{ "log", tp.extract(&src), tp.more })) {
 | 
			
		||||
        self.is_error = true;
 | 
			
		||||
        self.level = .err;
 | 
			
		||||
        var s = std.json.writeStream(self.msg.writer(), .{});
 | 
			
		||||
        var iter: []const u8 = m.buf;
 | 
			
		||||
        try @import("cbor").JsonStream(@TypeOf(self.msg)).jsonWriteValue(&s, &iter);
 | 
			
		||||
| 
						 | 
				
			
			@ -92,20 +98,21 @@ fn process_log(self: *Self, m: tp.message) !void {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
fn update_clear_time(self: *Self) void {
 | 
			
		||||
    const delay: i64 = std.time.ms_per_s * @as(i64, if (self.is_error) error_display_time_seconds else message_display_time_seconds);
 | 
			
		||||
    const delay: i64 = std.time.ms_per_s * @as(i64, if (self.level == .err) error_display_time_seconds else message_display_time_seconds);
 | 
			
		||||
    self.clear_time = std.time.milliTimestamp() + delay;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn set(self: *Self, msg: []const u8, is_error: bool) !void {
 | 
			
		||||
fn set(self: *Self, msg: []const u8, level: Level) !void {
 | 
			
		||||
    if (@intFromEnum(level) < @intFromEnum(self.level)) return;
 | 
			
		||||
    self.msg.clearRetainingCapacity();
 | 
			
		||||
    try self.msg.appendSlice(msg);
 | 
			
		||||
    self.is_error = is_error;
 | 
			
		||||
    self.level = level;
 | 
			
		||||
    self.update_clear_time();
 | 
			
		||||
    Widget.need_render();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn clear(self: *Self) void {
 | 
			
		||||
    self.is_error = false;
 | 
			
		||||
    self.level = .info;
 | 
			
		||||
    self.msg.clearRetainingCapacity();
 | 
			
		||||
    self.clear_time = 0;
 | 
			
		||||
    Widget.need_render();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue