3.1.4 • Published 3 months ago

swiss-node v3.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

swiss-node

Swiss Army Knife for node

A collection of helper functions and useful little things for node.js

Uses swiss-ak

ask

A collection of functions to ask the user for input.

text

ask.text(question: string | Breadcrumb, initial: string, validate: (value: string) => Error | string | boolean | void, lc: LineCounter): Promise<string>

Get a text input from the user.

const name = await ask.text('What is your name?'); // 'Jack'
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1initialNostring
2validateNo(value: string) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<string>

autotext

ask.autotext(question: string | Breadcrumb, choices: ask.PromptChoice<T>[], initial: T | string, validate: (item: T, index: number, typedValue: string) => Error | string | boolean | void, lc: LineCounter): Promise<T>

Get a text input from the user, with auto-completion.

const name = await ask.autotext('What is your name?', ['Jack', 'Jane', 'Joe']); // 'Jack'
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1choicesYesask.PromptChoice<T>[]
2initialNoT \| string
3validateNo(item: T, index: number, typedValue: string) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<T>

number

ask.number(question: string | Breadcrumb, initial: number, validate: (value: number) => Error | string | boolean | void, lc: LineCounter): Promise<number>

Get a number input from the user.

const age = await ask.number('How old are you?'); // 30
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1initialNonumber
2validateNo(value: number) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<number>

boolean

ask.boolean(question: string | Breadcrumb, initial: boolean, validate: (value: boolean) => Error | string | boolean | void, lc: LineCounter): Promise<boolean>

Get a boolean input from the user (yes or no)

const isCool = await ask.boolean('Is this cool?'); // true
#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1initialNobooleantrue
2validateNo(value: boolean) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<boolean>

booleanYN

ask.booleanYN(question: string | Breadcrumb, validate: (value: boolean) => Error | string | boolean | void, lc: LineCounter): Promise<boolean>

Get a boolean input from the user (yes or no)

Alternative interface to ask.boolean

const isCool = await ask.booleanYN('Is this cool?'); // true
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1validateNo(value: boolean) => Error \| string \| boolean \| void
2lcNoLineCounter
Return Type
Promise<boolean>

select

ask.select(question: string | Breadcrumb, choices: ask.PromptChoice<T>[], initial: ask.PromptChoice<T> | number, validate: (item: T, index: number) => Error | string | boolean | void, lc: LineCounter): Promise<T>

Get the user to select an option from a list.

const colour = await ask.select('Whats your favourite colour?', ['red', 'green', 'blue']); // 'red'
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1choicesYesask.PromptChoice<T>[]
2initialNoask.PromptChoice<T> \| number
3validateNo(item: T, index: number) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<T>

multiselect

ask.multiselect(question: string | Breadcrumb, choices: ask.PromptChoice<T>[], initial: ask.PromptChoice<T> | ask.PromptChoice<T>[] | number | number[], validate: (items: T[], indexes: number[]) => Error | string | boolean | void, lc: LineCounter): Promise<T[]>

Get the user to select multiple opts from a list.

const colours = await ask.multiselect('Whats your favourite colours?', ['red', 'green', 'blue']); // ['red', 'green']
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1choicesYesask.PromptChoice<T>[]
2initialNoask.PromptChoice<T> \| ask.PromptChoice<T>[] \| number \| number[]
3validateNo(items: T[], indexes: number[]) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<T[]>

date

ask.date(questionText: string | Breadcrumb, initial: Date, validate: (date: Date) => Error | string | boolean | void, lc: LineCounter): Promise<Date>

Get a date input from the user.

const date = await ask.date('Whats the date?');
// [Date: 2023-01-01T12:00:00.000Z] (user inputted date, always at 12 midday)
#Parameter NameRequiredType
0questionTextNostring \| Breadcrumb
1initialNoDate
2validateNo(date: Date) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<Date>

time

ask.time(questionText: string | Breadcrumb, initial: Date, validate: (date: Date) => Error | string | boolean | void, lc: LineCounter): Promise<Date>

Get a time input from the user.

const time = await ask.time('Whats the time?');
// [Date: 2023-01-01T12:00:00.000Z] (user inputted time, with todays date)

