1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/term
2023-07-12 11:07:34 +03:00
..
termios termios: new termios module (#17792) 2023-03-30 08:58:52 +03:00
ui checker: disallow voidptr cast to struct (#18845) 2023-07-12 11:07:34 +03:00
colors.v all: 2023 copyright 2023-03-28 22:55:57 +02:00
control.v all: 2023 copyright 2023-03-28 22:55:57 +02:00
declarations_default.c.v all: byte => u8 2022-04-15 14:45:52 +03:00
declarations_linux.c.v term: do not put the unused c_line field in C.termios in declarations_linux.c.v (breaks BSD build) 2021-09-06 19:15:47 +03:00
README.md docs: change term.input to term.ui in vlib/term/README.md 2022-03-06 15:50:22 +02:00
term_nix.c.v termios: new termios module (#17792) 2023-03-30 08:58:52 +03:00
term_test.v term: add a separate term.set_tab_title/1 API for controling the current tab title in emulators like Konsole, that support many tabs 2023-01-14 12:00:39 +02:00
term_windows.c.v term: fix typo in term_windows.c.v (#18745) 2023-07-02 23:53:56 +03:00
term.js.v term: get_terminal_size() for js_node, term.clear() for all js backends (#12189) 2021-10-15 10:10:40 +03:00
term.v vfmt: change all '$expr' to '${expr}' (#16428) 2022-11-15 16:53:13 +03:00
utf8.c.v vrepl: add support for Home and End keys (#16116) 2022-10-20 20:07:57 +03:00
utf8.v vrepl: add support for Home and End keys (#16116) 2022-10-20 20:07:57 +03:00

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()