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-printer

v0.4.0

Published

Print anything on a terminal screen.

Downloads

16

Readme

terminal-printer

This module allows you to turn your terminal screen info a canvas where you can simply print text in all imaginable colors. You can also do pixel-art and finally create those fancy stuff you always imagined!

Installation

Just install the package as dependency with npm or yarn:

npm install --save terminal-printer

# or

yarn add terminal-printer

Getting started

The module exports one class called Printer which you can use to create awesomeness.

import { Printer } from "terminal-printer";

const printer = new Printer();

Usually you want to get the cursor out of the way while printing. Nothing more easy that that!

printer.hideCursor();

First, let's print a blank canvas:

printer.print();

Per default the canvas spans accross the full width of your termial.

Now let's write some text:

printer.writeCenteredText("Hello world!");

Well, you might be sad until now, because there is nothing visible on the screen. Thats because the printer does not automatically update, you have to explicitly tell it to do so. (The reason for this simply is performance. You often want to write multiple things. The printer collects all updates and prints them in one go.)

So let's update the printer to see the result:

printer.update();

Horray! You should now see the following:

getting-started

In the end, don't forget to get your cursor back!

printer.showCursor();

Documentation

Constructor

The constructor accepts one optional argument config, which is an object that can be used to configure the printer:

const printer = new Printer(config);

typeof config = {
  /*
   * `height` and `width` can be used to controll the rows and columns
   * of the canvas where the printer should work on. Per default the
   * value from the node process stdout (`process.stdout.rows` and
   * `process.stdout.columns`) are used.
   */
  height?: number;
  width?: number;
  /*
   * You can optionally specify colors for background and foreground.
   * The type `Color` represents valid CSS color names as strings
   * (see https://www.w3schools.com/cssref/css_colors.asp for a list).
   */
  backgroundColor?: RgbColor | Color;
  foregroundColor?: RgbColor | Color;
};

/* This object type represents a color by its RGB values. */
type RgbColor = { r: number; g: number; b: number; };

Properties

Here are the available properties of a Printer object.

height

printer.height; // returns the height of the canvas, i.e. the number of rows

width

printer.width; // returns the width of the canvas, i.e. the number of columns

Methods

Here are the available methods of a Printer object.

clear

This method clears all content from the canvas. You can optionally specify the back- and foreground color after the reset. The method does not return anything.

printer.clear(colors);

typeof colors = {
  backgroundColor?: RgbColor | Color;
  foregroundColor?: RgbColor | Color;
}

getPixel

This method returns the data for a specific pixel in your canvas. You have to specify the row and the column for the pixel you want to get.

The response will be an object containing backgroundColor, foregroundColor and value for the pixel.

printer.getPixel(17, 42);
/*
 * returns for example
 * {
 *   backgroundColor: "Black",
 *   foregroundColor: { r: 235, g: 255, b: 195 },
 *   value: "*"
 * }
 */

hideCursor

This method hides the cursor in the terimal. It does not return anything:

printer.hideCursor();

print

This method prints the complete canvas. This means that also those pixels will be re-printed where the values have actually not changed. The method does not return anything.

printer.print();

setPixel

This method allows you to set the value and the colors for a single pixel. With the first two arguments you have to specify the row and column of the pixel. The third parameter and its properties are all optional, so you could e.g. only set the value and leave the colors unchanged. The value (if specified) has to be a string of length one. The method does not return anything.

printer.setPixel(1, 2, {
  backgroundColor: "Black",
  foregroundColor: { r: 235, g: 255, b: 195 },
  value: "*",
});
printer.setPixel(17, 42, {
  value: "X",
});
showCursor

This method shows the cursor in the terimal. It does not return anything:

printer.showCursor();

update

This method prints all pixels of the canvas that have changes since the last print or update. This means that only those pixels will be re-printed where the values have actually changed. It is therefore much more efficient to use update in comparison to print. The method does not return anything.

printer.update();

writeCenteredRow

This method writes some text into a specific row of the canvas and automatically centeres the text. The first argument is the number of the row, the second is the text. In an optional third argument you can specify back- and foreground colors. It does not return anyting:

printer.writeCenteredRow(1, "Hello world!", {
  backgroundColor: "Black",
  foregroundColor: { r: 235, g: 255, b: 195 },
});
printer.writeCenteredRow(2, "Here is some random text.");

writeCenteredText

This method writes some text on the canvas and automatically centeres the text vertically and horizontally. The first argument is the text. It will be automatically splitted by newline-characters. In an optional second argument you can specify back- and foreground colors. It does not return anyting:

printer.writeCenteredText("Hello world!\nThis is the next row...", {
  backgroundColor: "Black",
  foregroundColor: { r: 235, g: 255, b: 195 },
});
printer.writeCenteredText("Only one row in the end...works just as well!");

writeRow

This method writes some text into a specific row of the canvas. You can also choose in which column to start writing. The first argument is the number of the row, the second the column number and the third is the text. In an optional fourth argument you can specify back- and foreground colors. It does not return anyting:

printer.writeRow(7, 9, "Hello world!", {
  backgroundColor: "Black",
  foregroundColor: { r: 235, g: 255, b: 195 },
});
printer.writedRow(9, 0, "This text starts right in the first column.");