Skip to content

Commit

Permalink
refactor(cli): rewrite promptSelect() clear commands (#6215)
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
timreichen authored Nov 28, 2024
1 parent b86d762 commit 9904412
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
8 changes: 5 additions & 3 deletions cli/unstable_prompt_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const CR = "\r";
const INDICATOR = "❯";
const PADDING = " ".repeat(INDICATOR.length);

const CLR = "\r\u001b[K"; // Clear the current line
const CLR_ALL = "\x1b[J"; // Clear all lines after cursor

const encoder = new TextEncoder();
const decoder = new TextDecoder();
Expand Down Expand Up @@ -66,10 +66,12 @@ export function promptSelect(
case CR:
break loop;
}
Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length)));
Deno.stdout.writeSync(encoder.encode(`\x1b[${length}A`)); // move cursor after message
Deno.stdout.writeSync(encoder.encode(CLR_ALL));
}
if (clear) {
Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length + 1))); // clear values and message
Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`)); // move cursor before message
Deno.stdout.writeSync(encoder.encode(CLR_ALL));
}
Deno.stdin.setRaw(false);
return values[selectedIndex] ?? null;
Expand Down
27 changes: 18 additions & 9 deletions cli/unstable_prompt_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ Deno.test("promptSelect() handles arrow down", () => {
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
Expand Down Expand Up @@ -125,11 +127,13 @@ Deno.test("promptSelect() handles arrow up", () => {
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
Expand Down Expand Up @@ -185,7 +189,8 @@ Deno.test("promptSelect() handles up index overflow", () => {
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
Expand Down Expand Up @@ -240,15 +245,18 @@ Deno.test("promptSelect() handles down index overflow", () => {
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[3A",
"\x1b[J",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
Expand Down Expand Up @@ -305,7 +313,8 @@ Deno.test("promptSelect() handles clear option", () => {
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"\x1b[4A",
"\x1b[J",
];

let writeIndex = 0;
Expand Down

0 comments on commit 9904412

Please sign in to comment.