Skip to main content

@lightbery/dynamic-cli@1.0.7

A tool for building "dynamic" CLI.

Works with
This package works with Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
JSR Score
47%
Published
3 days ago

DynamicCLI

A tool for building "dynamic" CLI.

Example

import { DynamicCLI } from './DynamicCLI'

new DynamicCLI()
  .createPage('Page1', 'A Page', () => ['This', 'Is', 'A', 'Page'])
  .createPage('Page2', 'Another Page', () => ['This', 'Is', 'Another', 'Page'])

Installation

You can copy simply DynamicCLI.ts into your project or use JSR to install.

[!Note] DynamicCLI need @types/wcwidth and wcwidth as its dependencies.

Contents

DynamicCLI

import { DynamicCLI } from './DynamicCLI'

new DynamicCLI(<options>) // Create a CLI
  • options <undefined | object> | Options for the CLI.
    • render <boolean> | Should the CLI render stuff onto the console. Default: true
    • renderInterval <number> | The interval(ms) between each render. Default: 50

Getters

  • .pages <string[]> | Get IDs of all the pages.
  • .input <string> | Get user input.
  • .currentPage <string> | Get the ID of current page.

Stop()

.stop() // Stop the CLI

return <undefined>

setLayout()

.setLayout(<layout>) // Set the layout of the CLI
  • layout <any[]> | An contains components.

return <DynamicCLI>

setStyle()

.setStyle(<style>) // Set the style of the CLI
  • style <object>

- Default Style -

{
  background: BackgroundColor.reset,

  selectBackground: BackgroundColor.white,
  selectFont: TextColor.gray,
  notSelectBackground: BackgroundColor.gray,
  notSelectFont: TextColor.white
}

return <DynamicCLI>

createPage()

.createPage(<id>, <name>, <callback>) // Create a page
  • id <string> | The ID of the page.
  • name <string> | The name of the page.
  • callback <function> | The render function of the page, the function must return <string[]>.

return <DynamicCLI>

deletePage()

.deletePage(<id>) // Delete a page
  • id <string> | The ID of the page.

return <DynamicCLI>

setInput()

.setInput(<string>) // Set the input
  • string <string> | The content of the input.

return <undefined>

simulateInput()

.simulateInput(<input>, <key>) // Simulate user input
  • input <string> | The full input.
  • key <Buffer> | The keystroke of the input.

return <undefined>

switchPage()

.switchPage(<id>) // Switch page
  • id <string> | The id of the page.

return <undefined>

listen()

.listen(<name>, <callback>) // Listen to an event
  • name <string> | The name of the event.
  • callback <function> | The function that triggers when the event is called.

- All Events -

name callback data description
scroll ({ page, cursorY, scrollY }) Triggered when user scrolls.
switchPage (pageID) Triggered when user switches pages.
enter (input) Triggered when user press enter.
input (key) Triggered when user input.

return <undefined>

render()

.render() // Render the CLI

return <{ line: number, content: string }[]>

Component

import { Component } from './DynamicCLI'

Component.<name>(<parameters>)

- All Components -

name parameters description
blank () A blank line.
text (callback) A line of text, callback must be a function that return a .
pageTabs () A line of page tabs.
pageContent () Multiple lines of page content.
input (placeholder) A line of input.

TextColor

import { TextColor } from './DynamicCLI'

TextColor.<name>

- All Colors -

reset, red, brightRed, yellow, brightYellow, green, brightGreen, cyan, brightCyan, blue, brightBlue, purple, brightPurple, white, black, gray

BackgroundColor

import { BackgroundColor } from './DynamicCLI'

BackgroundColor.<name>

- All Colors -

reset, red, brightRed, yellow, brightYellow, green, brightGreen, cyan, brightCyan, blue, brightBlue, purple, brightPurple, white, black, gray

Built and signed on
GitHub Actions
View transparency log

Add Package

npx jsr add @lightbery/dynamic-cli

Import symbol

import * as mod from "@lightbery/dynamic-cli";

Add Package

yarn dlx jsr add @lightbery/dynamic-cli

Import symbol

import * as mod from "@lightbery/dynamic-cli";

Add Package

pnpm dlx jsr add @lightbery/dynamic-cli

Import symbol

import * as mod from "@lightbery/dynamic-cli";

Add Package

bunx jsr add @lightbery/dynamic-cli

Import symbol

import * as mod from "@lightbery/dynamic-cli";