class Termisu::Event::Source::Resize

Overview

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: Non-blockingly wakes the monitoring fiber for an immediate size check. A capacity-one channel coalesces signal bursts.
  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

Lifecycle changes are serialized. #stop cancels waits and blocked output, then waits for the current run to finish before allowing a restart.

Lifecycle

The source can be restarted after stopping. Every run owns its wake, cancellation, completion, and signal-handler state, so stale signals cannot affect a replacement run.

Defined in:

termisu/event/source/resize.cr

Constant Summary

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

Instance Method Summary

Instance methods inherited from class Termisu::Event::Source

name : String name, running? : Bool running?, start(output : Channel(Event::Any)) : Nil start, stop : Nil stop, stop_required? : Bool stop_required?

Constructor Detail

def self.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
)

[View source]

Instance Method Detail

def name : String #

Returns the source name for identification.


[View source]
def poll_interval : Time::Span #

Returns the current polling interval.


[View source]
def 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.


[View source]
def running? : Bool #

Returns true if the resize source is currently running.


[View source]
def 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. Starting an already-running source is an idempotent no-op.


[View source]
def stop : Nil #

Stops monitoring for resize events.

Disables the run-local signal callback, cancels any wait or blocked output send, and waits for the monitoring fiber to acknowledge completion. Once this method returns, the old run cannot query the provider or emit events.


[View source]