const time2 = await ask.time('Whats the time?', new Date('1999-12-31'));
// [Date: 1999-12-31T12:00:00.000Z] (user inputted time, with same date as initial)
#Parameter NameRequiredType
0questionTextNostring \| Breadcrumb
1initialNoDate
2validateNo(date: Date) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<Date>

datetime

ask.datetime(questionText: string | Breadcrumb, initial: Date, validate: (date: Date) => Error | string | boolean | void, lc: LineCounter): Promise<Date>

Get a date and time input from the user.

const when = await ask.datetime('Whats the date/time?');
// [Date: 2023-03-05T20:30:00.000Z] (user inputted time & date)
#Parameter NameRequiredType
0questionTextNostring \| Breadcrumb
1initialNoDate
2validateNo(date: Date) => Error \| string \| boolean \| void
3lcNoLineCounter
Return Type
Promise<Date>

dateRange

ask.dateRange(questionText: string | Breadcrumb, initialStart: Date, initialEnd: Date, validate: (dates: [Date, Date]) => Error | string | boolean | void, lc: LineCounter): Promise<[Date, Date]>

Get a date range input from the user.

const range = await ask.dateRange('When is the festival?');
// [
//   [Date: 2023-03-01T12:00:00.000Z],
//   [Date: 2023-03-31T12:00:00.000Z]
// ]
#Parameter NameRequiredType
0questionTextNostring \| Breadcrumb
1initialStartNoDate
2initialEndNoDate
3validateNo(dates: [Date, Date]) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<[Date, Date]>

fileExplorer

fileExplorer

ask.fileExplorer(questionText: string | Breadcrumb, selectType: 'd' | 'f', startPath: string, validate: (path: string) => Error | string | boolean | void, lc: LineCounter): Promise<string>

Get a file or folder path from the user.

const file = await ask.fileExplorer('What file?', 'f');
// '/Users/user/Documents/some_file.txt'

const dir = await ask.fileExplorer('What file?', 'd', '/Users/jackcannon/Documents');
// '/Users/jackcannon/Documents/some_folder'
#Parameter NameRequiredTypeDefault
0questionTextYesstring \| Breadcrumb
1selectTypeNo'd' \| 'f''f'
2startPathNostringprocess.cwd()
3validateNo(path: string) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<string>

multiFileExplorer

ask.multiFileExplorer(questionText: string | Breadcrumb, selectType: 'd' | 'f', startPath: string, validate: (paths: string[]) => Error | string | boolean | void, lc: LineCounter): Promise<string[]>

Get multiple file or folder paths from the user.

const files = await ask.multiFileExplorer('What files?', 'f');
// [
//   '/Users/user/Documents/some_file_1.txt',
//   '/Users/user/Documents/some_file_2.txt',
//   '/Users/user/Documents/some_file_3.txt'
// ]
#Parameter NameRequiredTypeDefault
0questionTextYesstring \| Breadcrumb
1selectTypeNo'd' \| 'f''f'
2startPathNostringprocess.cwd()
3validateNo(paths: string[]) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<string[]>

saveFileExplorer

ask.saveFileExplorer(questionText: string | Breadcrumb, startPath: string, suggestedFileName: string, validate: (dir: string, filename?: string) => Error | string | boolean | void): Promise<string>

Get a file path from the user, with the intention of saving a file to that path.

const HOME_DIR = '/Users/user/Documents';
const savePath = await ask.saveFileExplorer('Save file', HOME_DIR, 'data.json');
// '/Users/user/Documents/data.json'
#Parameter NameRequiredTypeDefault
0questionTextYesstring \| Breadcrumb
1startPathNostringprocess.cwd()
2suggestedFileNameNostring''
3validateNo(dir: string, filename?: string) => Error \| string \| boolean \| void
Return Type
Promise<string>

table

A collection of functions for asking questions with tables.

table.select

ask.table.select(question: string | Breadcrumb, items: T[], settings: AskTableDisplaySettings<T>, initial: T | number, validate: (item: T) => Error | string | boolean | void, lc: LineCounter): Promise<T>

Get a single selection from a table.

