KScript.Terminal Prompts, menus, colours, Markdown, and artisan-style labels, tasks and tables for terminal scripts.
Import it at the top of a script:
.kscript XML Copy
<import package="KScript.Terminal" />.kscript XML Copy
import KScript.TerminalElements Statements # <alert>alertPrints the text after a label: type is info (the default), success, warn or error. Errors go to stderr.
Holds text with { } values. Scripts only.
Attribute Option Value Description typeinfo|success|warn|errorThe label: info, success, warn or error.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<alert type="error">The file [data.csv] is empty.</alert>.kscript XML Copy
import KScript.Terminal
alert(type: error) "The file [data.csv] is empty."# <bullets>bulletsPrints each item of a list on its own line, after a ⇂.
Self-closing. A statement on its own. Scripts only.
Attribute Option Value Description items required expression The list.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<bullets items="['routes/web.kscript', 'views/home.html']" />.kscript XML Copy
import KScript.Terminal
bullets(items: ['routes/web.kscript', 'views/home.html'])# <clear>clearClears the terminal.
Self-closing. A statement on its own. Scripts only.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<clear />
<echo>A fresh screen</echo>.kscript XML Copy
import KScript.Terminal
clear
echo 'A fresh screen'# <confirm>confirmAsks a yes/no question and stores true or false.
Holds text with { } values. Scripts only.
Attribute Option Value Description into required a new variable's name The variable that receives the answer. defaulttrue or falseThe answer when nothing is typed.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<confirm into="sure">Delete the files?</confirm>.kscript XML Copy
import KScript.Terminal
confirm(into: sure) "Delete the files?"# <detail>detailPrints a name, a line of dots and a value: " Version ........ 1.2". The text is the value.
Holds text with { } values. Scripts only.
Attribute Option Value Description name required a string with { } The name on the left.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<detail name="Total">{ 1 + 2 }</detail>.kscript XML Copy
import KScript.Terminal
detail(name: 'Total') "{ 1 + 2 }"# <heading>headingPrints a heading in bold yellow, like the sections of kscript --help.
Holds text with { } values. Scripts only.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<heading>Settings:</heading>.kscript XML Copy
import KScript.Terminal
heading "Settings:"# <info>infoPrints 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
.kscript XML Copy
<import package="KScript.Terminal" />
<info>Saved [report.pdf].</info>.kscript XML Copy
import KScript.Terminal
info "Saved [report.pdf]."# <input>inputAsks 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.
Attribute Option Value Description into required a new variable's name The 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. defaultexpression The 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
.kscript XML Copy
<import package="KScript.Terminal" />
<input into="name">What's your name? </input>
<input into="age" type="int">Age: </input>.kscript XML Copy
import KScript.Terminal
input(into: name) "What's your name? "
input(into: age, type: int) "Age: "# <pause>pauseWaits for a key press (or Enter when input is piped). The element text is the prompt.
Holds text with { } values. Scripts only.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<pause>Press any key to continue...</pause>.kscript XML Copy
import KScript.Terminal
pause "Press any key to continue..."# <picker>pickerAsks for a file or folder path, and asks again until it exists.
Holds text with { } values. Scripts only.
Attribute Option Value Description into required a new variable's name The 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
.kscript XML Copy
<picker into="folder">Which folder? </picker>.kscript XML Copy
picker(into: folder) "Which folder? "# <progress>progressShows 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.
Attribute Option Value Description into required a new variable's name The variable that receives the bar. total required expression How many steps there are, such as $files->count().
Example
.kscript XML Copy
<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>.kscript XML Copy
import KScript.Terminal
$files = ['a.csv', 'b.csv', 'c.csv']
progress(into: bar, total: $files->count()) "Importing"
for file in $files {
$bar->advance()
}# <success>successPrints the text after a green DONE label.
Holds text with { } values. Scripts only.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<success>Saved [report.pdf].</success>.kscript XML Copy
import KScript.Terminal
success "Saved [report.pdf]."# <table>tablePrints 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.
Attribute Option Value Description rows required expression The rows: a list of maps (each a row), or a list of lists (the first is the heading row). columnsexpression Which 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. alignexpression left, center or right for each column, such as ['left', 'right']. Numbers line up right by default. headertrue or falsefalse: no heading row.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<table rows="[{ name: 'Joan', age: 82 }, { name: 'Sam', age: 9 }]" columns="{ Name: 'name', Age: 'age' }" />.kscript XML Copy
import KScript.Terminal
table(rows: [{ name: 'Joan', age: 82 }, { name: 'Sam', age: 9 }], columns: { Name: 'name', Age: 'age' })# <task>taskRuns 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.
Attribute Option Value Description name required a string with { } What the task does, such as Copying files.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<task name="Adding up">
<let name="total" value="1 + 2" />
</task>.kscript XML Copy
import KScript.Terminal
task(name: 'Adding up') {
$total = 1 + 2
}# <warn>warnPrints the text after a yellow WARN label.
Holds text with { } values. Scripts only.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<warn>Saved [report.pdf].</warn>.kscript XML Copy
import KScript.Terminal
warn "Saved [report.pdf]."Commands Commands are called with @, in any expression: @name(arguments).
Bold text.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @bold('Important') }</echo>.kscript XML Copy
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
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @color('Done', 'green') }</echo>.kscript XML Copy
import KScript.Terminal
echo @color('Done', 'green')Dimmed text.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @dim('(optional)') }</echo>.kscript XML Copy
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
.kscript XML Copy
<import package="KScript.Terminal" />
<if @is_terminal()><clear /></if>.kscript XML Copy
import KScript.Terminal
if @is_terminal() {
clear
}# @italic (text ) → string Italic text.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @italic('emphasis') }</echo>.kscript XML Copy
import KScript.Terminal
echo @italic('emphasis')# @terminal_width () → number The width of the terminal in characters (80 when it isn't a terminal).
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @terminal_width() }</echo>.kscript XML Copy
import KScript.Terminal
echo @terminal_width()# @underline (text ) → string Underlined text.
Example
.kscript XML Copy
<import package="KScript.Terminal" />
<echo>{ @underline('a link') }</echo>.kscript XML Copy
import KScript.Terminal
echo @underline('a link')Methods On progress_bar values: ->advance()