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()
- .setEmptyCellAttr()
- .setAttrAt()
- .setAttrCodeAt()
- .setAttrRegion()
- .setAttrCodeRegion()
- .getMisc()
- .getMiscAt()
- .moveTo()
- .moveToColumn()
- .moveToLine(), .moveToRow()
- .move()
- .moveUp()
- .moveDown()
- .moveLeft()
- .moveRight()
- .moveForward()
- .moveBackward()
- .moveToEndOfLine()
- .moveInBound()
- .insert()
- .delete()
- .backDelete()
- .newLine()
- .joinLine()
- .iterate()
- .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
This set the text content of the textBuffer.
It reset both the attr buffer and the misc buffer.
.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.
- 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 end of the current 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 - 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.
- 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.
- 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
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
This saves the raw text content into a file.