Coder Social home page Coder Social logo

xpl / as-table Goto Github PK

View Code? Open in Web Editor NEW
61.0 5.0 10.0 990 KB

A simple function that prints objects as ASCII tables. Supports ANSI styling and weird Unicode 💩 emojis – they won't break the layout.

Home Page: http://npmjs.com/package/as-table

License: MIT License

JavaScript 100.00%
ascii-tables pretty-print stringify javascript

as-table's Introduction

as-table

Build Status Coverage Status npm dependencies Status Scrutinizer Code Quality

A simple function that print objects and arrays as ASCII tables. Supports ANSI styling and weird 💩 Unicode emoji symbols (they won't break the layout), thanks to printable-characters.

npm install as-table

Printing objects

asTable = require ('as-table')

asTable ([ { foo: true,  string: 'abcde',      num: 42 },
           { foo: false, string: 'qwertyuiop', num: 43 },
           {             string:  null,        num: 44 } ])
foo    string      num
----------------------
true   abcde       42 
false  qwertyuiop  43 
       null        44 

Printing arrays

asTable ([['qwe',       '123456789', 'zxcvbnm'],
          ['qwerty',    '12',        'zxcvb'],
          ['qwertyiop', '1234567',   'z']])
qwe        123456789  zxcvbnm
qwerty     12         zxcvb
qwertyiop  1234567    z

Limiting total width by proportionally trimming cells + setting columns delimiter

asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (data)
qwe   | 1234… | zxc…
qwer… | 12    | zxc…
qwer… | 1234… | z   

Right align

asTable.configure ({ right: true }) (data)
      foo        bar      baz
-----------------------------
      qwe  123456789  zxcvbnm
   qwerty         12    zxcvb
qwertyiop    1234567        z

Providing a custom object printer

asTable.configure ({ print: x => (typeof x === 'boolean') ? (x ? 'yes' : 'no') : String (x) }) (data)
foo  string      num
--------------------
yes  abcde       42 
no   qwertyuiop  43 
     null        44 

The callback also receives a field name (in case of objects) or a column index (in case of arrays):

asTable = require ('as-table').configure ({
    print (x, k) {
        if (k === 'timestamp') return new Date (x).toGMTString()
        return String (x)
    }
})

asTable ([ { name: 'A', timestamp: 1561202591572 },
           { name: 'B', timestamp: 1558524240034 } ])

Obtaining a pre-configured function

asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })

asTable (data)

Customizing the title rendering and the header separator

With string coloring by ansicolor (just for the demo purposes, any library will fit):

asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })

console.log (
   asTable ([ { foo: true,  string: 'abcde',                             num: 42 },
              { foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ])

screen shot 2017-07-21 at 23 46 14

as-table's People

Contributors

dependabot[bot] avatar forivall avatar kroitor avatar neolao avatar xpl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

as-table's Issues

Please add border and index

Hi
Now only delimiter and dash can be set. Could you add borders to the table and replace the characters with more appropriate ones. I know that delimiter and dash can be set independently, but line intersections cannot. Also could you add the index as the first column?

Example:
2023-04-06_21-31-59

double width unicode character not displayed correctly

Problem

Tables with double width unicode characters are not correctly aligned (see screenshot).

observed output

grafik

sidenote

The vi Editor in the same terminal window can handles these characters correctly. (the cursor does not jump left/right when going up/down on lines that contain such characters)

versions

  • "as-table": "^1.0.55",
  • nodejs 15.12.0
  • GNOME Terminal 3.38.1
  • kitty terminal 0.20.3
  • Ubuntu 21.04
  • latest NerdFont

example program

const asTable = require ('as-table').configure({delimiter:" | "})
 
console.log(asTable ( [ 
{ string: 'abcde',      num: "ok" },
{ string: 'ᄍ', num: "ok" },
{ string: '無', num: "bad" },
{ string: '🕑', num: "bad" },
{ string: '💩', num: "bad" },
{ string: '⬢', num: "ok" },
]))

Wrap Text

First of all thanks for the module.

Is it possible to wrap the text of field data? Setting the widths of columns results in the data being truncated. I used the print option to format my data so that a newline was inserted at the 20th character but \n is rendered as is (i.e. not as a line break):

console.log(asTable.configure({
    maxTotalWidth: 100,
    delimiter: ' | ',
    print: x => x.slice(0, 20) + '\n' + x.slice(21)
})(data));

I think this is due to

cells = rows.map (r => r.map (c => c.replace (/\n/g, '\\n'))),

Is there any way I can get around this?

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.