const items = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 26 },
  { name: 'Derek', age: 27 }
];
const headers = [['Name', 'Age']];
const itemToRow = ({ name, age }) => [name, age];

const answer = await ask.table.select('Who?', items, undefined, itemToRow, headers);
// ┏━━━┳━━━━━━━┳━━━━━┓
// ┃   ┃ Name  ┃ Age ┃
// ┡━━━╇━━━━━━━╇━━━━━┩
// │   │ John  │ 25  │
// ├───┼───────┼─────┤
// │ ❯ │ Jane  │ 26  │
// ├───┼───────┼─────┤
// │   │ Derek │ 27  │
// └───┴───────┴─────┘
// Returns: { name: 'Jane', age: 26 }
#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1itemsYesT[]
2settingsNoAskTableDisplaySettings<T>{}
3initialNoT \| number
4validateNo(item: T) => Error \| string \| boolean \| void
5lcNoLineCounter
Return Type
Promise<T>

table.multiselect

ask.table.multiselect(question: string | Breadcrumb, items: T[], settings: AskTableDisplaySettings<T>, initial: T[] | number[], validate: (items: T[]) => Error | string | boolean | void, lc: LineCounter): Promise<T[]>

Get multiple selections from a table.

const items = [
  { name: 'John', age: 25 },
  { name: 'Jane', age: 26 },
  { name: 'Derek', age: 27 }
];
const headers = [['Name', 'Age']];
const itemToRow = ({ name, age }) => [name, age];

const answer = await ask.table.multiselect('Who?', items, undefined, itemToRow, headers);
┏━━━┳━━━━━━━┳━━━━━┓
┃   ┃ Name  ┃ Age ┃
┡━━━╇━━━━━━━╇━━━━━┩
│ ◉ │ John  │ 25  │
├───┼───────┼─────┤
│ ◯ │ Jane  │ 26  │
├───┼───────┼─────┤
│ ◉ │ Derek │ 27  │
└───┴───────┴─────┘
// [
//   { name: 'John', age: 25 },
//   { name: 'Derek', age: 27 }
// ]
#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1itemsYesT[]
2settingsNoAskTableDisplaySettings<T>{}
3initialNoT[] \| number[]
4validateNo(items: T[]) => Error \| string \| boolean \| void
5lcNoLineCounter
Return Type
Promise<T[]>

AskTableDisplaySettings

AskTableDisplaySettings<T>;

Settings for how the table should display the items

All settings are optional.

NameTypeDescription
rowsany[][] \| (item: T) => any[]Rows to display or function that takes an item and returns a row
headersany[][] \| RemapOf<T, string>Header to display, or object with title for each item property
optionstable.TableOptionsOptions object for table (some options are overridden)

trim

ask.trim(question: string | Breadcrumb, totalFrames: number, frameRate: number, initial: Partial<Handles<number>>, validate: (handles: Handles<number>) => Error | string | boolean | void, lc: LineCounter): Promise<Handles<number>>

Get a start and end frame from the user

#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1totalFramesYesnumber
2frameRateNonumber60
3initialNoPartial<Handles<number>>
4validateNo(handles: Handles<number>) => Error \| string \| boolean \| void
5lcNoLineCounter
Return Type
Promise<Handles<number>>

Extra

These are ask functions that don't prompt the user, but can help manage or organise how you use prompts

customise

ask.customise(options: Partial<ask.AskOptions>): void

Customise the behaviour/appearance of the ask prompts.

See ask.AskOptions for the options available.

ask.customise({ general: { themeColour: 'magenta' } }); // change the theme colour to magenta
ask.customise({ general: { lc } }); // set a line counter for that all prompts will add to when complete
ask.customise({ formatters: { formatPrompt: 'fullBox' } }); // change the format of the prompt
#Parameter NameRequiredType
0optionsYesPartial<ask.AskOptions>
Return Type
void

loading

ask.loading(question: string | Breadcrumb, isComplete: boolean, isError: boolean, lc: LineCounter): { stop: () => void; }

Display an animated loading indicator that imitates the display of a prompt

Intended to be indicate a question is coming, but something is loading first. For general 'loading' indicators, use out.loading.

