Class: Charming::Components::Image

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/image.rb

Overview

Image displays a Image::Source inline in the terminal, sized to rows×cols character cells. On a graphics-capable terminal (Phase 1: Ghostty/Kitty) it returns a block of Unicode placeholder cells and registers the image's one-time out-of-band transmission; on other terminals it returns fallback so layouts degrade gracefully.

Like Audio, the view itself is thin: the Image::Source (held in session) owns the bytes and transmit state. The placeholder block is a normal width-cols string that composes with row/column/box like any other view output.

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(source:, rows:, cols:, fallback: "", theme: nil) ⇒ Image

source is the Image::Source to display. rows/cols size the image in character cells. fallback is shown when the terminal lacks graphics support. theme is forwarded to the view layer.



17
18
19
20
21
22
23
# File 'lib/charming/presentation/components/image.rb', line 17

def initialize(source:, rows:, cols:, fallback: "", theme: nil)
  super(theme: theme)
  @source = source
  @rows = rows
  @cols = cols
  @fallback = fallback
end

Instance Method Details

#renderObject

Returns the placeholder block (registering the transmit once) on a graphics-capable terminal, otherwise the fallback string.



27
28
29
30
31
32
33
34
35
# File 'lib/charming/presentation/components/image.rb', line 27

def render
  return @fallback.to_s unless @source.supports_graphics?

  unless @source.transmitted?
    Charming::Escape.register(@source.transmit(rows: @rows, cols: @cols))
    @source.mark_transmitted
  end
  @source.placement(rows: @rows, cols: @cols)
end