1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/term
bpryer 3f7970db52
term: add get_cursor_position and set_terminal_title (#6279)
* added functions

added:
  - get_cursor_position()
  - set_terminal_title(title string)

* implement term.get_cursor_position and term.set_terminal_title on unix

* Cleanup

* make x,y fields of term.Coord mutable

* fix vrepl compilation

* use more descriptive var names in term_test.v

* do not change the current terminal title in dumb terminals; do not test term.set_terminal_title outside of CI

* unix: in term.set_terminal_title, return true even for dumb terminals

Co-authored-by: Brent Pryer <brent@pryermachine.com>
Co-authored-by: Delyan Angelov <delian66@gmail.com>
2020-09-08 22:00:10 +03:00
..
colors.v update copyright years 2020-02-03 05:00:36 +01:00
control.v term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00
README.md term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00
term_nix.c.v term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00
term_test.v term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00
term_windows.c.v term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00
term.js.v vlib: use new filename format 2020-04-11 19:51:32 +02:00
term.v term: add get_cursor_position and set_terminal_title (#6279) 2020-09-08 22:00:10 +03:00

Quickstart

The V term module is a module which is made to provide an interactive api that helps building TUI apps.

Use

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
        mut var := os.input('press q to quit: ')
        // Keep prompting until the user presses the q key
        for {
                if  var == 'q' {
                        break
                } else {
                        var = os.input('press q to quit: ')
                }
        }
}

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:

// returns the height and the width of the terminal
term.get_terminal_size() (width, height)

// returns the string as green text to be printed on stdout 
term.ok_message(string)

// returns the string as red text to be printed on stdout 
term.fail_message(string)

// returns the string as yellow text to be printed on stdout 
term.warning_message(string)

//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.<color>(string)

// transforms the given string into bold text
term.bold(string)

// puts a strikethrough into the given string
term.strikethrough(string)

// underlines the given string
term.underline(string)

// colors the background of the outup following the given color, the available colors are: black,blue,yellow,green,cyan,gray
term.bg_<color>(string)

// sets the position of the cursor at a given place in the terminal
term.set_cursor_position(term.Coord)

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