typescriptcommand-line-interfaceoclif

How to display db retrieved data in table in oclif-cli like mysql-client-cli?


I am developing a CLI tool using OCLIF Framework and TypeScript, I have command that returns all the values from the database , Everything works fine until the retrieval , How ever I wanted the retrieved data to be displayed like tables in the terminal.

enter image description here

Is there any pluggins or something else that helps in designing CLI to display like this ?


Solution

  • Have you had a look at cli-ux table function?

    import {Command} from '@oclif/command'
    import {cli} from 'cli-ux'
    
    export default class Users extends Command {
      static flags = {
        ...cli.table.flags()
      }
    
      async run() {
        const {flags} = this.parse(Users)
        /* ... */
    
        cli.table(users, {
          name: {
            minWidth: 7,
          },
          company: {
            get: row => row.company && row.company.name
          }
        }, {
          printLine: this.log,
          ...flags, // parsed flags
        })
      }
    }
    

    Results in:

    $ example-cli users
    Name                     Company
    Leanne Graham            Romaguera-Crona
    Ervin Howell             Deckow-Crist
    Clementine Bauch         Romaguera-Jacobson
    Patricia Lebsack         Robel-Corkery
    Chelsey Dietrich         Keebler LLC
    Mrs. Dennis Schulist     Considine-Lockman
    Kurtis Weissnat          Johns Group
    Nicholas Runolfsdottir V Abernathy Group
    Glenna Reichert          Yost and Sons
    Clementina DuBuque       Hoeger LLC