Width

Control the column width.

When dealing with large values or texts, you can set a fixed column (max-)width to force line breaks. By default each column width depends on the largest value in it. Or you can make the columns fill all the terminal's width.

width

Using the width option you can set a fixed numeric one, auto or stretch.

fixed

In this case, all columns have the same fixed width.

import { Table } from 'voici.js'

const data = [
  {
    small: 'Lorem ipsum dolor sit amet',
    medium:
      'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet',
    large:
      'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
  },
  {
    small: 'Lorem ipsum dolor sit amet',
    medium:
      'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet',
    large:
      'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
  }
];

const config = {
  header: {
    width: 20
  }
};

const table = new Table(data, config);
table.printPlain();

Output:

  small                   medium                  large                 
========================================================================
  Lorem ipsum dolor si    At vero eos et accus    Lorem ipsum dolor si  
  t amet                  am et justo duo dolo    t amet, consetetur s  
                          res et ea rebum. Ste    adipscing elitr, sed  
                          t clita kasd gubergr     diam nonumy eirmod   
                          en, no sea takimata     tempor invidunt ut l  
                          sanctus est Lorem ip    abore et dolore magn  
                          sum dolor sit amet      a aliquyam erat, sed  
                                                   diam voluptua. At v  
                                                  ero eos et accusam e  
                                                  t justo duo dolores   
                                                  et ea rebum. Stet cl  
                                                  ita kasd gubergren,   
                                                  no sea takimata sanc  
                                                  tus est Lorem ipsum   
                                                  dolor sit amet.       
  Lorem ipsum dolor si    At vero eos et accus    Lorem ipsum dolor si  
  t amet                  am et justo duo dolo    t amet, consetetur s  
                          res et ea rebum. Ste    adipscing elitr, sed  
                          t clita kasd gubergr     diam nonumy eirmod   
                          en, no sea takimata     tempor invidunt ut l  
                          sanctus est Lorem ip    abore et dolore magn  
                          sum dolor sit amet      a aliquyam erat, sed  
                                                   diam voluptua. At v  
                                                  ero eos et accusam e  
                                                  t justo duo dolores   
                                                  et ea rebum. Stet cl  
                                                  ita kasd gubergren,   
                                                  no sea takimata sanc  
                                                  tus est Lorem ipsum   
                                                  dolor sit amet.       

auto

Using auto, the default value, every column uses as much space as needed. The size is based on the cells' content length.

stretch

By setting stretch, the entire terminal width is used. The percentage share remains the same and is based on the cells' content.

maxWidth

Using the maxWidth option you can set a fixed maximum width that is applied for each column. In this case, columns that do not fill the entire space have auto width based on their cells' content.

import { Table } from 'voici.js'

const data = [
  {
    small: 'Lorem ipsum dolor sit amet',
    medium:
      'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet',
    large:
      'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
  },
  {
    small: 'Lorem ipsum dolor sit amet',
    medium:
      'At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet',
    large:
      'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'
  }
];

const config = {
  header: {
    maxWidth: 80
  }
};

const table = new Table(data, config);
table.printPlain();

Output:

  small                         medium                                                                              large                                                                             
======================================================================================================================================================================================================
  Lorem ipsum dolor sit amet    At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergr    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod   
                                en, no sea takimata sanctus est Lorem ipsum dolor sit amet                          tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At v  
                                                                                                                    ero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,   
                                                                                                                    no sea takimata sanctus est Lorem ipsum dolor sit amet.                           
  Lorem ipsum dolor sit amet    At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergr    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod   
                                en, no sea takimata sanctus est Lorem ipsum dolor sit amet                          tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At v  
                                                                                                                    ero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,   
                                                                                                                    no sea takimata sanctus est Lorem ipsum dolor sit amet.                           

Last updated