1.0.1 • Published 1 year ago

ttying v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

ttying

ttying is a simple cli interactive library, you declare your shortcut config, when pressed key matched, shortcut action works.

npm package Build Status Downloads Issues Commitizen Friendly Semantic Release

Install

npm install ttying
yarn add ttying
pnpm add ttying

Usage

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();

Options

shortcuts

type: ShortcutConfig[]

Shortcut config array

ShortcutConfig.trigger

type: string | readline.Key

Trigger could be a string or an object like readline.Key

ShortcutConfig.description

type: string

Description of current shortcut, it will be used to generate help content

ShortcutConfig.action

type: () => void | Promise<void>

The shortcut handler when trigger pressed.

helpFrequency

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

helpContent

type: string

ttying auto generate help content from your shortcuts config, if you want override the default helps, you can use this option.

How to debug my key press ?

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
1.0.1

1 year ago

1.0.0

1 year ago