• Beta
NodeJS UX
  • 18 Nov 2020
  • 3 Minutes To Read
  • Share
  • Dark
    Light

NodeJS UX

  • Share
  • Dark
    Light

ux.bold

ux.bold(text:string): string

Formats text as boldface for printing with ux.print or as a message in any of the other ux functions.

Formats correctly in the terminal and in Slack.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    const { permission } = await ux.prompt({
        type: "input",
        name: "permission",
        message: `If you are certain you want to ${ux.bold("permanently delete")} this repository, please enter its name`
    })
}

ux.italic

ux.italic(text:string): string

Formats text as italic for printing with ux.print or as a message in any of the other ux functions.

Formats correctly in the terminal and in Slack, but terminal support for italic text is limited.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    await ux.print(`We hope you have a ${ux.italic("very")} good experience with CTO.ai`)
}

ux.print

ux.print(text: string): Promise<void>

Display text to the user. Locally, prints to the terminal.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    await ux.print("Hello, welcome to the example command")
}

Output:

Hello, welcome to the example command

ux.progress.init

init(): ProgressBar

Create a progress bar object representing a single progress bar.

ProgressBar.start

start(length: number, initial: ?number, message: ?string): Promise<void>

Display a progress bar, with length increments of which initial are already ready, and with an accompanying optional message.

ProgressBar.increment

increment(amount?: number): Promise<void>

Increments the value of a progress bar by amount increments, or by 1 increment if no argument is provided.

ProgressBar.stop

stop(message?: string): Promise<void>

Completes the progress bar, incrementing it to 100% and optionally replacing the message with the given one.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    const progressBar = ux.progress.init()

    await progressBar.start(5, 0, 'Doing my complicated thing');

    // Do something
    await progressBar.increment();

    // Do something bigger
    await progressBar.increment(2);

    // Finish up
    await progressBar('Complicated thing done')
}

ux.prompt

prompt(questions: Question | Question[]): Promise<any>

Request input from the user. A variety of prompt types are available for different kinds of user input, as described on this page. A single call to ux.prompt may be passed a single prompt object or an array of objects; the result will contain answers to all of the prompts passed.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    const { name, age } = await ux.prompt([{
        type: "input",
        name: "name",
        message: "What is your name?"
    }, {
        type: "number",
        name: "age",
        message: "How old are you?",
        minimum: 0
    }])

    await ux.print(`Hi ${name}, I know you're ${age}.`)
}

ux.spinner.start

start(text: string): Promise<void>

Start a spinner accompanied with the given message.

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    await ux.spinner.start('Processing...')

    // Do your processing here
    await ux.spinner.stop('Processing complete!')
}

ux.spinner.stop

stop(text: string): Promise<void>

Stop the currently running spinner, replacing the message with the given text.

Example:

See ux.spinner.start above.

ux.wait

wait(duration: number): Promise<void>

Waits for 1 second or the given number of milliseconds

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    await ux.spinner.start('Waiting for two seconds...')

    await ux.wait(2000)

    await ux.spinner.stop('Wait over!')
}

ux.url

url(text: string, url: string): string

Create a terminal hyperlink, as supported by many terminals (e.g. iTerm2). Returns the raw text for use in sdk.log orux.print. If the current terminal does not support hyperlinks, renders as ${text} (${url}).

Example:

import { ux } from '@cto.ai/sdk'

const main = async () => {
    await ux.print("Visit my ${ux.url('homepage', 'https://cto.ai')}")
}

ux.table

Displays tabular data

ux.table(data, columns, options);

for more information: https://github.com/oclif/cli-ux#clitable

ux.tree

Generate a tree and display it

let tree = cli.tree();
tree.insert('foo');
tree.insert('bar');

let subtree = cli.tree();
subtree.insert('qux');
tree.nodes.bar.insert('baz', subtree);

tree.display();

Outputs:

├─ foo
└─ bar
   └─ baz
      └─ qux

🚀 What's next?

  • If you want to explore the options offered by our SDKs, go to the Node.js SDK page.
  • To see which prompts we offer for this SDK, you can go to Node.js Prompts page.
Was This Article Helpful?