KScript docs
EN

KScript.Terminal

Prompts, menus, colours, Markdown, and artisan-style labels, tasks and tables for terminal scripts.

Import it at the top of a script:

<import package="KScript.Terminal" />
import KScript.Terminal

ElementsStatements

#<alert>alert

Prints the text after a label: type is info (the default), success, warn or error. Errors go to stderr.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
typeinfo|success|warn|errorThe label: info, success, warn or error.
Example
<import package="KScript.Terminal" />
<alert type="error">The file [data.csv] is empty.</alert>
import KScript.Terminal
alert(type: error) "The file [data.csv] is empty."

#<bullets>bullets

Prints each item of a list on its own line, after a ⇂.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
items requiredexpressionThe list.
Example
<import package="KScript.Terminal" />
<bullets items="['routes/web.kscript', 'views/home.html']" />
import KScript.Terminal
bullets(items: ['routes/web.kscript', 'views/home.html'])

#<clear>clear

Clears the terminal.

Self-closing.A statement on its own. Scripts only.

Example
<import package="KScript.Terminal" />
<clear />
<echo>A fresh screen</echo>
import KScript.Terminal
clear
echo 'A fresh screen'

#<confirm>confirm

Asks a yes/no question and stores true or false.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the answer.
defaulttrue or falseThe answer when nothing is typed.
Example
<import package="KScript.Terminal" />
<confirm into="sure">Delete the files?</confirm>
import KScript.Terminal
confirm(into: sure) "Delete the files?"

#<detail>detail

Prints a name, a line of dots and a value: " Version ........ 1.2". The text is the value.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
name requireda string with { }The name on the left.
Example
<import package="KScript.Terminal" />
<detail name="Total">{ 1 + 2 }</detail>
import KScript.Terminal
detail(name: 'Total') "{ 1 + 2 }"

#<heading>heading

Prints a heading in bold yellow, like the sections of kscript --help.

Holds text with { } values. Scripts only.

Example
<import package="KScript.Terminal" />
<heading>Settings:</heading>
import KScript.Terminal
heading "Settings:"

#<info>info

Prints the text after a blue INFO label, as kscript's own commands do. [Bracketed] parts are shown bold.

Holds text with { } values. Scripts only.

Example
<import package="KScript.Terminal" />
<info>Saved [report.pdf].</info>
import KScript.Terminal
info "Saved [report.pdf]."

#<input>input

Asks for a line of input and stores it. The element text is the prompt. With type, the answer is converted and asked again until it fits.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the answer.
type'string'|'int'|'number'|'float'|'bool'|'email'|'handle'|'uuid'|'password'Convert the answer: string (the default), int, number, bool, email, handle, uuid or password. A password is never echoed back.
defaultexpressionThe value used when the answer is empty (or input has ended).
secrettrue or falsetrue hides what is typed. type="password" hides it too and keeps the answer as a password.
errora string with { }What to say when the answer doesn't fit the type.
Example
<import package="KScript.Terminal" />
<input into="name">What's your name? </input>
<input into="age" type="int">Age: </input>
import KScript.Terminal
input(into: name) "What's your name? "
input(into: age, type: int) "Age: "

#<menu>menu

Shows a numbered list and stores the chosen item. The element text is the title.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the chosen item.
options requiredexpressionThe choices: a list, or a map (the key is shown, the value is stored).
ata new variable's nameA variable for the chosen position, from 0 (a list of positions with multiple).
multipletrue or falsetrue lets the answer pick several, such as 1, 3: the variable is then a list of the chosen items, in the menu's order.
Example
<import package="KScript.Terminal" />
<menu into="choice" options="['Settings', 'Exit']">What next?</menu>
<menu into="toppings" options="['Cheese', 'Ham', 'Olives']" multiple="true">Toppings?</menu>
import KScript.Terminal
menu(into: choice, options: ['Settings', 'Exit']) "What next?"
menu(into: toppings, options: ['Cheese', 'Ham', 'Olives'], multiple: true) "Toppings?"

#<pause>pause

Waits for a key press (or Enter when input is piped). The element text is the prompt.

Holds text with { } values. Scripts only.

Example
<import package="KScript.Terminal" />
<pause>Press any key to continue...</pause>
import KScript.Terminal
pause "Press any key to continue..."

#<picker>picker

Asks for a file or folder path, and asks again until it exists.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the path.
typefolder|fileWhat to ask for: folder (the default) or file.
must_existtrue or falseno accepts paths that don't exist yet.
errora string with { }What to say when the path doesn't exist.
Example
<picker into="folder">Which folder? </picker>
picker(into: folder) "Which folder? "