const loader = ask.loading('What is your name?');
// ...
loader.stop();
#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1isCompleteNobooleanfalse
2isErrorNobooleanfalse
3lcNoLineCounter
Return Type
{ stop: () => void; }

countdown

ask.countdown(totalSeconds: number, template: (s: second) => string, isComplete: boolean, isError: boolean): Promise<void>

Animated countdown for a given number of seconds

await ask.countdown(5);
#Parameter NameRequiredType
0totalSecondsYesnumber
1templateNo(s: second) => string
2isCompleteNoboolean
3isErrorNoboolean
Return Type
Promise<void>

pause

ask.pause(text: string | Breadcrumb): Promise<void>

Pause the program until the user presses enter

await ask.pause();
#Parameter NameRequiredTypeDefault
0textNostring \| Breadcrumb'Press enter to continue...'
Return Type
Promise<void>

imitate

ask.imitate(question: string | Breadcrumb, result: any, isComplete: boolean, isError: boolean, errorMessage: string, lc: LineCounter): void

Imitate the display of a prompt

imitate('What is your name?', 'Jack', true);

ask.imitate('What is your name?', 'Jack', true);
#Parameter NameRequiredTypeDefault
0questionYesstring \| Breadcrumb
1resultNoany
2isCompleteNobooleantrue
3isErrorNobooleanfalse
4errorMessageNostring
5lcNoLineCounter
Return Type
void

prefill

ask.prefill(question: string | Breadcrumb, value: T | undefined, askFn: (question: string | Breadcrumb, lc: LineCounter) => Promise<T> | T, lc: LineCounter): Promise<T>

Auto-fills an ask prompt with the provided value, if defined.

Continues to display the 'prompt', but already 'submitted'

Good for keeping skipping parts of forms, but providing context and keeping display consistent

let data = {};
const name1 = ask.prefill(data.name, 'What is your name?', ask.text); // User input

data = {name: 'Jack'}
const name2 = ask.prefill(data.name, 'What is your name?', ask.text); // Jack
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1valueYesT \| undefined
2askFnYes(question: string \| Breadcrumb, lc: LineCounter) => Promise<T> \| T
3lcNoLineCounter
Return Type
Promise<T>

wizard

ask.wizard(startObj: Partial<T>): ask.Wizard<T>

Create a wizard object that can be used to build up a complex object

interface Example {
  foo: string;
  bar: number;
  baz: string;
}

const base: Partial<Example> = {
  baz: 'baz'
};

const wiz = ask.wizard<Example>(base);

await wiz.add('foo', ask.text('What is foo?')); // User input: foo

await wiz.add('bar', ask.number('What is bar?')); // User input: 123

const result = wiz.get(); // { baz: 'baz', foo: 'foo', bar: 123 }
#Parameter NameRequiredTypeDefault
0startObjNoPartial<T>{}
Return Type
ask.Wizard<T>

menu

ask.menu(question: string | Breadcrumb, items: MenuItem<T>[], initial: MenuItem<T> | T | number, validate: (value: T, index: number) => Error | string | boolean | void, lc: LineCounter): Promise<T>

Wrapper for ask.select that styles the output as a menu, with icons and colours

const menuItems: ask.MenuItem<string>[] = [
  { value: 'done', title: colr.dim(`[ Finished ]`), icon: '✔', colour: colr.dark.green.bold },
  { value: 'create', title: `${colr.bold('Create')} a new thing`, icon: '+', colour: colr.black.greenBg },
  { value: 'duplicate', title: `${colr.bold('Duplicate')} a thing`, icon: '⌥', colour: colr.black.cyanBg },
  { value: 'edit', title: `${colr.bold('Edit')} a thing`, icon: '↻', colour: colr.black.yellowBg },
  { value: 'delete', title: `${colr.bold('Remove')} thing(s)`, icon: '×', colour: colr.black.redBg },
  { value: 'delete-all', title: colr.bold(`Remove all`), icon: '✖', colour: colr.black.darkBg.redBg }
];

const result = await ask.menu('Pick a menu item', menuItems, 'edit'); // 'duplicate' (or other value)
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1itemsYesMenuItem<T>[]
2initialNoMenuItem<T> \| T \| number
3validateNo(value: T, index: number) => Error \| string \| boolean \| void
4lcNoLineCounter
Return Type
Promise<T>

