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

@cthru/console-writer

v1.1.1

Published

A small utility to make writing multiline output to the console a lot more visually appealing and easier to work with

Downloads

3

Readme

@cthru/console-writer

A simple utility to make it easier and more visually appealing to produce complex multiline console output in javascript.

Version Downloads/week License More Tools

Why

Multiline string output in JavaScript has traditionally been a pain in the rear-end. It looks dismal. You have to manage wrapping yourself. You have to make tons of edits if there is a change early on in the multiline to reformat the stuff that goes below.

I wanted something that would give me more flexibility in producing large multiline strings in a way that is visually appealing inside the script as well as outside.

Examples

There are a bunch of examples in the examples folder. Here is the skinny

Old Style

var multiStr = "This is the first line" + 
	"This is the second line" + 
	"This is more...";
// or
var multiStr = "This is the first line \ 
	This is the second line \
	This is more..."

With console-writer

let o = require('@cthru/console-writer').string()
let multiStr = o
('This is the first line')
('This is the sedond line')
('This is more...')
.toString()

Pre-fomatted text works even better

let o = require('@cthru/console-writer').string()
let multiStr = o(`
This is the first line
This is the second line
This is more...
`).toString()

You may say that we could just use template strings for this anyway, why wrap it in a function.. here's why:

//                                               wrap:50 -- this is why
let o = require('@cthru/console-writer').string({wrap:50})
let multiStr = o.w(`
Qui manu quisquam si caeca [Lavinia centum
contendere](http://confusaque.com/facta.php) asello, foret. Ferrum sereno
Sisyphio: squamosos utque. Molem morte, avertitur angustis nobis cornua meos,
non glande adgreditur fontes quod alas tua egere in.
`,true) // <- reflow breaks -- this is why
// predictable line-breaks -- this is why
('Some Heading')
('============')
(`
Qui manu quisquam si caeca [Lavinia centum
contendere](http://confusaque.com/facta.php) asello, foret. Ferrum sereno
Sisyphio: squamosos utque. Molem morte, avertitur angustis nobis cornua meos,
non glande adgreditur fontes quod alas tua egere in.
`)
.toString()

Main Api

Factory function

const writerFactory = require('@cthru/console-writer')

const console80=writerFactory({wrap:80})
const console120=writerFactory({wrap:120})

In this snippet, console80 will write directly to the console, wrapping at 80 characters. console120 will write directly to the console, wrapping at 120 characters.

console80('This is the same as `console.log()` except that it will wrap at 80 characters')

Chained Functions

Whenever you execute the console writer, you get back the same console-writer function. This means you can write using chained function calls:

console80('This will go on the first line')('This will go on the second line')

// For the purpose of code legibility you probably want to adopt this form:
console80
('This will go on the first line')
('This will go on the second line')

Capturing the console

If you want to get a string back from the document rather than print it to the console directly:

const writerFactory = require('@cthru/console-writer')
const buffer=writerFactory.string({wrap:80})

buffer('do some stuff')

console.log(buffer.toString())

Additional Functionality

It quickly became aparent that there is a lot more we could do with this. So to avoid making a module with 10 lines of code, I added some candy.


[writer].w(str, noBr=false)

Joins successive calls as if there were no line-breaks.

|||| |-----------|-----------|-| |str | | the string to be printed. |noBr |[false] | if set, newlines in the call will also be ignored.


[writer].title = (t = '', char, space = 1)

Print a title.

|||| |-----------|-----------|-| |t | | the string to be printed. |char |[#] | the character used to decorate the title. |space |[1] | number of spaces between the docoration line and the actual title.


[writer].title.full = (t = '', char, space = 1)

Print a full-width title, with centered text.

|||| |-----------|-----------|-| |t | | the string to be printed. |char |[#] | the character used to decorate the title. |space |[1] | number of spaces between the docoration line and the actual title.


[writer].center = (str, w)

Print a centerd line.

|||| |-----------|-----------|-| |str | | the string to be printed. |w |[cfg.wrap] | the width at which to wrap. Defaults to configured wrap width.


[writer].right = (str, w, p)

Print a right-aligned string.

|||| |-----------|-----------|-| |str | | the string to be printed. |w |[cfg.wrap] | the width at which to wrap. Defaults to configured wrap width. |p |[0] | indented padding from the right.


[writer].tab = (n = 1)

Print any number of tab characters. This will not produce a new line.

|||| |-----------|-----------|-| |n |[1] | The number of tabs to enter


[writer].pad = (n = 1, char = ' ')

Print any number of characters. This will not produce a new line.

|||| |-----------|-----------|-| |n |[1] | The number of tabs to enter |char |[:space:] | if set, newlines in the call will also be ignored


[writer].sep = (char = '-', n)

Print a separator

|||| |-----------|-----------|-| |char |[-] | The character to use for the separator |n |[cfg.wrap] | The width of the separator, defaulting to the configured wrap width


[writer].space = (n)

Print any number of spaces. This does not produce a newline.

|||| |-----------|-----------|-| |n |[false] | The number of spaces to print


[writer].nl = (n = 1)

Print any number of newlines

|||| |-----------|-----------|-| |n |[1] | The number of newlines to print


[writer].h1 = (str = '')

Print an H1

|||| |-----------|-----------|-| |str | | the string to be printed


[writer].h2 = (str = '')

Print an H2

|||| |-----------|-----------|-| |str | | the string to be printed


[writer].h3 = (str = '')

Print an H3

|||| |-----------|-----------|-| |str | | the string to be printed


[writer].preformatted = (str = '')

Print a preformatted string as-is

|||| |-----------|-----------|-| |str | | the string to be printed


[writer].inspect = (obj, { showHidden: false, depth: 5, color: true })

Print the inspected object

|||| |-----------|-----------|-| |obj | | the object to inspect
|showHidden | false | Show hidden items
|depth | 5 | Recrurse to this depth
|color | true | Include color in the output

A final example

const out = require('../src/writer-factory')({ wrap: 70 })

out
.title.full('This is a standard Title','#',0)
()
.right('This is right-aligned')
()
.right('This is right-aligned and wrapped at 30 characters',30)
()
.right('This is right-aligned and wrapped at 35 characters and right-padded by 5 characters',35, 5)
.sep()
.nl(2)
.h1('First Level Heading')
.center('This is center-aligned that will only wrap when hitting the edges')
()
.h2('Second Level Heading')
.center('This is center-aligned with content with a maximum width of 25 chars',25)
()
.h3('Third level Heading')
.pad(4)('This is left-aligned text with a maximum of 35 chars, and the first line indented by 4',35)
()
('This is left-aligned text with a maximum of 50 chars, and the full string is indented by 4',50,4)

Produces this output:

######################################################################
                       This is a standard Title
######################################################################

                                                 This is right-aligned

                                             This is right-aligned and
                                              wrapped at 30 characters

                                This is right-aligned and wrapped
                                at 35 characters and right-padded
                                                  by 5 characters
----------------------------------------------------------------------


First Level Heading
===================
  This is center-aligned that will only wrap when hitting the edges

Second Level Heading
--------------------

                        This is center-aligned
                         with content with a
                      maximum width of 25 chars

>>>>> Third level Heading

    This is left-aligned text with a
maximum of 35 chars, and the first
line indented by 4

   This is left-aligned text with a maximum of 50
   chars, and the full string is indented by 4