Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (run-pty) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
run-pty
run-pty is a command line tool that lets you run several commands concurrently and interactively. Show output for one command at a time. Kill all at once.
It’s like concurrently but the command outputs aren’t mixed, and you can restart commands individually and interact with them. I bet you can do the same with tmux if you – and your team mates – feel like installing and learning it. In bash you can use command1 & command2 together with fg, bg, jobs and ctrl+z to achieve a similar result, but run-pty tries to be easier to use, and cross-platform.
ctrl+z shows the dashboard, which gives you an overview of all your running commands and lets you switch between them.
ctrl+c kills commands.
A use case is running several watchers. Maybe one or two for frontend (webpack, Parcel, Vite), and one for backend (nodemon, or even some watcher for another programming language).
Another use case is running a couple of commands in parallel, using --auto-exit.
Example
{
"scripts": {
"start": "run-pty % npm run frontend % npm run backend",
"frontend": "parcel watch index.html",
"backend": "nodemon server.js"
}
}$ npm start
> @ start /Users/lydell/src/run-pty/demo
> run-pty % npm run frontend % npm run backend➡️
[1] 🟢 npm run frontend
[2] 🟢 npm run backend
[1-2] focus command (or click)
[ctrl+c] kill all
[↑/↓] move selection➡️ 1 ️️➡️
🟢 npm run frontend
> frontend
> vite --no-clearScreen
VITE v3.0.9 ready in 137 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
▊
[ctrl+c] kill (pid 63096)
[ctrl+z] dashboard➡️ ctrl+c ➡️
🟢 npm run frontend
> frontend
> vite --no-clearScreen
vite v2.8.4 dev server running at:
> Local: http://localhost:3000/
> Network: use `--host` to expose
ready in 136ms.
^C
⚪ npm run frontend
exit 0
[enter] restart
[ctrl+c] kill all
[ctrl+z] dashboard➡️ ctrl+z ➡️
[1] ⚪ exit 0 npm run frontend
[2] 🟢 npm run backend
[1-2] focus command (or click)
[ctrl+c] kill all
[↑/↓] move selection
[enter] restart exited➡️ ctrl+c ➡️
[1] ⚪ exit 0 npm run frontend
[2] ⚪ exit 0 npm run backend
$ ▊Installation
npm install --save-dev run-pty
npx run-pty --help
Advanced mode
The above example called run-pty like so:
run-pty % npm run frontend % npm run backendInstead of defining the commands at the command line, you can define them in a JSON file:
run-pty.json:
[
{
"command": ["npm", "run", "frontend"]
},
{
"command": ["npm", "run", "backend"]
}
]run-pty run-pty.json(The JSON file can be called anything – you specify the path to it on the command line.)
The JSON format lets you specify additional things apart from the command itself.
| Key | Type | Default | Description |
|---|---|---|---|
| command | Array<string> |
Required | The command to run. Must not be empty. |
| title | string |
command as a string |
What to show in the dashboard. |
| cwd | string |
"." |
Current working directory for the command. |
| status | { [regex: string]: [string, string] | null } |
{} |
Customize the status of the command in the dashboard. |
| defaultStatus | [string, string] | null |
null |
Customize the default status of the command in the dashboard. |
| killAllSequence | string |
"\u0003" |
Sequence to send to the command when using “kill all”. The default is the escape code for ctrl+c. |
command: On the command line, you let your shell split the commands into arguments. In the JSON format, you need to do it yourself. For example, if you had
run-pty % npm run frontendon the command line, the JSON version of it is["npm", "run", "frontend"]. Andrun-pty % echo 'hello world'would be["echo", "hello world"].title: If you have complicated commands, it might be hard to find what you’re looking for in the dashboard. This lets you use more human readable titles instead. The titles are also shown when you focus a command (before the command itself).
cwd: This is handy if you need to run some command as if you were in a subdirectory. When focusing a command, the
cwdis shown below the title/command (unless it’s"."(the CWD of therun-ptyprocess itself) or equal to the title):🟢 Custom title: npm run something 📂 my/cwd/pathstatus: It’s common to run watchers in
run-pty. Watchers wrap your program – if your program crashes, the watcher will still be up and running and wait for source code changes so it can restart your program and try again.run-ptywill display a 🟢 in the dashboard (since the watcher is successfully running), which makes things look all green. But in reality things are broken.statuslets you replace 🟢 with custom status indicators, such as 🚨 to indicate an error.The keys in the object are regexes with the
uflag.The values are either a tuple with two strings or
null.For each line of output,
run-ptymatches all the regexes from top to bottom. For every match, the status indicator is set to the corresponding value. If several regexes match, the last match wins. Graphic renditions are stripped before matching.This is how the value (
[string, string] | null) is used:- The first string is used primarily. The string is drawn in 2 character slots in the terminal – if your string is longer, it will be cut off. Emojis usually need 2 character slots.
- The second string is used on Windows (except if you use Windows Terminal instead of for example cmd.exe) or if the
NO_COLORenvironment variable is set. InNO_COLORmode, graphic renditions are stripped as well. So you can use ANSI codes (in either string) to make your experience more colorful while still letting people have monochrome output if they prefer. Unlike the first string, the second string is drawn in 1 character slot in the terminal. (Windows – except the newer Windows Terminal – does not support emojis in the terminal very well, and forNO_COLORyou might not want colored emojis, so a single character should do.) nullresets the indicator to the standard 🟢 one (notdefaultStatus).
defaultStatus: This lets you replace 🟢 with a custom status indicator at startup (before your command has written anything). The value works like for
status.killAllSequence: When you use “kill all” run-pty sends ctrl+c to all commands. However, not all commands exit when you do that. In such cases, you can use
killAllSequenceto specify what sequence of characters to send to the command to make it exit.
--auto-exit
If you want to run a couple of commands in parallel and once they’re done continue with something else, use --auto-exit:
run-pty --auto-exit % npm ci % dotnet restore && node build.js- You can enter the different commands while they are running to see their progress.
- Once all commands exit with code 0 (success), run-pty exits with code 0 as well.
- If some command fails, run-pty does not exit, so you can inspect the failure, and re-run that command if you want.
- If you exit run-pty before all commands have exited with code 0, run-pty exits with code 1, so that if run-pty was part of a longer command chain, that chain is ended.
- In CI – where there is no TTY – the
--auto-exitmode degrades to a simpler, non-interactive UI.
To limit how many commands run in parallel, use for example --auto-exit=5. Just --auto-exit is the same as --auto-exit=auto, which uses the number of logical CPU cores.
Note: --auto-exit is for conveniently running a couple of commands in parallel and get to know once they are done. I don’t want the feature to grow to GNU Parallel levels of complexity.
Credits
- microsoft/node-pty does all the heavy lifting of running the commands.
- apiel/run-screen was the inspiration for this tool.
iTerm2 flicker
iTerm2 has a bug where the window flickers when clearing the screen without GPU rendering: https://gitlab.com/gnachman/iterm2/-/issues/7677
GPU rendering seems to be enabled by default, as long as your computer is connected to power.
You can enable GPU rendering always by toggling “Preferences > General > Magic > GPU Rendering + Advanced GPU Settings… > Disable GPU rendering when disconnected from power.”
There might still be occasional flicker. Hopefully the iTerm2 developers will improve this some time. It does not happen in the standard Terminal app.
License
MIT.