section

ask.section(question: string | Breadcrumb, sectionHeader: (lc: LineCounter) => void | Promise<any>, ...questionFns: [...T][]): Promise<TupleFromQuestionFuncs<T>>

Allows information to be displayed before a question, and follow up questions to be asked, while only leaving the 'footprint' of a single question afterwards.

const ans1 = await ask.text('Question 1:');
const ans2 = await ask.section('Question 2:',
  (lc: LineCounter) => {
    lc.log('Some information');
  },
  (qst) => ask.text(qst),
  () => ask.text('Question 2b:')
);

During the section, it looks like this:

Question 1: answer1
┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄
Some information
┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄◦┄┄┄┄┄┄
Question 2: answer2
Question 2b: answer2b

After the last question in the section has been submitted, it looks like this:

Question 1: answer1
Question 2a: [ answer2, answer2b ]
#Parameter NameRequiredType
0questionYesstring \| Breadcrumb
1sectionHeaderNo(lc: LineCounter) => void \| Promise<any>
2…questionFnsNo[...T][]
Return Type
Promise<TupleFromQuestionFuncs<T>>

separator

ask.separator(version: 'down' | 'none' | 'up', spacing: number, offset: number, width: number, lc: LineCounter): void

Prints a separator line to the console.

ask.separator('down');
// ┄┄┄┄┄▿┄┄┄┄┄┄┄▿┄┄┄┄┄┄┄▿┄┄┄┄┄┄┄▿┄┄┄┄┄┄┄▿┄┄┄┄┄┄┄▿┄┄┄┄┄┄

ask.separator('none', 15);
// ┄┄┄┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄┄┄┄┄┄┄┄◦┄┄┄┄┄┄┄┄┄┄┄

ask.separator('up', 5, 2);
// ┄┄┄┄┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄▵┄┄┄┄┄┄┄┄
#Parameter NameRequiredTypeDefault
0versionNo'down' \| 'none' \| 'up''down'
1spacingNonumber8
2offsetNonumber0
3widthNonumberout.utils.getTerminalWidth() - 2
4lcNoLineCounter
Return Type
void

utils

itemsToPromptObjects

ask.utils.itemsToPromptObjects(items: T[], titles: string[], titleFn: TitleFn<T>): { title: string; value: T; }[]

Take an array of items and convert them to an array of prompt objects

ask.utils.itemsToPromptObjects(['lorem', 'ipsum', 'dolor'])
// [
//   { title: 'lorem', value: 'lorem' },
//   { title: 'ipsum', value: 'ipsum' },
//   { title: 'dolor', value: 'dolor' }
// ]

ask.utils.itemsToPromptObjects(['lorem', 'ipsum', 'dolor'], ['Lorem', 'Ipsum', 'Dolor'])
// [
//   { title: 'Lorem', value: 'lorem' },
//   { title: 'Ipsum', value: 'ipsum' },
//   { title: 'Dolor', value: 'dolor' }
// ]

ask.utils.itemsToPromptObjects(['lorem', 'ipsum', 'dolor'], undefined, (s) => s.toUpperCase())
// [
//   { title: 'LOREM', value: 'lorem' },
//   { title: 'IPSUM', value: 'ipsum' },
//   { title: 'DOLOR', value: 'dolor' }
// ]
#Parameter NameRequiredTypeDefault
0itemsYesT[]
1titlesNostring[][]
2titleFnNoTitleFn<T>
Return Type
{ title: string; value: T; }[]

AskOptions

ask.AskOptions;

Options to customise the behaviour/appearance of the ask prompts.

Use with ask.customise to set these options.

general Options

ask.AskOptions.general;

General options for customising ask prompts

