Skip to content

Commit

Permalink
refactor: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 25, 2023
1 parent f6fb9c6 commit 37ea06f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 2 additions & 3 deletions packages/utils-node/src/fixtures/prompt-autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { builtinModules } from "node:module";
import { colors } from "@hiogawa/utils";
import { promptAutocomplete } from "../prompt";

/*
Expand All @@ -10,12 +11,10 @@ demo

async function main() {
const result = await promptAutocomplete({
message: "Q. select node builtin module > ",
message: colors.cyan("?") + " select node builtin module > ",
suggest: (input) => {
return builtinModules.filter(v => v.includes(input));
},
limit: 30,
debug: true,
});
console.log("\n[answer]", result);
}
Expand Down
22 changes: 13 additions & 9 deletions packages/utils-node/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function promptAutocomplete(options: {
message: string;
suggest: (input: string) => string[] | Promise<string[]>;
limit?: number;
debug?: boolean;
hideCursor?: boolean;
}) {
const write = promisify(process.stdout.write.bind(process.stdout));
const manual = createManualPromise<void>();
Expand All @@ -44,8 +44,8 @@ export async function promptAutocomplete(options: {
let suggestions: string[] = [];
let suggestionIndex = 0;
let done = false;
const limit = options.limit ?? 10;
const debug = options.debug ?? false;
const limit = options.limit ?? Math.min(10, process.stdout.rows - 5);
const hideCursor = options.hideCursor ?? true;

async function render() {
// update states
Expand Down Expand Up @@ -73,12 +73,12 @@ export async function promptAutocomplete(options: {
.join(""),
].join("");

// 1. clean screen below
// 2. write content
// 3. reset cursor position (TODO: terminal cannot go up "height" when it exceeds termianl height)
// TODO: vscode's terminal have funky behavior when content height exceeds terminal height?
// TODO: IME (e.g Japanese input) cursor is currently not considered.
const height = computeHeight(content, process.stdout.columns);
await write(
[`${CSI}0J`, content, `${CSI}1A`.repeat(height - 1), `${CSI}1G`].join("")
// clear below + content + cursor up by "height"
`${CSI}0J` + content + `${CSI}1A`.repeat(height - 1) + `${CSI}1G`
);
}

Expand Down Expand Up @@ -124,14 +124,18 @@ export async function promptAutocomplete(options: {

let dispose: (() => void) | undefined;
try {
!debug && (await write(`${CSI}?25l`)); // hide cursor
if (hideCursor) {
await write(`${CSI}?25l`); // hide cursor
}
await render();
dispose = setupKeypressHandler(keypressHandler);
await manual.promise;
return { input, value };
} finally {
dispose?.();
await render();
!debug && (await write(`${CSI}?25h`)); // show cursor
if (hideCursor) {
await write(`${CSI}?25h`); // show cursor
}
}
}

0 comments on commit 37ea06f

Please # to comment.