term
Text colour and style, cursor movement, progress bars, reading keys and the mouse, and the terminal size.
import term
The term package has what a command line program needs to look good in a terminal: text colour and style, columns that line up, progress bars and spinners, and for full screen programs, cursor movement, the alternate screen and reading keys one at a time.
The colour functions, such as Red, return a new string and print nothing. In the default ColorAuto mode they add nothing when the output goes to a file or another program, when NO_COLOR is set or when TERM=dumb, so the text stays plain; SetColorMode changes that, for a --color option for instance. The functions whose names start with Seq only return an escape sequence, and the one with the same name without the prefix sends it right away. To read keys, put the terminal into raw mode with MakeRaw or MakeCbreak, and always put it back with Restore.
For one line questions such as a name or a yes or no, cli is simpler; for log lines, log; for padding plain text, fmt.
The things people get wrong:
- Padding coloured text with
len. A colour code adds bytes but no columns; usePadRightand the other functions here, which measure withWidth. - Trusting
Widthwith every script. It counts one column per character: Chinese and Japanese characters, which take two columns, count as one, and invisible ones such as the zero width non-joiner in Persianمیشودcount as one too. Tables of such text need padding of your own. - Leaving the terminal in raw mode. Call
Restorewithdeferright afterMakeRaw, and leave the alternate screen and show the cursor on every way out. - Expecting a progress bar in a log.
BarDrawdraws nothing when the output is not a terminal, andBarFinishprints the final bar once, so a CI log gets one clean line.
A coloured table that lines up
A colour code adds bytes to a string but no columns on the screen, so padding with len would push every coloured cell out of line. PadRight, PadLeft and Truncate measure with Width, which skips escape codes, so a table lines up whether its cells are coloured or not.
import term
struct Service:
pub name: str = ""
pub status: str = ""
pub memory: i64 = 0
pub uptime ms: i64 = 0
end
func colored status(s: str): str:
if s == "running":
ret term.Green(s)
else s == "degraded":
ret term.Yellow(s)
end
ret term.Red(s)
end
func main:
services := [
Service { name = "api", status = "running", memory = 157286400, uptime ms = 93784000 },
Service { name = "search-indexer", status = "degraded", memory = 1073741824, uptime ms = 4500000 },
Service { name = "mail", status = "stopped", memory = 0, uptime ms = 0 }
]
println term.Bold(term.PadRight("SERVICE", 14) + " " + term.PadRight("STATUS", 9) + " " + term.PadLeft("MEMORY", 9) + " UPTIME")
each s in services:
name := term.PadRight(term.Truncate(s.name, 14), 14)
status := term.PadRight(colored status(s.status), 9)
mut up := "-"
if s.uptime ms > 0:
up = term.FormatDuration(s.uptime ms)
end
println name, status, term.PadLeft(term.FormatBytes(s.memory), 9), "", up
end
endThe output above is plain because the program's output went to a file, not a terminal. Run it in a terminal and the status column turns green, yellow and red, with no change to the code. FormatBytes and FormatDuration turn raw numbers into the short forms people expect in a status table.
Colour on screen, plain text in the log
The same line often goes to two places: the terminal, where colour helps, and a log file, where escape codes are noise. Build the line once and pass it through Strip for the file. The colour functions already return plain text when colour is off, but Strip also cleans text that was coloured earlier or came from another program.
import term
func status line(tag: str, message: str): str:
mut label := "[" + tag + "]"
if tag == "ok":
label = term.Green(label)
else tag == "warn":
label = term.Yellow(label)
else tag == "fail":
label = term.Bold(term.Red(label))
end
ret term.PadRight(label, 7) + message
end
func main:
term.SetColorMode(term.ColorAlways)
line := status line("warn", "cache is 91% full")
println "bytes:", line.Len(), "| columns on screen:", term.Width(line)
mut log file := Vector {} as Vector<str>
defer log file.Free()
log file.Push(term.Strip(status line("ok", "config loaded")))
log file.Push(term.Strip(line))
log file.Push(term.Strip(status line("fail", "could not reach the mail server")))
println "--- the log file ---"
each entry in log file:
println entry
end
endThe example forces colour on with SetColorMode(ColorAlways) so the difference between bytes and columns is visible. Leave the mode at ColorAuto in real programs: it turns colour off for pipes, files and NO_COLOR, which is what users expect.
A progress bar that also works in CI
NewBar takes the total and a label; BarAdd moves it on and redraws it in place, and BarFinish fills it and ends the line. The fields change the look: a fixed width instead of one worked out from the terminal, the fill and empty characters, and show_count or show_eta for the numbers after the bar. BarRender returns the same text without drawing it, which is useful for a status line of your own.
import term
func main:
sizes := [120, 480, 64, 900, 236]
mut total := 0
each size in sizes: total += size end
mut bar := term.NewBar(total, "backup")
bar.width = 20
bar.fill = "#"
bar.empty = "."
bar.show_count = true
each i, size in sizes:
term.BarAdd(bar, size)
if i == 1: println "halfway:", term.BarRender(bar) end
end
term.BarFinish(bar)
println "copied", term.FormatBytes((total as i64) * 1024), "in", term.FormatDuration(7400)
endOn this page the output is not a terminal, so the five BarAdd calls drew nothing and BarFinish printed the finished bar once. In a terminal the same program shows one bar that fills up on a single line. NewBar picks █ and ░ when the locale is UTF-8; the example sets plain characters so its output is the same everywhere. For work with no known total, NewSpinner with SpinnerTick in a loop and SpinnerStop at the end does the same job.
An arrow key menu
A full screen program follows a fixed pattern: check that standard input is a terminal, switch it to raw mode so keys arrive one at a time without being echoed, move to the alternate screen so the user's scrollback is left alone, and undo all of it in reverse order at the end. ReadKey turns the bytes of a key press into a Key, so arrows, Enter and Escape are simple comparisons.
import term
func draw(items: Vector<str>, chosen: int):
term.Write(term.SeqClearScreen())
term.Write(term.Bold("Pick a language (arrows, Enter, q)") + "\r\n\r\n")
each i, item in items:
if i == chosen:
term.Write(term.Inverse(" > " + item + " ") + "\r\n")
else:
term.Write(" " + item + "\r\n")
end
end
term.Flush()
end
func main:
if not term.IsTTY(term.StdinFd):
printerrln "run this program in a terminal"
ret 1
end
mut items := Vector {} as Vector<str>
defer items.Free()
items.Push("Salam")
items.Push("C")
items.Push("Go")
items.Push("Rust")
mut state := term.MakeRaw(term.StdinFd)
term.EnterAltScreen()
term.HideCursor()
mut chosen := 0
mut picked := -1
until picked < 0:
draw(items, chosen)
k := term.ReadKey()
if k.code == term.KeyUp and chosen > 0:
chosen--
else k.code == term.KeyDown and chosen < items.Len() - 1:
chosen++
else k.code == term.KeyEnter:
picked = chosen
else k.code == term.KeyEOF or (k.code == term.KeyChar and k.ch == "q"):
break
end
end
term.ShowCursor()
term.ExitAltScreen()
term.Restore(state)
if picked >= 0:
println "you picked", items.Get(picked)
end
ret 0
endThis program needs a real terminal, so its output is not shown here. Run it with salam run term-guide-menu.salam. In raw mode the terminal no longer turns \n into a new line at the left edge, which is why the program writes \r\n. Always reach Restore, even on an error path, or the user's shell is left in raw mode; reset fixes it if that happens.
Which function for which job
| Job | Use |
|---|---|
| Highlight a word or a status | Red, Green, Bold and the other colour functions |
| Line up columns | PadRight, PadLeft, Center, Truncate |
| Measure what the user sees | Width, and Cols for the terminal width |
| Remove colour codes | Strip |
| Show progress | NewBar with BarAdd and BarDraw, or NewSpinner |
| Ask for a password | ReadPassword |
| Read single keys | MakeRaw or MakeCbreak, ReadKey, Restore |
| Draw a full screen view | EnterAltScreen, MoveTo, ClearScreen, ExitAltScreen |
Types
Bar
struct BarNewBar and set the fields to change how it looks.| Name | Type | Description |
|---|---|---|
total | int = 100 | The full value; 100 by default. |
current | int = 0 | The current value. |
width | int = 0 | The width of the bar in cells; zero means automatic, from the terminal width, between 8 and 80. |
label | str = "" | The text that comes before the bar. |
fill | str = "#" | The character of the filled part. |
head | str = "" | The head character; when it is not empty it takes the last filled cell. |
empty | str = "-" | The character of the empty part. |
color | int = 0 | The colour code of the filled part, such as FgGreen; zero means no colour. |
show_percent | bool = true | Show the percentage; on by default. |
show_count | bool = false | Show the count as 150/200. |
show_eta | bool = false | Show the estimated time remaining. |
hide_cursor | bool = true | Hide the cursor while drawing; on by default. |
start_ms | i64 = 0 | The start time, used to work out the time remaining. |
drawn | bool = false | Whether the bar has been drawn at least once. |
done | bool = false | Whether the bar has finished. |
import term
func main:
term.SetColorMode(term.ColorNever)
mut b := term.NewBar(200, "download")
b.width = 20
b.fill = "█"
b.empty = "░"
term.BarSet(b, 50)
println term.BarRender(b)
term.BarAdd(b, 100)
b.show_count = true
println term.BarRender(b)
mut s := term.NewSpinner("working")
defer term.SpinnerFree(s)
println s.label, s.frames.Len() > 0
mut frames := Vector {} as Vector<str>
frames.Push("◐")
frames.Push("◓")
mut s2 := term.NewSpinnerFrames("loading", frames)
defer term.SpinnerFree(s2)
repeat 3:
println term.SpinnerFrame(s2)
s2.idx = (s2.idx + 1) % frames.Len()
end
endSpinner
struct Spinner| Name | Type | Description |
|---|---|---|
frames | Vector | The frames of the spinner, shown in turn. |
idx | int = 0 | The index of the current frame. |
label | str = "" | The text beside the spinner. |
color | int = 0 | The colour code of the frame; zero means no colour. |
drawn | bool = false | Whether anything has been drawn on the screen. |
The example for this item is shown with the ones above: term-Bar.salam
Key
struct Key| Name | Type | Description |
|---|---|---|
code | int = 0 | The kind of key; one of the Key constants. |
ch | str = "" | The character, when the key is one. |
ctrl | bool = false | Control was held as well. |
alt | bool = false | Alt was held as well. |
shift | bool = false | Shift was held as well. |
raw | str = "" | The raw bytes that came from the terminal. |
import term
func describe(k: term.Key): str:
if k.code == term.KeyChar:
ret "char " + k.ch + (k.ctrl ? " with ctrl" : "") + (k.alt ? " with alt" : "")
else k.code == term.KeyUp:
ret "up"
else k.code == term.KeyEnter:
ret "enter"
else k.code == term.KeyEscape:
ret "escape"
else k.code >= term.KeyF1 and k.code < term.KeyF1 + 12:
ret "f" + (k.code - term.KeyF1 + 1)
end
ret "code " + k.code
end
func main:
each s in ["a", "\u001b[A", "\r", "\u001b", "\u0001", "\u001bx", "\u001bOQ", "\u001b[3~"]:
println describe(term.Decode(s))
end
m := term.DecodeMouse("\u001b[<0;12;5M")
println m.ok, m.button, m.col, m.row, m.pressed, m.motion
println term.KeyDelete, term.KeyTab, term.KeyBackspace, term.KeySpace, term.KeyEOF, term.KeyUnknown
println term.KeyDown, term.KeyRight, term.KeyLeft, term.KeyHome, term.KeyEnd, term.KeyPageUp, term.KeyPageDown, term.KeyInsert, term.KeyMouse, term.EscDelayMs
endMouseEvent
struct MouseEvent| Name | Type | Description |
|---|---|---|
button | int = 0 | The button number: zero left, 1 middle, 2 right. |
col | int = 0 | The column, from 1. |
row | int = 0 | The row, from 1. |
pressed | bool = false | The button was pressed, not released. |
motion | bool = false | The event is a movement. |
ok | bool = false | The sequence was read correctly. |
The example for this item is shown with the ones above: term-Decode.salam
TermState
struct TermStateRestore.| Name | Type | Description |
|---|---|---|
fd | int = 0 | The file descriptor of the terminal. |
saved | void* | The previous terminal settings on Unix. |
mode_in | int = 0 | The previous input mode on Windows. |
mode_out | int = 0 | The previous output mode on Windows. |
ok | bool = false | Whether the mode change succeeded. |
import term
func main:
if not term.IsTTY(term.StdinFd):
printerrln "this program has to run in a terminal"
ret 1
end
mut state := term.MakeRaw(term.StdinFd)
defer term.Restore(state)
term.Write("press a key (q to quit)\r\n")
until true:
if not term.KeyReady(500):
continue
end
k := term.ReadKey()
if k.code == term.KeyChar and k.ch == "q":
break
end
term.Write("code: " + k.code + "\r\n")
end
k2 := term.GetKey()
b := term.ReadByte()
password := term.ReadPassword("password: ")
println k2.code, b, password.Len()
s2 := term.MakeCbreak(term.StdinFd)
mut s3 := s2
term.Restore(s3)
ret 0
endWinSize
struct WinSize| Name | Type | Description |
|---|---|---|
cols | int = 80 | The number of columns. |
rows | int = 24 | The number of rows. |
import term
func main:
w := term.Size()
println w.cols > 0, w.rows > 0, term.Cols() > 0, term.Rows() > 0
t := term.IsTerminal()
println t == term.IsTTY(term.StdoutFd)
println term.StdinFd, term.StdoutFd, term.StderrFd
term.SetColorMode(term.ColorAlways)
println term.SupportsColor()
c256 := term.Supports256Color()
ctrue := term.SupportsTrueColor()
println c256 or not c256, ctrue or not ctrue
endConstants and variables
Key codes Key…
code field of a Key. For the function keys, KeyF1 is the code of F1 and F2 through F12 follow it in order.| Name | Value |
|---|---|
KeyUnknown | 0 |
KeyChar | 1 |
KeyEnter | 2 |
KeyTab | 3 |
KeyBackspace | 4 |
KeyEscape | 5 |
KeySpace | 6 |
KeyEOF | 7 |
KeyUp | 10 |
KeyDown | 11 |
KeyRight | 12 |
KeyLeft | 13 |
KeyHome | 14 |
KeyEnd | 15 |
KeyPageUp | 16 |
KeyPageDown | 17 |
KeyInsert | 18 |
KeyDelete | 19 |
KeyF1 | 30 |
KeyMouse | 50 |
The example for this item is shown with the ones above: term-Decode.salam
EscDelayMs
const EscDelayMs: i32 = 50The example for this item is shown with the ones above: term-Decode.salam
Reset
const Reset: str = "[0m"import term
import str
func show(s: str): str:
ret str.Replace(s, term.Esc, "\\e")
end
func main:
term.SetColorMode(term.ColorAlways)
println show(term.Red("error")), show(term.Green("ok")), show(term.Yellow("warning"))
println show(term.Bold("important")), show(term.Underline("link")), show(term.Italic("quote"))
println show(term.Dim("a")), show(term.Blink("b")), show(term.Inverse("c")), show(term.Hidden("d")), show(term.Strike("e"))
println show(term.Black("a")), show(term.Blue("b")), show(term.Magenta("c")), show(term.Cyan("d")), show(term.White("e")), show(term.Gray("f"))
println show(term.BrightRed("a")), show(term.BrightGreen("b")), show(term.BrightYellow("c")), show(term.BrightBlue("d")), show(term.BrightMagenta("e")), show(term.BrightCyan("f")), show(term.BrightWhite("g"))
println show(term.OnRed("a")), show(term.OnGreen("b")), show(term.OnYellow("c")), show(term.OnBlue("d")), show(term.OnMagenta("e")), show(term.OnCyan("f")), show(term.OnWhite("g"))
println show(term.Color256("orange", 208)), show(term.OnColor256("x", 236))
println show(term.RGB("hello", 255, 92, 0)), show(term.OnRGB("x", 10, 10, 10))
println show(term.Style("x", term.StyleBold)), show(term.Paint("x", term.Sgr(term.FgRed)))
println show(term.SeqFg256(208)), show(term.SeqBg256(17)), show(term.SeqFgRGB(1, 2, 3)), show(term.SeqBgRGB(4, 5, 6))
println show(term.Reset), show(term.Csi)
term.SetColorMode(term.ColorNever)
println term.Red("no colour"), term.ColorMode() == term.ColorNever, term.ColorAuto
endStyle codes Style…
Style or Sgr.| Name | Value |
|---|---|
StyleBold | 1 |
StyleDim | 2 |
StyleItalic | 3 |
StyleUnderline | 4 |
StyleBlink | 5 |
StyleReverse | 7 |
StyleHidden | 8 |
StyleStrike | 9 |
The example for this item is shown with the ones above: term-Color.salam
Text colours Fg…
Bright to one of them gives the bright variant of the same colour, so 31 + 60 is bright red.| Name | Value |
|---|---|
FgBlack | 30 |
FgRed | 31 |
FgGreen | 32 |
FgYellow | 33 |
FgBlue | 34 |
FgMagenta | 35 |
FgCyan | 36 |
FgWhite | 37 |
FgDefault | 39 |
FgBright | 60 |
The example for this item is shown with the ones above: term-Color.salam
Background colours Bg…
| Name | Value |
|---|---|
BgBlack | 40 |
BgRed | 41 |
BgGreen | 42 |
BgYellow | 43 |
BgBlue | 44 |
BgMagenta | 45 |
BgCyan | 46 |
BgWhite | 47 |
BgDefault | 49 |
The example for this item is shown with the ones above: term-Color.salam
Standard file descriptors Std…
IsTTY and MakeRaw.| Name | Value |
|---|---|
StdinFd | 0 |
StdoutFd | 1 |
StderrFd | 2 |
The example for this item is shown with the ones above: term-Size.salam
Esc
const Esc: str = ""The example for this item is shown with the ones above: term-Color.salam
Csi
const Csi: str = "["[.The example for this item is shown with the ones above: term-Color.salam
Colour modes Color…
SetColorMode. In automatic mode, colour is only written when the output is a terminal.| Name | Value |
|---|---|
ColorAuto | 0 |
ColorAlways | 1 |
ColorNever | 2 |
The example for this item is shown with the ones above: term-Color.salam
Functions
FormatDuration
func FormatDuration(ms: i64): str9.5s or 1h 02m.import term
func main:
term.SetColorMode(term.ColorAlways)
colored := term.Green("hello")
println colored.Len(), term.Width(colored), term.Strip(colored)
println term.Width("hello"), term.Width("日本"), term.Width("ab")
term.SetColorMode(term.ColorNever)
println "[" + term.Truncate("the Salam programming language", 10) + "]"
println "[" + term.PadRight("name", 8) + "]", "[" + term.PadLeft("42", 6) + "]", "[" + term.Center("ok", 6) + "]"
println term.FormatDuration(9500), term.FormatDuration(3721000)
println term.FormatBytes(512), term.FormatBytes(1536), term.FormatBytes(3221225472)
endFormatBytes
func FormatBytes(n: i64): str512 B, 1.5 KB or 1.7 MB; each unit is 1024 times the one before.The example for this item is shown with the ones above: term-Width.salam
NewBar
func NewBar(total: int, label: str): Bar█ and ░, otherwise # and -.The example for this item is shown with the ones above: term-Bar.salam
BarRender
func BarRender(b &: Bar): strThe example for this item is shown with the ones above: term-Bar.salam
BarDraw
func BarDraw(b &: Bar)import term
import time
func main:
mut b := term.NewBar(50, "processing")
repeat 50:
term.BarAdd(b, 1)
term.BarDraw(b)
time.Sleep(20)
end
term.BarFinish(b)
mut s := term.NewSpinner("connecting to the server")
repeat 20:
term.SpinnerTick(s)
time.Sleep(50)
end
term.SpinnerStop(s, "connected")
term.SpinnerFree(s)
endBarSet
func BarSet(b &: Bar, n: int)The example for this item is shown with the ones above: term-Bar.salam
BarAdd
func BarAdd(b &: Bar, delta: int)The example for this item is shown with the ones above: term-Bar.salam
BarFinish
func BarFinish(b &: Bar)The example for this item is shown with the ones above: term-Progress.salam
NewSpinner
func NewSpinner(label: str): Spinner| / - \ otherwise.The example for this item is shown with the ones above: term-Bar.salam
NewSpinnerFrames
func NewSpinnerFrames(label: str, frames: Vector<str>): SpinnerThe example for this item is shown with the ones above: term-Bar.salam
SpinnerFrame
func SpinnerFrame(s &: Spinner): strThe example for this item is shown with the ones above: term-Bar.salam
SpinnerTick
func SpinnerTick(s &: Spinner)The example for this item is shown with the ones above: term-Progress.salam
SpinnerStop
func SpinnerStop(s &: Spinner, final: str)The example for this item is shown with the ones above: term-Progress.salam
SpinnerFree
func SpinnerFree(s &: Spinner)The example for this item is shown with the ones above: term-Bar.salam
Decode
func Decode(seq: str): Key\e[A is the up key. It is also how you test a program without a real terminal.The example for this item is shown with the ones above: term-Decode.salam
DecodeMouse
func DecodeMouse(seq: str): MouseEvent\e[<0;12;5M.The example for this item is shown with the ones above: term-Decode.salam
ReadByte
func ReadByte(): intThe example for this item is shown with the ones above: term-ReadKey.salam
KeyReady
func KeyReady(timeout_ms: int): boolThe example for this item is shown with the ones above: term-ReadKey.salam
ReadKey
func ReadKey(): KeyThe example for this item is shown with the ones above: term-ReadKey.salam
GetKey
func GetKey(): KeyThe example for this item is shown with the ones above: term-ReadKey.salam
ReadPassword
func ReadPassword(prompt: str): strThe example for this item is shown with the ones above: term-ReadKey.salam
Sgr
func Sgr(code: int): strThe example for this item is shown with the ones above: term-Color.salam
SeqFg256
func SeqFg256(n: int): strThe example for this item is shown with the ones above: term-Color.salam
SeqBg256
func SeqBg256(n: int): strThe example for this item is shown with the ones above: term-Color.salam
SeqFgRGB
func SeqFgRGB(r: int, g: int, b: int): strThe example for this item is shown with the ones above: term-Color.salam
SeqBgRGB
func SeqBgRGB(r: int, g: int, b: int): strThe example for this item is shown with the ones above: term-Color.salam
Paint
func Paint(s: str, prefix: str): strThe example for this item is shown with the ones above: term-Color.salam
Style
func Style(s: str, code: int): strThe example for this item is shown with the ones above: term-Color.salam
Bold
func Bold(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Dim
func Dim(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Italic
func Italic(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Underline
func Underline(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Blink
func Blink(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Inverse
func Inverse(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Hidden
func Hidden(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Strike
func Strike(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Black
func Black(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Red
func Red(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Green
func Green(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Yellow
func Yellow(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Blue
func Blue(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Magenta
func Magenta(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Cyan
func Cyan(s: str): strThe example for this item is shown with the ones above: term-Color.salam
White
func White(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Gray
func Gray(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightRed
func BrightRed(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightGreen
func BrightGreen(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightYellow
func BrightYellow(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightBlue
func BrightBlue(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightMagenta
func BrightMagenta(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightCyan
func BrightCyan(s: str): strThe example for this item is shown with the ones above: term-Color.salam
BrightWhite
func BrightWhite(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnRed
func OnRed(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnGreen
func OnGreen(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnYellow
func OnYellow(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnBlue
func OnBlue(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnMagenta
func OnMagenta(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnCyan
func OnCyan(s: str): strThe example for this item is shown with the ones above: term-Color.salam
OnWhite
func OnWhite(s: str): strThe example for this item is shown with the ones above: term-Color.salam
Color256
func Color256(s: str, n: int): strThe example for this item is shown with the ones above: term-Color.salam
OnColor256
func OnColor256(s: str, n: int): strThe example for this item is shown with the ones above: term-Color.salam
RGB
func RGB(s: str, r: int, g: int, b: int): strThe example for this item is shown with the ones above: term-Color.salam
OnRGB
func OnRGB(s: str, r: int, g: int, b: int): strThe example for this item is shown with the ones above: term-Color.salam
Strip
func Strip(s: str): strThe example for this item is shown with the ones above: term-Width.salam
Width
func Width(s: str): intThe example for this item is shown with the ones above: term-Width.salam
Truncate
func Truncate(s: str, n: int): strn characters as Width counts them, keeping the colour codes intact.The example for this item is shown with the ones above: term-Width.salam
PadRight
func PadRight(s: str, n: int): strn; for laying out columns.The example for this item is shown with the ones above: term-Width.salam
PadLeft
func PadLeft(s: str, n: int): strn.The example for this item is shown with the ones above: term-Width.salam
Center
func Center(s: str, n: int): strn.The example for this item is shown with the ones above: term-Width.salam
Write
func Write(s: str)import term
func main:
term.EnableAnsi()
term.SetTitle("dashboard")
term.EnterAltScreen()
term.HideCursor()
term.ClearScreen()
term.Home()
term.MoveTo(2, 4)
term.Write(term.Bold("hello from the dashboard"))
term.SaveCursor()
term.Down(2)
term.Right(4)
term.Emit("status: good")
term.RestoreCursor()
term.Up(1)
term.Left(1)
term.NextLine(1)
term.PrevLine(1)
term.Column(1)
term.ClearLine()
term.ClearToLineEnd()
term.ClearBelow()
term.ResetLine()
term.SetScrollRegion(3, term.Rows() - 1)
term.ScrollUp(1)
term.ScrollDown(1)
term.InsertLines(1)
term.DeleteLines(1)
term.ResetScrollRegion()
term.EnableMouse()
term.DisableMouse()
term.Bell()
term.Flush()
term.ClearScrollback()
term.ShowCursor()
term.ExitAltScreen()
term.WriteErr("done\n")
w := term.Size()
println w.cols, w.rows, term.Cols()
endWriteErr
func WriteErr(s: str)The example for this item is shown with the ones above: term-Screen.salam
Flush
func Flush()The example for this item is shown with the ones above: term-Screen.salam
Emit
func Emit(s: str)The example for this item is shown with the ones above: term-Screen.salam
IsTTY
func IsTTY(fd: int): boolThe example for this item is shown with the ones above: term-Size.salam
IsTerminal
func IsTerminal(): boolThe example for this item is shown with the ones above: term-Size.salam
Size
func Size(): WinSizeThe example for this item is shown with the ones above: term-Size.salam
Cols
func Cols(): intThe example for this item is shown with the ones above: term-Size.salam
Rows
func Rows(): intThe example for this item is shown with the ones above: term-Size.salam
SetColorMode
func SetColorMode(mode: int)--color.The example for this item is shown with the ones above: term-Color.salam
ColorMode
func ColorMode(): intThe example for this item is shown with the ones above: term-Color.salam
SupportsColor
func SupportsColor(): boolNO_COLOR is set, when TERM=dumb, or when the output is not a terminal; CLICOLOR_FORCE turns colour on.The example for this item is shown with the ones above: term-Size.salam
Supports256Color
func Supports256Color(): boolTERM contains 256color or COLORTERM is set.The example for this item is shown with the ones above: term-Size.salam
SupportsTrueColor
func SupportsTrueColor(): boolCOLORTERM is truecolor or 24bit.The example for this item is shown with the ones above: term-Size.salam
EnableAnsi
func EnableAnsi(): boolThe example for this item is shown with the ones above: term-Screen.salam
MakeRaw
func MakeRaw(fd: int): TermStateThe example for this item is shown with the ones above: term-ReadKey.salam
MakeCbreak
func MakeCbreak(fd: int): TermStateThe example for this item is shown with the ones above: term-ReadKey.salam
Restore
func Restore(st &: TermState): booldefer right after the mode change.The example for this item is shown with the ones above: term-ReadKey.salam
Bell
func Bell()The example for this item is shown with the ones above: term-Screen.salam
SeqTitle
func SeqTitle(title: str): strimport term
import str
func show(s: str): str:
ret str.Replace(str.Replace(s, term.Esc, "\\e"), "\u0007", "\\a")
end
func main:
println show(term.SeqUp(2)), show(term.SeqDown(1)), show(term.SeqRight(3)), show(term.SeqLeft(3))
println show(term.SeqNextLine(1)), show(term.SeqPrevLine(1)), show(term.SeqColumn(10)), show(term.SeqMoveTo(5, 20)), show(term.SeqHome())
println show(term.SeqSaveCursor()), show(term.SeqRestoreCursor()), show(term.SeqHideCursor()), show(term.SeqShowCursor())
println show(term.SeqClearScreen()), show(term.SeqClearScrollback()), show(term.SeqClearLine())
println show(term.SeqClearToLineEnd()), show(term.SeqClearToLineStart()), show(term.SeqClearBelow()), show(term.SeqClearAbove()), show(term.SeqResetLine())
println show(term.SeqScrollUp(2)), show(term.SeqScrollDown(2)), show(term.SeqInsertLines(1)), show(term.SeqDeleteLines(1))
println show(term.SeqScrollRegion(2, 20)), show(term.SeqResetScrollRegion())
println show(term.SeqEnterAltScreen()), show(term.SeqExitAltScreen())
println show(term.SeqEnableMouse()), show(term.SeqEnableMouseMotion()), show(term.SeqDisableMouse())
println show(term.SeqTitle("my program"))
endSetTitle
func SetTitle(title: str)The example for this item is shown with the ones above: term-Screen.salam
SeqUp
func SeqUp(n: int): strn rows up.The example for this item is shown with the ones above: term-Seq.salam
SeqDown
func SeqDown(n: int): strn rows down.The example for this item is shown with the ones above: term-Seq.salam
SeqRight
func SeqRight(n: int): strn columns right.The example for this item is shown with the ones above: term-Seq.salam
SeqLeft
func SeqLeft(n: int): strn columns left.The example for this item is shown with the ones above: term-Seq.salam
SeqNextLine
func SeqNextLine(n: int): strnth next row.The example for this item is shown with the ones above: term-Seq.salam
SeqPrevLine
func SeqPrevLine(n: int): strnth previous row.The example for this item is shown with the ones above: term-Seq.salam
SeqColumn
func SeqColumn(col: int): strThe example for this item is shown with the ones above: term-Seq.salam
SeqMoveTo
func SeqMoveTo(row: int, col: int): strThe example for this item is shown with the ones above: term-Seq.salam
SeqHome
func SeqHome(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqSaveCursor
func SeqSaveCursor(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqRestoreCursor
func SeqRestoreCursor(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqHideCursor
func SeqHideCursor(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqShowCursor
func SeqShowCursor(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearScreen
func SeqClearScreen(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearScrollback
func SeqClearScrollback(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearLine
func SeqClearLine(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearToLineEnd
func SeqClearToLineEnd(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearToLineStart
func SeqClearToLineStart(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearBelow
func SeqClearBelow(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqClearAbove
func SeqClearAbove(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqResetLine
func SeqResetLine(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqScrollUp
func SeqScrollUp(n: int): strn rows.The example for this item is shown with the ones above: term-Seq.salam
SeqScrollDown
func SeqScrollDown(n: int): strn rows.The example for this item is shown with the ones above: term-Seq.salam
SeqInsertLines
func SeqInsertLines(n: int): strn empty lines at the cursor.The example for this item is shown with the ones above: term-Seq.salam
SeqDeleteLines
func SeqDeleteLines(n: int): strn lines from the cursor.The example for this item is shown with the ones above: term-Seq.salam
SeqScrollRegion
func SeqScrollRegion(top: int, bottom: int): strtop through bottom.The example for this item is shown with the ones above: term-Seq.salam
SeqResetScrollRegion
func SeqResetScrollRegion(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqEnterAltScreen
func SeqEnterAltScreen(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqExitAltScreen
func SeqExitAltScreen(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqEnableMouse
func SeqEnableMouse(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqEnableMouseMotion
func SeqEnableMouseMotion(): strThe example for this item is shown with the ones above: term-Seq.salam
SeqDisableMouse
func SeqDisableMouse(): strThe example for this item is shown with the ones above: term-Seq.salam
Up
func Up(n: int)n rows up.The example for this item is shown with the ones above: term-Screen.salam
Down
func Down(n: int)n rows down.The example for this item is shown with the ones above: term-Screen.salam
Right
func Right(n: int)n columns right.The example for this item is shown with the ones above: term-Screen.salam
Left
func Left(n: int)n columns left.The example for this item is shown with the ones above: term-Screen.salam
NextLine
func NextLine(n: int)nth next row.The example for this item is shown with the ones above: term-Screen.salam
PrevLine
func PrevLine(n: int)nth previous row.The example for this item is shown with the ones above: term-Screen.salam
Column
func Column(col: int)The example for this item is shown with the ones above: term-Screen.salam
MoveTo
func MoveTo(row: int, col: int)The example for this item is shown with the ones above: term-Screen.salam
Home
func Home()The example for this item is shown with the ones above: term-Screen.salam
SaveCursor
func SaveCursor()The example for this item is shown with the ones above: term-Screen.salam
RestoreCursor
func RestoreCursor()The example for this item is shown with the ones above: term-Screen.salam
HideCursor
func HideCursor()The example for this item is shown with the ones above: term-Screen.salam
ShowCursor
func ShowCursor()The example for this item is shown with the ones above: term-Screen.salam
ClearScreen
func ClearScreen()The example for this item is shown with the ones above: term-Screen.salam
ClearScrollback
func ClearScrollback()The example for this item is shown with the ones above: term-Screen.salam
ClearLine
func ClearLine()The example for this item is shown with the ones above: term-Screen.salam
ClearToLineEnd
func ClearToLineEnd()The example for this item is shown with the ones above: term-Screen.salam
ClearBelow
func ClearBelow()The example for this item is shown with the ones above: term-Screen.salam
ResetLine
func ResetLine()The example for this item is shown with the ones above: term-Screen.salam
ScrollUp
func ScrollUp(n: int)n rows.The example for this item is shown with the ones above: term-Screen.salam
ScrollDown
func ScrollDown(n: int)n rows.The example for this item is shown with the ones above: term-Screen.salam
InsertLines
func InsertLines(n: int)n empty lines at the cursor.The example for this item is shown with the ones above: term-Screen.salam
DeleteLines
func DeleteLines(n: int)n lines from the cursor.The example for this item is shown with the ones above: term-Screen.salam
SetScrollRegion
func SetScrollRegion(top: int, bottom: int)top through bottom, to keep a header still, for instance.The example for this item is shown with the ones above: term-Screen.salam
ResetScrollRegion
func ResetScrollRegion()The example for this item is shown with the ones above: term-Screen.salam
EnterAltScreen
func EnterAltScreen()The example for this item is shown with the ones above: term-Screen.salam
ExitAltScreen
func ExitAltScreen()The example for this item is shown with the ones above: term-Screen.salam
EnableMouse
func EnableMouse()The example for this item is shown with the ones above: term-Screen.salam
DisableMouse
func DisableMouse()The example for this item is shown with the ones above: term-Screen.salam