From 02ba05c500670be817f8f03c31130e20154f05ce Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 11 Feb 2025 11:31:16 +0100 Subject: [PATCH 1/3] feat(buffer): restore previous file type when switching buffers --- src/buffer/Buffer.zig | 4 ++++ src/tui/editor.zig | 41 +++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index ac3e7db..7a38ede 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -50,6 +50,10 @@ curr_history: ?*UndoNode = null, mtime: i64, utime: i64, +file_type_name: ?[]const u8 = null, +file_type_icon: ?[]const u8 = null, +file_type_color: ?u24 = null, + pub const EolMode = enum { lf, crlf }; pub const EolModeTag = @typeInfo(EolMode).Enum.tag_type; diff --git a/src/tui/editor.zig b/src/tui/editor.zig index ba15cb3..3d4780e 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -541,13 +541,14 @@ pub const Editor = struct { return self.open_buffer(file_path, try self.buffer_manager.open_scratch(file_path, content), file_type); } - fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type: ?[]const u8) !void { + fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type_: ?[]const u8) !void { errdefer self.buffer_manager.retire(new_buf, null); self.cancel_all_selections(); self.get_primary().reset(); self.file_path = try self.allocator.dupe(u8, file_path); if (self.buffer) |_| try self.close(); self.buffer = new_buf; + const file_type = file_type_ orelse new_buf.file_type_name; if (new_buf.root.lines() > root_mod.max_syntax_lines) { self.logger.print("large file threshold {d} lines < file size {d} lines", .{ @@ -583,6 +584,11 @@ pub const Editor = struct { const ftn = if (self.syntax) |syn| syn.file_type.name else "text"; const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹"; const ftc = if (self.syntax) |syn| syn.file_type.color else 0x000000; + if (self.buffer) |buffer| { + buffer.file_type_name = ftn; + buffer.file_type_icon = fti; + buffer.file_type_color = ftc; + } if (self.buffer) |buffer| if (buffer.get_meta()) |meta| try self.extract_state(meta, .none); @@ -1949,12 +1955,12 @@ pub const Editor = struct { } fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { - if(is_white_space_at_cursor(root, cursor, metrics)) return false; + if (is_white_space_at_cursor(root, cursor, metrics)) return false; var next = cursor.*; next.move_left(root, metrics) catch return true; const next_is_white_space = is_white_space_at_cursor(root, &next, metrics); - if(next_is_white_space) return true; + if (next_is_white_space) return true; const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics); const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics); @@ -1987,12 +1993,12 @@ pub const Editor = struct { } fn is_word_boundary_right_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { - if(is_white_space_at_cursor(root, cursor, metrics)) return false; + if (is_white_space_at_cursor(root, cursor, metrics)) return false; var next = cursor.*; next.move_right(root, metrics) catch return true; const next_is_white_space = is_white_space_at_cursor(root, &next, metrics); - if(next_is_white_space) return true; + if (next_is_white_space) return true; const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics); const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics); @@ -2427,7 +2433,7 @@ pub const Editor = struct { if (all_stop) return error.Stop; - return .{text.items, root}; + return .{ text.items, root }; } pub fn cut_internal_vim(self: *Self, _: Context) Result { @@ -2458,7 +2464,7 @@ pub const Editor = struct { self.clamp(); } pub const cut_internal_vim_meta = .{ .description = "Cut selection or current line to internal clipboard (vim)" }; - + pub fn cut(self: *Self, _: Context) Result { const primary = self.get_primary(); const b = self.buf_for_update() catch return; @@ -2632,10 +2638,10 @@ pub const Editor = struct { const b = try self.buf_for_update(); var root = b.root; - if(std.mem.eql(u8, text[text.len-1..], "\n")) text = text[0..text.len-1]; + if (std.mem.eql(u8, text[text.len - 1 ..], "\n")) text = text[0 .. text.len - 1]; if (std.mem.indexOfScalar(u8, text, '\n')) |idx| { - if(idx == 0) { + if (idx == 0) { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { try move_cursor_end(root, &cursel.cursor, self.metrics); root = try self.insert(root, cursel, "\n", b.allocator); @@ -2643,8 +2649,8 @@ pub const Editor = struct { text = text[1..]; } if (self.cursels.items.len == 1) { - const primary = self.get_primary(); - root = try self.insert_line_vim(root, primary, text, b.allocator); + const primary = self.get_primary(); + root = try self.insert_line_vim(root, primary, text, b.allocator); } else { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { root = try self.insert_line_vim(root, cursel, text, b.allocator); @@ -2652,15 +2658,15 @@ pub const Editor = struct { } } else { if (self.cursels.items.len == 1) { - const primary = self.get_primary(); - root = try self.insert(root, primary, text, b.allocator); + const primary = self.get_primary(); + root = try self.insert(root, primary, text, b.allocator); } else { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { root = try self.insert(root, cursel, text, b.allocator); }; } } - + try self.update_buf(root); self.clamp(); self.need_render(); @@ -2677,7 +2683,7 @@ pub const Editor = struct { pub fn cut_forward_internal(self: *Self, _: Context) Result { const b = try self.buf_for_update(); - const text, const root= try self.cut_to(move_cursor_right, b.root); + const text, const root = try self.cut_to(move_cursor_right, b.root); self.set_clipboard_internal(text); try self.update_buf(root); self.clamp(); @@ -2702,7 +2708,7 @@ pub const Editor = struct { pub fn cut_word_left_vim(self: *Self, _: Context) Result { const b = try self.buf_for_update(); - const text, const root= try self.cut_to(move_cursor_word_left_vim, b.root); + const text, const root = try self.cut_to(move_cursor_word_left_vim, b.root); self.set_clipboard_internal(text); try self.update_buf(root); self.clamp(); @@ -2719,7 +2725,7 @@ pub const Editor = struct { pub fn cut_word_right_vim(self: *Self, _: Context) Result { const b = try self.buf_for_update(); - const text, const root= try self.cut_to(move_cursor_word_right_vim, b.root); + const text, const root = try self.cut_to(move_cursor_word_right_vim, b.root); self.set_clipboard_internal(text); try self.update_buf(root); self.clamp(); @@ -2870,7 +2876,6 @@ pub const Editor = struct { } pub const move_word_left_vim_meta = .{ .description = "Move cursor left by word (vim)" }; - pub fn move_word_right(self: *Self, _: Context) Result { const root = try self.buf_root(); self.with_cursors_const(root, move_cursor_word_right) catch {}; From d305e7844dea15e42d2af8a15c174cfd9c34de05 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 11 Feb 2025 11:49:20 +0100 Subject: [PATCH 2/3] feat(buffer): restore eol_mode in undo/redo/reset operations --- src/buffer/Buffer.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index 7a38ede..d2e1785 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -62,6 +62,7 @@ const UndoNode = struct { next: ?*UndoNode = null, branches: ?*UndoBranch = null, meta: []const u8, + file_eol_mode: EolMode, }; const UndoBranch = struct { @@ -1248,6 +1249,7 @@ pub fn reset_to_last_saved(self: *Self) void { if (self.last_save) |last_save| { self.store_undo(&[_]u8{}) catch {}; self.root = last_save; + self.file_eol_mode = self.last_save_eol_mode; self.mtime = std.time.milliTimestamp(); } } @@ -1385,6 +1387,7 @@ fn create_undo(self: *const Self, root: Root, meta_: []const u8) error{OutOfMemo h.* = UndoNode{ .root = root, .meta = meta, + .file_eol_mode = self.file_eol_mode, }; return h; } @@ -1420,6 +1423,7 @@ pub fn undo(self: *Self, meta: []const u8) error{Stop}![]const u8 { self.undo_history = h.next; self.curr_history = h; self.root = h.root; + self.file_eol_mode = h.file_eol_mode; self.push_redo(r); self.mtime = std.time.milliTimestamp(); return h.meta; @@ -1432,6 +1436,7 @@ pub fn redo(self: *Self) error{Stop}![]const u8 { self.redo_history = h.next; self.curr_history = h; self.root = h.root; + self.file_eol_mode = h.file_eol_mode; self.push_undo(u); self.mtime = std.time.milliTimestamp(); return h.meta; From 1b03f782136773f2e2cbdfa51a72d5b8b49cc5ee Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 11 Feb 2025 13:29:52 +0100 Subject: [PATCH 3/3] feat(buffer): show file icons in buffer palette --- src/tui/mode/overlay/buffer_palette.zig | 64 ++++++++++++++++++++-- src/tui/mode/overlay/file_type_palette.zig | 23 +------- src/tui/mode/overlay/palette.zig | 10 +--- src/tui/tui.zig | 19 +++++++ 4 files changed, 81 insertions(+), 35 deletions(-) diff --git a/src/tui/mode/overlay/buffer_palette.zig b/src/tui/mode/overlay/buffer_palette.zig index ea3a6e4..f9814dd 100644 --- a/src/tui/mode/overlay/buffer_palette.zig +++ b/src/tui/mode/overlay/buffer_palette.zig @@ -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; diff --git a/src/tui/mode/overlay/file_type_palette.zig b/src/tui/mode/overlay/file_type_palette.zig index f09df23..9a065e5 100644 --- a/src/tui/mode/overlay/file_type_palette.zig +++ b/src/tui/mode/overlay/file_type_palette.zig @@ -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; diff --git a/src/tui/mode/overlay/palette.zig b/src/tui/mode/overlay/palette.zig index b152452..9366c4a 100644 --- a/src/tui/mode/overlay/palette.zig +++ b/src/tui/mode/overlay/palette.zig @@ -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 {}; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 6eafaad..888015f 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1289,3 +1289,22 @@ pub fn message(comptime fmt: anytype, args: anytype) void { var buf: [256]u8 = undefined; tp.self_pid().send(.{ "message", std.fmt.bufPrint(&buf, fmt, args) catch @panic("too large") }) catch {}; } + +pub fn render_file_icon(self: *renderer.Plane, icon: []const u8, color: u24) void { var cell = self.cell_init(); + _ = self.at_cursor_cell(&cell) catch return; + if (!(color == 0xFFFFFF or color == 0x000000 or color == 0x000001)) { + cell.set_fg_rgb(@intCast(color)) catch {}; + } + _ = self.cell_load(&cell, icon) catch {}; + _ = self.putc(&cell) catch {}; + self.cursor_move_rel(0, 1) catch {}; +} + +pub fn render_match_cell(self: *renderer.Plane, y: usize, x: usize, theme_: *const Widget.Theme) !void { + self.cursor_move_yx(@intCast(y), @intCast(x)) catch return; + var cell = self.cell_init(); + _ = self.at_cursor_cell(&cell) catch return; + cell.set_style(theme_.editor_match); + _ = self.putc(&cell) catch {}; +} +