Git Product home page Git Product logo

stmux's Introduction

stmux

Simple Terminal Multiplexing for Node Environments

Abstract

This is a simple terminal multiplexing utility for Node.js environments. It is inspired by the awesome and unreachable tmux native Unix utility. The stmux utility is intended to provide just a very tiny subset of the original tmux functionality, but in a portable way for bare Node.js environments and with some special features for application build environments. Most notably, stmux has a built-time error detection and notification feature, can automatically restart terminated commands, and can automatically close or wait after all spawned commands have successfully or unsuccessfully terminated.

Internally, stmux is based on the awesome Blessed screen rendering environment and emulates full XTerm-compatible pseudo-terminals to the spawned programs with the help of Blessed XTerm and the underlying XTerm.js terminal rendering and node-pty pseudo-terminal integration modules.

Example

The following command...

$ stmux -- [ [ -s 1/3 bash .. vim ] : mc ]

...leads to the following particular terminal multiplexing environment, where GNU bash, Vim and Midnight Commander are running side-by-side inside their own XTerm emulating terminal widget (and, just for fun, CTRL+a ? was pressed to open up the stmux help window):

stmux usage

Intention

This utility is primarily intended to be used from within a package.json script to easily side-by-side run various NPM-based commands in a Node.js build-time environment. Sample package.json entries from a top-level NPM-based project follows, which on npm run dev allows one to conveniently run the commands of two sub-projects. First, the build-time of the frontend user interface (UI) project. Second, the build-time of the backend server (SV) project. Third, the run-time of the backend server project.

{
    ...
    "dependencies": {
        "stmux":      "*"
    },
    "scripts": {
        "install":    "npm run install:ui && npm run install:sv",
        "install:ui": "cd ui && npm install",
        "install:sv": "cd sv && npm install",

        "build":      "npm run build:ui && npm run build:sv",
        "build:ui":   "cd ui && npm run build",
        "build:sv":   "cd sv && npm run build",

        "start":      "cd sv && npm start",

        "clean":      "npm run clean:ui && npm run clean:sv",
        "clean:ui":   "cd ui && npm run clean",
        "clean:sv":   "cd sv && npm run clean",

        "dev":        "stmux -w always -e ERROR -m beep,system -- [ [ \"npm run dev:ui\" .. \"npm run dev:sv\" ] : -s 1/3 -f \"npm start\" ]",
        "dev:ui":     "cd ui && npm run build:watch",
        "dev:sv":     "cd sv && npm run build:watch"
    }
}

In case of a build-time error in the frontend user interface (SV), the result might be similar to the following one:

stmux example

Installation

$ npm install -g stmux

Transitive Dependency

The dependencies of stmux transitively include node-pty which must be built with node-gyp during npm install. Please check out the documentation of node-gyp on how to provide the necessary C/C++ compiler environment on your operating system.

  • macOS: install the "Command Line Tools" under "Preferences > Downloads" in Xcode.
  • Windows: open an elevated cmd.exe and run the commands npm install --global windows-build-tools and npm config set msvs_version 2015 --global
  • Windows Subsystem for Linux (WSL), Ubuntu distribution: run the commands sudo apt-get update and sudo apt-get install -y python make build-essential
  • Under Linux or FreeBSD you usually don't have to do anything.

Usage

The following command line arguments are supported:

$ stmux [-h] [-V] [-w <condition>] [-a <activator>] [-t <title>]
        [-c <type>] [-n] [-e <regexp>] [-m <method>] [-M] [-f <file>]
        [-- <spec>]
  • -h, --help
    Show usage help.
  • -V, --version
    Show program version information.
  • -w <condition>, --wait <condition>
    Wait after last finished command (and do not shutdown automatically), either if any command terminated with an error or just always.
  • -a <activator>, --activator <activator>
    Use CTRL+<activator> as the prefix to special commands. The default activator character is a. For instance, for the default activator case, opening the help popup requires you to press CTRL+a (and release it again) and then (separately) press ?.
  • -t <title>, --title <title>
    Set title on terminal. The default title is stmux.
  • -c <type>, --cursor <type>
    Set type of cursor to block (default), underline or line.
  • -n, --number
    Show terminal number in terminal title.
  • -e <regexp>[,...], --error <regexp>[,...]
    Observe terminal lines for errors (global option). One or more regular expressions can be specified and have to match on a single line. If a regular expression is preceeded with the prefix !, it is required that it does not match.
  • -m <methods>, --method <methods>
    In case of detected errors, use the comma-separated list of methods to perform user notification. The default is no extra notification (just the terminal annotation). Possible methods are beep and system.
  • -M, --mouse
    Enable mouse event handling. This enables the focus switching by left mouse click, the scrolling with mouse wheel and sends down mouse events to the terminal as mouse key sequences.
  • -f <file>, --file <file>
    Read specification <spec> from a configuration file. The default is to use the specification inside the command line arguments or (alternatively) to read the specification from stdin.

