class

Termisu::Event::Source::Resize

Inherits Termisu::Event::Source / Reference / Object

Terminal resize event source.

Monitors terminal size changes via polling and SIGWINCH signal handling. Generates Event::Resize events with old and new dimensions for efficient partial redraws.

Usage

# Create with a size provider (typically backend.size)
resize = Termisu::Event::Source::Resize.new(-> { backend.size })

loop = Termisu::Event::Loop.new
loop.add_source(resize)
loop.start

while event = loop.output.receive?
  case event
  when Termisu::Event::Resize
    puts "Terminal resized to #{event.width}x#{event.height}"
    if event.changed?
      puts "Changed from #{event.old_width}x#{event.old_height}"
    end
  end
end

Detection Strategy

Uses a hybrid approach:

  1. SIGWINCH Signal: Sets the @signal_received atomic flag; the polling fiber checks this flag each iteration and skips the sleep when set, providing near-immediate resize detection.
  2. Polling Fallback: Periodic size checks (default 100ms) catch resizes that signals might miss.

Runtime Configuration

The poll interval can be changed while the source is running via poll_interval=. Changes take effect on the next poll cycle.

Thread Safety

Uses Atomic(Bool) for the running state. Safe to call start/stop from different fibers. Uses compare_and_set for idempotent lifecycle operations - calling start twice or stop twice is safe.

Lifecycle

The source can be restarted after stopping. Each start resets the signal flag and reinstalls the SIGWINCH handler.

Constants

DEFAULT_POLL_INTERVAL = 100.milliseconds

Default polling interval for size checks. 100ms provides reasonable responsiveness without excessive CPU usage. SIGWINCH signals trigger immediate checks regardless of this interval.

Log = Termisu::Logs::Event

Constructors

new(size_provider : SizeProvider, poll_interval : Time::Span = DEFAULT_POLL_INTERVAL)

Creates a new resize source.

  • size_provider - Proc that returns current terminal size as {width, height}
  • poll_interval - Time between size checks (default: 100ms)

Example:

# Using terminal backend
resize = Termisu::Event::Source::Resize.new(-> { backend.size })

# Custom poll interval for more responsive detection
resize = Termisu::Event::Source::Resize.new(
  -> { backend.size },
  poll_interval: 50.milliseconds
)
Source

Instance methods

name

Returns the source name for identification.

Source
poll_interval

Returns the current polling interval.

Source
poll_interval=(value : Time::Span)

Sets the polling interval.

The new interval takes effect on the next poll cycle. Can be changed while the source is running.

Source
running?

Returns true if the resize source is currently running.

Source
start(output : Channel(Event::Any)) : Nil

Starts monitoring for resize events.

Installs a SIGWINCH signal handler and spawns a fiber that polls for size changes. Events are sent to the output channel.

Prevents double-start with compare_and_set.

Source
stop

Stops monitoring for resize events.

Sets the running flag to false, causing the fiber to exit on its next poll cycle. Resets the SIGWINCH handler.

Source

Nested types