tui

    Immediate-mode terminal UI library for MoonBit native targets, built on moonbit-community/tty.

    tui
    terminal
    immediate-mode
    native
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    17 days ago
    Downloads
    24

    #moonbit-community/tui

    An immediate-mode terminal UI library for MoonBit native targets, built on moonbit-community/tty.

    Early work in progress. The API is small and will change.

    #The model

    The screen is a function of your state. Each time something changes you paint one complete Frame and hand it to the terminal; the library compares it with the frame already on screen and sends only the cells that differ.

    • A Frame is a grid of styled cells that starts blank.
    • You lay it out top-down: split the frame's area into smaller areas, then paint a Line into each. You never compute a column by hand.
    • The loop is yours: read an event, update your state, draw. There are no callbacks, message types, or widget trees.
    • A Terminal is a full-screen session. It owns raw mode, the alternate screen, and restoring the terminal when your code returns, raises, or is cancelled.

    ///|
    struct State {
    mut count : Int32
    }

    ///|
    fn view(state : State, size : @tui.Size) -> @tui.Frame {
    let frame = @tui.Frame(width=size.width, height=size.height)
    let dim = @tui.Style(fg=Basic(BrightBlack))
    // Two rows at the top; the rest of the screen stays blank.
    let rows = @tui.Layout::vertical([Length(1), Length(1), Fill(1)])
    guard! rows.split(frame.area()) is [count, help, _]
    frame.line(count, @tui.Line([Span("count: "), Span("\{state.count}")]))
    frame.line(help, @tui.Line::from_text("space: +1 · Esc: quit", style=dim))
    frame
    }

    ///|
    async fn main {
    @tui.with_terminal(terminal => {
    let state = State::{ count: 0, }
    for ;; {
    terminal.draw(view(state, terminal.size()))
    match terminal.read_event() {
    Input(Key(key)) =>
    match key.code {
    Escape => break
    Char(' ') => state.count += 1
    _ => ()
    }
    // A resize needs nothing special: the next turn paints a frame of
    // the new size.
    _ => ()
    }
    }
    })
    }

    view never touches the terminal, so it can be tested directly: view(state, size).to_text() returns what the screen would show, .cursor() where the cursor is, and .style_at(row~, col~) the style of a cell.

    #Layout

    There are two layers, and neither involves coordinates.

    Areas say where things go. frame.area() is the whole frame, and a Layout divides an area into smaller ones: Layout::vertical stacks them top to bottom, Layout::horizontal puts them side by side. Each piece has a constraint, and splitting an area returns one area per constraint. Destructure the result to name them:

    let rows = @tui.Layout::vertical([Length(1), Fill(1), Length(1)])
    guard! rows.split(frame.area()) is [header, body, footer]
    let columns = @tui.Layout::horizontal([Fill(1), Length(8)], spacing=1)
    guard! columns.split(header) is [title, clock]

    The pattern matches as long as it lists as many names as there are constraints. guard! panics if it does not; write guard ... is [...] else { ... } to handle that case instead.

    ConstraintSize
    Length(n)exactly n
    Percentage(p)p percent of the space
    Ratio(a, b)a / b of the space
    Min(n)at least n; grows when no Fill claims the leftover space
    Max(n)at most n; takes n when there is room
    Fill(w)a share of the leftover space, in proportion to its weight w

    The sizes are worked out in order, and the rule is short enough to predict. Each piece asks for a size: its number, its share, or nothing for a Fill. The pieces are served first to last, so when the space runs out it is the later ones that shrink. What is left over goes to the Fill pieces by weight; if there are none, to the Min pieces equally; and if there are none of those either it stays empty.

    spacing puts gaps between the pieces and margin a border of empty cells around them. flex says where the pieces go when space stays empty: Start, Center, End, or SpaceBetween. That is how to center a dialog:

    let rows = @tui.Layout::vertical([Length(5)], flex=Center)
    guard! rows.split(frame.area()) is [band]
    let columns = @tui.Layout::horizontal([Length(40)], flex=Center)
    guard! columns.split(band) is [dialog]

    Lines say what goes there. A Line is a row of Spans, each with its own style. A line knows its width() in display columns and can wrap(width) itself into several lines.

    let prompt = @tui.Line([Span("> ", style=dim), Span(input)])
    frame.line(area, prompt) // clipped to the area
    frame.line(area, status, align=Right)
    frame.lines(body, long_line.wrap(body.width))
    frame.set_cursor(area, col=prompt.width()) // relative to the area

    Painting is clipped to the area it is given, so a line that is too long is cut off instead of spilling into its neighbour.

    #Scrolling

    An area becomes a window onto a longer list of lines when you draw it with a Scroll. The scroll position is part of your state, like everything else:

    struct State {
    log : Array[String]
    scroll : @tui.Scroll // Scroll(stick_to_bottom=true) for a chat or a log
    }

    // In view: wrap to the area's width, then draw through the scroll position.
    frame.lines(body, lines, scroll=state.scroll)

    // In the event loop:
    PageUp => state.scroll.page_up()
    PageDown => state.scroll.page_down()
    Input(Mouse({ kind: Scroll(Up), .. })) => state.scroll.scroll_by(-3)

    Drawing tells the Scroll how many lines there are and how tall the area is. That is what keeps the position inside the content when lines are removed or the terminal is resized, and what lets page_up know the size of a page. With stick_to_bottom, the view follows new lines until the user scrolls up, and follows again once they return to the end. at_bottom() is there for a "more below" hint.

    Mouse events, including the wheel, arrive only when the session is started with with_terminal(mouse=true, ...). The terminal then stops handling the mouse itself, so its own text selection does not work while the session runs.

    #Selection and copy

    Since the terminal no longer selects text once mouse reporting is on, the application does it. A Selection is the selection over the lines of one area:

    // In view:
    frame.lines(body, lines, scroll=state.scroll, selection=state.selection)

    // In the event loop:
    Input(Mouse(mouse)) => {
    state.selection.mouse(mouse)
    if mouse.kind is Release(Left) && state.selection.text() is Some(text) {
    terminal.set_clipboard(text)
    }
    }

    Dragging with the left button selects, and the selected text is drawn in reverse video. A click without a drag removes the selection. When to copy is your choice: the snippet copies as soon as the button is released, but text() can just as well be read on a key press.

    • The selection is tied to the lines, not to screen cells. It stays on the same text while the area scrolls, and it can be longer than the screen: dragging past the top or bottom edge scrolls the area.
    • Lines made by Line::wrap are copied as the text was before wrapping. Only real line ends become newlines.
    • A selection is dropped when the area's width changes or when the lines it covers are gone.
    • Terminal::set_clipboard asks the terminal to set the system clipboard (OSC 52), which also works over SSH. Some terminals ignore that request, so when the program runs on the user's own machine the text is also handed to pbcopy (macOS) or wl-copy, xclip, or xsel (Linux). Over SSH only the terminal can reach the user's clipboard, and there is no way to tell whether it did.

    #Text input

    The moonbit-community/tui/editor package is a multi-line text editor. It is a separate package built only on the public API above, so it is also the pattern for writing your own widgets: a value in your state that knows how to draw itself into an area and how to take events.

    // In view. The input grows with its text, so measure it before splitting.
    let rows = state.editor.height(width=size.width - 2, max=4)
    let layout = @tui.Layout::vertical([Fill(1), Length(rows)])
    guard! layout.split(frame.area()) is [body, input]
    let layout = @tui.Layout::horizontal([Length(2), Fill(1)])
    guard! layout.split(input) is [marker, text]
    frame.line(marker, @tui.Line::from_text("> "))
    state.editor.draw(frame, text)

    // In the event loop.
    Input(Key(key)) =>
    if !state.editor.key(key) && key.code is Enter {
    submit(state.editor.text())
    state.editor.clear()
    }
    Input(Paste(text)) => state.editor.insert(text)

    key applies the usual terminal bindings: arrows, Home/End (with Ctrl, to the start or end of the whole text), Backspace and Delete, the Emacs chords (Ctrl+A/E/B/F/P/N/H/D/U/K/W), and word movement with Alt or Ctrl. It returns whether it used the key. It leaves a plain Enter alone, so the application decides what submitting means; Shift+Enter, Alt+Enter, and Ctrl+J insert a line break. Up on the first row and Down on the last are also left alone, which is where an application can recall history.

    Long lines wrap at the area's width, character by character. When the text has more rows than the area, the editor scrolls to keep the cursor in view.

    The same widget can be dressed up as a code editor. draw takes the pieces as optional arguments, and examples/text_editor uses all of them:

    state.editor.draw(
    frame,
    body,
    style=text_style, // the text, and the area behind it
    highlight=highlight_moonbit, // (String) -> Array[Span], one line at a time
    line_numbers=line_number_style, // a number column on the left
    current_line=current_line_style, // laid over the cursor's line
    )

    highlight is given the text of a line and returns that text cut into styled spans; the editor maps the styles onto the wrapped rows. position() returns the cursor's line and column for a status row, and mouse(event) moves the cursor to where the left button was pressed.

    #What is here

    • Style: foreground, background, bold, italic, underline, reverse.
    • Span, Line: styled text with width, wrap, and to_text. Text is measured in display columns, so CJK and other wide graphemes take two cells. Wrapping breaks at spaces, and anywhere for text without spaces.
    • Rect, Layout, Constraint, Flex: areas, and dividing them along one direction with spacing, a margin, and a choice of where unused space goes.
    • Frame: area, line, lines, fill, set_cursor, and text for painting at an exact cell; to_text, cursor, and style_at for reading a frame back in tests. Control characters never become cell content.
    • Scroll: a scroll position with scroll_by, page_up, page_down, to_top, to_bottom, and optional following of the end.
    • Selection: a mouse selection over an area's lines, with mouse, text, and clear.
    • with_terminal, Terminal::size, Terminal::draw, Terminal::read_event, Terminal::set_clipboard. Each draw is one write, wrapped in a synchronized update. Mouse reporting is opt-in.
    • editor package: Editor, a multi-line text input with draw, key, mouse, insert, text, set_text, clear, height, position, and the individual movement and deletion operations. Drawing can add syntax highlighting, line numbers, and a highlight for the current line; layout returns the visible rows and the cursor instead, for an application that draws the text itself.

    Run the examples:

    moon run examples/counter # the snippet above moon run examples/hello # an input box, scrolling, selection and copy moon run examples/text_editor -- frame.mbt # a code editor: highlighting, line numbers, mouse

    #What is not here yet

    • Double-click to select a word, and keyboard-driven selection.
    • More than one selectable area sharing a selection, or a selection that spans areas.
    • Telling which area a mouse event landed in, apart from Selection. Events carry a row and column; matching them to an area is up to you.
    • Keeping the same text in view when a resize re-wraps the lines. A Scroll counts lines, so the view can shift after the width changes.
    • In the editor: undo, selecting text, word wrapping, and horizontal scrolling.
    • Other widgets, such as lists and menus.
    • Padding and borders as part of drawing, and layouts that size a piece from its content. A piece whose size depends on what is in it is measured first and given a Length.
    • An inline (non-full-screen) mode. Full-screen is the only mode.

    #Development

    The library builds against the released moonbit-community/tty on mooncakes, so a checkout is enough:

    moon fmt moon check --deny-warn moon test moon info

    Align

    pub(all) enum Align {
    Left
    Center
    Right
    } derive(Eq,
    Debug
    )

    Where a line sits when it is narrower than its area.

    Align::equal

    fn Align::equal(Align, Align) -> Bool

    Constraint

    pub(all) enum Constraint {
    Length(Int)
    Percentage(Int)
    Ratio(Int, Int)
    Min(Int)
    Max(Int)
    Fill(Int)
    } derive(Eq,
    Debug
    )

    How much of the space one piece of a layout takes.

    Constraint::equal

    fn Constraint::equal(Constraint, Constraint) -> Bool

    Cursor

    pub struct Cursor {
    row : Int
    col : Int
    } derive(Eq,
    Debug
    )

    Where the cursor is shown in a frame: a 0-based row and column.

    Cursor::Cursor

    fn Cursor::Cursor(row~ : Int, col~ : Int) -> Cursor

    A cursor position, for comparing with what Frame::cursor returns.

    Cursor::equal

    fn Cursor::equal(Cursor, Cursor) -> Bool

    Flex

    pub(all) enum Flex {
    Start
    Center
    End
    SpaceBetween
    } derive(Eq,
    Debug
    )

    Where the pieces of a layout sit when they do not use all of the space, which happens only when none of them is a Fill or a Min.

    Flex::equal

    fn Flex::equal(Flex, Flex) -> Bool

    Frame

    type Frame

    One full picture of the screen: a grid of styled cells plus an optional cursor.

    A frame starts blank with the cursor hidden. Paint the whole picture into it, then hand it to Terminal::draw; there is no need to track what the previous frame looked like.

    Lay a frame out by splitting area() into smaller areas and painting into those. Rows and columns are 0-based, and painting outside the frame is clipped.

    Frame::Frame

    fn Frame::Frame(width~ : Int, height~ : Int) -> Frame

    Build a blank frame of width columns by height rows.

    Frame::area

    fn Frame::area(self : Frame) -> Rect

    The area covering the whole frame.

    Frame::cursor

    fn Frame::cursor(self : Frame) -> Cursor?

    Where the frame shows the cursor, or None when it is hidden. Together with to_text this is what a test of a view function can check.

    Frame::fill

    fn Frame::fill(self : Frame, area : Rect, style? : Style) -> Unit

    Fill area with blank cells in style, replacing what was there. The part of the area outside the frame is ignored.

    Frame::height

    fn Frame::height(self : Frame) -> Int

    The frame's height in rows.

    Frame::line

    fn Frame::line(self : Frame, area : Rect, line : Line, align? : Align) -> Unit

    Paint line on the first row of area.

    The line is clipped to the area: a line wider than the area is cut at the area's right edge, and nothing is painted outside it. align places a line narrower than the area.

    Frame::lines

    fn Frame::lines(self : Frame, area : Rect, lines : ArrayView[Line], align? : Align, scroll? : Scroll, selection? : Selection) -> Unit

    Paint lines down the rows of area, one line per row, starting at the top. Lines that do not fit in the area's height are not painted.

    With scroll, the area is a window onto lines: painting starts at the scroll position instead of the first line. Drawing also updates scroll with the number of lines and the area's height, which keeps the position within the content.

    With selection, the text of this area can be selected with the mouse. The selected part is painted in reverse video, and drawing tells selection what is on screen so it can turn mouse coordinates into text.

    Frame::set_cursor

    fn Frame::set_cursor(self : Frame, area : Rect, row? : Int, col? : Int) -> Unit

    Show the terminal cursor in area, row rows down and col columns right of its top-left corner. The position is kept inside the area and inside the frame. A frame that never calls this leaves the cursor hidden.

    Frame::style_at

    fn Frame::style_at(self : Frame, row~ : Int, col~ : Int) -> Style

    The style of the cell at (row, col), or the default style when that is outside the frame. Like to_text and cursor, it is for tests of view functions: it is how a test checks a color or a highlight.

    Frame::text

    fn Frame::text(self : Frame, row~ : Int, col~ : Int, text : StringView, style? : Style) -> Unit

    Paint text at an exact cell: on row, starting at column col.

    This is the lowest-level way to put text in a frame. Prefer line, which places text in an area and handles alignment and clipping to that area. Painting stops at the frame's right edge; a row outside the frame paints nothing.

    Frame::to_text

    fn Frame::to_text(self : Frame) -> String

    The frame's text without styles: rows joined by newlines, trailing blanks of each row dropped. Meant for snapshot tests of view functions.

    Frame::width

    fn Frame::width(self : Frame) -> Int

    The frame's width in columns.

    Layout

    type Layout

    A way of dividing an area into pieces along one direction.

    Layout::vertical stacks the pieces top to bottom and Layout::horizontal puts them side by side; split applies the layout to an area. A layout is a plain value: build it where it is used, or keep one and split different areas with it.

    The sizes are worked out in three steps, the same way in both directions:

    1. Each piece asks for a size: Length, Min and Max for their number, Percentage and Ratio for their share of the space, Fill for nothing.
    2. The pieces are served in order. Each gets what it asked for, or what is left if that is less, so when the space runs out it is the later pieces that shrink.
    3. Space still left goes to the Fill pieces in proportion to their weights. With no Fill, it goes to the Min pieces in equal parts. With neither, it stays empty and flex says where.

    Layout::horizontal

    fn Layout::horizontal(constraints : Array[Constraint], spacing? : Int, margin? : Int, flex? : Flex) -> Layout

    A layout that puts its pieces side by side, left to right, one per constraint.

    spacing leaves that many empty columns between neighbouring pieces, and margin that many rows and columns around all of them. flex places the pieces when they do not fill the space.

    Layout::split

    fn Layout::split(self : Layout, area : Rect) -> Array[Rect]

    Divide area into one area per constraint.

    The result always has as many areas as the layout has constraints, in order, so it can be destructured to name them:

    guard! Layout::vertical([Length(1), Fill(1), Length(1)]).split(area)
    is [header, body, footer]

    Pieces that do not fit come out with a size of zero.

    Layout::vertical

    fn Layout::vertical(constraints : Array[Constraint], spacing? : Int, margin? : Int, flex? : Flex) -> Layout

    A layout that stacks its pieces top to bottom, one per constraint.

    spacing leaves that many empty rows between neighbouring pieces, and margin that many rows and columns around all of them. flex places the pieces when they do not fill the space.

    Line

    One row of text: spans laid out left to right.

    A line knows how wide it is and how to wrap itself, so placing text never needs hand-computed columns. Control characters in the spans, including tab and newline, are not displayed and take no width: split text into lines and expand tabs before building a Line.
    impl Eq for Line

    Line::Line

    fn Line::Line(spans : Array[Span]) -> Line

    Build a line from spans, in left-to-right order.

    Line::equal

    fn Line::equal(self : Line, other : Line) -> Bool

    Line::from_text

    fn Line::from_text(text : StringView, style? : Style) -> Line

    A line holding one span of text.

    Line::to_text

    fn Line::to_text(self : Line) -> String

    The line's text without styles, as it would be displayed.

    Line::width

    fn Line::width(self : Line) -> Int

    The line's width in display columns. Wide graphemes such as CJK characters count as two.

    Line::wrap

    fn Line::wrap(self : Line, width : Int) -> Array[Line]

    Break the line into lines no wider than width columns, keeping styles.

    A break is made at the last space that fits, and that space is dropped; a word longer than width, or text without spaces such as CJK, is broken wherever it reaches the edge. An empty line wraps to one empty line, so blank lines keep their row. A width below one gives no lines.

    Rect

    pub struct Rect {
    row : Int
    col : Int
    width : Int
    height : Int
    } derive(Eq,
    Debug
    )

    A rectangular area of a frame: height rows starting at row, width columns starting at col. Rows and columns are 0-based.

    Rect::Rect

    fn Rect::Rect(row~ : Int, col~ : Int, width~ : Int, height~ : Int) -> Rect

    Build an area. A negative width or height becomes zero.

    Rect::equal

    fn Rect::equal(Rect, Rect) -> Bool

    Scroll

    type Scroll

    The scroll position of a list of lines that may be longer than the area showing it.

    Keep one Scroll in your state per scrollable region, pass it to Frame::lines when drawing, and move it from your event handling. Drawing records how many lines there are and how tall the area is, so the position always stays within the content and page_up/page_down know what a page is.

    Scroll::Scroll

    fn Scroll::Scroll(stick_to_bottom? : Bool) -> Scroll

    Build a scroll position at the top.

    With stick_to_bottom, the view starts at the end and stays there as lines are added, until the user scrolls up; scrolling back to the end resumes following. This is the behaviour a chat log or a live output wants.

    Scroll::at_bottom

    fn Scroll::at_bottom(self : Scroll) -> Bool

    Whether the last line is visible, as of the last draw or scroll. Useful for showing a "more below" hint.

    Scroll::offset

    fn Scroll::offset(self : Scroll) -> Int

    The index of the first visible line, as of the last draw or scroll.

    Scroll::page_down

    fn Scroll::page_down(self : Scroll) -> Unit

    Scroll down by one page.

    Scroll::page_up

    fn Scroll::page_up(self : Scroll) -> Unit

    Scroll up by one page.

    Scroll::scroll_by

    fn Scroll::scroll_by(self : Scroll, rows : Int) -> Unit

    Scroll by rows lines: positive moves down, towards the end; negative moves up. The position stops at either end.

    Scroll::to_bottom

    fn Scroll::to_bottom(self : Scroll) -> Unit

    Scroll to the last line.

    Scroll::to_top

    fn Scroll::to_top(self : Scroll) -> Unit

    Scroll to the first line.

    Selection

    type Selection

    A mouse selection over the lines of one area.

    Keep one Selection in your state for the region whose text can be selected, pass it to Frame::lines when drawing, and feed it mouse events with mouse. The selection is made by dragging with the left button; it is highlighted on the next draw, and text returns what it covers.

    The selection is anchored to the lines, not to the screen, so it stays on the same text while the area scrolls, and it can cover more than fits on screen: dragging past the top or bottom edge scrolls the area.

    Selection::Selection

    fn Selection::Selection() -> Selection

    Build an empty selection.

    Selection::clear

    fn Selection::clear(self : Selection) -> Unit

    Remove the selection.

    Selection::mouse

    Update the selection from a mouse event.

    Pressing the left button inside the area starts a selection, dragging extends it, and releasing ends it. A click without a drag, or a press outside the area, removes the selection. Other buttons and the wheel are ignored, so every mouse event can be passed here.

    Selection::text

    fn Selection::text(self : Selection) -> String?

    The selected text, or None when nothing is selected.

    Lines are joined with newlines, except where Line::wrap broke one line into several: those are joined the way they were before wrapping, so the result is the original text rather than the text as it happened to wrap.

    Size

    pub struct Size {
    width : Int
    height : Int
    } derive(Eq,
    Debug
    )

    The terminal's size in columns and rows.

    Size::equal

    fn Size::equal(Size, Size) -> Bool

    Span

    pub struct Span {
    text : String
    style : Style
    } derive(Eq,
    Debug
    )

    A run of text in one style.

    Span::Span

    fn Span::Span(text : StringView, style? : Style) -> Span

    Build a span. Without style the text is unstyled.

    Span::equal

    fn Span::equal(Span, Span) -> Bool

    Style

    pub struct Style {
    fg :
    Color

    bg :
    Color

    bold : Bool
    italic : Bool
    underline : Bool
    reverse : Bool
    } derive(Eq,
    Debug
    )

    How a cell is drawn: its colors and text attributes.

    Style::Style

    fn Style::Style(fg? :
    Color
    , bg? :
    Color
    , bold? : Bool, italic? : Bool, underline? : Bool, reverse? : Bool) -> Style

    Build a style. Omitted fields keep the terminal default, so Style() is unstyled text and Style(fg=Basic(Red), bold=true) changes only those two.

    Style::equal

    fn Style::equal(Style, Style) -> Bool

    Terminal

    type Terminal

    A full-screen terminal session.

    A Terminal exists only inside with_terminal. Draw complete frames to it with draw and read what the user did with read_event.

    Terminal::draw

    async fn Terminal::draw(self : Terminal, frame : Frame) -> Unit

    Make the screen show frame.

    Only the cells that differ from the frame already on screen are sent to the terminal, in one write. A frame larger than the terminal is clipped. frame is copied, so it can be reused or dropped after the call.

    Terminal::read_event

    async fn Terminal::read_event(self : Terminal, esc_timeout_ms? : Int) ->
    Event

    Wait for the next terminal event: a key, a paste, a resize, or, when the session was started with mouse, a mouse action.

    After a Resize event size reports the new size, and the next draw repaints the whole screen.

    A lone Escape byte could be the Escape key or the start of a longer key sequence; esc_timeout_ms is how long to wait for the rest before deciding it was the key.

    Terminal::set_clipboard

    async fn Terminal::set_clipboard(self : Terminal, text : StringView) -> Unit

    Put text on the system clipboard, replacing what was there.

    This asks the terminal to do it (OSC 52), which also works over SSH. Not every terminal supports the request, so when the program runs on the user's own machine the text is also handed to the system's clipboard program: pbcopy on macOS, wl-copy, xclip, or xsel on Linux. Over SSH only the terminal can reach the user's clipboard.

    There is no way to tell whether it worked: a terminal that does not support the request ignores it, and a failing clipboard program is ignored too. Waits for the clipboard program, at most two seconds.

    Terminal::size

    fn Terminal::size(self : Terminal) -> Size

    The terminal's current size. It changes when read_event returns a Resize event.

    with_terminal

    async fn[T] with_terminal(mouse? : Bool, f : async (Terminal) -> T) -> T

    Run f with the terminal in full-screen mode, and restore it afterwards whether f returns, raises, or is cancelled.

    Opens the controlling terminal, switches to raw mode and the alternate screen, requests the kitty keyboard "disambiguate" level, and enables bracketed paste. Raises if the process has no terminal.

    With mouse, clicks, drags, and the scroll wheel arrive as Mouse events from read_event. The terminal then stops handling the mouse itself, so its own text selection no longer works while the session runs; use a Selection to provide one.