Term helper


1Presentation

This helper facilitates terminal management (TTY) when writing command-line scripts.

The \Temma\Utils\Term object provides static methods.


2input()

This static method retrieves text entered by the user.

Method signature:

input() : string

Example:

use \Temma\Utils\Term as TµTerm;

print("What is your name?\n");
$name = TµTerm::input();
print("Hello '$name'\n");

3password()

This static method retrieves text entered by the user, without the text appearing on the screen.

Method signature:

password() : string

Example:

use \Temma\Utils\Term as TµTerm;

print("Please enter your password\n");
$pwd = TµTerm::input();
if (mb_strlen($pwd) < 12)
    print("Your password is too short\n");

4clear()

Clears the screen.

Method signature:

clear() : void

Example:

\Temma\Utils\Term::clear();

5clearFromCursor()

Clears the screen from the current position of the cursor.

Method signature:

clearFromCursor() : void

Example:

\Temma\Utils\Term::clearFromCursor();

6reset()

Clears the screen and resets its configuration. Display history is lost.

Method signature:

reset() : void

Example:

\Temma\Utils\Term::reset();

7clearLine()

Clears the line on which the cursor is positioned. The cursor is repositioned at the beginning of the line.

Method signature:

clearLine() : void

Example:

\Temma\Utils\Term::clearLine();

8clearLineFromCursor()

Clears the current line from the position of the cursor.

Method signature:

clearLineFromCursor() : void

Example:

\Temma\Utils\Term::clearLineFromCursor();

9hideCursor()

Hides the cursor.

Method signature:

hideCursor() : void

Example:

\Temma\Utils\Term::hideCursor();

10showCursor()

Shows the cursor.

Method signature:

showCursor() : void

Example:

\Temma\Utils\Term::showCursor();

11saveCursor()

Saves the current position of the cursor (in order to restore it later with the restoreCursor() method).

Method signature:

saveCursor() : void

Example:

\Temma\Utils\Term::saveCursor();

12restoreCursor()

Restore the position of the cursor, which has been saved with the saveCursor() method.

Method signature:

restoreCursor() : void

Example:

\Temma\Utils\Term::restoreCursor();

13moveCursorHome()

Moves the cursor to the top-left corner (position 0,0) of the terminal.

Method signature:

moveCursorHome() : void

Example:

\Temma\Utils\Term::moveCursorHome();

14moveCursorTo()

Moves the cursor to the given coordinates.

Method signature:

moveCursorTo(int $x, int $y) : void

Example:

\Temma\Utils\Term::moveCursorTo(17, 4);

15moveCursorLineStart()

Moves the cursor to the start of the current line.

Method signature:

moveCursorLineStart() : void

Example:

\Temma\Utils\Term::moveCursorLineStart();

16moveCursorUp()

Moves the cursor up by the number of lines passed in parameter (1 line by default).

Method signature:

moveCursorUp(int $nbrLines=1) : void

Example:

use \Temma\Utils\Term as TµTerm;

// move one line up
TµTerm::moveCursorUp();
// moves three lines up
TµTerm::moveCursorUp(3);

17moveCursorDown()

Moves the cursor down by the number of lines passed in parameter (1 line by default).

Method signature:

moveCursorDown(int $nbrLines=1) : void

Example:

use \Temma\Utils\Term as TµTerm;

// move one line down
TµTerm::moveCursorDown();
// move three lines down
TµTerm::moveCursorDown(3);

18moveCursorRight()

Moves the cursor to the right of the number of columns passed in parameter (1 column by default).

Method signature:

moveCursorRight(int $nbrColumns=1) : void

Example:

use \Temma\Utils\Term as TµTerm;

// move one column to the right
TµTerm::moveCursorRight();
// move three columns to the right
TµTerm::moveCursorRight(3);

19moveCursorLeft()

Moves the cursor to the left by the number of columns passed in parameter (1 column by default).

Method signature:

moveCursorLeft(int $nbrColumns=1) : void

Example:

use \Temma\Utils\Term as TµTerm;

// move one column to the left
TµTerm::moveCursorLeft();
// move three columns to the left
TµTerm::moveCursorLeft(3);

20getCursorPosition()

This method returns the current position of the cursor.

Method signature:

getCursorPosition() : array

Example:

use \Temma\Utils\Term as TµTerm;

[$x, $y] = TµTerm::getCursorPosition();

21getScreenSize()

This method returns the screen size (number of columns and rows).

Method signature:

getScreenSize(bool $force=false) : array

Parameter:

  • $force: true to force retrieval (without using cache). Unnecessary most of the time, as the object updates cached values when the terminal is resized.

Example:

use \Temma\Utils\Term as TµTerml;

[$width, $height] = TµTerm::getScreenSize();