#<progress>progress

Shows a progress bar for work done in steps, and stores it: $bar->advance() moves it on. The element text is its label. At a terminal it is redrawn in place; piped output gets one line when it is done.

Holds text with { } values. Scripts only.

AttributeOptionValueDescription
into requireda new variable's nameThe variable that receives the bar.
total requiredexpressionHow many steps there are, such as $files->count().
Example
<import package="KScript.Terminal" />
<let name="files" value="['a.csv', 'b.csv', 'c.csv']" />
<progress into="bar" total="$files->count()">Importing</progress>
<for each="file" in="$files">
  <do>$bar->advance()</do>
</for>
import KScript.Terminal
$files = ['a.csv', 'b.csv', 'c.csv']
progress(into: bar, total: $files->count()) "Importing"
for file in $files {
  $bar->advance()
}

#<success>success

Prints the text after a green DONE label.

Holds text with { } values. Scripts only.

Example
<import package="KScript.Terminal" />
<success>Saved [report.pdf].</success>
import KScript.Terminal
success "Saved [report.pdf]."

#<table>table

Prints a list of maps (or of lists) as a table with borders, like kscript queue status. It takes the same rows, columns, align and header as a document's Table.

Self-closing.A statement on its own. Scripts only.

AttributeOptionValueDescription
rows requiredexpressionThe rows: a list of maps (each a row), or a list of lists (the first is the heading row).
columnsexpressionWhich values of each map to show, in order: a list of keys ['name', 'age'], or a map from heading to key { Name: 'name' }. Defaults to the first row's keys.
alignexpressionleft, center or right for each column, such as ['left', 'right']. Numbers line up right by default.
headertrue or falsefalse: no heading row.
Example
<import package="KScript.Terminal" />
<table rows="[{ name: 'Joan', age: 82 }, { name: 'Sam', age: 9 }]" columns="{ Name: 'name', Age: 'age' }" />
import KScript.Terminal
table(rows: [{ name: 'Joan', age: 82 }, { name: 'Sam', age: 9 }], columns: { Name: 'name', Age: 'age' })

#<task>task

Runs what's inside, then prints " name ........ 12ms DONE" (or FAIL, when it throws: the error goes on to be caught as usual).

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
name requireda string with { }What the task does, such as Copying files.
Example
<import package="KScript.Terminal" />
<task name="Adding up">
  <let name="total" value="1 + 2" />
</task>
import KScript.Terminal
task(name: 'Adding up') {
  $total = 1 + 2
}

#<warn>warn

Prints the text after a yellow WARN label.

Holds text with { } values. Scripts only.

Example
<import package="KScript.Terminal" />
<warn>Saved [report.pdf].</warn>
import KScript.Terminal
warn "Saved [report.pdf]."

Commands

Commands are called with @, in any expression: @name(arguments).

#@bold(text) → string

Bold text.
Example
<import package="KScript.Terminal" />
<echo>{ @bold('Important') }</echo>
import KScript.Terminal
echo @bold('Important')

#@color(text, color) → string

Text in a colour: red, green, yellow, blue, magenta, cyan, white, gray (or a bright_ one). Plain text when the output isn't a terminal.

Arguments can be named: @color(…, color: …).

Example
<import package="KScript.Terminal" />
<echo>{ @color('Done', 'green') }</echo>
import KScript.Terminal
echo @color('Done', 'green')

#@dim(text) → string

Dimmed text.
Example
<import package="KScript.Terminal" />
<echo>{ @dim('(optional)') }</echo>
import KScript.Terminal
echo @dim('(optional)')

#@is_terminal() → bool

True when a person is at a terminal, false when input or output is redirected (a pipe, a file, a test). Use it to skip animations and prompts.
Example
<import package="KScript.Terminal" />
<if @is_terminal()><clear /></if>
import KScript.Terminal
if @is_terminal() {
  clear
}

#@italic(text) → string

Italic text.
Example
<import package="KScript.Terminal" />
<echo>{ @italic('emphasis') }</echo>
import KScript.Terminal
echo @italic('emphasis')

#@terminal_width() → number

The width of the terminal in characters (80 when it isn't a terminal).
Example
<import package="KScript.Terminal" />
<echo>{ @terminal_width() }</echo>
import KScript.Terminal
echo @terminal_width()

#@underline(text) → string

Underlined text.
Example
<import package="KScript.Terminal" />
<echo>{ @underline('a link') }</echo>
import KScript.Terminal
echo @underline('a link')

Methods

On progress_bar values: ->advance()