Skip to content
Kward Search API index

Module: Kward::TerminalSequences

Defined in:
lib/kward/terminal_sequences.rb

Overview

Terminal control escape sequence builders.

Constant Summary collapse

KEYBOARD_PROTOCOL_ENABLE =
"\e[>25u".freeze
KEYBOARD_PROTOCOL_RESTORE =
"\e[<u".freeze
BRACKETED_PASTE_ENABLE =
"\e[?2004h".freeze
BRACKETED_PASTE_RESTORE =
"\e[?2004l".freeze
BRACKETED_PASTE_START =
"\e[200~".freeze
BRACKETED_PASTE_END =
"\e[201~".freeze
SYNCHRONIZED_OUTPUT_ENABLE =
"\e[?2026h".freeze
SYNCHRONIZED_OUTPUT_DISABLE =
"\e[?2026l".freeze
CURSOR_SHOW =
"\e[?25h".freeze
CURSOR_HIDE =
"\e[?25l".freeze
CURSOR_SHAPE_DEFAULT =
"\e[0 q".freeze
CURSOR_SHAPE_BAR =
"\e[6 q".freeze
MOUSE_REPORTING_ENABLE =
"\e[?1003h\e[?1006h".freeze
MOUSE_REPORTING_DISABLE =
"\e[?1006l\e[?1003l".freeze
SGR_RESET =
"\e[0m".freeze
SGR_INVERSE =
"\e[7m".freeze
SGR_INVERSE_OFF =
"\e[27m".freeze

Class Method Summary collapse

Class Method Details

.iterm2_image(data, name: nil, width: nil, height: nil) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/kward/terminal_sequences.rb', line 77

def iterm2_image(data, name: nil, width: nil, height: nil)
  params = ["inline=1", "preserveAspectRatio=1"]
  params << "width=#{width}" if width
  params << "height=#{height}" if height
  params << "name=#{Base64.strict_encode64(File.basename(name))}" if name
  "\e]1337;File=#{params.join(";")}:#{data}\a"
end

.kitty_delete(image_id) ⇒ Object



69
70
71
# File 'lib/kward/terminal_sequences.rb', line 69

def kitty_delete(image_id)
  "\e_Ga=d,d=I,i=#{image_id},q=2;\e\\"
end

.kitty_graphics(data, image_id:, columns: nil, rows: nil, move_cursor: true) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kward/terminal_sequences.rb', line 47

def kitty_graphics(data, image_id:, columns: nil, rows: nil, move_cursor: true)
  payload = data.to_s
  chunks = payload.scan(/.{1,4096}/m)
  chunks = [""] if chunks.empty?

  chunks.each_with_index.map do |chunk, index|
    more = index < chunks.length - 1 ? 1 : 0
    params = index.zero? ? ["a=T", "f=100", "t=d"] : []
    params << "i=#{image_id}" if index.zero? && image_id
    params << "c=#{columns.to_i}" if index.zero? && columns
    params << "r=#{rows.to_i}" if index.zero? && rows
    params << "C=1" if index.zero? && !move_cursor
    params << "q=2" if index == chunks.length - 1
    params << "m=#{more}"
    "\e_G#{params.join(",")};#{chunk}\e\\"
  end.join
end

.kitty_query(image_id = 31) ⇒ Object



65
66
67
# File 'lib/kward/terminal_sequences.rb', line 65

def kitty_query(image_id = 31)
  "\e_Gi=#{image_id},s=1,v=1,a=q,t=d,f=24;AAAA\e\\"
end

.move_to(row, col) ⇒ Object



35
36
37
# File 'lib/kward/terminal_sequences.rb', line 35

def move_to(row, col)
  "\e[#{row};#{col}H"
end

.move_to_column(column) ⇒ Object



39
40
41
# File 'lib/kward/terminal_sequences.rb', line 39

def move_to_column(column)
  "\e[#{column}G"
end

.osc52(text) ⇒ Object



43
44
45
# File 'lib/kward/terminal_sequences.rb', line 43

def osc52(text)
  "\e]52;c;#{Base64.strict_encode64(text.to_s)}\a"
end

.restore_scroll_regionObject



31
32
33
# File 'lib/kward/terminal_sequences.rb', line 31

def restore_scroll_region
  "\e[r"
end

.scroll_region(top, bottom) ⇒ Object



27
28
29
# File 'lib/kward/terminal_sequences.rb', line 27

def scroll_region(top, bottom)
  "\e[#{top};#{bottom}r"
end

.tmux_passthrough(sequence) ⇒ Object



73
74
75
# File 'lib/kward/terminal_sequences.rb', line 73

def tmux_passthrough(sequence)
  "\ePtmux;#{sequence.to_s.gsub("\e", "\e\e")}\e\\"
end