Overview
Description
The term
module is designed to provide the building blocks for building
very simple TUI apps. For more complex apps, you should really look at the
term.ui
module, as it includes terminal events, is easier to use and
is much more performant for large draws.
Usage
You can use the term
module to either color the output on a terminal
or to decide on where to put the output in your terminal.
For example let's make a simple program which prints colored text in the middle of the terminal.
import term import os fn main() { term.clear() // clears the content in the terminal width, height := term.get_terminal_size() // get the size of the terminal term.set_cursor_position(x: width / 2, y: height / 2) // now we point the cursor to the middle of the terminal println(term.strikethrough(term.bright_green('hello world'))) // Print green text term.set_cursor_position(x: 0, y: height) // Sets the position of the cursor to the bottom of the terminal // Keep prompting until the user presses the q key for { if var := os.input_opt('press q to quit: ') { if var != 'q' { continue } break } println('') break } println('Goodbye.') }
This simple program covers many of the principal aspects of the term
module.
API
Here are some functions you should be aware of in the term
module:
import term // returns the height and the width of the terminal width, height := term.get_terminal_size() // returns the string as green text to be printed on stdout term.ok_message('cool') // returns the string as red text to be printed on stdout term.fail_message('oh, no') // returns the string as yellow text to be printed on stdout term.warn_message('be warned') // clears the entire terminal and leaves a blank one term.clear() // colors the output of the output, the available colors are: // black,blue,yellow,green,cyan,gray,bright_blue,bright_green,bright_red,bright_black,bright_cyan term.yellow('submarine') // transforms the given string into bold text term.bold('and beautiful') // puts a strikethrough into the given string term.strikethrough('the core of the problem') // underlines the given string term.underline('important') // colors the background of the output following the given color // the available colors are: black, blue, yellow, green, cyan, gray term.bg_green('field') // sets the position of the cursor at a given place in the terminal term.set_cursor_position(x: 5, y: 10) // moves the cursor up term.cursor_up() // moves the cursor down term.cursor_down() // moves the cursor to the right term.cursor_forward() // moves the cursor to the left term.cursor_back() // shows the cursor term.show_cursor() // hides the cursor term.hide_cursor()
Aliases
This section is empty.
Constants
This section is empty.
Sum types
This section is empty.
Functions
bg_magenta formats the msg
in magenta background.
bright_bg_black formats the msg
in bright black background.
bright_bg_blue formats the msg
in bright blue background.
bright_bg_cyan formats the msg
in bright cyan background.
bright_bg_green formats the msg
in bright green background.
bright_bg_magenta formats the msg
in bright magenta background.
bright_bg_red formats the msg
in bright red background.
bright_bg_white formats the msg
in bright white background.
bright_bg_yellow formats the msg
in bright yellow background.
bright_black formats the msg
in bright black.
bright_blue formats the msg
in bright blue.
bright_cyan formats the msg
in bright cyan.
bright_green formats the msg
in bright green.
bright_magenta formats the msg
in bright magenta.
bright_white formats the msg
in bright white.
bright_yellow formats the msg
in bright yellow.
fn can_show_color_on_stderr() bool
can_show_color_on_stderr returns true if colors are allowed in stderr; returns false otherwise.
fn can_show_color_on_stdout() bool
can_show_color_on_stdout returns true if colors are allowed in stdout; returns false otherwise.
fn clear()
clear clears current terminal screen.
Implementation taken from https://docs.microsoft.com/en-us/windows/console/clearing-the-screen#example-2.
fn clear_previous_line()
clear_previous_line - useful for progressbars.
It moves the cursor to start of line, then 1 line above, then erases the line. In effect the next println will overwrite the previous content.
colorize returns a colored string by running the specified cfn
over the message s
, only if colored stdout is supported by the terminal.
Example:
term.colorize(term.yellow, 'the message')
fn cursor_forward(n int)
cursor_forward moves the cursor forward n
character positions.
ecolorize returns a colored string by running the specified cfn
over the message s
, only if colored stderr is supported by the terminal.
Example:
term.ecolorize(term.bright_red, 'the message')
fn erase_clear()
erase_clear clears the entire terminal window and returns the cursor to the top left corner.
fn erase_display(t string)
erase_display erases display characters based on the given parameter, t
.
t
can be of the following values in string:
0
: current cursor position to end of the terminal window.
1
: current cursor position to beginning of the terminal window.
2
: clears the entire terminal window.
3
: clears the entire terminal window and also deletes the scrollback buffer.
fn erase_line(t string)
erase_line erases the current line based on the given parameter, t
.
t
can be of the following values in string:
0
: current cursor position to the end of the line.
1
: current cursor position to the beginning of the line.
2
: clears the entire line.
Note: Cursor position does not change.
fn erase_line_tobeg()
erase_line_tobeg erases from the start of the line to the cursor position.
fn erase_line_toend()
erase_line_toend erases from the cursor position to the end of the line.
fn erase_tobeg()
erase_tobeg erases from the cursor to the beginning of the terminal window.
fn erase_toend()
erase_to_end erases from the cursor to the end of the terminal window.
fail_message returns a colored string with red color.
If colors are not allowed, returns a given string.
failed returns a bold white on red version of the string s
If colors are not allowed, returns the string s
format_esc produces an ANSI escape code, for selecting the graphics rendition of the following text. Each of the attributes that can be passed in code
, separated by ;
, will be in effect, until the terminal encounters another SGR ANSI escape code. For more details about the different codes, and their meaning, see: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
format_rgb returns ANSI escape coded msg
formatted with a preceding open
, a succeeding close
and the provided RGB colors r
, g
, and b
.
fn get_cursor_position() !Coord
get_cursor_position returns a Coord containing the current cursor position
fn get_cursor_position() !Coord
get_cursor_position returns a Coord containing the current cursor position
get_terminal_size returns a number of colums and rows of terminal window.
get_terminal_size returns a number of colums and rows of terminal window.
get_terminal_size returns a number of colums and rows of terminal window.
gray formats the msg
in gray (equivalent to bright_black
).
h_divider returns a horizontal divider line with a dynamic width, that depends on the current terminal settings.
If an empty string is passed in, print enough spaces to make a new line
header returns a horizontal divider line with a centered text in the middle.
e.g: term.header('TEXT', '=') =============== TEXT ===============
header_left returns a horizontal divider line with a title text on the left.
e.g: term.header_left('TITLE', '=') ==== TITLE =========================
highlight_command highlights the command with an on-brand background to make CLI commands immediately recognizable.
n is number of cells direction: A is up / North direction: B is down / South direction: C is forward / East direction: D is backward / West
ok_message returns a colored string with green color.
If colors are not allowed, returns a given string.
rapid_blink will surround the msg
with ANSI escape codes for blinking (over 150 times per minute).
Note that unlike slow_blink, this is not very widely supported.
rbg returns the msg
with the foreground in the specified RGB color For example, rgb(0, 255, 0, 'hi')
returns the 'hi'
string in lime color.
fn set_cursor_position(c Coord)
Sources for ANSI Control Sequences https://github.com/RajeshPatkarInstitute/Panim https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html https://en.wikipedia.org/wiki/ANSI_escape_code Support for Windows https://en.wikipedia.org/wiki/ANSI.SYS #include <windows.h>
C.SetConsoleMode(C.ENABLE_VIRTUAL_TERMINAL_INPUT) Setting cursor to the given position x is the x coordinate y is the y coordinate
set_tab_title changes the terminal tab title, for terminal emulators that do support several tabs
set_tab_title changes the terminal tab title, for terminal emulators like Konsole, that support several tabs
set_terminal_title changes the terminal title
set_terminal_title change the terminal title (usually that is the containing terminal window title)
slow_blink will surround the msg
with ANSI escape codes for blinking (less than 150 times per minute).
strikethrough returns the given msg
in strikethrough.
strip_ansi removes any ANSI sequences in the text
fn utf8_getchar() ?rune
utf8_getchar returns an utf8 rune from standard input
utf8_len calculates the length of a utf8 rune to read, according to its first byte
warn_message returns a colored string with yellow color.
If colors are not allowed, returns a given string.
Structs
pub struct C.termios {
mut:
c_iflag int
c_oflag int
c_cflag int
c_lflag int
// c_line byte
c_cc [10]int
}
Coord - used by term.get_cursor_position and term.set_cursor_position
pub struct C.CONSOLE_SCREEN_BUFFER_INFO {
mut:
dwSize C.COORD
dwCursorPosition C.COORD
wAttributes u16
srWindow C.SMALL_RECT
dwMaximumWindowSize C.COORD
}
win: CONSOLE_SCREEN_BUFFER_INFO https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str
Interfaces
This section is empty.
Enums
This section is empty.
bg_black formats the
msg
in black background.