class Termisu

Overview

Version information for Termisu.

All version components are parsed from shard.yml at compile time. Format: MAJOR.MINOR.PATCH[-STATE] (e.g., "0.1.0-alpha", "1.0.0")

Defined in:

termisu.cr
termisu/log.cr
termisu/version.cr

Constant Summary

Log = ::Log.for("termisu")

Main log instance for Termisu library

VERSION = {{ (`shards version`).chomp.stringify }}

Full version string from shard.yml

VERSION_MAJOR = 0
VERSION_MINOR = 0
VERSION_PATCH = 1
VERSION_STATE = nil

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Initializes Termisu with all required components.

Sets up terminal I/O, rendering, and input reader. Automatically enables raw mode and enters alternate screen.


[View source]

Instance Method Detail

def clear #

Clears the cell buffer (fills with spaces).

Note: This clears the buffer, not the screen. Call render() to apply.


[View source]
def close #

Closes Termisu and cleans up all resources.

Exits alternate screen, disables raw mode, and closes all components.


[View source]
def disable_enhanced_keyboard(*args, **options) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def disable_enhanced_keyboard(*args, **options, &) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def disable_mouse(*args, **options) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def disable_mouse(*args, **options, &) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def each_event(timeout_ms : Int32 = -1, &) #

Yields each event as it becomes available.

This is a convenient way to process events in a loop. Use timeout_ms to control polling behavior.

Example:

termisu.each_event(100) do |event|
  case event
  when Termisu::Event::Key
    break if event.key.escape?
  end
  termisu.render
end

[View source]
def enable_enhanced_keyboard(*args, **options) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def enable_enhanced_keyboard(*args, **options, &) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def enable_mouse(*args, **options) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def enable_mouse(*args, **options, &) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def enhanced_keyboard?(*args, **options) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def enhanced_keyboard?(*args, **options, &) #

Enables enhanced keyboard protocol for disambiguated key reporting.

In standard terminal mode, certain keys are indistinguishable:

  • Tab sends the same byte as Ctrl+I (0x09)
  • Enter sends the same byte as Ctrl+M (0x0D)
  • Backspace may send the same byte as Ctrl+H (0x08)

Enhanced mode enables the Kitty keyboard protocol and/or modifyOtherKeys, which report keys in a way that preserves the distinction.

Note: Not all terminals support these protocols. Unsupported terminals will silently ignore the escape sequences and continue with legacy mode. Supported terminals include: Kitty, WezTerm, foot, Ghostty, recent xterm.

Example:

termisu.enable_enhanced_keyboard
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      # Now Ctrl+I and Tab are distinguishable!
      if event.ctrl? && event.key.lower_i?
        puts "Ctrl+I pressed"
      elsif event.key.tab?
        puts "Tab pressed"
      end
    end
  end
end
termisu.disable_enhanced_keyboard

[View source]
def hide_cursor(*args, **options) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def hide_cursor(*args, **options, &) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def input_available? : Bool #

Checks if input data is available.


[View source]
def mouse_enabled?(*args, **options) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def mouse_enabled?(*args, **options, &) #

Enables mouse input tracking.

Once enabled, mouse events will be reported via poll_event. Supports SGR extended protocol (mode 1006) for large terminals and falls back to normal mode (1000) for compatibility.

Example:

termisu.enable_mouse
loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Mouse
      puts "Click at #{event.x},#{event.y}"
    end
  end
end
termisu.disable_mouse

[View source]
def peek_byte(*args, **options) #

[View source]
def peek_byte(*args, **options, &) #

[View source]
def poll_event(timeout_ms : Int32 = -1) : Event::Any | Nil #

Polls for an input event with optional timeout.

This is the recommended way to handle keyboard and mouse input. Returns structured Event objects (Event::Key, Event::Mouse, Event::Resize, Event::Tick) instead of raw bytes.

Parameters:

  • timeout_ms: Timeout in milliseconds (-1 for blocking, 0 for non-blocking)

Returns an Event or nil if timeout/no data.

Example:

loop do
  if event = termisu.poll_event(100)
    case event
    when Termisu::Event::Key
      break if event.ctrl_c? || event.key.escape?
      puts "Key: #{event.key}"
    when Termisu::Event::Mouse
      puts "Mouse: #{event.x},#{event.y}"
    end
  end
  termisu.render
end

[View source]
def read_byte(*args, **options) #

[View source]
def read_byte(*args, **options, &) #

[View source]
def read_bytes(*args, **options) #

[View source]
def read_bytes(*args, **options, &) #

[View source]
def render(*args, **options) #

Renders cell buffer changes to the screen.

Only cells that have changed since the last render are redrawn (diff-based). This is more efficient than clear_screen + write for partial updates.


[View source]
def render(*args, **options, &) #

Renders cell buffer changes to the screen.

Only cells that have changed since the last render are redrawn (diff-based). This is more efficient than clear_screen + write for partial updates.


[View source]
def set_cell(*args, **options) #

Sets a cell at the specified position.

Parameters:

  • x: Column position (0-based)
  • y: Row position (0-based)
  • ch: Character to display
  • fg: Foreground color (default: white)
  • bg: Background color (default: default terminal color)
  • attr: Text attributes (default: None)

Returns false if coordinates are out of bounds.

Example:

termisu.set_cell(10, 5, 'A', fg: Color.red, attr: Attribute::Bold)
termisu.render # Apply changes

[View source]
def set_cell(*args, **options, &) #

Sets a cell at the specified position.

Parameters:

  • x: Column position (0-based)
  • y: Row position (0-based)
  • ch: Character to display
  • fg: Foreground color (default: white)
  • bg: Background color (default: default terminal color)
  • attr: Text attributes (default: None)

Returns false if coordinates are out of bounds.

Example:

termisu.set_cell(10, 5, 'A', fg: Color.red, attr: Attribute::Bold)
termisu.render # Apply changes

[View source]
def set_cursor(*args, **options) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def set_cursor(*args, **options, &) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def show_cursor(*args, **options) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def show_cursor(*args, **options, &) #

Sets cursor position and makes it visible. Hides the cursor (rendered on next render()). Shows the cursor (rendered on next render()).


[View source]
def size(*args, **options) #

Returns terminal size as {width, height}.


[View source]
def size(*args, **options, &) #

Returns terminal size as {width, height}.


[View source]
def sync(*args, **options) #

Forces a full redraw of all cells.

Useful after terminal resize or screen corruption.


[View source]
def sync(*args, **options, &) #

Forces a full redraw of all cells.

Useful after terminal resize or screen corruption.


[View source]
def terminal : Terminal #

Returns the underlying terminal for direct access.


[View source]
def wait_event : Event::Any #

Waits for and returns the next input event (blocking).

This method blocks until an event is available.

Example:

event = termisu.wait_event
puts "Got event: #{event}"

[View source]
def wait_for_input(timeout_ms : Int32) : Bool #

Waits for input data with a timeout in milliseconds.


[View source]