The following PEG-style grammar loosly describes the specification <spec>. For exact details see the real PEG grammar of stmux.

spec      ::= "[" directive (":"  directive)* "]"  /* vertical   split */
            | "[" directive (".." directive)* "]"  /* horizontal split */

directive ::= option* spec                         /* RECURSION */
            | option* string                       /* shell command */

option    ::= ("-f" | "--focus")                   /* focus terminal initially */
            | ("-r" | "--restart")                 /* restart command automatically */
            | ("-d" | "--delay") number            /* delay <number> seconds on restart */
            | ("-t" | "--title") string            /* set title of terminal */
            | ("-s" | "--size") size               /* request a size on terminal */
            | ("-e" | "--error") regexp            /* observe terminal for errors (local option) */

size      ::= /^\d+$/                              /* fixed character size */
            | /^\d+\.\d+$/                         /* total size factor */
            | /^\d+\/\d+$/                         /* total size fraction */
            | /^\d+%$/                             /* total size percentage */

The following keystrokes are supported under run-time:

  • CTRL+activator activator:
    Send the CTRL+activator key-sequence to the focused terminal.
  • CTRL+activator BACKSPACE:
    Switch the focus to the previous terminal in sequence.
  • CTRL+activator SPACE:
    Switch the focus to the next terminal in sequence.
  • CTRL+activator LEFT/RIGHT/UP/DOWN:
    Switch the focus to the best matching terminal in a direction.
  • CTRL+activator 1/2/.../9:
    Directly switch to a particular terminal.
  • CTRL+activator n:
    Toggle showing/hiding of terminal numbers.
  • CTRL+activator z:
    Toggle the zooming of focused terminal.
  • CTRL+activator v:
    Switch the focused terminal into visual/scrolling mode. Use PAGEUP/PAGEDOWN during this mode to scroll up/down. Any other key leaves this mode.
  • CTRL+activator l:
    Relayout the screen.
  • CTRL+activator r:
    Restart the program in the focused terminal.
  • CTRL+activator k:
    Kill the application and all shell commands in all terminals.
  • CTRL+activator ?:
    Show help window.

Specification Examples

  • stmux [ A ]:

    +-----------+
    |           |
    |     A     |
    |           |
    +-----------+
    
  • stmux [ A .. B ]:

    +-----+-----+
    |     |     |
    |  A  |  B  |
    |     |     |
    +-----+-----+
    
  • stmux [ A : B ]:

    +-----------+
    |     A     |
    +-----------+
    |     B     |
    +-----------+
    
  • stmux [ [ A .. B ] : C ]:

    +-----+-----+
    |  A  |  B  |
    +-----+-----+
    |     C     |
    +-----------+
    
  • stmux [ [ A : B ] .. C ]:

    +-----+-----+
    |  A  |     |
    +-----+  C  |
    |  B  |     |
    +-----+-----+
    
  • stmux [ [ A : B ] .. [ C : D ] ]:

    +-----+-----+
    |  A  |  C  |
    +-----+-----+
    |  B  |  D  |
    +-----+-----+
    
  • stmux [ [ A .. B ] : [ C .. D ] ]:

    +-----+-----+
    |  A  |  B  |
    +-----+-----+
    |  C  |  D  |
    +-----+-----+
    
  • stmux -- [ [ -s 1/3 A .. B ] : [ C .. -s 1/3 D ] ]:

    +---+-------+
    | A |    B  |
    +---+---+---+
    |    C  | D |
    +-------+---+
    

