diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-15 17:06:27 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-09-15 17:24:42 -0300 |
| commit | 6560da36cf8111575cd3fec443a42de6eff82f5f (patch) | |
| tree | 5c977f487c705b4e801de840b9a7720ad8b7b18f /test/mode.zig | |
| parent | 9ce11bfeff73b96704e521b41cffe4625b02f103 (diff) | |
| download | pardes-6560da36cf8111575cd3fec443a42de6eff82f5f.tar.gz pardes-6560da36cf8111575cd3fec443a42de6eff82f5f.zip | |
Cycle pane input modes with Mode and retain terminal tag access
Diffstat (limited to 'test/mode.zig')
| -rw-r--r-- | test/mode.zig | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/test/mode.zig b/test/mode.zig new file mode 100644 index 00000000..6d694b42 --- /dev/null +++ b/test/mode.zig @@ -0,0 +1,114 @@ +const std = @import("std"); +const pardes = @import("pardes"); +const panes = pardes.panes; +const Pardes = pardes.Pardes; + +fn mode(p: *Pardes, id: usize, expected: pardes.Pane.Mode) !void { + try std.testing.expect(p.executeBuiltinLine(id, "Mode")); + try std.testing.expectEqual(expected, p.panes[id].?.mode); + try std.testing.expect(!p.panes[id].?.tag_edit); +} + +fn clickModeTag(p: *Pardes) !void { + const pane = p.panes[0].?; + const text = try p.tagText(p.scratch.allocator(), pane); + const col = std.mem.indexOf(u8, text, "Mode") orelse return error.MissingModeTag; + const rect = p.rects[0]; + const row = if (p.settings.tag_bottom) rect.y + rect.h - pardes.BOX_H else rect.y; + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| + p.update(.{ .mouse = .{ .button = .left, .kind = kind, .col = rect.x + pardes.config.GUTTER + @as(u16, @intCast(col)), .row = row } }); + try std.testing.expect(pane.tag_edit); +} + +fn customTag(pane: *pardes.Pane, text: []const u8) void { + @memcpy(pane.tag_tail[0..text.len], text); + pane.tag_tail_len = text.len; + pane.tag_init = true; +} + +test "Mode cycles terminal modes and keeps legacy toggle and custom tags" { + if (comptime !panes.Terminal.enabled) return error.SkipZigTest; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100 }); + defer p.deinit(); + p.presentation.enabled = false; + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + try std.testing.expectEqual(pardes.Pane.Mode.tty, pane.mode); + try std.testing.expect(std.mem.indexOf(u8, try p.tagText(p.scratch.allocator(), pane), " Mode") != null); + try mode(p, 0, .normal); + try mode(p, 0, .insert); + try clickModeTag(p); + try std.testing.expectEqual(pardes.Pane.Mode.insert, pane.tag_mode); + p.update(.{ .key = .{ .cp = pardes.Key.tab } }); + try std.testing.expectEqual(pardes.Pane.Mode.tty, pane.mode); + try std.testing.expect(!pane.tag_edit); + while (p.nextEffect()) |effect| try std.testing.expect(effect != .write); + + customTag(pane, " Togglettymode Keep"); + try mode(p, 0, .normal); + try mode(p, 0, .insert); + try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); + try std.testing.expectEqual(pardes.Pane.Mode.tty, pane.mode); + p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); + try std.testing.expectEqual(pardes.Pane.Mode.normal, pane.mode); + p.update(.{ .key = .{ .cp = 'b', .ctrl = true } }); + try std.testing.expectEqual(pardes.Pane.Mode.tty, pane.mode); + try std.testing.expectEqualStrings(" Togglettymode Keep", pane.tag_tail[0..pane.tag_tail_len]); +} + +test "Mode from actual tag Exec preserves parked file and output insert transactions" { + for ([_]bool{ false, true }) |output| { + for ([_]bool{ false, true }) |normal_tag| { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100 }); + defer p.deinit(); + p.presentation.enabled = false; + const pane = try p.setTestFile("ab\n"); + if (output) pane.file.?.output = .{ .from = .search }; + try std.testing.expect(std.mem.indexOf(u8, try p.tagText(p.scratch.allocator(), pane), " Mode") == null); + customTag(pane, " Mode"); + try mode(p, 0, .insert); + p.update(.{ .key = .{ .cp = '界', .text = "界" } }); + p.update(.{ .key = .{ .cp = 'x', .text = "x" } }); + try std.testing.expectEqualStrings("界xab\n", pane.file.?.content); + try clickModeTag(p); + try std.testing.expectEqual(pardes.Pane.Mode.insert, pane.tag_mode); + if (normal_tag) pane.mode = .normal; + p.update(.{ .key = .{ .cp = pardes.Key.tab } }); + try std.testing.expectEqual(pardes.Pane.Mode.normal, pane.mode); + try std.testing.expect(!pane.tag_edit); + try std.testing.expect(pane.append_at == null); + p.update(.{ .key = .{ .cp = 'u' } }); + try std.testing.expectEqualStrings("ab\n", pane.file.?.content); + p.update(.{ .key = .{ .cp = 'a' } }); + p.update(.{ .key = .{ .cp = 'z', .text = "z" } }); + try std.testing.expect(pane.append_at != null); + try mode(p, 0, .normal); + try std.testing.expect(pane.append_at == null); + p.update(.{ .key = .{ .cp = 'u' } }); + try std.testing.expectEqualStrings("ab\n", pane.file.?.content); + } + } +} + +test "Mode leaves image and PDF document modes unchanged" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + const picture = try panes.Image.create(p, 1, "/mode-test.png", &.{}); + try mode(p, 1, .normal); + try std.testing.expect(picture.image != null); + if (comptime panes.Pdf.enabled) { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const bytes = try panes.Pdf.pdf.makeLinksTestPdf(std.testing.allocator); + defer std.testing.allocator.free(bytes); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "mode.pdf", .data = bytes }); + var buffer: [4096]u8 = undefined; + const len = try tmp.dir.realPath(std.testing.io, &buffer); + const path = try std.fmt.allocPrint(std.testing.allocator, "{s}/mode.pdf", .{buffer[0..len]}); + defer std.testing.allocator.free(path); + const document = try panes.Pdf.openPane(p, 2, path, 1); + const page = document.pdf.?.page; + try mode(p, 2, .normal); + try std.testing.expectEqual(page, document.pdf.?.page); + } +} |
