Giter Site home page Giter Site logo

node-charm's Introduction

charm

Use ansi terminal characters to write colors and cursor positions.

me lucky charms

example

lucky

var charm = require('charm')(process);
charm.reset();

var colors = [ 'red', 'cyan', 'yellow', 'green', 'blue' ];
var text = 'Always after me lucky charms.';

var offset = 0;
var iv = setInterval(function () {
    var y = 0, dy = 1;
    for (var i = 0; i < 40; i++) {
        var color = colors[(i + offset) % colors.length];
        var c = text[(i + offset) % text.length];
        charm
            .move(1, dy)
            .foreground(color)
            .write(c)
        ;
        y += dy;
        if (y <= 0 || y >= 5) dy *= -1;
    }
    charm.position(0, 1);
    offset ++;
}, 150);
 
charm.on('data', function (buf) {
    if (buf[0] === 3) {
        clearInterval(iv);
        charm.destroy();
    }
});

events

Charm objects pass along the data events from their input stream except for events generated from querying the terminal device.

Because charm puts stdin into raw mode, charm emits two special events: "^C" and "^D" when the user types those combos. It's super convenient with these events to do:

charm.on('^C', process.exit)

methods

var charm = require('charm')(param or stream, ...)

Create a new charm given a param with stdout and stdin streams, such as process or by passing the streams in themselves separately as parameters.

Protip: you can pass in an http response object as an output stream and it will just work™.

charm.reset()

Reset the entire screen, like the /usr/bin/reset command.

charm.destroy()

Destroy the input stream.

charm.write(msg)

Pass along msg to the output stream.

charm.position(x, y) or charm.position(cb)

Set the cursor position to the absolute coordinates x, y or query the position and get the response as cb(x, y).

charm.move(x, y)

Move the cursor position by the relative coordinates x, y.

charm.up(y)

Move the cursor up by y rows.

charm.down(y)

Move the cursor down by y rows.

charm.left(x)

Move the cursor left by x columns.

charm.right(x)

Move the cursor right by x columns.

charm.push(withAttributes=false)

Push the cursor state and optionally the attribute state.

charm.pop(withAttributes=false)

Pop the cursor state and optionally the attribute state.

charm.erase(s)

Erase a region defined by the string s.

s can be:

  • end - erase from the cursor to the end of the line
  • start - erase from the cursor to the start of the line
  • line - erase the current line
  • down - erase everything below the current line
  • up - erase everything above the current line
  • screen - erase the entire screen

charm.display(attr)

Set the display mode with the string attr.

attr can be:

  • reset
  • bright
  • dim
  • underscore
  • blink
  • reverse
  • hidden

charm.foreground(color)

Set the foreground color with the string color, which can be:

  • red
  • yellow
  • green
  • blue
  • cyan
  • magenta
  • black
  • white

charm.background(color)

Set the background color with the string color, which can be:

  • red
  • yellow
  • green
  • blue
  • cyan
  • magenta
  • black
  • white

charm.cursor(visible)

Set the cursor visibility with a boolean visible.

install

With npm do:

npm install charm

node-charm's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.