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

terminal-canvas

v3.1.2

Published

Manipulate the cursor in your terminal via high-performant, low-level, canvas-like API

Downloads

220

Readme

terminal-canvas

Travis (.com) Codecov npm

GitHub Follow Twitter Follow

Manipulate the cursor in your terminal via high-performant, low-level, canvas-like API.

Entirely written with TypeScript, terminal-canvas exposes features that you can use for rendering in terminal. High-performance algorithms and optimizations allow to render only cells which have been changed. Just look at the demo videos below to see, what you can do with terminal-canvas :smiley:

Demo

| Touhou - Bad Apple | Rick Astley - Never Gonna Give You Up | Casa Linda | | :------------------------: | :-----------------------------------: | :--------------------------------: | | 1 | 1 | 1 |

Getting Started

Install it via npm:

npm install terminal-canvas

Include in your project:

const Canvas = require('terminal-canvas');
const canvas = new Canvas();

canvas.moveTo(10, 10).write('Hello, world').flush();

Examples

A lot of examples are available to you here

API Reference

API Reference is accessible here

How terminal-canvas works

Control Sequences

terminal-canvas uses VT100 compatible control sequences to manipulate the cursor in the terminal.

You can find a lot of useful information about that here:

The next thing which terminal-canvas does is wrap those control codes, so you are able to call JavaScript methods.

Virtual Terminal

The first releases of the terminal-canvas (which was kittik-cursor) were based on real-time updating terminal cursor, when you are calling some method on the canvas.

This caused performance issues. So I decided to create my own wrapper around terminal cells.

Each real cell in the terminal has a wrapper (class Cell in the src folder). The main problem, which Cell resolves, is to render each cell independently from another cells. So I can grab any cell at any coordinate and render it independently from others.

It works the following way. Each Cell has style settings and position in the real terminal. When you are converting Cell to the control sequences, it concatenates the following sequences:

  1. Convert cell position to control sequence
  2. Convert foreground and background color to control sequence
  3. Convert display settings to control sequences
  4. Pre-pend the cell char with sequences above
  5. Reset all display settings to default

That way, each cell wrapped in own control sequences that can be flushed at any moment.

Difference between two frames

The last thing I did, was update only cells that really changed.

The algorithm is simple.

When you are writing to the canvas, all write operations mark virtual cells as modified cells. After some time, you decide to flush changes. When flush() method is called it does 2 things:

  1. Iterate through all cells and find only cells with modified marker;
  2. Convert modified cell to control sequence and compare that sequence with the sequence that was used at the previous frame;
  3. If they are not equal, store new control sequence and write to stream, otherwise, ignore it

That's how I made it possible to render videos in the terminal at 30 FPS.

BTW, if I remove Throttle stream, I'm getting 120 FPS :smiley:

License

MIT