In an oclif-cli interface similar to a mysql-client-cli, what is the process for presenting data retrieved from a database

Currently, I am in the process of building a CLI tool utilizing OCLIF Framework and TypeScript. One of the commands I have developed retrieves all values from the database successfully. However, once retrieved, I would like to display this data in tables format within the terminal.

https://i.sstatic.net/8CJ1G.png

Are there any plugins or alternative methods available to assist in formatting the CLI output as shown in the image above?

Answer №1

Are you familiar with the table function provided by cli-ux library?

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
    })
  }
}

This code snippet generates the following output:

$ 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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Import statements can only be used within a module in NodeJS when using Typescript

Having trouble writing Jest tests in my Express Typescript application. Whenever I try to import functions from another file in the test files, I encounter a SyntaxError: Cannot use import statement outside a module. Here is an excerpt from my package.json ...

Mixing TypeScript generic types loosely

Let's explore a simple example of typescript mixins: import { Observable, of } from 'rxjs'; class Service<TDataType> { public foo(f: TDataType): Observable<TDataType> { return of(f); } } type GConstructor<T = {}> = new ...

It appears that I am encountering an issue when attempting to utilize the async pipe operator

<div class="image" *ngFor="let entry of (currentUserData | async)?.entries"></div> Error message: The term 'currentUserData' is not recognized. Please check the component declaration, template variable declarations, and element refe ...

The type 'MockStoreEnhanced<unknown, {}>' is not compatible with the type 'IntrinsicAttributes & Pick[...]

I'm currently working on creating a unit test for a container in TypeScript. From what I've gathered from various responses, it's recommended to use a mock store and pass it to the container using the store attribute. This method seems to o ...

Guide to creating a SVG component using typescript in a React application

I have a component where I am passing SVG icons props as an array containing my SVG component data. However, TypeScript is showing me an error while working with Material UI. Type '{ key: number; data: { id: number; icon: ReactNode; }; }' is not ...

You cannot utilize Lesson as a JSX Component in Next JS TypeScript

Below is my updated page.tsx code: import Aspects from '@/components/Aspects'; import FreeForm from '@/components/FreeForm'; import Lesson from '@/components/Lesson'; import React from 'react'; import { Route, Route ...

Utilizing Angular/TypeScript to perform mathematical calculations within the component.ts file, it has been found that the addition operator (+) does not

In this scenario, I have chosen to utilize a different approach instead of using type="number" because users can easily remove it by pressing F12. While including type="number" in my HTML code does work, it still remains unclear why the minus sign work ...

Mat backspin dialogue spinner

Our current setup involves using a progress spinner for each API call. The spinner is registered at the app module level, but we are facing an issue where the spinner hides behind the dialog popup. In order to solve this problem, we have decided to includ ...

Guide to transforming API Response into Custom type in Angular 5

Describing my method to structure the API Response - interface MyTest { property2: string } Incorporating Angular 5 Service Code - getAPI(searchKey: string) { this.productsAPIUrl = https://localhost:44331/api/SampleData/WeatherFore ...

Issue with Angular 8: ng lint error - Reached maximum call stack size limit

I am facing an issue with linting my project. When I run "ng lint," I encounter the following error: $ ng lint Linting "app"... An unhandled exception occurred: Maximum call stack size exceeded See "C:\Users\6100BR~1\AppData\Local&bsol ...

Retrieve a single record in Angular/Typescript and extract its ID value

There is data stored in a variable that is displayed in the Chrome console like this: 0: @attributes: actPer: "1", id: "19" 1: @attributes: actPer: "1" id: "17" etc To filter this data, the following code was used: myvar = this.obj.listR ...

Can I combine tuple types in Typescript?

type A1 = ['x','y','z'] type A2 = ['u','v','w'] type AN = [.., .., ..] type C = Combine<A1,A2,...,AN> //or Combine<[A1,A2,...,AN]> //resulting in ['x','y','z& ...

error TS5023: Compiler option 'enableIvy' is not recognized

I recently attempted to integrate IVY into my angular 7 beta project. To do this, I included enableIvy: true in the compilerOptions section of src/tsconfig.app.json. However, upon running ng build --prod --aot --output-hashing none, an error was encounter ...

When a function is transferred from a parent component to a child component's Input() property, losing context is a common issue

I have encountered an issue while passing a function from the parent component to the child component's Input() property. The problem arises when the parent's function is called within the child component, causing the this keyword to refer to th ...

What are the best practices for passing the type of getServerSideProps to the page props in Next.js?

After discovering two working solutions, I am unsure which approach is more consistent. interface ICatalogHome { response: Result<CatalogHomePageResponse>; } export const getServerSideProps: GetServerSideProps<ICatalogHome> = async (contex ...

Angular2 and Typescript paired with Visual Studio 2013

Currently, I am utilizing the angular2 QUICKSTART and encountering an issue where Visual Studio fails to recognize Angular2 with typescript import Modules. However, everything else seems to be functioning correctly: https://i.stack.imgur.com/0s46Y.jpg Th ...

Exploring the best way to access ViewContainerRef: ViewChild vs Directive

While researching, I came across a recommendation in the Angular Docs that suggests using a directive to access the ViewContainerRef for creating dynamic components. Here is an example of such a directive: import { Directive, ViewContainerRef } from &apos ...

New options for outdated Webpack i18n plugin and loader

I am currently working on a TypeScript project that requires loading translations from individual .json files assigned to each country. For instance, we would have separate language files like en.json, es.json. The goal is to be able to access these trans ...

What is the best way to transform the request query id = ' [12eetftt76237,jhgasduyas7657] ' into an array of elements or strings like [12eetftt76237,jhgasduyas7657]?

Hey there, I am working on a project using hapijs and typescript. I have a requirement to send an array of IDs as parameters through the request URL. Here is an example of the URL: localhost:3444/?id='[askjajk78686,ajshd67868]' I attempted to u ...

After destructuring, useParams may sometimes return as undefined

I'm encountering an issue with undefined variables being returned even after destructuring when using useParams(). I've tried various solutions but none seem to work for me. const App = () => { return ( <div className="container"> ...