diff --git a/src/ui/ui-root.ts b/src/ui/ui-root.ts index fe6cb3a..e7f7d97 100644 --- a/src/ui/ui-root.ts +++ b/src/ui/ui-root.ts @@ -9,7 +9,7 @@ import { Command } from "commander"; import log from "../utils/log.js"; import { getBackspaceSequence, Shell } from "../utils/shell.js"; import isterm from "../isterm/index.js"; -import { eraseLinesBelow } from "../utils/ansi.js"; +import { eraseLinesBelow, resetToInitialState } from "../utils/ansi.js"; import { SuggestionManager, MAX_LINES, KeyPressEvent } from "./suggestionManager.js"; export const renderConfirmation = (live: boolean): string => { @@ -22,6 +22,7 @@ export const render = async (program: Command, shell: Shell, underTest: boolean, const suggestionManager = new SuggestionManager(term, shell); let hasActiveSuggestions = false; let previousSuggestionsRows = 0; + const stdinStartedInRawMode = process.stdin.isRaw; if (process.stdin.isTTY) process.stdin.setRawMode(true); readline.emitKeypressEvents(process.stdin); @@ -141,6 +142,8 @@ export const render = async (program: Command, shell: Shell, underTest: boolean, }); term.onExit(({ exitCode }) => { + if (!stdinStartedInRawMode) process.stdin.setRawMode(false); + process.stdout.write(resetToInitialState); process.exit(exitCode); }); process.stdout.on("resize", () => { diff --git a/src/utils/ansi.ts b/src/utils/ansi.ts index da392ca..b3efc27 100644 --- a/src/utils/ansi.ts +++ b/src/utils/ansi.ts @@ -24,6 +24,7 @@ export const cursorNextLine = CSI + "E"; export const eraseLine = CSI + "2K"; export const resetColor = CSI + "0m"; export const resetLine = CSI + "2K"; +export const resetToInitialState = ESC + "c"; // RIS - Reset to Initial State export const cursorBackward = (count = 1) => CSI + count + "D"; export const cursorForward = (count = 1) => CSI + count + "C"; export const cursorTo = ({ x, y }: { x?: number; y?: number }) => {