|
| 1 | +/* jscpd:ignore-start */ |
| 2 | +import * as Command from "@effect/platform/Command" |
| 3 | +import type * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 4 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 5 | +import { Effect, Option, pipe } from "effect" |
| 6 | + |
| 7 | +const terminalSaneEscape = "\u001B[0m" + // reset rendition |
| 8 | + "\u001B[?25h" + // show cursor |
| 9 | + "\u001B[?1l" + // normal cursor keys mode |
| 10 | + "\u001B>" + // normal keypad mode |
| 11 | + "\u001B[?1000l" + // disable mouse click tracking |
| 12 | + "\u001B[?1002l" + // disable mouse drag tracking |
| 13 | + "\u001B[?1003l" + // disable any-event mouse tracking |
| 14 | + "\u001B[?1005l" + // disable UTF-8 mouse mode |
| 15 | + "\u001B[?1006l" + // disable SGR mouse mode |
| 16 | + "\u001B[?1015l" + // disable urxvt mouse mode |
| 17 | + "\u001B[?1007l" + // disable alternate scroll mode |
| 18 | + "\u001B[?1004l" + // disable focus reporting |
| 19 | + "\u001B[?2004l" + // disable bracketed paste |
| 20 | + "\u001B[>4;0m" + // disable xterm modifyOtherKeys |
| 21 | + "\u001B[>4m" + // reset xterm modifyOtherKeys |
| 22 | + "\u001B[<u" // disable kitty keyboard protocol |
| 23 | + |
| 24 | +const controllingTtyPath = "/dev/tty" |
| 25 | +const shellPath = "/bin/sh" |
| 26 | +const sttyPath = "/usr/bin/stty" |
| 27 | +const snapshotPattern = /^[0-9a-fA-F:]+$/u |
| 28 | + |
| 29 | +export type TerminalCursorRuntime = CommandExecutor.CommandExecutor | FileSystem.FileSystem |
| 30 | +export type TerminalResetFallbackWrite = (chunk: string) => void |
| 31 | + |
| 32 | +const optionOrElse = <A>(option: Option.Option<A>, fallback: A): A => pipe(option, Option.getOrElse(() => fallback)) |
| 33 | + |
| 34 | +const succeeds = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<boolean, never, R> => |
| 35 | + pipe( |
| 36 | + effect, |
| 37 | + Effect.as(true), |
| 38 | + Effect.option, |
| 39 | + Effect.map((result) => optionOrElse(result, false)) |
| 40 | + ) |
| 41 | + |
| 42 | +const hasInteractiveTty = (): boolean => process.stdin.isTTY && process.stdout.isTTY |
| 43 | + |
| 44 | +const disableRawMode = (): Effect.Effect<void> => { |
| 45 | + if (typeof process.stdin.setRawMode !== "function") { |
| 46 | + return Effect.void |
| 47 | + } |
| 48 | + |
| 49 | + return pipe( |
| 50 | + Effect.try(() => { |
| 51 | + process.stdin.setRawMode(false) |
| 52 | + }), |
| 53 | + Effect.ignore |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +const ttyShellCommand = (script: string): Command.Command => |
| 58 | + pipe( |
| 59 | + Command.make(shellPath, "-c", script), |
| 60 | + Command.stdin("inherit"), |
| 61 | + Command.stdout("pipe"), |
| 62 | + Command.stderr("pipe") |
| 63 | + ) |
| 64 | + |
| 65 | +const runTtyShell = (script: string): Effect.Effect<boolean, never, CommandExecutor.CommandExecutor> => |
| 66 | + pipe( |
| 67 | + ttyShellCommand(script), |
| 68 | + Command.exitCode, |
| 69 | + Effect.map((exitCode) => Number(exitCode) === 0), |
| 70 | + Effect.option, |
| 71 | + Effect.map((result) => optionOrElse(result, false)) |
| 72 | + ) |
| 73 | + |
| 74 | +const runTtyShellString = (script: string): Effect.Effect<string, never, CommandExecutor.CommandExecutor> => |
| 75 | + pipe( |
| 76 | + ttyShellCommand(script), |
| 77 | + Command.string, |
| 78 | + Effect.map((output) => output.trim()), |
| 79 | + Effect.option, |
| 80 | + Effect.map((result) => optionOrElse(result, "")) |
| 81 | + ) |
| 82 | + |
| 83 | +const snapshotTerminalState = (): Effect.Effect<string | null, never, CommandExecutor.CommandExecutor> => { |
| 84 | + if (!hasInteractiveTty()) { |
| 85 | + return Effect.succeed(null) |
| 86 | + } |
| 87 | + |
| 88 | + return Effect.gen(function*(_) { |
| 89 | + yield* _(disableRawMode()) |
| 90 | + const snapshot = yield* _( |
| 91 | + runTtyShellString( |
| 92 | + `if [ -c ${controllingTtyPath} ]; then ${sttyPath} -g < ${controllingTtyPath} 2>/dev/null || true; fi` |
| 93 | + ) |
| 94 | + ) |
| 95 | + return snapshotPattern.test(snapshot) ? snapshot : null |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +const writeTerminalReset = ( |
| 100 | + fallbackWrite?: TerminalResetFallbackWrite |
| 101 | +): Effect.Effect<boolean, never, FileSystem.FileSystem> => |
| 102 | + Effect.gen(function*(_) { |
| 103 | + const fs = yield* _(FileSystem.FileSystem) |
| 104 | + const wroteTty = yield* _(succeeds(fs.writeFileString(controllingTtyPath, terminalSaneEscape))) |
| 105 | + if (wroteTty) { |
| 106 | + return true |
| 107 | + } |
| 108 | + |
| 109 | + if (fallbackWrite !== undefined) { |
| 110 | + return yield* _( |
| 111 | + succeeds( |
| 112 | + Effect.try(() => { |
| 113 | + fallbackWrite(terminalSaneEscape) |
| 114 | + }) |
| 115 | + ) |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + return yield* _( |
| 120 | + succeeds( |
| 121 | + Effect.try(() => { |
| 122 | + process.stdout.write(terminalSaneEscape) |
| 123 | + }) |
| 124 | + ) |
| 125 | + ) |
| 126 | + }) |
| 127 | + |
| 128 | +const runSttySane = (): Effect.Effect<boolean, never, CommandExecutor.CommandExecutor> => |
| 129 | + runTtyShell( |
| 130 | + `if [ -c ${controllingTtyPath} ]; then ${sttyPath} sane < ${controllingTtyPath} > ${controllingTtyPath} 2>/dev/null; else exit 1; fi` |
| 131 | + ) |
| 132 | + |
| 133 | +const restoreSttySnapshot = (snapshot: string): Effect.Effect<boolean, never, CommandExecutor.CommandExecutor> => |
| 134 | + snapshotPattern.test(snapshot) |
| 135 | + ? runTtyShell( |
| 136 | + `if [ -c ${controllingTtyPath} ]; then ${sttyPath} '${snapshot}' < ${controllingTtyPath} > ${controllingTtyPath} 2>/dev/null; else exit 1; fi` |
| 137 | + ) |
| 138 | + : Effect.succeed(false) |
| 139 | + |
| 140 | +// CHANGE: share the low-level tty repair across SSH launch and TUI suspend/resume |
| 141 | +// WHY: both paths must reset the same controlling terminal before interactive output |
| 142 | +// QUOTE(ТЗ): "при подключении по SSH контейнер забаганный. Кривокосо печатается текст" |
| 143 | +// REF: user-request-2026-04-20-menu-select-ssh-terminal |
| 144 | +// SOURCE: n/a |
| 145 | +// FORMAT THEOREM: forall t: interactive(t) -> sane_tty(t) |
| 146 | +// PURITY: SHELL |
| 147 | +// EFFECT: Effect<void, never, TerminalCursorRuntime> |
| 148 | +// INVARIANT: fallback writer is used only when /dev/tty repair is unavailable |
| 149 | +// COMPLEXITY: O(1) |
| 150 | +export const repairInteractiveTerminal = ( |
| 151 | + fallbackWrite?: TerminalResetFallbackWrite |
| 152 | +): Effect.Effect<void, never, TerminalCursorRuntime> => { |
| 153 | + if (!hasInteractiveTty()) { |
| 154 | + return Effect.void |
| 155 | + } |
| 156 | + |
| 157 | + return Effect.gen(function*(_) { |
| 158 | + yield* _(disableRawMode()) |
| 159 | + const sane = yield* _(runSttySane()) |
| 160 | + const wroteReset = sane ? yield* _(writeTerminalReset(fallbackWrite)) : false |
| 161 | + if (!wroteReset) { |
| 162 | + yield* _(writeTerminalReset(fallbackWrite)) |
| 163 | + } |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +const restoreTerminalState = ( |
| 168 | + snapshot: string | null |
| 169 | +): Effect.Effect<void, never, TerminalCursorRuntime> => { |
| 170 | + if (!hasInteractiveTty()) { |
| 171 | + return Effect.void |
| 172 | + } |
| 173 | + |
| 174 | + return Effect.gen(function*(_) { |
| 175 | + yield* _(disableRawMode()) |
| 176 | + const restored = snapshot === null ? false : yield* _(restoreSttySnapshot(snapshot)) |
| 177 | + if (!restored) { |
| 178 | + yield* _(runSttySane()) |
| 179 | + } |
| 180 | + yield* _(writeTerminalReset()) |
| 181 | + }) |
| 182 | +} |
| 183 | + |
| 184 | +// CHANGE: ensure the terminal cursor is visible before handing control to interactive SSH |
| 185 | +// WHY: Ink/TTY transitions can leave cursor hidden, which makes SSH shells look frozen |
| 186 | +// QUOTE(ТЗ): "не виден курсор в SSH терминале" |
| 187 | +// REF: issue-3 |
| 188 | +// SOURCE: n/a |
| 189 | +// FORMAT THEOREM: forall t: interactive(t) -> cursor_visible(t) |
| 190 | +// PURITY: SHELL |
| 191 | +// EFFECT: Effect<void, never, TerminalCursorRuntime> |
| 192 | +// INVARIANT: escape sequence is emitted only in interactive tty mode |
| 193 | +// COMPLEXITY: O(1) |
| 194 | +export const ensureTerminalCursorVisible = (): Effect.Effect<void, never, TerminalCursorRuntime> => |
| 195 | + repairInteractiveTerminal() |
| 196 | + |
| 197 | +export const withPreservedTerminalState = <A, E, R>( |
| 198 | + use: Effect.Effect<A, E, R> |
| 199 | +): Effect.Effect<A, E, R | TerminalCursorRuntime> => |
| 200 | + Effect.gen(function*(_) { |
| 201 | + const snapshot = yield* _(snapshotTerminalState()) |
| 202 | + yield* _(ensureTerminalCursorVisible()) |
| 203 | + return yield* _(use.pipe(Effect.ensuring(restoreTerminalState(snapshot)))) |
| 204 | + }) |
| 205 | +/* jscpd:ignore-end */ |
0 commit comments