Struct text_terminal::Unit
source · pub struct Unit { /* private fields */ }
Expand description
A Unit
is a single character block displayed in the terminal.
Some terminal emulators call this structure a cell
,
but this is different from the concept of a cell
because it may contain
more than just a single displayable character, in order to support complex Unicode/emoji.
1-to-1 Relationship between Units and Columns
It is guaranteed that one Unit
in the scrollback buffer corresponds to exactly one screen column,
which makes it easy to calculate the conversions between screen cursor coordinate points
and scrollback buffer coordinate points.
For example, if the screen is 80 columns wide, a Line with 120 units will display across
exactly 1.5 lines, displaying right up to the 40th column of the second row.
Because a complex Unicode character may require more than one column to display,
such as a tab or emoji sequence, there are flags in a Unit
that indicate whether it is
part of a wider display character sequence.
There are flags for both the beginning unit and all of the placeholder units that follow it
(which exist solely to satisfy the 1-to-1 relationship between screen columns and scrollback buffer units).
Thus, it is easy to determine where multi-column Unit sequences start and end.
Wide-display character sequences like tabs and emoji are always stored completely
in the starting Unit (as a Character::Multi
variant),
with the following placeholder Units containing a default empty Character::Single
with the null character within it.
This conveniently allows the screen cursor to store a single Unit
object within it
that represents the entirety of that displayable Unit,
instead of more complex storage strategy that splits up wide character sequences into
multiple Unit
s.
What Units are Not
Displayable control/escape sequences, i.e., those that affect text style,
do not exist as individual Unit
s,
though their effects on text style are represented by a Unit
’s FormatFlags
.
Non-displayable control/escape sequences, i.e., bells, backspace, delete, etc,
are NOT saved as Unit
s in the terminal’s scrollback buffer,
as they cannot be displayed and are simply transient actions.
Methods from Deref<Target = Character>§
sourcepub fn displayable_width(&self) -> u16
pub fn displayable_width(&self) -> u16
Returns the number of columns required to display this Character
within a Unit
,
either a single char
or a String
.
A return value of 0
indicates this Unit
requires special handling
to determine its displayable width.
This includes characters like new lines, carriage returns, tabs, etc.