npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ternimal

v0.0.1-alpha.6

Published

Minimal readline interface wrapper for interactive CLIs.

Downloads

182

Readme

[!CAUTION]

This is an alpha version of the package: currently untested and is considered unstable. Usage and API may change at any time. Use at your own risk.

ternimal

npm

Minimal readline interface wrapper for interactive CLIs.

This project was inspired by serverline.

ternimal example preview

Install

npm install ternimal

Usage

Import the module (ESM only):

import readline from 'readline';
import create from 'ternimal';

const term = create(() => ({
  stdin: process.stdin,
  stdout: process.stdout,
  stderr: process.stderr, // optional
  rl: readline.createInterface({
    prompt: '> ',
    input: process.stdin, // should match options.stdin
    output: process.stdout // should match options.stdout
  })
}));

term.rl.on('line', () => {
  term.prompt(true);
});
term.prompt();
term.console.log('Hello %s!', 'World');
Hello World!
>

[!TIP]

See example usage that leverages popular CLI tools like inquirer and ora.

create(init)

Type: (init: InitFunction) => Terminal

The create(init) function returns the terminal instance and requires an init function that returns an options object.

Options

options.rl

Type: readline.Interface | readline.promises.Interface

The readline interface instance.

The input and output streams for this instance should match the stdin and stdout options respectively.

options.stdin

Type: NodeJS.ReadableStream

The stdin read stream. Should match the input from the rl instance.

options.stdout

Type: NodeJS.WritableStream

The stdout write stream. Should match the output from the rl instance.

options.stderr

Type: NodeJS.WritableStream (optional)

The stderr write stream.

API

The Terminal object contains the following properties and methods.

rl

Type: readline.Interface | readline.promises.Interface

The readline interface instance.

This is the same rl instance passed through the terminal options.

console

Type: Console

The console instance.

Logging through this console will write the output above the prompt line. The write streams (stdout, stderr) for this console can be paused, resumed, and muted.

stdout

Type: NodeJS.WritableStream

The stdout write stream of console.

stderr

Type: NodeJS.WritableStream | undefined

The stderr write stream of console if provided.

raw

Type: object

The read and write streams from the provided options.

  • raw.stdin - The stdin read stream.
  • raw.stdout - The stdout write stream.
  • raw.stderr - The stderr write stream if provided.

status

Type: object

Get the prompt state and the statuses of the streams.

terminal.status.active(); // boolean
terminal.status.stdin(); // 'paused' | 'resumed'
terminal.status.stdout(); // 'paused' | 'resumed' | 'muted'
terminal.status.stderr(); // 'paused' | 'resumed' | 'muted'

active(active?)

Type: (active?: boolean) => this

Set the prompt state manually (default active value: true). The prompt state is set to active when calling prompt and is set to inactive when a line event is emitted by the rl instance.

When the prompt state is active:

prompt()

Type: (preserveCursor?: boolean) => this

Call rl.prompt() and set the prompt state to active.

It is recommended to use this instead of calling rl.prompt() directly to properly update and keep track of the prompt state.

The prompt state is reset every time a line event is emitted by the rl instance.

setPrompt(prompt)

Type: (prompt: string) => this

Set the prompt with rl.setPrompt() and call refreshLine.

pause(options?)

Type: (options?: PauseOptions) => this

Pause the read (stdin) and write (stdout, stderr) streams.

By default, all streams are paused unless options are provided.

// pause all streams
terminal.pause();
// pause by options
terminal.pause({
  stdin: true,
  stdout: { mute: false }, // boolean or object
  stderr: { mute: true } // boolean or object
});

Chunks are buffered when the write streams (stdout, stderr) are paused and are then flushed and written once resumed. Set the mute option to drop these chunks instead.

Note that this pauses the raw stdin directly via raw.stdin.pause() instead of rl.pause().

resume(options?)

Type: (options?: ResumeOptions) => this

Resume the read (stdin) and write (stdout, stderr) streams.

By default, all streams are resumed unless options are provided.

// resume all streams
terminal.resume();
// resume stdout only
terminal.resume({ stdout: true });

Note that this resumes the raw stdin directly via raw.stdin.resume() instead of rl.resume().

setLine(line, refresh?)

Type: (line: string, refresh?: boolean) => this

Set the rl.line and call refreshLine.

Set the refresh option to call refreshLine after setting the line (default: true).

refreshLine(force?)

Type: (force?: boolean) => this

Refresh the rl instance prompt line.

This is disabled if rl.terminal is false or if the prompt is not active. Update the prompt state with prompt or active.

Set force option to true to ignore the prompt state.

use(setup)

Type: (setup: SetupFunction) => MaybePromise<void>

Add and run a setup function. This function is rerun when the terminal is reinitialized.

The setup function can return an optional cleanup function that is run for deinit.

reinit(init?)

Type: (init?: InitFunction) => MaybePromise<void>

Reinitialize the terminal and rerun all setup functions.

Make sure to run the deinit method first before reinitializing the terminal.

deinit(close?)

Type: (close?: boolean) => MaybePromise<void>

Run the cleanup functions returned from the setup functions and close the rl instance.

Set the close option to false to skip calling rl.close().

License

Licensed under the MIT License.