From 96546af4758bfb120f04a7e6e81abc5cdc09589d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Oct 2023 17:23:27 -0700 Subject: [PATCH] terminal: test REP --- src/terminal/Terminal.zig | 51 ++++++++++++++++++++++++++++++++--- website/app/vt/rep/page.mdx | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 website/app/vt/rep/page.mdx diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 75ca871a3..67dfcc310 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -856,11 +856,10 @@ fn clearWideSpacerHead(self: *Terminal) void { } /// Print the previous printed character a repeated amount of times. -pub fn printRepeat(self: *Terminal, count: usize) !void { - // TODO: test +pub fn printRepeat(self: *Terminal, count_req: usize) !void { if (self.previous_char) |c| { - var i: usize = 0; - while (i < count) : (i += 1) try self.print(c); + const count = @max(count_req, 1); + for (0..count) |_| try self.print(c); } } @@ -6082,3 +6081,47 @@ test "Terminal: tabClear all" { try t.horizontalTab(); try testing.expectEqual(@as(usize, 29), t.screen.cursor.x); } + +test "Terminal: printRepeat simple" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + try t.printString("A"); + try t.printRepeat(1); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("AA", str); + } +} + +test "Terminal: printRepeat wrap" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + try t.printString(" A"); + try t.printRepeat(1); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings(" A\nA", str); + } +} + +test "Terminal: printRepeat no previous character" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + try t.printRepeat(1); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("", str); + } +} diff --git a/website/app/vt/rep/page.mdx b/website/app/vt/rep/page.mdx new file mode 100644 index 000000000..91eba03cd --- /dev/null +++ b/website/app/vt/rep/page.mdx @@ -0,0 +1,53 @@ +import VTSequence from "@/components/VTSequence"; + +# Repeat Previous Character (REP) + + + +Repeat the previously printed character `n` times. + +The parameter `n` must be an integer greater than or equal to 1. If `n` is less than +or equal to 0, adjust `n` to be 1. If `n` is omitted, `n` defaults to 1. + +In xterm, only characters with single byte (less than decimal 256) are +supported. In most other mainstream terminals, any character is supported. + +Each repeated character behaves identically to if it was manually typed in. +Therefore, soft-wrapping, margins, etc. all behave the same as if the +character was typed. + +The previously printed character is any character that is printed through +any means. The previously printed character is not limited to characters +a user manually types. If there is no previously typed character, this sequence +does nothing. + +## Validation + +### REP V-1: Simple Usage + +```bash +printf "\033[1;1H" # move to top-left +printf "\033[0J" # clear screen +printf "A" +printf "\033[b" +``` + +``` +|AAc_______| +``` + +### REP V-2: Soft-Wrap + +```bash +cols=$(tput cols) +printf "\033[1;1H" # move to top-left +printf "\033[0J" # clear screen +printf "\033[${cols}G" +printf "A" +printf "\033[b" +``` + +``` +|_________A| +|Ac________| +```