EXPORTS
Nothing per default, but can export the following per request:
:all
:api
Init
Close
Interrupt
Flush
SetCursor
HideCursor
SetCell
GetCell
SetChar
SetFg
SetBg
CellBuffer
ParseEvent
PollRawEvent
PollEvent
Size
Clear
SetInputMode
SetOutputMode
Sync
NAME
Termbox::Go::WinVT - Windows Terminal Termbox implementation
DESCRIPTION
This document describes the Termbox library for Perl, using the Windows Terminal, which was introduced with Windows 10.
The advantage of the Termbox library is the use of an standard. Termbox contains a few functions with which terminal applications can be developed with high portability and interoperability.
Note: Windows Terminal still requires parts of the classic Console API, e.g. to set the input or output mode and codepage.
BACKEND SELECTION
On Windows, Termbox::Go selects Termbox::Go::WinVT when WT_SESSION is set. If WT_SESSION is not set, it selects Termbox::Go::Win32.
PS C:\> $env:WT_SESSION # check if WT_SESSION is set
6361ae93-eabd-4e94-9523-54a64ee30ded
PS C:\> $env:WT_SESSION="" # unset WT_SESSION
BENCHMARK
The following numbers are a single snapshot and not a full benchmark series.
Method:
Measurement phase: 40 frames.
Per frame, all cells are written with
SetCell()/Flush().Workload limit: 80x24 cells (1920 cells per frame).
Single-machine result (measured with Windows Version 10.0.26100.8246):
Backend Impl Time(s) FPS Cells/s
Win32 Console API Termbox::Go::Win32 1.183 33.81 64907
WinVT Termbox::Go::WinVT 2.340 17.10 32827
In this run, Win32 was about twice as fast as WinVT.
COPYRIGHT AND LICENCE
This file is part of the port of Termbox.
Copyright (C) 2012 by termbox-go authors
This library content was taken from the termbox-go implementation of Termbox
which is licensed under MIT licence.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
AUTHORS
2024,2025 by J. Schneider https://github.com/brickpool/
DISCLAIMER OF WARRANTIES
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
REQUIRES
SEE ALSO
SUBROUTINES
CellBuffer
my \@arrayref = CellBuffer();
Returns a slice into the termbox's back buffer. You can get its dimensions using "Size" function. The slice remains valid as long as no "Clear" or "Flush" function calls were made after call to this function.
Clear
my $errno = Clear($fg, $bg);
Clears the internal back buffer.
Close
my $errno = Close();
Finalizes termbox library, should be called after successful initialization when termbox's functionality isn't required anymore.
Flush
my $errno = Flush();
Synchronizes the internal back buffer with the terminal.
GetCell
my \%Cell = GetCell($x, $y);
Returns the specified cell from the internal back buffer.
HideCursor
my $errno = HideCursor();
The shortcut for SetCursor(-1, -1).
Init
my $errno = Init();
Initializes termbox library. This function should be called before any other functions. After successful initialization, the library must be finalized using "Close" function.
Example usage:
my $err = Init();
if ($err != 0) {
die "Error: $err"
}
Close();
Interrupt
my $errno = Interrupt();
Interrupt an in-progress call to "PollEvent" by causing it to return EventInterrupt. Note that this function will block until the "PollEvent" function has successfully been interrupted.
ParseEvent
my \%event = ParseEvent($data);
After getting a raw event from PollRawEvent function call, you can parse it again into an ordinary one using termbox logic. That is parse an event as termbox would do it. Returned event in addition to usual Event struct fields sets N field to the amount of bytes used within data slice. If the length of data slice is zero or event cannot be parsed for some other reason, the function will return a special event type: EventNone.
IMPORTANT: EventNone may contain a non-zero N, which means you should skip these bytes, because termbox cannot recognize them.
NOTE: This API is experimental and may change in future.
PollEvent
my \%Event = PollEvent();
Wait for an event and return it. This is a blocking function call.
PollRawEvent
my \%event = PollRawEvent($data);
Wait for an event and return it. This is a blocking function call. Instead of EventKey and EventMouse it returns EventRaw events. Raw event is written into data slice and Event's N field is set to the amount of bytes written. The minimum required length of the data slice is 1. This requirement may vary on different platforms.
NOTE: This API is experimental and may change in future.
SetBg
my $errno = SetBg($x, $y, $bg);
Changes cell's background attributes in the internal back buffer at the specified position.
SetCell
my $errno = SetCell($x, $y, $ch, $fg, $bg);
Changes cell's parameters in the internal back buffer at the specified position.
SetChar
my $errno = SetChar($x, $y, $ch);
Changes cell's character (utf8) in the internal back buffer at the specified position.
SetCursor
my $errno = SetCursor($x, $y);
Sets the position of the cursor. See also "HideCursor".
SetFg
my $errno = SetFg($x, $y, $fg);
Changes cell's foreground attributes in the internal back buffer at the specified position.
SetInputMode
my $current = SetInputMode($mode);
Sets termbox input mode. Termbox has two input modes:
1. Esc input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC means 'KeyEsc'. This is the default input mode.
2. Alt input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC enables 'ModAlt' modifier for the next keyboard event.
Both input modes can be OR'ed with Mouse mode. Setting Mouse mode bit up will enable mouse button press/release and drag events.
If $mode is 'InputCurrent', returns the current input mode. See also 'Input*' constants.
SetOutputMode
my $current = SetOutputMode($mode);
Sets the termbox output mode. Termbox has four output options:
1. OutputNormal => [1..8] This mode provides 8 different colors: black, red, green, yellow, blue, magenta, cyan, white Shortcut: ColorBlack, ColorRed, ... Attributes: AttrBold, AttrUnderline, AttrReverse
Example usage:
SetCell($x, $y, '@', ColorBlack | AttrBold, ColorRed);
2. Output256 => [1..256] In this mode you can leverage the 256 terminal mode: 0x01 - 0x08: the 8 colors as in OutputNormal 0x09 - 0x10: Color* | AttrBold 0x11 - 0xe8: 216 different colors 0xe9 - 0x1ff: 24 different shades of grey
Example usage:
SetCell($x, $y, '@', 184, 240);
SetCell($x, $y, '@', 0xb8, 0xf0);
3. Output216 => [1..216] This mode supports the 3rd range of the 256 mode only. But you don't need to provide an offset.
4. OutputGrayscale => [1..26] This mode supports the 4th range of the 256 mode and black and white colors from 3th range of the 256 mode But you don't need to provide an offset.
In all modes, 0x00 represents the default color.
perl examples/output.pl to see its impact on your terminal.
If $mode is 'OutputCurrent', it returns the current output mode.
Note that this may return a different OutputMode than the one requested, as the requested mode may not be available on the target platform.
Size
my ($x, $y) = Size();
Returns the size of the internal back buffer (which is mostly the same as terminal's window size in characters). But it doesn't always match the size of the terminal window, after the terminal size has changed, the internal back buffer will get in sync only after "Clear" or "Flush" function calls.
Sync
my $errno = Sync();
Sync comes handy when something causes desync between termbox's understanding of a terminal buffer and the reality. Such as a third party process. Sync forces a complete resync between the termbox and a terminal, it may not be visually pretty though.
peek_console_input
my @events = peek_console_input($hConsoleInput);
Reads the current event type from the specified console input buffer without removing an event from the buffer (or undef on errors).
peek_named_pipe
my $nTotalBytesAvail = peek_named_pipe($hNamedPipe);
Returns information about data in the pipe. The parameter can be a handle to a named pipe instance, as returned by the CreateNamedPipe or CreateFile function. The handle must have GENERIC_READ access. Further details:: https://stackoverflow.com/a/73571717
read_console_input
my @events = read_console_input($hConsoleInput);
Reads data from a console input buffer and removes it from the buffer. Returns a list of values, which depending on the event's type.
Information about a keyboard event:
wEventType: KEY_EVENT = 0x0001
bKeyDown: If the key is pressed, this member is TRUE. Otherwise FALSE.
wRepeatCount: The repeat count (indicates that a key is being held down).
wVirtualKeyCode: A virtual-key code that identifies the given key.
wVirtualScanCode: The virtual scan code of the given key.
uChar: Translated Unicode character
dwControlKeyState: The state of the control keys
Information about a mouse movement or button press event:
wEventType: MOUSE_EVENT = 0x0002
dwMousePosition: A COORD structure that contains the location of the cursor
dwButtonState: The status of the mouse buttons.
dwControlKeyState: The state of the control keys.
dwEventFlags: The type of mouse event.
Information about the new size of the console screen buffer:
wEventType: WINDOW_BUFFER_SIZE_EVENT = 0x0004
dwSize: A COORD structure that contains the size of the console.
Used internally and should be ignored:
wEventType: MENU_EVENT = 0x0008
dwCommandId: Reserved.
Used internally and should be ignored:
wEventType: FOCUS_EVENT = 0x0010
bSetFocus: Reserved.