diff options
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 92 |
1 files changed, 88 insertions, 4 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index fa1c3230..6c86cb98 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -47,9 +47,10 @@ const Key = pardes.Key; /// query, the files opened lately, the panes, the asking text's /// declarations, the lines holding the query in every open pane (All, its /// places in each), the jump list, rows handed over (a language server's -/// answer, SPC e's places), titled by their word, and the messages' -/// history (SPC h m). -pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list, messages }; +/// answer, SPC e's places), titled by their word, the messages' history +/// (SPC h m), and the themes (SPC t t, the window wearing the one under +/// the cursor). +pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list, messages, themes }; /// Each source's word in the tag and the leader keys of the builtin that /// opens it, its key cells (`␣ f Find`). @@ -63,6 +64,7 @@ const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 .jumplist = .{ .word = "Jumplist", .keys = "j" }, .list = .{ .word = "List", .keys = "l" }, .messages = .{ .word = "Messages", .keys = "hm" }, + .themes = .{ .word = "Themes", .keys = "t" }, }); /// The pickers kept after they roll up, for their list words and Resume. @@ -183,6 +185,8 @@ pub fn open(p: *Pardes, from: usize, source: Source, word: []const u8, query: [] unroll(p, pk, query); start(p, pk); filter(p, pk); + // The themes: the cursor on the one worn (their order, the field empty). + if (source == .themes and query.len == 0 and p.custom_theme == null) p.view().tag_state.rows.at = @intCast(p.settings.theme); } /// A new picker in the ring, the oldest kept one dropped for it. @@ -219,6 +223,13 @@ fn hostIn(p: *Pardes, pk: *Pick, from: usize) !void { const asker = p.panes[from].?; pk.owner = asker.serial; pk.made = false; + // A theme is no place: its list is the asking pane's own tag (§3.5), + // and its preview the whole window. + if (pk.source == .themes) { + pk.landing = asker.serial; + p.view().active = from; + return; + } const reader = if (Trail.reading(p, asker)) from else Trail.readerOf(p, asker); pk.landing = p.panes[reader orelse try landingFor(p, from)].?.serial; pk.made = reader == null; @@ -249,6 +260,8 @@ fn unroll(p: *Pardes, pk: *Pick, query: []const u8) void { s.rows = .{ .at = @intCast(@min(pk.at, std.math.maxInt(u16))) }; pk.unrolled = true; pk.shown = no_row; + // The whole window previews a theme: no pane dimmed (SPEC §3.6). + if (pk.source == .themes) s.previewing = .full; if (p.paneBySerial(pk.landing)) |id| { s.previewing.set(id); const pane = p.panes[id].?; @@ -286,6 +299,9 @@ fn start(p: *Pardes, pk: *Pick) void { Output.messagesText(p, &out.writer) catch return; addRows(p, pk, out.written()); }, + .themes => for (pardes.colors.themes, 0..) |t, i| { + pk.names.add(p.gpa, t.name, 0, t.name.len, 0, @intCast(i)) catch return; + }, } } @@ -383,6 +399,8 @@ pub fn declsArrived(p: *Pardes, serial: u32) void { pub fn produceAll(p: *Pardes, tick: bool) void { for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { if (pk.names.busy()) pk.names.rescore(p.gpa); + // A theme's preview in the step of the key that moved to it. + if (pk.source == .themes) pardes.colors.previewTheme(p, themeAt(p, pk)); if (tick) preview(p, pk); }; } @@ -397,6 +415,14 @@ pub fn busy(p: *const Pardes) bool { // ---- the rows ---- +/// The theme the cursor of a themes picker is on. +fn themeAt(p: *const Pardes, pk: *const Pick) ?usize { + const n = pk.names.hits.items.len; + if (n == 0) return null; + const at = @min(p.view().tag_state.rows.at, n - 1); + return pk.names.cands.items[pk.names.hits.items[at].cand].ref; +} + /// The rows the list has now. pub fn count(p: *const Pardes, pk: *const Pick) usize { if (fuzzy(pk)) return pk.names.hits.items.len; @@ -500,6 +526,7 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { const f = owner.file orelse return row; row.target = .{ .place = .{ .path = f.path, .at = .{ .line = cand.ref } } }; }, + .themes => {}, .recent, .jumplist, .list, .grep, .all => { const t = look.parsePathLine(text); row.pre = @intCast(t.end); @@ -560,6 +587,12 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { try text.print(arena, " {d}/{d}", .{ if (n == 0) 0 else s.rows.at + 1, n }); if (pk.marks.items.len > 0) try text.print(arena, " •{d}", .{pk.marks.items.len}); if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); + // Themes have no acts and no other sources: no second row. + if (pk.source == .themes) { + try text.appendSlice(arena, " ⏎ wear it · esc"); + for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); + return .{ .text = text.items, .spans = spans.items, .cells = @intCast(ncells), .caret = caret }; + } // The second row: the spines go on through it from the key cells, // then the other sources' words, each switching to it. try text.append(arena, '\n'); @@ -579,7 +612,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { for (acts) |act| if (offered(pk, act)) try text.print(arena, "{s} ", .{act.word}); try text.appendSlice(arena, "|"); var first = false; - inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages) { + inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes) { if (src != pk.source) { if (!first) try text.append(arena, ' '); first = false; @@ -723,6 +756,7 @@ fn openText(p: *Pardes, path: []const u8) ?[]u8 { /// The landing pane as the picker found it: its own text back, or the /// pane made for the picker taken away; the places it lit put out. fn restore(p: *Pardes, pk: *Pick) void { + if (pk.source == .themes) return pardes.colors.previewTheme(p, null); const id = p.paneBySerial(pk.landing) orelse return; for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pk.id) tp.places.clear(p.gpa); if (pk.made) { @@ -805,6 +839,18 @@ pub fn key(p: *Pardes, k: Key) void { return; } if (pk.spine == .acts) return actKey(p, pk, k); + // Themes: Enter wears the one under the cursor; the rows and the field. + if (pk.source == .themes) { + if (plain and k.cp == Key.enter) { + const at = themeAt(p, pk) orelse return; + TagState.toRest(p); + pardes.colors.setThemeIndex(p, at); + return; + } + if (s.rows.key(k, n)) return; + if (s.field.key(k)) filter(p, pk); + return; + } if (pk.spine == .edit) { if (plain and k.cp == Key.enter) return editRows(p, pk); if (plain and k.cp == Key.backspace and s.field.len == 0 or alt and k.cp == 'h') { @@ -1810,3 +1856,41 @@ test "a picker from a reading pane unrolls in that pane, its own text set aside try std.testing.expect(p.panes[id].?.file.?.output == null); try std.testing.expectEqual(npanes, paneCount(p)); } + +test "SPC t t is a picker of the themes in the asking pane's tag, the whole window wearing the row under the cursor and nothing dimmed; Esc puts the theme back, Enter wears it" { + const Key_ = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); + defer p.deinit(); + _ = try p.setTestFile("one\n"); + const asker = p.view().active; + p.newScratchBelow(asker); + p.view().active = asker; + p.settings.jumps = .walk; + const themes = pardes.colors.themes; + const was = p.settings.theme; + const worn = p.theme().name; + for (" tt") |c| p.update(.{ .key = .{ .cp = c } }); + const s = &p.view().tag_state; + try std.testing.expectEqual(TagState.Kind.pick, s.kind); + try std.testing.expectEqual(Source.themes, live(p).?.source); + try std.testing.expectEqual(p.panes[asker].?.serial, s.host); + try std.testing.expectEqual(@as(u16, @intCast(was)), s.rows.at); + try std.testing.expectEqual(pardes.MAX_PANES, s.previewing.count()); + // Down: the window wears the next theme, the session's unchanged. + p.update(.{ .key = .{ .cp = if (was + 1 < themes.len) Key_.down else Key_.up } }); + const shown = s.rows.at; + try std.testing.expect(std.mem.eql(u8, p.theme().name, themes[shown].name) and shown != was); + try std.testing.expectEqual(was, p.settings.theme); + // Esc: its own again. + p.update(.{ .key = .{ .cp = Key_.escape } }); + try std.testing.expectEqualStrings(worn, p.theme().name); + try std.testing.expect(p.theme_preview == null); + // Again, and Enter wears it. + for (" tt") |c| p.update(.{ .key = .{ .cp = c } }); + p.update(.{ .key = .{ .cp = if (was + 1 < themes.len) Key_.down else Key_.up } }); + p.update(.{ .key = .{ .cp = Key_.enter } }); + try std.testing.expectEqual(@as(@TypeOf(p.settings.theme), @intCast(shown)), p.settings.theme); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expect(p.theme_preview == null); + try std.testing.expectEqualStrings(themes[shown].name, p.theme().name); +} |
