NAME
Terminal::Control - Perl extension for the terminal window control
SYNOPSIS
use Terminal::Control;
# Clear screen.
clear_screen();
# Reset screen.
reset_screen();
# Get terminal size and print to screen.
my ($rows, $cols, $xpix, $ypix) = winsize();
printf ("%s\n%s\n%s\n%s\n", $rows, $cols, $xpix, $ypix);
# Get chars and print to screen.
($rows, $cols) = chars();
printf ("%s\n%s\n", $rows, $cols);
# Get pixels and print to screen.
($xpix, $ypix) = pixels();
printf ("%s\n%s\n", $xpix, $ypix);
# Get cursor position.
my ($y, $x) = get_cursor_position();
printf ("%s\n%s\n", $y, $x);
# Set cursor position.
my $y = 20;
my $x = 80;
set_cursor_position($y, $x);
Requirement
The Perl header 'sys/ioctl.ph' is required for the ioctl call of the function TIOCGWINSZ. The equivalent C/C++ header is 'sys/ioctl.h'. The Perl command h2ph converts '.h' C/C++ header files to '.ph' Perl header files.
In contrast to modules in general, the module installation process cannot be told to create a sys/ioctl.ph. This is necessary manually via the h2ph command.
To prevent tests within CPAN from failing due to a missing sys/ioctl.ph, a fallback solution based on the BASH command stty is programmed.
Motivation
The idea for the necessity of the module arises from the fact that especially the system call system("reset") of the BASH command reset is noticeably slow. The BASH command reset is slow, the system call system("reset") is slower. By using so-called ASCII escape sequences, a significant acceleration can be achieved. The logical consequence is the programming of a Perl command that replaces the system call. A simple method is the best way to realise this. There is no need to implement a special Class to achive this.
Benchmark
Exemplary comparison comparison of time of execution:
system("reset") => 1.026892 Seconds = 1026,892 Milliseconds = 1026892 Microseconds
reset_screen() => 0.000076 Seconds = 0,076 Milliseconds = 76 Microseconds
This one example already shows a significant advantage of the new routine.
DESCRIPTION
Implemented Methods
The following methods have been implemented so far:
clear_screen()
reset_screen()
get_cursor_position()
set_cursor_position()
winsize()
chars()
pixels()
echo_on()
echo_off()
cursor_on()
cursor_off()
Aliases
clear_screen and reset_screen can also be used with this aliases:
reset_terminal <= reset_screen
clear_terminal <= clear_screen
reset <= reset_screen
clear <= clear_screen
Method description
The method clear_screen is clearing the terminal. This is similar to the system call system('clear'). The method reset_screen is reseting the terminal. This is similar to the system call system('reset').
The method winsize gets the dimensions in x-direction and y-direction of the terminal. The methods chars and pixels extract chars (rows and cols) and pixels (xpix and ypix.)
The method get_cursor_position() gets the current cursor position in the terminal window. The method set_cursor_position() sets the cursor position in the terminal window.
echo_on and echo_off turnes echo of commands on or off. cursor_on and cursor_off shows or hides the cursor.
Programmable background
The methods clear_screen and reset_screen are using escape sequences. Doing this no other Perl modules are needed to clear and reset a screen. Both are in principle one liners.
The method winsize is using the C/C++ header for the system ioctl call and there the command TIOCGWINSZ. The call return winsize in rows and cols and xpix and ypix.
SEE ALSO
ASCII escape sequences
AUTHOR
Dr. Peter Netz, <ztenretep@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2022 by Dr. Peter Netz
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.30.0 or, at your option, any later version of Perl 5 you may have available.