TermUI.Clipboard.Selection (TermUI v0.2.0)

View Source

Selection state management for clipboard operations.

Tracks text selection with start and end positions, supporting selection expansion with Shift+arrow keys and clearing on navigation without Shift.

Usage

# Create selection
selection = Selection.new()

# Start selection at cursor
selection = Selection.start(selection, 5)

# Extend selection
selection = Selection.extend(selection, 10)

# Get selected range
{start, finish} = Selection.range(selection)

# Extract content
selected_text = Selection.extract(selection, "Hello World")

Summary

Functions

Checks if there is an active selection.

Clears the selection.

Checks if a position is within the selection.

Checks if the selection is empty (start equals end).

Expands the selection in a direction.

Extends the selection to a new position.

Extracts selected content from a string.

Returns the length of the selection.

Moves the selection by a delta.

Creates a new empty selection.

Returns the selection range as {start, end}.

Selects all text.

Selects a word at the given position.

Starts a new selection at the given position.

Types

t()

@type t() :: %TermUI.Clipboard.Selection{
  active: boolean(),
  anchor: integer() | nil,
  end_pos: integer() | nil,
  start_pos: integer() | nil
}

Functions

active?(selection)

@spec active?(t()) :: boolean()

Checks if there is an active selection.

clear(selection)

@spec clear(t()) :: t()

Clears the selection.

contains?(selection, position)

@spec contains?(t(), integer()) :: boolean()

Checks if a position is within the selection.

empty?(selection)

@spec empty?(t()) :: boolean()

Checks if the selection is empty (start equals end).

expand(selection, direction, text, cursor_pos)

@spec expand(t(), atom(), String.t(), integer()) :: t()

Expands the selection in a direction.

Direction can be :left, :right, :word_left, :word_right, :line_start, :line_end, :all.

extend(selection, position)

@spec extend(t(), integer()) :: t()

Extends the selection to a new position.

The selection extends from the anchor to the new position.

extract(selection, text)

@spec extract(t(), String.t()) :: String.t()

Extracts selected content from a string.

Returns empty string if no selection is active.

length(selection)

@spec length(t()) :: integer()

Returns the length of the selection.

move(selection, delta)

@spec move(t(), integer()) :: t()

Moves the selection by a delta.

Both start and end positions are adjusted.

new()

@spec new() :: t()

Creates a new empty selection.

range(selection)

@spec range(t()) :: {integer(), integer()} | nil

Returns the selection range as {start, end}.

Returns nil if no selection is active.

select_all(selection, text)

@spec select_all(t(), String.t()) :: t()

Selects all text.

select_word(selection, text, position)

@spec select_word(t(), String.t(), integer()) :: t()

Selects a word at the given position.

start(selection, position)

@spec start(t(), integer()) :: t()

Starts a new selection at the given position.

This sets the anchor point for the selection.