2022-01-05 19:06:08 +03:00
|
|
|
## Description
|
2020-08-17 21:16:18 +03:00
|
|
|
|
2022-01-05 19:06:08 +03:00
|
|
|
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
|
2022-03-06 16:50:22 +03:00
|
|
|
`term.ui` module, as it includes terminal events, is easier to use and
|
2022-01-05 19:06:08 +03:00
|
|
|
is much more performant for large draws.
|
2020-08-17 21:16:18 +03:00
|
|
|
|
2022-01-05 19:06:08 +03:00
|
|
|
## Usage
|
2020-08-17 21:16:18 +03:00
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
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.
|
2020-08-17 21:16:18 +03:00
|
|
|
|
|
|
|
For example let's make a simple program which prints colored text in the middle of the terminal.
|
|
|
|
|
|
|
|
```v
|
|
|
|
import term
|
|
|
|
import os
|
|
|
|
|
|
|
|
fn main() {
|
2020-12-06 00:54:41 +03:00
|
|
|
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 {
|
2021-02-27 13:50:15 +03:00
|
|
|
if var := os.input_opt('press q to quit: ') {
|
|
|
|
if var != 'q' {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-06 00:54:41 +03:00
|
|
|
break
|
|
|
|
}
|
2021-02-27 13:50:15 +03:00
|
|
|
println('')
|
|
|
|
break
|
2020-12-06 00:54:41 +03:00
|
|
|
}
|
2021-02-27 13:50:15 +03:00
|
|
|
println('Goodbye.')
|
2020-08-17 21:16:18 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This simple program covers many of the principal aspects of the `term ` module.
|
|
|
|
|
2022-01-05 19:06:08 +03:00
|
|
|
## API
|
2020-08-17 21:16:18 +03:00
|
|
|
|
|
|
|
Here are some functions you should be aware of in the `term `module:
|
|
|
|
|
2020-11-18 20:28:28 +03:00
|
|
|
```v oksyntax
|
2020-11-27 14:03:32 +03:00
|
|
|
import term
|
2020-12-06 00:54:41 +03:00
|
|
|
|
2020-08-17 21:16:18 +03:00
|
|
|
// returns the height and the width of the terminal
|
2020-11-27 14:03:32 +03:00
|
|
|
width, height := term.get_terminal_size()
|
2020-11-18 20:28:28 +03:00
|
|
|
// returns the string as green text to be printed on stdout
|
2020-11-27 14:03:32 +03:00
|
|
|
term.ok_message('cool')
|
2020-11-18 20:28:28 +03:00
|
|
|
// returns the string as red text to be printed on stdout
|
2020-11-27 14:03:32 +03:00
|
|
|
term.fail_message('oh, no')
|
2020-11-18 20:28:28 +03:00
|
|
|
// returns the string as yellow text to be printed on stdout
|
2021-10-15 06:08:31 +03:00
|
|
|
term.warn_message('be warned')
|
2020-12-06 00:54:41 +03:00
|
|
|
// clears the entire terminal and leaves a blank one
|
2020-08-17 21:16:18 +03:00
|
|
|
term.clear()
|
2021-08-16 10:11:44 +03:00
|
|
|
// 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
|
2020-11-27 14:03:32 +03:00
|
|
|
term.yellow('submarine')
|
2020-08-17 21:16:18 +03:00
|
|
|
// transforms the given string into bold text
|
2020-11-27 14:03:32 +03:00
|
|
|
term.bold('and beautiful')
|
2020-08-17 21:16:18 +03:00
|
|
|
// puts a strikethrough into the given string
|
2020-11-27 14:03:32 +03:00
|
|
|
term.strikethrough('the core of the problem')
|
2020-08-17 21:16:18 +03:00
|
|
|
// underlines the given string
|
2020-11-27 14:03:32 +03:00
|
|
|
term.underline('important')
|
2020-11-18 20:28:28 +03:00
|
|
|
// colors the background of the output following the given color
|
|
|
|
// the available colors are: black, blue, yellow, green, cyan, gray
|
2020-11-27 14:03:32 +03:00
|
|
|
term.bg_green('field')
|
2020-08-17 21:16:18 +03:00
|
|
|
// sets the position of the cursor at a given place in the terminal
|
2020-11-27 14:03:32 +03:00
|
|
|
term.set_cursor_position(x: 5, y: 10)
|
2020-08-17 21:16:18 +03:00
|
|
|
// 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()
|
|
|
|
```
|