NameTypeDescription
themeColourstring (Colour)Set the main theme colour
lcLineCounterA line counter that all ask prompts will add to when complete
boxType'thin' \| 'thick'What type of box drawing lines to use
beepsbooleanWhether to make an audio beeps when appropriate
maxItemsOnScreennumberHow many select/multiselect items to have on screen at most
scrollMarginnumberHow much space to leaving when 'scrolling' lists of items
fileExplorerColumnWidthnumberHow wide to make each panel of the fileExplorer interface
fileExplorerMaxItemsnumberHow many items to show in each panel of the fileExplorer interface
tableSelectMaxHeightPercentagenumberPercent of terminal height to use at max for table selects
timelineSpeednumberHow many frames to move on a timeline at a time
timelineFastSpeednumberHow many frames to move on a timeline at a time (fast mode)

text Options

ask.AskOptions.text;

English natural-language elements that you may wish to localise

NameTypeDescription
boolTrueKeysstringWhat buttons to use to indicate true for boolean prompts
boolFalseKeysstringWhat buttons to use to indicate false for boolean prompts
boolYesstring'Yes'
boolNostring'No'
boolYesNoSeparatorstring'/'
boolYNstring'(Y/n)'
selectAllstring'Select All'
donestring'done'
items(num: number) => string'X items'
countdown(secs: number) => string'Starting in Xs...'
filestring'File'
directorystring'Directory'
loadingstring'Loading...'
selected(num: number) => string'X selected'
specialNewFolderEnterNothingCancelstring'Enter nothing to cancel'
specialNewFolderAddingFolderTostring'Adding folder to '
specialNewFolderQuestion(hl: any) => string'What do you want to name the new folder?'
specialSaveFileSavingFileTostring'Saving file to '
specialSaveFileQuestion(hl: any) => string'What do you want to name the file?'

formatters Options

ask.AskOptions.formatters;

Functions for formatting how the prompts should display

formatPrompt
ask.AskOptions.formatters.formatPrompt;

How to format the prompts

Presets: oneLine, halfBox, halfBoxClosed, fullBox, fullBoxClosed

Type:

(
  question: string | Breadcrumb,
  value: string,
  items: string | undefined,
  errorMessage: string | undefined,
  theme: AskOptionsForState,
  isComplete: boolean,
  isExit: boolean
) => string;
formatItems
ask.AskOptions.formatters.formatItems;

How to format lists of items

Presets: block, blockAlt, simple, simpleAlt

Type:

<T extends unknown>(
  allItems: PromptChoiceFull<T>[],
  scrolledItems: ScrolledItems<PromptChoiceFull<T>>,
  selected: number[] | undefined,
  type: 'single' | 'multi',
  theme: AskOptionsForState,
  isExit: boolean
) => string;

colours Options

ask.AskOptions.colours;

Colours for all the different elements

All colours can be a single WrapFn value, or a set of WrapFn values, one for each state (normal, error, done) When single value, it is used for all states. When only a few states are set, the others will remain unchanged.

