Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/flag/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export const Flag = {
/** Never read or write the saved console /models listing (tests, which mock the fetch). */
REDROB_DISABLE_MODELS_CACHE: truthy("REDROB_DISABLE_MODELS_CACHE"),
REDROB_DISABLE_MOUSE: truthy("REDROB_DISABLE_MOUSE"),
/** Keep mouse reporting on in Git Bash (mintty), where it is off by default. */
REDROB_FORCE_MOUSE: truthy("REDROB_FORCE_MOUSE"),
REDROB_FAKE_VCS: process.env["REDROB_FAKE_VCS"],
REDROB_SERVER_PASSWORD: process.env["REDROB_SERVER_PASSWORD"],
REDROB_SERVER_USERNAME: process.env["REDROB_SERVER_USERNAME"],
Expand Down
17 changes: 17 additions & 0 deletions packages/redrob/bin/redrob
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ const os = require("os")

const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"]

// Mouse reporting, bracketed paste, kitty keys, alternate screen, hidden cursor.
const TERMINAL_RESET = "\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1015l\x1b[?2004l\x1b[<u\x1b[?1049l\x1b[?25h"

// When the TUI ends without cleaning up (killed or crashed), undo its terminal
// modes so the shell does not print mouse moves. Only on a Windows terminal.
function restoreTerminal(code, signal) {
if (process.platform !== "win32") return
if (!signal && code === 0) return
if (!process.stdout.isTTY && !process.env.MSYSTEM) return
try {
fs.writeSync(1, TERMINAL_RESET)
} catch {
// stdout is gone.
}
}

function run(target) {
const child = childProcess.spawn(target, process.argv.slice(2), {
stdio: "inherit",
Expand All @@ -30,6 +46,7 @@ function run(target) {
}

child.on("exit", (code, signal) => {
restoreTerminal(code, signal)
for (const forwardedSignal of forwardedSignals) {
process.removeListener(forwardedSignal, forwarders[forwardedSignal])
}
Expand Down
5 changes: 3 additions & 2 deletions packages/tui/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import type { EventSource } from "./context/sdk"
import { DialogVariant } from "./component/dialog-variant"
import { createTuiAttention } from "./attention"
import * as TuiAudio from "./audio"
import { win32DisableProcessedInput, win32FlushInputBuffer } from "./terminal-win32"
import { win32DisableProcessedInput, win32FlushInputBuffer, win32GitBash } from "./terminal-win32"
import { destroyRenderer } from "./util/renderer"
import { cliErrorMessage, errorFormat } from "./util/error"

Expand Down Expand Up @@ -203,7 +203,8 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
useKittyKeyboard: {},
autoFocus: false,
openConsoleOnError: false,
useMouse: !Flag.REDROB_DISABLE_MOUSE && input.config.mouse,
useMouse:
!Flag.REDROB_DISABLE_MOUSE && input.config.mouse && (Flag.REDROB_FORCE_MOUSE || !win32GitBash()),
consoleOptions: {
keyBindings: [{ name: "y", ctrl: true, action: "copy-selection" }],
},
Expand Down
10 changes: 10 additions & 0 deletions packages/tui/src/terminal-win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,13 @@ export function win32InstallCtrlCGuard() {

return unhook
}

/**
* Git Bash (mintty) without a pseudo console. Ctrl+C there ends a native program
* at once, so no exit handler runs and mouse reporting stays on in the shell.
*/
export function win32GitBash() {
if (process.platform !== "win32") return false
if (!process.env["MSYSTEM"]) return false
return process.env["TERM_PROGRAM"] === "mintty" || !process.stdin.isTTY
}
Loading