feat(buffer): show file icons in buffer palette
This commit is contained in:
		
							parent
							
								
									d305e7844d
								
							
						
					
					
						commit
						1b03f78213
					
				
					 4 changed files with 81 additions and 35 deletions
				
			
		| 
						 | 
				
			
			@ -7,6 +7,7 @@ const command = @import("command");
 | 
			
		|||
const tui = @import("../../tui.zig");
 | 
			
		||||
pub const Type = @import("palette.zig").Create(@This());
 | 
			
		||||
const module_name = @typeName(@This());
 | 
			
		||||
const Widget = @import("../../Widget.zig");
 | 
			
		||||
 | 
			
		||||
pub const label = "Switch buffers";
 | 
			
		||||
pub const name = " buffer";
 | 
			
		||||
| 
						 | 
				
			
			@ -16,7 +17,9 @@ const hidden_indicator = "-";
 | 
			
		|||
 | 
			
		||||
pub const Entry = struct {
 | 
			
		||||
    label: []const u8,
 | 
			
		||||
    hint: []const u8,
 | 
			
		||||
    icon: []const u8,
 | 
			
		||||
    color: ?u24,
 | 
			
		||||
    indicator: []const u8,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub fn load_entries(palette: *Type) !usize {
 | 
			
		||||
| 
						 | 
				
			
			@ -24,15 +27,20 @@ pub fn load_entries(palette: *Type) !usize {
 | 
			
		|||
    const buffers = try buffer_manager.list_most_recently_used(palette.allocator);
 | 
			
		||||
    defer palette.allocator.free(buffers);
 | 
			
		||||
    for (buffers) |buffer| {
 | 
			
		||||
        const hint = if (buffer.is_dirty())
 | 
			
		||||
        const indicator = if (buffer.is_dirty())
 | 
			
		||||
            dirty_indicator
 | 
			
		||||
        else if (buffer.is_hidden())
 | 
			
		||||
            hidden_indicator
 | 
			
		||||
        else
 | 
			
		||||
            "";
 | 
			
		||||
        (try palette.entries.addOne()).* = .{ .label = buffer.file_path, .hint = hint };
 | 
			
		||||
        (try palette.entries.addOne()).* = .{
 | 
			
		||||
            .label = buffer.file_path,
 | 
			
		||||
            .icon = buffer.file_type_icon orelse "",
 | 
			
		||||
            .color = buffer.file_type_color,
 | 
			
		||||
            .indicator = indicator,
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
    return if (palette.entries.items.len == 0) label.len else 2;
 | 
			
		||||
    return if (palette.entries.items.len == 0) label.len else 4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn clear_entries(palette: *Type) void {
 | 
			
		||||
| 
						 | 
				
			
			@ -44,12 +52,58 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
 | 
			
		|||
    defer value.deinit();
 | 
			
		||||
    const writer = value.writer();
 | 
			
		||||
    try cbor.writeValue(writer, entry.label);
 | 
			
		||||
    try cbor.writeValue(writer, entry.hint);
 | 
			
		||||
    try cbor.writeValue(writer, entry.icon);
 | 
			
		||||
    try cbor.writeValue(writer, entry.color);
 | 
			
		||||
    try cbor.writeValue(writer, entry.indicator);
 | 
			
		||||
    try cbor.writeValue(writer, matches orelse &[_]usize{});
 | 
			
		||||
    try palette.menu.add_item_with_handler(value.items, select);
 | 
			
		||||
    palette.items += 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.Theme, selected: bool) bool {
 | 
			
		||||
    const style_base = theme.editor_widget;
 | 
			
		||||
    const style_label = if (button.active) theme.editor_cursor else if (button.hover or selected) theme.editor_selection else theme.editor_widget;
 | 
			
		||||
    const style_hint = if (tui.find_scope_style(theme, "entity.name")) |sty| sty.style else style_label;
 | 
			
		||||
    button.plane.set_base_style(style_base);
 | 
			
		||||
    button.plane.erase();
 | 
			
		||||
    button.plane.home();
 | 
			
		||||
    button.plane.set_style(style_label);
 | 
			
		||||
    if (button.active or button.hover or selected) {
 | 
			
		||||
        button.plane.fill(" ");
 | 
			
		||||
        button.plane.home();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    button.plane.set_style(style_hint);
 | 
			
		||||
    const pointer = if (selected) "⏵" else " ";
 | 
			
		||||
    _ = button.plane.print("{s}", .{pointer}) catch {};
 | 
			
		||||
 | 
			
		||||
    var iter = button.opts.label;
 | 
			
		||||
    var file_path_: []const u8 = undefined;
 | 
			
		||||
    var icon: []const u8 = undefined;
 | 
			
		||||
    var color: u24 = undefined;
 | 
			
		||||
    if (!(cbor.matchString(&iter, &file_path_) catch false)) @panic("invalid buffer file path");
 | 
			
		||||
    if (!(cbor.matchString(&iter, &icon) catch false)) @panic("invalid buffer file type icon");
 | 
			
		||||
    if (!(cbor.matchInt(u24, &iter, &color) catch false)) @panic("invalid buffer file type color");
 | 
			
		||||
    tui.render_file_icon(&button.plane, icon, color);
 | 
			
		||||
    button.plane.set_style(style_label);
 | 
			
		||||
    _ = button.plane.print(" {s} ", .{file_path_}) catch {};
 | 
			
		||||
 | 
			
		||||
    var indicator: []const u8 = undefined;
 | 
			
		||||
    if (!(cbor.matchString(&iter, &indicator) catch false))
 | 
			
		||||
        indicator = "";
 | 
			
		||||
    button.plane.set_style(style_hint);
 | 
			
		||||
    _ = button.plane.print_aligned_right(0, "{s} ", .{indicator}) catch {};
 | 
			
		||||
 | 
			
		||||
    var index: usize = 0;
 | 
			
		||||
    var len = cbor.decodeArrayHeader(&iter) catch return false;
 | 
			
		||||
    while (len > 0) : (len -= 1) {
 | 
			
		||||
        if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
 | 
			
		||||
            tui.render_match_cell(&button.plane, 0, index + 4, theme) catch break;
 | 
			
		||||
        } else break;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
 | 
			
		||||
    var file_path: []const u8 = undefined;
 | 
			
		||||
    var iter = button.opts.label;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -90,7 +90,7 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.
 | 
			
		|||
    if (!(cbor.matchString(&iter, &description_) catch false)) @panic("invalid file_type description");
 | 
			
		||||
    if (!(cbor.matchString(&iter, &icon) catch false)) @panic("invalid file_type icon");
 | 
			
		||||
    if (!(cbor.matchInt(u24, &iter, &color) catch false)) @panic("invalid file_type color");
 | 
			
		||||
    render_file_icon(button, icon, color);
 | 
			
		||||
    tui.render_file_icon(&button.plane, icon, color);
 | 
			
		||||
    button.plane.set_style(style_label);
 | 
			
		||||
    _ = button.plane.print(" {s} ", .{description_}) catch {};
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -104,31 +104,12 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.
 | 
			
		|||
    var len = cbor.decodeArrayHeader(&iter) catch return false;
 | 
			
		||||
    while (len > 0) : (len -= 1) {
 | 
			
		||||
        if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
 | 
			
		||||
            render_match_cell(button, 0, index + 4, theme) catch break;
 | 
			
		||||
            tui.render_match_cell(&button.plane, 0, index + 4, theme) catch break;
 | 
			
		||||
        } else break;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn render_match_cell(button: *Type.ButtonState, y: usize, x: usize, theme: *const Widget.Theme) !void {
 | 
			
		||||
    button.plane.cursor_move_yx(@intCast(y), @intCast(x)) catch return;
 | 
			
		||||
    var cell = button.plane.cell_init();
 | 
			
		||||
    _ = button.plane.at_cursor_cell(&cell) catch return;
 | 
			
		||||
    cell.set_style(theme.editor_match);
 | 
			
		||||
    _ = button.plane.putc(&cell) catch {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn render_file_icon(button: *Type.ButtonState, icon: []const u8, color: u24) void {
 | 
			
		||||
    var cell = button.plane.cell_init();
 | 
			
		||||
    _ = button.plane.at_cursor_cell(&cell) catch return;
 | 
			
		||||
    if (!(color == 0xFFFFFF or color == 0x000000 or color == 0x000001)) {
 | 
			
		||||
        cell.set_fg_rgb(@intCast(color)) catch {};
 | 
			
		||||
    }
 | 
			
		||||
    _ = button.plane.cell_load(&cell, icon) catch {};
 | 
			
		||||
    _ = button.plane.putc(&cell) catch {};
 | 
			
		||||
    button.plane.cursor_move_rel(0, 1) catch {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
 | 
			
		||||
    var description_: []const u8 = undefined;
 | 
			
		||||
    var icon: []const u8 = undefined;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -139,20 +139,12 @@ pub fn Create(options: type) type {
 | 
			
		|||
            var len = cbor.decodeArrayHeader(&iter) catch return false;
 | 
			
		||||
            while (len > 0) : (len -= 1) {
 | 
			
		||||
                if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
 | 
			
		||||
                    render_cell(&button.plane, 0, index + 1, theme.editor_match) catch break;
 | 
			
		||||
                    tui.render_match_cell(&button.plane, 0, index + 1, theme) catch break;
 | 
			
		||||
                } else break;
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fn render_cell(plane: *Plane, y: usize, x: usize, style: Widget.Theme.Style) !void {
 | 
			
		||||
            plane.cursor_move_yx(@intCast(y), @intCast(x)) catch return;
 | 
			
		||||
            var cell = plane.cell_init();
 | 
			
		||||
            _ = plane.at_cursor_cell(&cell) catch return;
 | 
			
		||||
            cell.set_style(style);
 | 
			
		||||
            _ = plane.putc(&cell) catch {};
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fn on_resize_menu(self: *Self, _: *Menu.State(*Self), _: Widget.Box) void {
 | 
			
		||||
            self.do_resize();
 | 
			
		||||
            // self.start_query(0) catch {};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue