1.163 class terminal_block

One command a client ran in a terminal_image, as its OSC 133 semantic prompt marks describe it: the prompt (A), the line the user edited (B), the output of running it (C) and the end of that output (D, or the prompt after it). The terminal builds them as the marks arrive and keeps them in terminal_block<-blocks, oldest first. Subclass of object.

A block is what lets the window be addressed by what it was used for rather than by row and column: terminal_block<-content hands over the command or its output as a string, terminal_block->select and terminal_block->copy put either on the clipboard, terminal_block->scroll_to brings it back into view and terminal_block->fold takes its output off the display.

A command can take more than one line to type. The client asks for each of them separately -- a Prolog term is read a line at a time until its full stop -- and marks all but the first with A;k=s, the secondary prompt. Those lines are one command and one block, anchored to the line it started on.

The commandline editor of SWI-Prolog emits the marks by default; see el_set/2 prompt_marks(Boolean) in library(editline). A client that marks nothing produces no blocks.

1.163.1 Lifetime

terminal_block->remove is the one thing that changes the buffer rather than reading it: everything else here only says what is already there.

A block outlives the marks it was made from but not the text it points at. Its positions are lines of the scrollback, so a line pushed out of the buffer takes with it the marks that named it, and a block with nothing left is dropped from terminal_block<-blocks with its terminal_block<-terminal set to @nil. A block still held elsewhere is then harmless: every method that needs text fails.

terminal_block<-prompt, terminal_block<-input, terminal_block<-output and terminal_block<-end report positions in the flat character space of the terminal, which composes with terminal_block<-contents, terminal_block<-find, terminal_block->selection and terminal_block->scroll_to -- but see Character indices in the documentation of class terminal_image: those indices shift as output pushes lines out of the scrollback. terminal_block<-id is the handle that outlives them.

See also
class terminal_image

1.163.2 Instance variables

terminal_block <- terminal: terminal_image*
Terminal the block belongs to, or @nil once its lines have left the scrollback.
terminal_block <- id: int
Handle that outlives the character indices. See terminal_block<-block.
terminal_block <- folded: bool
Whether its output is collapsed. Assigning @on folds and @off unfolds; it fails rather than folding a command still running, one that printed nothing, or anything while an application holds the alternate screen.
terminal_block <- running: bool
The command has been entered and is still writing its output, i.e. C was seen and D was not.

1.163.3 Send methods

terminal_block ->initialise: terminal=terminal_image
Create a block on a terminal. Blocks are made by the terminal as the marks arrive; there is normally no reason to make one.
terminal_block ->select: what=[{command,output,all}]
Make the block the selection of its terminal. command is the line the user entered, output (the default) what it printed, and all both with the prompt in front.
terminal_block ->copy: what=[{command,output,all}], which=[{primary,clipboard}]
Put the text of the block on the clipboard. It goes by terminal_block<-content rather than by the region of the screen the block covers, so a copy of the command is what was typed: without the return that entered it, and without the continuation prompts the client drew down the left of a command it collected over several lines. all keeps them, being what the window shows. Lines are separated by a single newline, as terminal_block<-content separates them, and not by the \r\n of terminal_image<-selected: what is copied here is a command to be read back, not a region of the screen. Does not disturb the selection; terminal_block->select is for that.
terminal_block ->scroll_to:
Scroll the prompt of the block into view, opening a fold that hides it.
terminal_block ->remove:
Take the command out of the buffer altogether: the lines it covers go and the text under them moves up into the gap. The block is dropped from terminal_block<-blocks with its terminal_block<-terminal set to @nil. There is no undo.

The buffer gets shorter at the end the client is writing to, so the older history is left where it is. Everything that names a line by its place in the scrollback is moved with the text: the caret, the window and the marks of every other command. The selection and a running incremental search are ended rather than followed, since what they were on may be what is going.

Fails for a command still running, for one whose output has not been marked as ended, and while an application holds the alternate screen.

terminal_block ->fold:
terminal_block ->unfold:
terminal_block ->toggle_fold:
Hide the output of the command, show it again, or the one or the other. Wrappers over terminal_block->folded.

1.163.4 Get methods

terminal_block <-prompt: -> int
Index where the prompt starts (OSC 133 A). Fails for a read that printed no prompt, and once the line has left the buffer.
terminal_block <-input: -> int
Index where the line the user entered starts (OSC 133 B).
terminal_block <-output: -> int
Index where its output starts (OSC 133 C). Fails while the line is still being edited.
terminal_block <-end: -> int
Index where its output ends (OSC 133 D). Fails while the command is still running.
terminal_block <-content: what=[{command,output,all}] -> string
New string with the text of the block; what as for terminal_block->select. Lines are separated by a single newline, as terminal_block<-contents does. A part that has not been reached yet -- the output of a command still running -- reaches to where the client is writing.

command is what was typed: it stops short of the return that entered it, and passes over the continuation prompt in front of each line of a command the client collected over several. all keeps both, being the text of the window.