NAME
Tickit::Term - terminal formatting abstraction
SYNOPSIS
DESCRIPTION
Provides terminal control primatives for Tickit; a number of methods that control the terminal by writing control strings. This object itself performs no acutal IO work; it writes bytes to a delegated object given to the constructor called the writer.
This object is not normally constructed directly by the containing application; instead it is used indirectly by other parts of the Tickit distribution.
CONSTRUCTOR
$term = Tickit::Term->find_for_term( %params )
Attempts to load and construct a subclass determined by the current terminal type (as given by $ENV{TERM}). If this fails, returns a normal Tickit::Term instead.
$term = Tickit::Term->new( %params )
Constructs a new Tickit::Term object.
Takes the following named arguments at construction time:
- UTF8 => BOOL
-
If defined, overrides locale detection to enable or disable UTF-8 mode. If not defined then this will be detected from the locale by using Perl's
${^UTF8LOCALE}variable. - writer => OBJECT
-
An object delegated to for sending strings of terminal control bytes to the terminal itself. This object must support a single method,
write, taking a string of bytes.$writer->write( $data )Such an interface is supported by an
IO::Handleobject. - output_handle => HANDLE
-
Optional. If supplied, will be used as the terminal filehandle for querying the size. Even if supplied, all writing operations will use the
writerfunction rather than performing IO operations on this filehandle. - input_handle => HANDLE
-
Optional. If supplied, will be used as the terminal filehandle for reading keypress and other events.
- on_resize => CODE
-
Optional. Event handler function for when the terminal window is resized. Will be passed the
Tickit::Terminstance, and the new size.$on_resize->( $term, $lines, $cols ) - on_key => CODE
-
Optional. Event handler function for when a key is pressed. Will be passed the
Tickit::Terminstance, a type string (eithertextfor unmodified Unicode orkeyfor special keys or modified Unicode) and a string containing a representation of the text or key.$on_key->( $term, $type, $str ) - on_mouse => CODE
-
Optional. Event handler function for when a mouse button is pressed or released, or the cursor dragged with a button pressed. Will be passed the
Tickit::Terminstance, a string indicatingpress,drag,releaseorwheel, the button number or wheel direction, and the 0-based line and column index. Forwheelevents, the direction will be one ofupordown.$on_mouse->( $term, $ev, $button_dir, $line, $col )
METHODS
$fh = $term->get_input_handle
Returns the input handle set by the input_handle constructor arg.
$fh = $term->get_output_handle
Returns the output handle set by the output_handle constructor arg.
$term->set_output_buffer( $len )
Sets the size of the output buffer
$term->flush
Flushes the output buffer to the terminal
$id = $term->bind_event( $ev, $code, $data )
Installs a new event handler to watch for the event specified by $ev, invoking the $code reference when it occurs. $code will be invoked with the given the terminal, the event name, a HASH reference containing event arguments, and the $data value. It returns an ID value that may be used to remove the handler by calling unbind_event_id.
$code->( $term, $ev, $args, $data )
The $args hash will contain keys depending on the event type:
- key
-
The hash will contain
type(a dualvar giving the key event type as an integer or string event name,textorkey), andstr(a string containing the key event string). - mouse
-
The hash will contain
type(a dualvar giving the mouse event type as an integer or string event name,press,drag,releaseorwheel),button(an integer for non-wheel events, or a dualvar for wheel events giving the wheel direction asupordown), andlineandcolas integers. - resize
-
The hash will contain
linesandcolsas integers.
$term->unbind_event_id( $id )
Removes an event handler that returned the given $id value.
$term->set_on_resize( $on_resize )
$term->set_on_key( $on_key )
$term->set_on_mouse( $on_mouse )
Set a new CODE references to handle events.
$term->refresh_size
If a filehandle was supplied to the constructor, fetch the size of the terminal and update the cached sizes in the object. May invoke on_resize if the new size is different.
$term->set_size( $lines, $cols )
Defines the size of the terminal. Invoke on_resize if the new size is different.
$lines = $term->lines
$cols = $term->cols
Query the size of the terminal, as set by the most recent refresh_size or set_size operation.
$term->print( $text )
Print the given text to the terminal at the current cursor position
$term->goto( $line, $col )
Move the cursor to the given position on the screen. If only one parameter is defined, does not alter the other. Both $line and $col are 0-based.
$term->move( $downward, $rightward )
Move the cursor relative to where it currently is.
$success = $term->scrollrect( $top, $left, $lines, $cols, $downward, $rightward )
Attempt to scroll the rectangle of the screen defined by the first four parameters by an amount given by the latter two. Since most terminals cannot perform arbitrary rectangle scrolling, this method returns a boolean to indicate if it was successful. The caller should test this return value and fall back to another drawing strategy if the attempt was unsuccessful.
The cursor may move as a result of calling this method; its location is undefined if this method returns successful.
$term->chpen( $pen )
$term->chpen( %attrs )
Changes the current pen attributes to those given. Any attribute whose value is given as undef is reset. Any attributes not named are unchanged.
For details of the supported pen attributes, see Tickit::Pen.
$term->setpen( $pen )
$term->setpen( %attrs )
Similar to chpen, but completely defines the state of the terminal pen. Any attribute not given will be reset to its default value.
$term->clear
Erase the entire screen
$term->erasech( $count, $moveend )
Erase $count characters forwards. If $moveend is true, the cursor is moved to the end of the erased region. If defined but false, the cursor will remain where it is. If undefined, the terminal will perform whichever of these behaviours is more efficient, and the cursor will end at some undefined location.
Using $moveend may be more efficient than separate erasech and goto calls on terminals that do not have an erase function, as it will be implemented by printing spaces. This removes the need for two cursor jumps.
$success = $term->setctl_int( $ctl, $value )
Sets the value of an integer terminal control option. $ctl should be one of the following options. They can be specified either as integers, using the following named constants, or as strings giving the part following TERMCTL_ in lower-case.
- TERMCTL_ALTSCREEN
-
Enables DEC Alternate Screen mode
- TERMCTL_CURSORVIS
-
Enables cursor visible mode
- TERMCTL_CURSORBLINK
-
Enables cursor blinking mode
- TERMCTL_CURSORSHAPE
-
Sets the shape of the cursor.
$valueshould be one ofTERM_CURSORSHAPE_BLOCK,TERM_CURSORSHAPE_UNDERorTERM_CURSORSHAPE_LEFT_BAR. - TERMCTL_KEYPAD_APP
-
Enables keypad application mode
- TERMCTL_MOUSE
-
Enables mouse tracking mode
$success = $term->setctl_str( $ctl, $value )
Sets the value of a string terminal control option. $ctrl should be one of the following options. They can be specified either as integers or strings, as for setctl_int.
- TERMCTL_ICON_TEXT
- TERMCTL_TITLE_TEXT
- TERMCTL_ICONTITLE_TEXT
-
Sets the terminal window icon text, title, or both.
$term->mode_altscreen( $on )
Set or clear the DEC Alternate Screen mode. This method is deprecated in favour of setctl_int.
$term->mode_cursorvis( $on )
Set or clear the cursor visible mode. This method is deprecated in favour of setctl_int.
$term->mode_mouse( $on )
Set or clear the mouse tracking mode. This method is deprecated in favour of setctl_int.
$term->input_push_bytes( $bytes )
Feeds more bytes of input. May result in on_key or on_mouse events.
$term->input_readable
Informs the term that the input handle may be readable. Attempts to read more bytes of input. May result in on_key or on_mouse events.
$term->input_wait
Block until some input is available, and process it. Returns after one round of input has been processed. May result in on_key or on_mouse events.
$timeout = $term->check_timeout
Returns a number in seconds to represent when the next timeout should occur on the terminal, or undef if nothing is waiting. May invoke expired timeouts, and cause a on_key event to occur.
TODO
Track cursor position, and optimise (or eliminate entirely)
gotocalls.
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>