Raxol.Terminal.Commands.History (Raxol v0.5.0)

View Source

Manages command history for the terminal emulator.

This module provides functionality for:

  • Storing and retrieving command history
  • Navigating through command history
  • Persisting command history
  • Managing history size limits

Summary

Functions

Adds a command to the history.

Clears the command history.

Returns the current command history as a list.

Adds to the emulator's command history if the input is a newline (10), or appends to the current command buffer if it's a printable character. Returns the updated emulator struct.

Creates a new command history manager.

Retrieves the next command in history.

Retrieves the previous command in history.

Saves the current input state.

Updates the command history configuration. Delegates to update_size/2 for compatibility.

Updates the maximum size of the command history. Truncates the history if needed.

Types

command_history()

@type command_history() :: [String.t()]

command_history_config()

@type command_history_config() :: %{
  max_size: non_neg_integer(),
  current_input: String.t()
}

t()

@type t() :: %Raxol.Terminal.Commands.History{
  commands: command_history(),
  current_index: integer(),
  current_input: String.t(),
  max_size: non_neg_integer()
}

Functions

add(history, command)

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

Adds a command to the history.

Examples

iex> history = History.new(1000)
iex> history = History.add(history, "ls -la")
iex> history.commands
["ls -la"]

clear(history)

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

Clears the command history.

Examples

iex> history = History.new(1000)
iex> history = History.add(history, "ls -la")
iex> history = History.clear(history)
iex> history.commands
[]

list(history)

@spec list(t()) :: command_history()

Returns the current command history as a list.

Examples

iex> history = History.new(1000)
iex> history = History.add(history, "ls -la")
iex> history = History.add(history, "cd /tmp")
iex> History.list(history)
["cd /tmp", "ls -la"]

maybe_add_to_history(emulator, char)

@spec maybe_add_to_history(Emulator.t(), integer()) :: Emulator.t()
@spec maybe_add_to_history(Emulator.t(), integer()) :: Emulator.t()
@spec maybe_add_to_history(Emulator.t(), any()) :: Emulator.t()

Adds to the emulator's command history if the input is a newline (10), or appends to the current command buffer if it's a printable character. Returns the updated emulator struct.

new(max_size)

@spec new(non_neg_integer()) :: t()

Creates a new command history manager.

Examples

iex> history = History.new(1000)
iex> history.max_size
1000

next(history)

@spec next(t()) :: {String.t() | nil, t()}

Retrieves the next command in history.

Examples

iex> history = History.new(1000)
iex> history = History.add(history, "ls -la")
iex> history = History.add(history, "cd /tmp")
iex> {_, history} = History.previous(history)
iex> History.next(history)
{"ls -la", %History{...}}

previous(history)

@spec previous(t()) :: {String.t() | nil, t()}

Retrieves the previous command in history.

Examples

iex> history = History.new(1000)
iex> history = History.add(history, "ls -la")
iex> history = History.add(history, "cd /tmp")
iex> History.previous(history)
{"cd /tmp", %History{...}}

save_input(history, input)

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

Saves the current input state.

Examples

iex> history = History.new(1000)
iex> history = History.save_input(history, "ls -l")
iex> history.current_input
"ls -l"

update_config(history, config)

Updates the command history configuration. Delegates to update_size/2 for compatibility.

update_size(history, new_size)

@spec update_size(t(), non_neg_integer()) :: t()

Updates the maximum size of the command history. Truncates the history if needed.