ttying is a simple cli interactive library, you declare your shortcut config, when pressed key matched, shortcut action works.
npm install ttying
yarn add ttying
pnpm add ttying
import ttying from 'ttying';
const ttyingInstance = ttying({
shortcuts: [
{
trigger: 'p',
description: 'Execute Task P',
action() {
console.log('Running Task P...');
console.log('Task P Over');
},
},
{
trigger: 'r',
description: 'Execute Task R',
action() {
console.log('Running Task R...');
console.log('Task R Over');
},
},
{
trigger: 'h',
description: 'Print helps',
action() {
ttyingInstance.help();
},
},
{
trigger: 'x',
description: 'Exit Process',
action() {
process.exit();
},
},
],
});
ttyingInstance.start();
type: ShortcutConfig[]
Shortcut config array
type: string | readline.Key
Trigger could be a string or an object like readline.Key
type: string
Description of current shortcut, it will be used to generate help content
type: () => void | Promise<void>
The shortcut handler when trigger pressed.
type: 'always' | 'once' | false
Determine when to print help content:
"always"
: print after every action over
"once"
: only print once when start
false
: never print
type: string
ttying
auto generate help content from your shortcuts config, if you want override the default helps, you can use this option.
ttying
use debug
print debug info, so if you are not sure your trigger key, you can set DEBUG
env to ttying
and launch your cli app, then you can see the press key info in terminal.
write a example:
import ttying from 'ttying';
const ttyingInstance = ttying({
shortcuts: [],
});
ttyingInstance.start();
run with DEBUG
env:
DEBUG=ttying node app.js
press you key (such as F2), you will see:
ttying input: undefined, keyInfo: { sequence: '\x1BOQ', name: 'f2', ctrl: false, meta: false, shift: false, code: 'OQ' }, runningAction: false