License

Copyright (c) 2017-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)

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.

stmux's People

Contributors

cheshrkat avatar rse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stmux's Issues

`-e <regex>` option not working

Simple example:

npx stmux -- [ -e i_like_pancakes "yes Error" ]
  • expected: Should not be red, because nothing on the console matches our regex
  • actual: Is red.

LMK if you don't have time to fix this, I might be able to raise a PR

Watch tasks results in ERROR showing (even no exit has happened)

For some reason, stmux seems to think all watch tasks (long running processes that does not exit), such as chokidar, tsc --watch, jest --watch, results in errors, as can be seen from the screenshot below. This has no functional consequences, but it is annoying, as people think that something has gone wrong, even though it has not.

$ cat .stmux-spec
[
-s 1/30 ['./stmux-help.sh' ]
:
-s 1/10 [ 'npm run dev:clinic' .. 'npm run dev:people' ]
:
[ '$(pnpm bin)/tsc --watch' .. 'npm run test -- --watch'  ]
:
[ 'cd code; npx chokidar "*/src/**/*.js*" "*/src/**/*.ts*" -c "npx eslint_d {path}"']
]

image

'Unknown encoding: null' due to passing wrong argument to Buffer#toString()

I just searched for a tmux replacement using NPM today and found this. Seems perfect. The only issue is that it crashed on first try! And it did this on all Node versions and on both macOS (ARM) and Linux (x86).

If you start up stmux without any arguments and press any key it will crash:

$ npx stmux

Unknown encoding: null

Just wanted to register the issue before filing a fix. I have verified this issue with Node 12, 14 and 16, so I am not sure why no one has reported, as the code is untouched since 2017 and it can't ever have worked. The above error is for Node 12 and 14, Node 16 is more explicit:

node:buffer:809
    throw new ERR_UNKNOWN_ENCODING(encoding);
    ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: null
    at new NodeError (node:internal/errors:371:5)
    at Buffer.toString (node:buffer:809:11)
    at STMUX.parseOptions (/Users/carlerik/.npm/_npx/feb1cc2f2f2b52b3/node_modules/stmux/bin/stmux-1-options.js:153:28)
    at STMUX.main (/Users/carlerik/.npm/_npx/feb1cc2f2f2b52b3/node_modules/stmux/bin/stmux.js:54:10)
    at Object.<anonymous> (/Users/carlerik/.npm/_npx/feb1cc2f2f2b52b3/node_modules/stmux/bin/stmux.js:77:7)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
  code: 'ERR_UNKNOWN_ENCODING'
}

Node 12 issue

When I trying to install a package after updating node to 12.20 I have something like this:

make: Leaving directory '/home/USER/n/lib/node_modules/stmux/node_modules/node-pty/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/USER/n/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:196:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:257:12)
gyp ERR! System Linux 5.0.7-200.fc29.x86_64
gyp ERR! command "/home/USER/n/bin/node" "/home/USER/n/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/USER/n/lib/node_modules/stmux/node_modules/node-pty
gyp ERR! node -v v12.2.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN [email protected] requires a peer of acorn@^6.0.0 but none is installed. You must install peer dependencies yourself.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node scripts/install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Security vulnerability

Hi there,

first of all I want to thank you for your impressive work. It is really great to have a tool like this for the community. So tanks in advance ๐Ÿ˜‰

I have a CLI tool called mockium that uses your package for offereing multiple and connected panels in the terminal.

Recently I noticed that my package process was warned because a security issue related to Lodash in one of your dependencies. So I decided to give you a hand with it and update the version in order to fix the vulnerability.
I opened a PR for that. I hope this small contribution helps you with your fantastic package.

Again. Thanks for your work

Activator key not working on macOS

Tried using the default CTRL+a and also passing the -a a or other keys, but no activator command works. e.g., CTRL+a ? for help doesn't come up.

On macOS Mojave and tried in both iTerm2 and terminal apps.

Is there any more info I can provide to debug?

Bug in parser definition for embedded quotes or curly braces in strings

We have a stmux config looking like this:

