Class: Aspera::Cli::TerminalImage

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/terminal_image.rb

Overview

Render an image for terminal output. Uses either colored text blocks or the iTerm2 inline-image protocol when available.

Defined Under Namespace

Modules: Backend

Class Method Summary collapse

Class Method Details

.build(blob, text: false, reserve: 3, double: nil, font_ratio: DEFAULT_FONT_RATIO) ⇒ String

Render an image blob for display in the current terminal.

Parameters:

  • blob (String) —

    The image as a binary string

  • text (Boolean) (defaults to: false) —

    true to display the image as text, false to use iTerm2 if supported

  • reserve (Integer) (defaults to: 3) —

    Number of lines to reserve for other text than the image

  • double (Boolean, nil) (defaults to: nil) —

    true to use colors on half lines (Unicode half block), false to use colors on full lines, nil for true if Unicode is supported

  • font_ratio (Float) (defaults to: DEFAULT_FONT_RATIO) —

    ratio = font height / font width

Returns:

  • (String) —

    The image as text, or the iTerm2 escape sequence



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/aspera/cli/terminal_image.rb', line 139

def build(blob, text: false, reserve: 3, double: nil, font_ratio: DEFAULT_FONT_RATIO)
  return '[Image display requires a terminal]' unless Environment.terminal?
  double = Environment.terminal_supports_unicode? if double.nil?
  return iterm_display_image(blob) if iterm_supported? && !text
  pixel_colors =
    begin
      Log.log.debug('Trying chunky_png')
      Backend::ChunkyPNG.new(blob, reserve: reserve, double: double, font_ratio: font_ratio).terminal_pixels
    rescue => e
      Log.log.debug(e.message)
      begin
        Log.log.debug('Trying rmagick')
        Backend::RMagick.new(blob, reserve: reserve, double: double, font_ratio: font_ratio).terminal_pixels
      rescue StandardError, LoadError => e
        # LoadError: optional gem rmagick not installed
        Log.log.debug(e.message)
        nil
      end
    end
  if pixel_colors.nil?
    return iterm_display_image(blob) if iterm_supported?
    raise 'Cannot decode picture.'
  end
  # Convert decoded pixels into terminal glyphs.
  text_pixels = []
  pixel_colors.each_with_index do |row_data, row|
    next if double && (row.odd? || row.eql?(pixel_colors.length - 1))
    row_data.each_with_index do |pixel_rgb, col|
      text_pixels.push("\n") if col.eql?(0) && !row.eql?(0)
      if double
        text_pixels.push('▄'.bg(pixel_rgb).fg(pixel_colors[row + 1][col]))
      else
        text_pixels.push(' '.bg(pixel_rgb))
      end
    end
  end
  return text_pixels.join
end

.iterm_display_image(blob) ⇒ String

Build the iTerm2 inline-image escape sequence. https://iterm2.com/documentation-images.html

Parameters:

  • blob (String) —

    image binary content

Returns:

  • (String) —

    escape sequence that displays the image inline



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/aspera/cli/terminal_image.rb', line 183

def iterm_display_image(blob)
  # image = Magick::ImageList.new.from_blob(blob)
  # Parameters accepted by the iTerm2 inline-image protocol.
  arguments = {
    inline:              1,
    preserveAspectRatio: 1,
    size:                blob.length
    # width:               image.columns,
    # height:              image.rows
  }.map { |k, v| "#{k}=#{v}" }.join(';')
  # `\a` is BEL and `\e` is ESC.
  # See: https://github.com/ruby/ruby/blob/master/doc/syntax/literals.rdoc#label-Strings
  # Return the full escape sequence expected by iTerm2-compatible terminals.
  return "\e]1337;File=#{arguments}:#{Base64.strict_encode64(blob)}\a"
end

.iterm_supported? ⇒ Boolean

Detect whether the current terminal supports iTerm2 inline images.

Returns:

  • (Boolean) —

    true when the current terminal advertises iTerm2 image support



202
203
204
205
206
207
# File 'lib/aspera/cli/terminal_image.rb', line 202

def iterm_supported?
  TERM_ENV_VARS.each do |env_var|
    return true if ITERM_NAMES.any? { |term| ENV[env_var]&.include?(term) }
  end
  false
end