Node.js UX

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.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.progress.init

init(): ProgressBar

Create a progress bar object representing a single progress bar.

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

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.

Example:

See ux.progress.init above

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.

Example:

See ux.progress.init above

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:

See ux.progress.init 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 or
ux.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.