[ 
-s 1/30 ['./stmux-help.sh' ]
:
-s 1/10 [ 'npm run dev:app1' .. 'npm run dev:app2' ] 
: 
[ '$(pnpm bin)/tsc --watch' .. 'npm run test -- --watch'  ]
:
[ "npx chokidar 'code/*/src/**/*.{js,ts,jsx,tsx}' -c 'npx eslint {path}'"]
]

If firing this up it seems that the wrong data is sent over to Blessed, as it crashes with

TypeError: Cannot read properties of undefined (reading 'slice')
    at /home/carlerik/code/nimble/frontend/node_modules/.pnpm/[email protected]/node_modules/blessed/lib/program.js:2547:18

This is the Program.prototype._attr = function(param, val) { ... function and the value of param and val is js,ts,jsx,tsx and undefined respectively.

I had a quick glance at the parser PEG file, but could not immediately spotting what to look for, and since there is no test coverage in the project I do not dare touching it either in fear of breaking existing behavior ๐Ÿ˜ฐ Running the command in a shell works fine: sh -c 'npx chokidar "code/*/src/**/*.{js,ts,jsx,tsx}" -c "npx eslint {path}"'

click doesn't correctly change pane focus

Hi, thanks for you tool

with --mouse I noticed that mouse click doesn't correctly change pane focus for all features:
if I start stmux, click on the 3rd pane, then reload (ctrl+ r), the first pane will be reloaded instead of the 3rd

Rendering and activation issues on windows

Hi, I'm running windows 10 and the rendering gets messed up whenever anything changes on the screen.

The first weird thing that happens is that when I open two instances of bash side by side, the cursor first appears to be active on the second panel, but as soon as I type anything it enters it on the second panel.

Sometimes this also results in the rendering being messed up where the cursor on the screen is being rendered at an offset from what it is in the terminal (probably related to the border)

When the text in an instance of bash gets to the bottom, it renders over the border and messes up even more.

Secondly, I've been unable to get the activation to work even when changing the activation key.

Add flag to disable all keyboard input to running processes

Hi, thanks for this great package.

I am using it to run a bunch of "watch" processes. None of these require any keyboard input. It is confusing to remember that I have to press Ctrl-A K in order to quit. Would be nice if Ctrl-C would just kill everything.

This request is similar to #1 and #6, but the difference is that it's not a special case for Ctrl-C. For example, Ctrl-Z would put the entire stmux session in the background.

It would be nice if the "--mouse" option would continue to work, so that I could scroll up individual split terminal buffers.

Thank you

Exit on npm CTR-C

Hi there,

Trying to use this like the example shown with NPM. When I CTR-C the running commands stop but the stmux window does not close.

Is this possible?

I also cannot for the life of me get the activator key to work on Mac... :(

Thanks!

Toggle zoom glitch in lower panels

Hi, I found a bug while using the toggle zoom feature.

My system:
macOS Mojave: 10.14.5
Node: 8.15.0
NPM: 6.8.0

Stmux: 1.7.1

Steps to reproduce

  1. Run for example stmux [ [ 'echo 1' .. 'echo 2' ] : [ 'echo 3' .. 'vim' ] ] opening 4 panels of the same size
  2. Go to any panel placed at the bottom: CTRL + activator 4
  3. Toggle zoom: CTRL + activator z

Expected result

The program in panel 4 should be covering all terminal screen and the other three should not be present in the foreground.

Current result

The panel zooms in but only covering 3/4 of the page while one of the panels that should be hidden remains visible (see image below)

1__stmux__vim_

Sorry if I'm missing some more information or if this is the wrong channel.

child process lost

This probably not directly related to stmux but i think it should be addressed here :

I want to use stmux along with rollup with the watch option to build my file, and an other terminal along to have my storybook around.

The rollup watch option seems like popin new process everytime there's a new build to do and it seems like not working well with stmux.

basically, when building, the first time, rollup process would build and hang (waiting for a new build), and when i save a file stmux would declared "process terminated" - which makes me think rollup flow is to wait silently and kill itself/restart a new one when necessary.

is there a way, an option to get this working with stmux ?

Cannot copy and paste when using `-M`

I cannot seem to be able to copy and paste when using -M option.

If I remove -M I can select things in the terminal, but I cannot scroll through the individual inner terminals.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.