Raxol.Terminal.Buffer.LineOperations (Raxol v0.5.0)

View Source

Provides 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

clear_line(buffer, line_index, style \\ nil)

Clears a line in the buffer with optional styling.

Parameters

  • buffer - The screen buffer to modify
  • line_index - The index of the line to clear
  • style - 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}

create_empty_line(width, style \\ nil)

Creates a single empty line with the given width and optional style.

Parameters

  • width - The width of the line
  • style - 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

create_empty_lines(width, count)

@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 line
  • count - 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

delete_lines(buffer, count)

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.

do_insert_lines(buffer, cursor_y, count, bottom)

Helper function to handle the line insertion logic.

Parameters

  • buffer - The screen buffer to modify
  • cursor_y - The y-coordinate of the cursor
  • count - The number of lines to insert
  • bottom - 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

erase_chars(buffer, row, col, count)

Erases a specified number of characters in a line.

Parameters

  • buffer - The screen buffer to modify
  • row - The row to modify
  • col - The starting column
  • count - 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

get_line(buffer, line_index)

Gets a line from the buffer.

insert_lines(buffer, count)

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.

pop_top_lines(buffer, count)

Removes lines from the top of the buffer.

prepend_lines(buffer, lines)

Prepends lines to the top of the screen buffer, shifting existing content down. Lines shifted off the bottom are moved to the scrollback buffer.

update_line(buffer, line_index, new_line)

Updates a line in the buffer with new cells.

Parameters

  • buffer - The screen buffer to modify
  • line_index - The index of the line to update
  • new_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"