Class: Reflex::Terminal

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

Constant Summary collapse

PALETTE_RANGE =
(0..(COLOR_PALETTE_LAST - COLOR_PALETTE_FIRST)).freeze
HISTORY_CHUNK_SIZE =
500

Instance Method Summary collapse

Constructor Details

#initialize(columns = 80, rows = 24, scrollback_bytes: 8 * 1024 * 1024) ⇒ Terminal

Returns a new instance of Terminal.



16
17
18
19
20
21
22
23
# File 'lib/reflex/terminal.rb', line 16

def initialize(
  columns = 80, rows = 24,
  # a memory budget rather than a line count: how many lines fit
  # depends on how wide the terminal is. 0 keeps no scrollback
  scrollback_bytes: 8 * 1024 * 1024)

  initialize! columns, rows, scrollback_bytes
end

Instance Method Details

#background_colorObject



132
# File 'lib/reflex/terminal.rb', line 132

def         background_color()      =         get_color! COLOR_BACKGROUND

#cursor_colorObject



134
# File 'lib/reflex/terminal.rb', line 134

def             cursor_color()      =         get_color! COLOR_CURSOR

#default_background_colorObject



124
# File 'lib/reflex/terminal.rb', line 124

def default_background_color()      = get_default_color! COLOR_BACKGROUND

#default_background_color=(color) ⇒ Object



108
109
110
# File 'lib/reflex/terminal.rb', line 108

def default_background_color=(color)
  set_or_clear_default_color COLOR_BACKGROUND, color
end

#default_cursor_colorObject



126
# File 'lib/reflex/terminal.rb', line 126

def     default_cursor_color()      = get_default_color! COLOR_CURSOR

#default_cursor_color=(color) ⇒ Object



112
113
114
# File 'lib/reflex/terminal.rb', line 112

def default_cursor_color=(color)
  set_or_clear_default_color COLOR_CURSOR, color
end

#default_foreground_colorObject



122
# File 'lib/reflex/terminal.rb', line 122

def default_foreground_color()      = get_default_color! COLOR_FOREGROUND

#default_foreground_color=(color) ⇒ Object



104
105
106
# File 'lib/reflex/terminal.rb', line 104

def default_foreground_color=(color)
  set_or_clear_default_color COLOR_FOREGROUND, color
end

#default_palette(index) ⇒ Object



128
# File 'lib/reflex/terminal.rb', line 128

def          default_palette(index) = get_default_color! to_palette_index(index)

#each_history_line {|line| ... } ⇒ Enumerator

Yields each line of the scrollback, oldest first.

The history is walked in chunks rather than handed over at once, since it can hold far more than fits in memory.

Yields:

  • (line)

    a line of the history, without its trailing spaces

Returns:

  • (Enumerator)

    when no block is given



89
90
91
92
93
94
95
# File 'lib/reflex/terminal.rb', line 89

def each_history_line(&block)
  return enum_for :each_history_line unless block
  (0...history_rows).step HISTORY_CHUNK_SIZE do |row|
    get_history_lines!(row, HISTORY_CHUNK_SIZE).each(&block)
  end
  self
end

#each_line {|line| ... } ⇒ Enumerator

Yields each line of the visible screen.

Yields:

  • (line)

    a row of the screen, without its trailing spaces and without a newline of its own

Returns:

  • (Enumerator)

    when no block is given



69
70
71
72
# File 'lib/reflex/terminal.rb', line 69

def each_line(&block)
  return enum_for :each_line unless block
  each_line!(&block)
end

#each_span {|x, y, width, text, fg, bg, flags| ... } ⇒ Enumerator

Yields each style span of the visible screen.

Yields:

  • (x, y, width, text, fg, bg, flags)

    a run of cells sharing the same style; x, y and width are in cells, and fg/bg are 0xRRGGBB or nil for the terminal's default color

Returns:

  • (Enumerator)

    when no block is given



57
58
59
60
# File 'lib/reflex/terminal.rb', line 57

def each_span(&block)
  return enum_for :each_span unless block
  each_span!(&block)
end

#foreground_colorObject



130
# File 'lib/reflex/terminal.rb', line 130

def         foreground_color()      =         get_color! COLOR_FOREGROUND

#linesArray<String>

Returns the visible screen, one string per row.

Returns:

  • (Array<String>)

    the visible screen, one string per row



76
77
78
# File 'lib/reflex/terminal.rb', line 76

def lines()
  each_line.to_a
end

#palette(index) ⇒ Object



136
# File 'lib/reflex/terminal.rb', line 136

def                  palette(index) =         get_color! to_palette_index(index)

#resize(columns, rows, cell_width: 8, cell_height: 16, screen_width: 0, screen_height: 0) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/reflex/terminal.rb', line 25

def resize(
  columns, rows,
    cell_width: 8,   cell_height: 16,
  screen_width: 0, screen_height: 0)

  resize! columns, rows, cell_width, cell_height, screen_width, screen_height
end

#set_default_palette(index, color) ⇒ Object



116
117
118
119
120
# File 'lib/reflex/terminal.rb', line 116

def set_default_palette(index, color)
  Array(index).each do |i|
    set_or_clear_default_color to_palette_index(i), color
  end
end

#spawn(*args) ⇒ self

Starts a child process on a new pseudo terminal.

Follows the Kernel#spawn convention: a leading hash sets environment variables (a nil value removes one), a single string runs via the shell (so quoting and pipes work), and multiple arguments exec directly. Unlike Kernel#spawn, unsetenv_others is not supported.

Returns:

  • (self)

    self



42
43
44
45
46
47
# File 'lib/reflex/terminal.rb', line 42

def spawn(*args)
  envs = args.first.is_a?(Hash) ? args.shift : {}
  args = args.compact
  args = shell_command(args.first) if args.size == 1
  spawn! args, envs
end