stdout-update

Updates the previous output in the terminal. Useful for correct rendering progress bars, animations, etc.

Usage no npm install needed!

<script type="module">
  import stdoutUpdate from 'https://cdn.skypack.dev/stdout-update';
</script>

README

stdout-update logo

Build Status npm Standard Shared Config

Purely and accurately overwrites the previous output in the terminal, while maintaining the history of third-party logs.

Install

npm install stdout-update

Usage

import { UpdateManager } from '../lib/UpdateManager';

const manager = UpdateManager.getInstance();
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
const messages = ['Swapping time and space...', 'Have a good day.', "Don't panic...", 'Updating Updater...', '42'];
let i = 0;
let j = 0;
let ticks = 60;

manager.hook();

console.log(' - log message');
console.error(' - error message');
console.warn(' - warn message');

const id = setInterval(() => {
  if (--ticks < 0) {
    clearInterval(id);
    manager.update(['✔ Success', '', 'Messages:', 'this line is be deleted!!!']);
    manager.erase(1);
    manager.unhook(false);
  } else {
    const frame = frames[(i = ++i % frames.length)];
    const message = messages[(j = Math.round(ticks / 10) % messages.length)];

    manager.update([`${frame} Some process...`, message]);
  }
}, 80);

Examples

tasktree-cli - simple terminal task tree, helps you keep track of your tasks in a tree structure.

API

UpdateManager

Accessors

  • get isHooked(): boolean - Hook activity status
  • get lastLength(): number - Last printed rows count
  • get outside(): number - Rows count outside editable area

Methods

  • static getInstance(stdout?: WriteStream, stderr?: WriteStream): UpdateManager - Method to get the object to control the streams update
  • erase(count?: number): void - Removes from the bottom of output up the specified count of lines
  • hook(): boolean - Hook stdout and stderr streams
  • unhook(separateHistory?: boolean): boolean - Unhooks both stdout and stderr streams and print their story of logs
    • separateHistory: Default true, if true, will add an empty line to the history output for individual recorded lines and console logs.
  • update(rows: string[], from?: number): void - Update output
    • rows: Text lines to write to standard output
    • from: Default 0, index of the line starting from which the contents of the terminal are being overwritten.