A textBuffer is a buffer holding text contents that can be used for things like text areas, or even text editors.
A textBuffer is way more flexible than a raw screenBuffer for that purpose. Nonetheless, a textBuffer is always backed by a screenBuffer, i.e. its dst (destination) should be a screenBuffer.
Internally, it has 3 buffers:
- a buffer holding raw text (line of text): raw text buffer
- a buffer holding attributes (colors, styles): the attr buffer
- a misc buffer holding userland data, useful for your application: the misc buffer
It comes with a lot of facilities to interact with the text, to manage the cursor, to colorize the text, to manage tabs...
Note: The TextBuffer is still in beta, some methods are missing but the existing API should be stable or remain backward compatible. If you wonder what can be done with textBuffers, have a look to Neon, a small text-editor in early alpha stage, featuring a javascript syntax hilighter.
-
Constructor and static methods:
-
Properties:
-
Methods:
- .getText()
- .setText()
- .getHidden()
- .setHidden()
- .getContentSize()
- .getCursorOffset()
- .setCursorOffset()
- .setEmptyCellAttr()
- .setAttrAt()
- .setAttrCodeAt()
- .setAttrRegion()
- .setAttrCodeRegion()
- .getMisc()
- .getMiscAt()
- .moveTo()
- .moveToColumn()
- .moveToLine(), .moveToRow()
- .move()
- .moveUp()
- .moveDown()
- .moveLeft()
- .moveRight()
- .moveForward()
- .moveBackward()
- .moveToStartOfWord()
- .moveToEndOfWord()
- .moveToStartOfLine()
- .moveToEndOfLine()
- .moveToStartOfBuffer()
- .moveToEndOfBuffer()
- .moveInBound()
- .insert()
- .prepend()
- .append()
- .delete()
- .backDelete()
- .newLine()
- .joinLine()
- .iterate()
- .wrapLine()
- .wrapAllLines()
- .draw()
- .drawCursor()
- .load()
- .save()
- options
Object
, where:- dst:
ScreenBuffer
the destination to write on - width
integer
(optional, default: Infinity) width, i.e. max-length of a line - height
integer
(optional, default: Infinity) height, i.e. maximum number of lines - x:
integer
(optional) default x-position in the dst - y:
integer
(optional) default y-position in the dst - tabWidth:
integer
(optional, default: 4) set the tabulation width - forceInBound:
integer
(optional, default: false) if true, the cursor cannot move out of bounds - hidden:
boolean
(optional, default: false) if true, the text is invisible - wrap:
boolean
(optional, default: false) set the wrapping behavior
- dst:
This creates a TextBuffer instance with the appropriate options.
DEPRECATED, use new TextBuffer() instead.
Those properties are respectively the x and the y coordinate, in the dst (destination), where the textBuffer should be drawn. This can be overriden when invoking .draw().
It extracts and returns the text content of the textBuffer.
- text
string
the text content - markup
boolean
or 'ansi', true if the text contains markup that should be interpreted 'ansi' if it contains ANSI code (default: false - raw text) - baseAttr
object
orinteger
, an attribute used as the base attribute for the text content (if there is markup, the markup attributes are stacked with this base attribute)
This set the text content of the textBuffer.
It reset both the attr buffer (or set it to the correct attribute if there is markup) and the misc buffer.
For the markup option, see also the full style markup reference.
.getHidden()
It returns true if the textBuffer is in hidden mode.
.setHidden( state )
- state
boolean
if true, it enables the hidden mode, else it disables it
It set on or off the hidden mode. The hidden mode is useful if your textBuffer is holding things like password.
It returns an object with a width and height properties: the size of the text content.
It returns the cursor offset in the raw text content.
- offset
integer
the new offset of the cursor
It set the cursor offset in the raw text content.
- attr
Object
orinteger
attributes of the chars (attribute object or bit flags, see: the attribute object)
This set the attributes for empty cells, i.e. cells in the screenBuffer where there is no textBuffer content (not even spaces).
- attr
Object
orinteger
attributes of the char (attribute object or bit flags, see: the attribute object) - x
integer
the x-coordinate (i.e. the column number) - y
integer
the y-coordinate (i.e. the row number)
This set the attributes for the textBuffer cell at (x,y) coordinates.
- attr
integer
attributes of the char in the bit flags form - x
integer
the x-coordinate (i.e. the column number) - y
integer
the y-coordinate (i.e. the row number)
Like .setAttrAt(), but it only accepts attributes in the bit flags form (faster).
- attr
Object
orinteger
attributes of the char (attribute object or bit flags, see: the attribute object) - region
Object
(optional, default to the whole textBuffer) the targeted rectangular region, where:- xmin
integer
the minimal x-coordinate - xmax
integer
the maximal x-coordinate - ymin
integer
the minimal y-coordinate - ymax
integer
the maximal y-coordinate
- xmin
This set the attributes for the textBuffer cells in the rectangular region (if specified), or all cells of the textBuffer.
- attr
integer
attributes of the char in the bit flags form - region
Object
(optional, default to the whole textBuffer) the targeted rectangular region, where:- xmin
integer
the minimal x-coordinate - xmax
integer
the maximal x-coordinate - ymin
integer
the minimal y-coordinate - ymax
integer
the maximal y-coordinate
- xmin
Like .setAttrRegion(), but it only accepts attributes in the bit flags form (faster).
Get the misc meta data at the current cursor position.
- x
integer
the x-coordinate (i.e. the column number) - y
integer
the y-coordinate (i.e. the row number)
Get the misc meta data at the (x,y) coordinates.
- x
integer
the x-coordinate (i.e. the column number) - y
integer
the y-coordinate (i.e. the row number)
It moves the textBuffer's cursor to the (x,y) coordinates.
- x
integer
the x-coordinate (i.e. the column number)
It moves the textBuffer's cursor to the xth column.
- y
integer
the y-coordinate (i.e. the row number)
It moves the textBuffer's cursor to the yth row/line.
- x
integer
the relative x-coordinate (i.e. the column number) - y
integer
the relative y-coordinate (i.e. the row number)
Like .moveTo(), but it moves the textBuffer's cursor relative to its current position.
It moves the textBuffer's cursor one cell up.
It moves the textBuffer's cursor one cell down.
It moves the textBuffer's cursor one cell left.
It moves the textBuffer's cursor one cell right.
- justSkipNullCells
boolean
It moves the textBuffer's cursor one character forward, different form .moveRight(), since it would move the cursor at the begining of the next line if it was on the last character of a line.
It always skips null cells. Null cells are produced by tabulation: there is only one text character but followed by n null cells that are not part of the actual text content.
If justSkipNullCells is set, it does not move forward unless the cursor is over a null cell.
- justSkipNullCells
boolean
It moves the textBuffer's cursor one character backward, different form .moveLeft(), since it would move the cursor at the end of the previous line if it was on the first character of a line.
It always skips null cells. Null cells are produced by tabulation: there is only one text character but followed by n null cells that are not part of the actual text content.
If justSkipNullCells is set, it does not move backward unless the cursor is over a null cell.
It moves the textBuffer's cursor to the start of the current word.
It moves the textBuffer's cursor to the end of the current word.
It moves the textBuffer's cursor to the begining of the current line.
It moves the textBuffer's cursor to the end of the current line.
It moves the textBuffer's cursor to the begining of the buffer (i.e. the begining of the first line).
It moves the textBuffer's cursor to the end of the buffer (i.e. the end of the last line).
- ignoreCx
boolean
do not affect the cursor's x-coordinate
It moves the textBuffer's cursor in bound, i.e. to a cell that has text content.
- text
string
the raw text to insert - markup
boolean
or 'ansi', true if the text contains markup that should be interpreted 'ansi' if it contains ANSI code (default: false - raw text) - attr
Object
orinteger
(optional, default: the empty cell attributes) attributes of the text about to be inserted (attribute object or bit flags, see: the attribute object)
It inserts the text at the current cursor position, with the given attributes.
- text
string
the raw text to insert - markup
boolean
or 'ansi', true if the text contains markup that should be interpreted 'ansi' if it contains ANSI code (default: false - raw text) - attr
Object
orinteger
(optional, default: the empty cell attributes) attributes of the text about to be inserted (attribute object or bit flags, see: the attribute object)
It prepend the text (i.e. insert it at the begining), with the given attributes.
- text
string
the raw text to insert - markup
boolean
or 'ansi', true if the text contains markup that should be interpreted 'ansi' if it contains ANSI code (default: false - raw text) - attr
Object
orinteger
(optional, default: the empty cell attributes) attributes of the text about to be inserted (attribute object or bit flags, see: the attribute object)
It append the text (i.e. insert it at the end), with the given attributes.
- n
integer
(optional, default: 1) the number of chars to delete
It deletes n characters at the current cursor position. This is the action usually bound to the delete key.
- n
integer
(optional, default: 1) the number of chars to delete
It deletes n characters backward starting from the current cursor position. This is the action usually bound to the backspace key.
It inserts a new line at the current cursor position. It will split the current line in two, if there are characters at/after the current cursor position.
It moves the cursor to the end of the line and joins the current line with the following line.
- options
Object
where:- finalCall
boolean
call the callback one more time at the end of the buffer with an empty string
- finalCall
- callback
Function( cellData )
, where:- cellData
Object
where:- offset
integer
the offset/position of the current cell in the raw/serialized text - x
integer
the x-coordinate (i.e. the column number) of the current cell - y
integer
the y-coordinate (i.e. the row number) of the current cell - text
string
a single character string, the character of the current cell - attr
integer
the attributes of the current cell in the bit flags mode, use ScreenBuffer.attr2object() to convert it if necessary - misc
Object
userland meta-data for the current cell
- offset
- cellData
It iterates over the whole textBuffer, using the callback for each cell.
- y
integer
the line to wrap (default: current line, the line where the cursor is) - width
integer
the wanted width (default: the current textBuffer's lineWrapWidth) - wordWrap
boolean
if true, force word-aware line-splitting (default: the current textBuffer's wordWrap)
It wraps the current line (or the line y), splitting it as many times it is necessary to fit the wanted width.
The wordWrap option can be used to avoid splitting in a middle of a word or before punctuation.
- width
integer
the wanted width (default: the current textBuffer's lineWrapWidth) - wordWrap
boolean
if true, force word-aware line-splitting (default: the current textBuffer's wordWrap)
Same than .wrapLine()
, but for all lines.
- options
Object
(optional) if provided, each defined option will override the default behavior. Available options are:- dst
ScreenBuffer
(optional) overridetextBuffer.dst
- x
integer
(optional) overridetextBuffer.x
- y
integer
(optional) overridetextBuffer.y
- srcClipRect
Rect
(optional, default: the whole source region is used) the source clipping rectangle - dstClipRect
Rect
(optional, default: the whole destination region is used) the destination clipping rectangle - blending
boolean
(optional, default: false) if true blending (transparencies) is allowed - wrap
boolean
orstring
(optional, default: false) if set, wrapping will be enabled, it can be set to:- 'x': only wrap along the x-axis
- 'y': only wrap along the y-axis
- true, 'both': wrap along the x and y axis
- false: no wrapping
- tile
boolean
(optional, default: false) if true, the source will fill the destination entirely using tiling: the source is repeated multiple times along the x and y axis.
- dst
This draws the current textBuffer into its dst (destination), which is a ScreenBuffer
instance.
To actually display a textBuffer, you need to:
- draw the textBuffer to a screenBuffer
- then draw that screenBuffer to the terminal (or draw the whole screenBuffer chain until the terminal)
- options
Object
(optional) if provided, each defined option will override the default behavior. Available options are:- dst
ScreenBuffer
(optional) overridetextBuffer.dst
- dst
This draws the current textBuffer's cursor into its dst (destination), which is a ScreenBuffer
instance.
Drawing the cursor means that the destination's cursor is moved to the coordinate of the source's cursor.
It updates the cursor position so the user know where he is typing.
- filepath
string
the path of the file to load - callback
Function( error )
completion callback, DEPRECATED: prefer the Promise interface.
If callback is omitted, it returns a Promise on completion.
This erases all contents (text, attr and misc) and loads the content of the file (which is a text file).
- filepath
string
the path of the file to save into - callback
Function( error )
completion callback, DEPRECATED: prefer the Promise interface.
If callback is omitted, it returns a Promise on completion.
This saves the raw text content into a file.