1.3.1 • Published 6 years ago

@totemish/shell v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

Shell

Core Build Status codecov Commitizen friendly semantic-release downloads version license

Totemish shell is a set of tools for styling the console output and prompting for user input.

Installation

npm i --save @totemish/shell

Usage

Input

To prompt for user input, you need to call Shell.prompt(). You need to provide a question you want to ask. Optionally, you may provide a default value that will be referred to as an answer in case user gives no input, and a values array, that represents a set of values and at least one of them must be picked by user or they will be informed about invalid choice provided and prompted the same question once again. Prompt returns user answer, automatically resolving floats, numbers and boleans.

import { Shell } from '@totemish/shell';

// NOTE: Shell.prompt() method is asynchronous
Shell.prompt('Pick a number').then((a) => { // ? Pick a number
  Shell.prompt('What?', 'Nothing').then((b) => { // ? What? (Nothing)
    Shell.prompt('Lemon is yellow', true, [true, false]).then(); // ? Lemon is yellow? [true, false] (true)
  });
});

Output

import { Shell } from '@totemish/shell';

/**
 * There are five default helper methods
 */
Shell.write('white');
Shell.warn('yellow');
Shell.success('green');
Shell.info('blue');
Shell.error('red');

/**
 * Prints a blank line
 */
Shell.blank();

/**
 * You can specify amount of blank lines to be printed
 */
Shell.blank(2);

/**
 * You can apply three types of enhanced decoration - bold, underline and inverse
 * The spread operator is optional but it removes the nasty TypeScript incompatibility anger
 */
Shell.success(...Shell.bold('bold green'));
Shell.warn(...Shell.underline('underlined yellow'));
Shell.info(...Shell.inverse('inversed blue'));

/**
 * Shell.describe method prints out the contents of an array or an object in a nice coloured way
 * Booleans are given in magenta color, numbers - in yellow, object keys - in cyan and strings - in green
 */
Shell.describe({
  boolean: true,
  number: 42,
  object: {
    string: 'green',
    array: [
      "each",
      "item",
      "is",
      "green",
    ],
    nestedObject: {
      hello: 'world'
    }
  }
});
Shell.describe([1,4,3,2]);

Controlling behaviour

  • You can disable awaiting prompt input by providing a --y flag when starting the process. If process.argv includes the --y flag, prompter will automatically apply default value if it is given, or an empty string ('').

  • You can disable applying colors to console output by providing a --no-color flag when starting the process. If process.argv includes the --no-color flag, writer will write output to console without any ASCI magic.

  • You can disable output to console by providing a --silent flag when starting the process. If process.argv includes the --silent flag, prompter will automatically apply default value if it is given, or an empty string ('') and writer will give no output to console.

Custom output

You can apply several styles and wrap up everything like so:

import { Shell } from '@totemish/shell';

Shell.write(Shell.green('hello'), Shell.red(' world'));

Links