Class: Termpix::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/termpix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol: nil) ⇒ Display

Returns a new instance of Display.



8
9
10
11
# File 'lib/termpix.rb', line 8

def initialize(protocol: nil)
  @protocol = protocol || detect_protocol
  @current_image = nil
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



6
7
8
# File 'lib/termpix.rb', line 6

def protocol
  @protocol
end

Instance Method Details

#atomic_replace?Boolean

Check if protocol supports atomic replace (show new without clearing first) Kitty can replace images without flash; w3m/sixel need explicit clear

Returns:

  • (Boolean)


60
61
62
# File 'lib/termpix.rb', line 60

def atomic_replace?
  @protocol == :kitty
end

#clear(x: 0, y: 0, width: 80, height: 24, term_width: 80, term_height: 24) ⇒ Object

Clear all displayed images



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/termpix.rb', line 38

def clear(x: 0, y: 0, width: 80, height: 24, term_width: 80, term_height: 24)
  return false unless @protocol

  case @protocol
  when :kitty
    Protocols::Kitty.clear
  when :sixel
    Protocols::Sixel.clear
  when :w3m
    Protocols::W3m.clear(x: x, y: y, width: width, height: height, term_width: term_width, term_height: term_height)
  end

  true
end

#infoObject

Get information about the current protocol



65
66
67
68
69
70
71
# File 'lib/termpix.rb', line 65

def info
  {
    protocol: @protocol,
    supported: supported?,
    current_image: @current_image
  }
end

#show(image_path, x: 0, y: 0, max_width: 80, max_height: 24) ⇒ Object

Display an image at the specified position

Parameters:

  • image_path (String)

    Path to the image file

  • x (Integer) (defaults to: 0)

    X position in terminal characters

  • y (Integer) (defaults to: 0)

    Y position in terminal characters

  • max_width (Integer) (defaults to: 80)

    Maximum width in terminal characters

  • max_height (Integer) (defaults to: 24)

    Maximum height in terminal characters



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/termpix.rb', line 19

def show(image_path, x: 0, y: 0, max_width: 80, max_height: 24)
  return false unless @protocol
  return false unless File.exist?(image_path)

  case @protocol
  when :kitty
    Protocols::Kitty.display(image_path, x: x, y: y, max_width: max_width, max_height: max_height)
  when :sixel
    Protocols::Sixel.display(image_path, x: x, y: y, max_width: max_width, max_height: max_height)
  when :w3m
    Protocols::W3m.display(image_path, x: x, y: y, max_width: max_width, max_height: max_height)
  else
    return false
  end

  true
end

#supported?Boolean

Check if image display is supported

Returns:

  • (Boolean)


54
55
56
# File 'lib/termpix.rb', line 54

def supported?
  !@protocol.nil?
end