Skip to content

Commit

Permalink
chore: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 24, 2023
1 parent f50439e commit 2bfe4e2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
6 changes: 2 additions & 4 deletions packages/utils-node/src/prompt-utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences
// https://github.com/nodejs/node/blame/a717fa211194b735cdfc1044c7351c6cf96b8783/lib/internal/readline/utils.js#L22-L29
// https://github.com/nodejs/node/blob/a717fa211194b735cdfc1044c7351c6cf96b8783/lib/internal/readline/utils.js
// https://github.com/terkelg/sisteransi/blob/e219f8672540dde6e92a19e48a3567ef2548d620/src/index.js
const kEscape = `\x1b`;
const CSI = `${kEscape}[`;
export const kClearToLineBeginning = `${CSI}1K`;
export const kClearToLineEnd = `${CSI}1K`;
export const kClearLine = `${CSI}2K`;
export const kClearScreenDown = `${CSI}0J`;
export const kSave = `${kEscape}7`;
export const kRestore = `${kEscape}8`;

export function cursorTo(x: number, y?: number) {
return typeof y !== "number" ? `${CSI}${x + 1}G` : `${CSI}${y + 1};${x + 1}H`;
Expand All @@ -19,7 +17,7 @@ export function moveCursor(dx: number, dy: number) {
dx < 0 && `${CSI}${-dx}D`,
dx > 0 && `${CSI}${dx}C`,
dy < 0 && `${CSI}${-dy}A`,
dy > 0 && `${CSI}${dy}A`,
dy > 0 && `${CSI}${dy}B`,
]
.filter(Boolean)
.join("");
Expand Down
7 changes: 2 additions & 5 deletions packages/utils-node/src/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import readline from "node:readline";
import { createManualPromise } from "@hiogawa/utils";
import {
cursorTo,
kClearLine,
} from "./prompt-utils";
import { cursorTo, kClearLine } from "./prompt-utils";

// cf. https://github.com/google/zx/blob/956dcc3bbdd349ac4c41f8db51add4efa2f58456/src/goods.ts#L83
export async function promptQuestion(query: string): Promise<string> {
Expand Down Expand Up @@ -52,7 +49,7 @@ export async function promptAutocomplete(config: {
}

const dispose = setupKeypressHandler(async (str: string, key: KeyInfo) => {
// TODO: move command
// TODO: more special keys
// https://github.com/terkelg/prompts/blob/735603af7c7990ac9efcfba6146967a7dbb15f50/lib/util/action.js#L18-L26
if (key.name === "escape" || (key.ctrl && key.name === "c")) {
manual.resolve({ input, value: "", ok: false });
Expand Down

0 comments on commit 2bfe4e2

Please # to comment.