Skip to content

Commit

Permalink
fix: make it work again in Deno < 1.40 (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 26, 2024
1 parent 90af734 commit 34e8ed2
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/console/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@ export function showCursor() {
Deno.stderr.writeSync(encoder.encode("\x1B[?25h"));
}

export const isOutputTty = safeConsoleSize() != null && Deno.stderr.isTerminal();
export const isOutputTty = safeConsoleSize() != null && isTerminal(Deno.stderr);

function isTerminal(pipe: { isTerminal?(): boolean; rid?: number }) {
if (typeof pipe.isTerminal === "function") {
return pipe.isTerminal();
} else if (
pipe.rid != null &&
// deno-lint-ignore no-deprecated-deno-api
typeof Deno.isatty === "function"
) {
// deno-lint-ignore no-deprecated-deno-api
return Deno.isatty(pipe.rid);
} else {
throw new Error("Unsupported pipe.");
}
}

export function resultOrExit<T>(result: T | undefined): T {
if (result == null) {
Expand All @@ -86,7 +101,7 @@ export interface SelectionOptions<TReturn> {
}

export function createSelection<TReturn>(options: SelectionOptions<TReturn>): Promise<TReturn | undefined> {
if (!isOutputTty || !Deno.stdin.isTerminal()) {
if (!isOutputTty || !isTerminal(Deno.stdin)) {
throw new Error(`Cannot prompt when not a tty. (Prompt: '${options.message}')`);
}
if (safeConsoleSize() == null) {
Expand Down

0 comments on commit 34e8ed2

Please sign in to comment.