Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): rewrite promptSelect() clear commands #6215

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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