Raxol.Terminal.Buffer.LineOperations (Raxol v0.5.0)
View SourceProvides line-level operations for the screen buffer. This module handles operations like inserting, deleting, and manipulating lines.
Summary
Functions
Clears a line in the buffer with optional styling.
Creates a single empty line with the given width and optional style.
Creates a specified number of empty lines with the given width.
Deletes a specified number of lines starting from the current cursor position. Lines below the deleted lines are shifted up, and blank lines are added at the bottom.
Helper function to handle the line insertion logic.
Erases a specified number of characters in a line.
Gets a line from the buffer.
Inserts a specified number of blank lines at the current cursor position. Lines below the cursor are shifted down, and lines shifted off the bottom are discarded.
Removes lines from the top of the buffer.
Prepends lines to the top of the screen buffer, shifting existing content down. Lines shifted off the bottom are moved to the scrollback buffer.
Updates a line in the buffer with new cells.
Functions
@spec clear_line( Raxol.Terminal.ScreenBuffer.t(), non_neg_integer(), Raxol.Terminal.ANSI.TextFormatting.text_style() | nil ) :: Raxol.Terminal.ScreenBuffer.t()
Clears a line in the buffer with optional styling.
Parameters
buffer
- The screen buffer to modifyline_index
- The index of the line to clearstyle
- Optional text style for the cleared line
Returns
The updated screen buffer.
Examples
iex> buffer = ScreenBuffer.new(80, 24)
iex> style = %{fg: :red, bg: :blue}
iex> buffer = LineOperations.clear_line(buffer, 0, style)
iex> LineOperations.get_line(buffer, 0) |> hd() |> Map.get(:style)
%{fg: :red, bg: :blue}
@spec create_empty_line( non_neg_integer(), Raxol.Terminal.ANSI.TextFormatting.text_style() | nil ) :: [ Raxol.Terminal.Cell.t() ]
Creates a single empty line with the given width and optional style.
Parameters
width
- The width of the linestyle
- Optional text style for the cells
Returns
A list of empty cells representing an empty line.
Examples
iex> line = LineOperations.create_empty_line(80)
iex> length(line)
80
iex> line = LineOperations.create_empty_line(80, %{fg: :red})
iex> hd(line).style.fg
:red
@spec create_empty_lines(non_neg_integer(), non_neg_integer()) :: [ [Raxol.Terminal.Cell.t()] ]
Creates a specified number of empty lines with the given width.
Parameters
width
- The width of each linecount
- The number of lines to create
Returns
A list of empty lines, where each line is a list of empty cells.
Examples
iex> lines = LineOperations.create_empty_lines(80, 2)
iex> length(lines)
2
iex> length(hd(lines))
80
@spec delete_lines(Raxol.Terminal.ScreenBuffer.t(), non_neg_integer()) :: Raxol.Terminal.ScreenBuffer.t()
Deletes a specified number of lines starting from the current cursor position. Lines below the deleted lines are shifted up, and blank lines are added at the bottom.
@spec do_insert_lines( Raxol.Terminal.ScreenBuffer.t(), non_neg_integer(), non_neg_integer(), non_neg_integer() ) :: Raxol.Terminal.ScreenBuffer.t()
Helper function to handle the line insertion logic.
Parameters
buffer
- The screen buffer to modifycursor_y
- The y-coordinate of the cursorcount
- The number of lines to insertbottom
- The bottom boundary of the scroll region
Returns
The updated screen buffer.
Examples
iex> buffer = ScreenBuffer.new(80, 24)
iex> buffer = LineOperations.do_insert_lines(buffer, 0, 5, 23)
iex> length(buffer.cells)
24
@spec erase_chars( Raxol.Terminal.ScreenBuffer.t(), non_neg_integer(), non_neg_integer(), non_neg_integer() ) :: Raxol.Terminal.ScreenBuffer.t()
Erases a specified number of characters in a line.
Parameters
buffer
- The screen buffer to modifyrow
- The row to modifycol
- The starting columncount
- The number of characters to erase
Returns
The updated screen buffer.
Examples
iex> buffer = ScreenBuffer.new(80, 24)
iex> buffer = LineOperations.erase_chars(buffer, 0, 0, 10)
iex> LineOperations.get_line(buffer, 0) |> Enum.take(10) |> Enum.all?(fn cell -> cell.char == "" end)
true
@spec get_line(Raxol.Terminal.ScreenBuffer.t(), non_neg_integer()) :: [Raxol.Terminal.Cell.t()] | nil
Gets a line from the buffer.
@spec insert_lines(Raxol.Terminal.ScreenBuffer.t(), non_neg_integer()) :: Raxol.Terminal.ScreenBuffer.t()
Inserts a specified number of blank lines at the current cursor position. Lines below the cursor are shifted down, and lines shifted off the bottom are discarded.
@spec pop_top_lines(Raxol.Terminal.ScreenBuffer.t(), non_neg_integer()) :: {[[Raxol.Terminal.Cell.t()]], Raxol.Terminal.ScreenBuffer.t()}
Removes lines from the top of the buffer.
@spec prepend_lines(Raxol.Terminal.ScreenBuffer.t(), [[Raxol.Terminal.Cell.t()]]) :: Raxol.Terminal.ScreenBuffer.t()
Prepends lines to the top of the screen buffer, shifting existing content down. Lines shifted off the bottom are moved to the scrollback buffer.
@spec update_line(Raxol.Terminal.ScreenBuffer.t(), non_neg_integer(), [ Raxol.Terminal.Cell.t() ]) :: Raxol.Terminal.ScreenBuffer.t()
Updates a line in the buffer with new cells.
Parameters
buffer
- The screen buffer to modifyline_index
- The index of the line to updatenew_line
- The new line content
Returns
The updated screen buffer.
Examples
iex> buffer = ScreenBuffer.new(80, 24)
iex> new_line = List.duplicate(%Cell{char: "A"}, 80)
iex> buffer = LineOperations.update_line(buffer, 0, new_line)
iex> LineOperations.get_line(buffer, 0) |> hd() |> Map.get(:char)
"A"