NameDescription
decorationGeneral decoration and cosmetics
questionTextThe text of the question of the prompt
specialIconSpecial icon for the 'state'
openingIconThe initial/opening icon
promptIconThe icon that indicates where you are typing
resultGeneral result
resultTextString results
resultNumberNumber results
resultBooleanBoolean results
resultArrayArray results
resultDateDate results
loadingIconIcon for ask.loading
errorMsgThe error message (if there is one)
itemA normal item in a list
itemIconIcon for a normal item in a list
itemHoverA hovered item in a list
itemHoverIconIcon for a hovered item in a list
itemBlockHoverA hovered item in a list (block mode)
itemBlockHoverIconIcon for a hovered item in a list (block mode)
itemSelectedA selected item in a list
itemSelectedIconIcon for a selected item in a list
itemUnselectedAn unselected item in a list
itemUnselectedIconIcon for an unselected item in a list
scrollbarTrackThe track for the scrollbar
scrollbarBarThe bar for the scrollbar
selectAllText'Select All' item in a multi-select
boolYNTextThe '(Y/n)' bit for the booleanYN prompt
countdownask.countdown
pauseask.pause
specialHoverThe focus of what the user is controlling (for dates, fileExplorer, etc)
specialSelectedSomething that has been selected (for dates, fileExplorer, etc)
specialHighlightMore important that normal (e.g. date within a range) (for dates, fileExplorer, etc)
specialNormalNormal items (for dates, fileExplorer, etc)
specialFadedNot important (for dates, fileExplorer, etc)
specialHintHints/tips/advice (for dates, fileExplorer, etc)
specialInactiveHoverThe focus of what the user is controlling (Inactive) (for dates, fileExplorer, etc)
specialInactiveSelectedSomething that has been selected (Inactive) (for dates, fileExplorer, etc)
specialInactiveHighlightMore important that normal (e.g. date within a range) (Inactive) (for dates, fileExplorer, etc)
specialInactiveNormalNormal items (Inactive) (for dates, fileExplorer, etc)
specialInactiveFadedNot important (Inactive) (for dates, fileExplorer, etc)
specialInactiveHintHints/tips/advice (Inactive) (for dates, fileExplorer, etc)
specialInfoAction bar at bottom (for dates, fileExplorer, etc)
specialErrorMsgError messages (for dates, fileExplorer, etc)
specialErrorIconIcon for errors (for dates, fileExplorer, etc)
tableSelectHoverHover for table selects only (shouldn't be 'block'/bg styles)
timelineTrackThe (inactive) track of a timeline
timelineTrackActiveThe active track of a timeline
timelineHandleThe (inactive) control handle on a timeline
timelineHandleActiveThe active control handle on a timeline

symbols Options

ask.AskOptions.symbols;

Variety of symbols and 'icons' for different aspects of the display

All symbols can be a single string value, or a set of string values, one for each state (normal, error, done) When single value, it is used for all states. When only a few states are set, the others will remain unchanged.

NameDescription
specialIconSpecial icon for the 'state'
openingIconThe initial/opening icon
promptIconThe icon that indicates where you are typing
errorMsgPrefixIcon shown before error messages
itemIconIcon for a normal item in a list
itemHoverIconIcon for a hovered item in a list
itemSelectedIconIcon for a selected item in a list
itemUnselectedIconIcon for an unselected item in a list
scrollUpIconUsed to indicate you can scroll up
scrollDownIconUsed to indicate you can scroll down
scrollbarTrackThe track part of the scrollbar
scrollbarTrackTrimTopThe trimmed top of the track (half height)
scrollbarTrackTrimBottomThe trimmed bottom of the track (half height)
scrollbarBarThe bar part of the scrollbar
scrollbarBarTrimTopThe trimmed top of the bar (half height)
scrollbarBarTrimBottomThe trimmed bottom of the bar (half height)
separatorLineLine added by ask.separator
separatorNodeDownNode is ask.separator line that indicates 'down'
separatorNodeNoneNode is ask.separator line that breaks up the pattern
separatorNodeUpNode is ask.separator line that indicates 'up'
specialErrorIconIcon for errors (for dates, fileExplorer, etc)
folderOpenableIconShown at end of line for folders to show they can be opened (right-wards)
fileOpenableIconFile version of folderOpenableIcon. Typically empty
timelineTrackThe track of a timeline
timelineHandleThe control handle on a timeline
timelineBarThe 'bar' (active portion) of a timeline

PromptChoice

ask.PromptChoice<T>;

A choice for a prompt

Equivalent to T | { title?: string; value?: T; selected?: boolean; }

out

A collection of functions to print to the console

getWidth

out.getWidth(text: string): number

A rough approximation of the width of the given text (as it would appear in the terminal)

Removes all ansi escape codes, and attempts to count emojis as 2 characters wide

Note: Many special characters may not be counted correctly. Emoji support is also not perfect.

out.getWidth('FOO BAR'); // 7
out.getWidth('↓←→↑'); // 4
out.getWidth(colr.red('this is red')); // 11
#Parameter NameRequiredType
0textYesstring
Return Type
number

pad

out.pad(line: string, start: number, end: number, replaceChar: string): string

Pad before and after the given text with the given character.

pad('foo', 3, 1, '-'); // '---foo-'
pad('bar', 10, 5, '_'); // '__________bar_____'
#Parameter NameRequiredTypeDefault
0lineYesstring
1startYesnumber
2endYesnumber
3replaceCharNostring' '
Return Type
string

center

out.center(item: any, width: number, replaceChar: string, forceWidth: boolean): string

Align the given text to the center within the given width of characters/columns

Giving a width of 0 will use the terminal width

out.center('foo', 10); // '   foo    '
out.center('something long', 10); // 'something long'
out.center('lines\n1\n2', 5);
// 'lines' + '\n' +
// '  1  ' + '\n' +
// '  2  '
#Parameter NameRequiredTypeDefault
0itemYesany
1widthNonumberout.utils.getTerminalWidth()
2replaceCharNostring' '
3forceWidthNobooleantrue
Return Type
string

left

out.left(item: any, width: number, replaceChar: string, forceWidth: boolean): string

Align the given text to the left within the given width of characters/columns

Giving a width of 0 will use the terminal width

out.left('foo', 10); // 'foo       '
out.left('something long', 10); // 'something long'
out.left('lines\n1\n2', 5);
// 'lines' + '\n' +
// '1    ' + '\n' +
// '2    '
#Parameter NameRequiredTypeDefault
0itemYesany
1widthNonumberout.utils.getTerminalWidth()
2replaceCharNostring' '
3forceWidthNobooleantrue
Return Type
string

right

out.right(item: any, width: number, replaceChar: string, forceWidth: boolean): string

Align the given text to the right within the given width of characters/columns

Giving a width of 0 will use the terminal width

out.right('foo', 10); // '       foo'
out.right('something long', 10); // 'something long'
out.right('lines\n1\n2', 5);
// 'lines' + '\n' +
// '    1' + '\n' +
// '    2'
#Parameter NameRequiredTypeDefault
0itemYesany
1widthNonumberout.utils.getTerminalWidth()
2replaceCharNostring' '
3forceWidthNobooleantrue
Return Type
string

justify

out.justify(item: any, width: number, replaceChar: string, forceWidth: boolean): string

Evenly space the text horizontally across the given width.

Giving a width of 0 will use the terminal width

const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
out.justify(out.wrap(lorem, 20), 20);
// 'Lorem  ipsum   dolor' + '\n' +
// 'sit            amet,' + '\n' +
// 'consectetur         ' + '\n' +
// 'adipiscing      elit'
#Parameter NameRequiredTypeDefault
0itemYesany
1widthNonumberout.utils.getTerminalWidth()
2replaceCharNostring' '
3forceWidthNobooleantrue
Return Type
string

leftLines

out.leftLines(lines: string[], width: number): string[]

Align each line of the given text to the left within the given width of characters/columns

out.leftLines(['This is line 1', 'This is a longer line 2', 'Line 3']);
// [
//   'This is line 1         ',
//   'This is a longer line 2',
//   'Line 3                 '
// ]
#Parameter NameRequiredTypeDefault
0linesYesstring[]
1widthNonumbergetLongestLen(lines)
Return Type
string[]

centerLines

out.centerLines(lines: string[], width: number): string[]

Align each line of the given text to the center within the given width of characters/columns

out.rightLines(['This is line 1', 'This is a longer line 2', 'Line 3']);
// [
//   '         This is line 1',
//   'This is a longer line 2',
//   '                 Line 3'
// ]
#Parameter NameRequiredTypeDefault
0linesYesstring[]
1widthNonumbergetLongestLen(lines)
Return Type
string[]

rightLines

out.rightLines(lines: string[], width: number): string[]

Align each line of the given text to the right within the given width of characters/columns

out.centerLines(['This is line 1', 'This is a longer line 2', 'Line 3']);
// [
//   '    This is line 1     ',
//   'This is a longer line 2',
//   '        Line 3         '
// ]
#Parameter NameRequiredTypeDefault
0linesYesstring[]
1widthNonumbergetLongestLen(lines)
Return Type
string[]
3.1.4

3 months ago

3.1.3

3 months ago

3.1.2

3 months ago

3.1.1

4 months ago

3.1.0

4 months ago

3.0.1

4 months ago

3.0.0

4 months ago

2.0.2

4 months ago

2.0.1

4 months ago

2.0.0

5 months ago

1.4.8

5 months ago

1.4.6

5 months ago

1.4.7

5 months ago

1.4.5

9 months ago

1.4.4

9 months ago

1.4.3

9 months ago

1.4.2

9 months ago

1.4.1

9 months ago

1.4.0

9 months ago

1.2.0

1 year ago

1.1.0

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago