/* ** stmux -- Simple Terminal Multiplexing for Node Environments ** Copyright (c) 2017-2026 Dr. Ralf S. Engelschall ** ** Permission is hereby granted, free of charge, to any person obtaining ** a copy of this software and associated documentation files (the ** "Software"), to deal in the Software without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Software, and to ** permit persons to whom the Software is furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import os from "node:os" import childProcess from "node:child_process" import which from "which" import Blessed from "blessed" import type { Constructor, STMUXBase } from "./stmux-0-types.js" /* detect legacy (pre-10) Windows consoles */ const isLegacyWindows = () => os.platform() === "win32" && parseInt(os.release(), 10) < 10 export default >(Base: T) => class extends Base { override establishScreen (): void { /* Older/ancient Windows console API does not support inverse or underline for displaying the emulated cursor, so we have to force the use of the special character based "line" cursor. */ if (isLegacyWindows()) this.argv.cursor = "line" /* workaround for trouble under Windows + ConEmu + TERM=cygwin (where Blessed needs a hint to work correctly under the "cygwin" terminal type and for even better rendering quality we switch to the "windows-ansi" terminal type) */ if (os.platform() === "win32" && process.env.TERM === "cygwin") { process.env.NCURSES_NO_UTF8_ACS = "1" process.env.TERM = "windows-ansi" } /* sanity check to prevent trouble under Windows + MinTTY (where Node sees no TTY on stdio because MinTTY does not actually emulate a real TTY; the only workaround is to use the "winpty" utility and start Node with "winpty node") */ if (os.platform() === "win32" && process.env.TERM === "xterm" && !process.stdin.isTTY && !process.stdout.isTTY) { let winpty: string try { winpty = which.sync("winpty") } catch { this.fatal("under Windows/MinTTY you need the \"winpty\" utility on PATH") } const child = childProcess.spawnSync(winpty, process.argv, { stdio: "inherit" }) if (child.error) this.fatal(`failed to pass-through execution to "winpty": ${child.error.message}`) process.exit(child.status ?? (child.signal !== null ? 1 : 0)) } /* final sanity check for TTY */ if (!process.stdin.isTTY || !process.stdout.isTTY) this.fatal("we are not attached to a TTY device") /* establish Blessed screen */ this.screen = Blessed.screen({ title: this.argv.title, smartCSR: true, autoPadding: false, warnings: false, fullUnicode: this.argv.fullUnicode }) /* disable cursor */ this.screen.program.hideCursor() /* optionally enable mouse event handling */ if (this.argv.mouse) this.screen.enableMouse() /* determine screen size */ this.calcScreenSize() /* handle screen resizing */ this.screen.on("resize", () => { this.calcScreenSize() this.provisionAgain() this.helpBox.left = Math.max(0, Math.floor((this.screenWidth - this.helpW) / 2)) this.helpBox.top = Math.max(0, Math.floor((this.screenHeight - this.helpH) / 2)) this.helpBox.hide() this.screen.render() }) } override calcScreenSize (): void { this.screenWidth = this.screen.width as number this.screenHeight = this.screen.height as number if (isLegacyWindows()) { /* nasty hack for older Windows versions where all types of consoles (including ConEmu) scroll by an extra line (which breaks the screen rendering) once the right bottom character is rendered. As a workaround ensure we are not hitting the right margin on the last line. */ this.screenWidth-- } if (this.screenWidth < 3 || this.screenHeight < 3) this.fatal("attached terminal is too small") } override renderScreen (): void { this.screen.render() } }