• Stars
    star
    169
  • Rank 217,371 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 πŸ˜ƒ

Demo

Touhou - Bad Apple (ASCII Art) Touhou - Bad Apple (Colorful Edition) Doom (Colorful Edition)
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 πŸ˜ƒ

License

MIT

More Repositories

1

type-challenges-solutions

Solutions for the collection of TypeScript type challenges with explanations
HTML
462
star
2

iterm-fish-fisher-osx

Semi-automatic installer for Command Line Tools + Homebrew + iTerm2 + Fish Shell + Fisher + Plugins/Themes
Shell
370
star
3

generator-sails-rest-api

Yeoman generator for scaffolding Sails REST API with predefined features
JavaScript
334
star
4

llvm-kaleidoscope

LLVM Tutorial: Kaleidoscope (Implementing a Language with LLVM)
C++
217
star
5

kittik

Create slides in TypeScript and present them in the terminal using ASCII only!
TypeScript
177
star
6

awesome-internals

A curated list of awesome resources and learning materials in the field of X internals
120
star
7

telegram-bot-github

Allows to you receive GitHub notifications right in the Telegram
JavaScript
116
star
8

ghaiklor-os-gcc

Simple operating system with its own bootloader, drivers for screen and keyboard, libc (for educational purposes)
C
113
star
9

backup-my-github

Clones all your repositories to local machine
Shell
106
star
10

sails-hook-cron

Sails hook for running cron tasks
JavaScript
88
star
11

icecast-parser

Node.js module for getting and parsing metadata from SHOUTcast/Icecast radio streams
TypeScript
69
star
12

nand-2-tetris

Computer built from the ground up on top of own CPU, while compiler and assembler for it implemented in Rust language
Assembly
64
star
13

sails-service-mailer

Service for Sails framework with Mailer features [DEAD]
JavaScript
31
star
14

pascal-interpreter

A simple interpreter for a large subset of Pascal language written for educational purposes
JavaScript
31
star
15

generator-es6-npm-module

Yeoman generator for starting ES6 npm module with Mocha, Istanbul, Travis, Coveralls
JavaScript
28
star
16

passport-google-plus-token

Passport strategy for authenticating with Google Plus via OAuth2 access tokens
JavaScript
23
star
17

flappy-bird

Flappy Bird, a game you can barely play for more than a few seconds without throwing your phone
JavaScript
22
star
18

node-print-message

Node.js module for printing out beautiful messages in console
JavaScript
21
star
19

sails-service-pusher

Service for Sails framework with Pusher features [DEAD]
JavaScript
20
star
20

advent-of-code-2015

Solutions for Advent of Code - a series of small programming puzzles for a variety of skill levels
JavaScript
19
star
21

assetsjsloader

Async loader which loads CSS and JS files
JavaScript
15
star
22

iterum

Interpreter for a subset of ECMA specification implemented on top of recursive descent parser
TypeScript
15
star
23

sails-service-payment

Service for Sails framework with Payment features [DEAD]
JavaScript
15
star
24

passport-instagram-token

Passport strategy for authenticating with Instagram via OAuth2 access tokens
JavaScript
13
star
25

sails-service-sms

Service for Sails framework with SMS features [DEAD]
JavaScript
12
star
26

sails-service-image

Service for Sails framework with Image features [DEAD]
JavaScript
10
star
27

passport-github-token

Passport strategy for authenticating with GitHub via OAuth2 access tokens
JavaScript
10
star
28

sails-service-storage

Service for Sails framework with Storage features [DEAD]
JavaScript
10
star
29

battlecity

The most popular game clone from our childhood
JavaScript
10
star
30

sails-service-social

Service for Sails framework with Social features [DEAD]
JavaScript
8
star
31

tiny-three-pass-compiler-codewars

Three-pass compiler for a simple programming language into a small assembly language
JavaScript
7
star
32

codemirror-mode-jsonata

CodeMirror mode for the JSONata language
JavaScript
6
star
33

polycolors

Yet another command-line coloring library with RGB support
JavaScript
5
star
34

sails-service-cipher

Service for Sails framework with Cipher features [DEAD]
JavaScript
5
star
35

bash-im-terminal

Terminal client for viewing Bash.IM
JavaScript
5
star
36

sails-service-location

Service for Sails framework with Location features [DEAD]
JavaScript
4
star
37

openlayers-tools

OpenLayers wrapper, which simplifies work with it
JavaScript
4
star
38

sails-service-hash

Service for Sails framework with Hash features [DEAD]
JavaScript
4
star
39

arves

Another RISC-V Educational Softcore
VHDL
4
star
40

passport-reddit-token

Passport strategy for authenticating with Reddit via OAuth2 access tokens
JavaScript
3
star
41

passport-soundcloud-token

Passport strategy for authenticating with SoundCloud via OAuth2 access tokens
JavaScript
3
star
42

flood-it

Flood It! is a simple addictive strategy game in which you have to flood the whole game board with one color
JavaScript
3
star
43

sails-hook-acl

Sails hook that implements ACL that don't tight with session or databases and easy configurable [DEAD]
JavaScript
3
star
44

passport-vkontakte-token

Passport strategy for authenticating with Vkontakte via OAuth2 access tokens
JavaScript
2
star
45

passport-yahoo-token

Passport strategy for authenticating with Yahoo via OAuth2 access tokens
JavaScript
2
star
46

cloudflare-multiplex-sw-2016

Technical task for CloudFlare company (Aug 2016)
JavaScript
2
star
47

passport-twitch-token

Passport strategy for authenticating with Twitch via OAuth2 access tokens
JavaScript
2
star
48

passport-yandex-token

Passport strategy for authenticating with Yandex via OAuth2 access tokens
JavaScript
1
star
49

tessel-vesnasoft-2015

Tessel application for demonstrating accel, ambient, camera, climate, servo and leds
JavaScript
1
star
50

ghaiklor-os-rust

Simple operation system created in educational purposes with Rust.
Rust
1
star
51

atmega-2560-hexapod

Arduino sketch for DAGU Spider controller
C++
1
star
52

passport-windows-live-token

Passport strategy for authenticating with Windows Live via OAuth2 access tokens
JavaScript
1
star
53

dimension-shock-poc

A re-imagining of the chess game, where game involves playing in two distinct dimensions simultaneously
JavaScript
1
star
54

passport-amazon-token

Passport strategy for authenticating with Amazon via OAuth2 access tokens
JavaScript
1
star