Class: RubyRich::Terminal

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

Constant Summary collapse

MOUSE_ENABLE =
"\e[?1000h\e[?1002h\e[?1006h"
MOUSE_DISABLE =
"\e[?1006l\e[?1002l\e[?1000l"
AUTOWRAP_ENABLE =
"\e[?7h"
AUTOWRAP_DISABLE =
"\e[?7l"
ALT_SCREEN_ENABLE =
"\e[?1049h"
ALT_SCREEN_DISABLE =
"\e[?1049l"
BRACKETED_PASTE_ENABLE =
"\e[?2004h"
BRACKETED_PASTE_DISABLE =
"\e[?2004l"
STD_INPUT_HANDLE =
-10
STD_OUTPUT_HANDLE =
-11
ENABLE_MOUSE_INPUT =
0x0010
ENABLE_WINDOW_INPUT =
0x0008
ENABLE_QUICK_EDIT_MODE =
0x0040
ENABLE_EXTENDED_FLAGS =
0x0080
ENABLE_VIRTUAL_TERMINAL_INPUT =
0x0200
ENABLE_PROCESSED_OUTPUT =
0x0001
ENABLE_VIRTUAL_TERMINAL_PROCESSING =
0x0004
INPUT_RECORD_SIZE =
20
KEY_EVENT =
0x0001
MOUSE_EVENT =
0x0002
FROM_LEFT_1ST_BUTTON_PRESSED =
0x0001
RIGHTMOST_BUTTON_PRESSED =
0x0002
FROM_LEFT_2ND_BUTTON_PRESSED =
0x0004
MOUSE_MOVED =
0x0001
DOUBLE_CLICK =
0x0002
MOUSE_WHEELED =
0x0004
MOUSE_HWHEELED =
0x0008
RIGHT_ALT_PRESSED =
0x0001
LEFT_ALT_PRESSED =
0x0002
RIGHT_CTRL_PRESSED =
0x0004
LEFT_CTRL_PRESSED =
0x0008
SHIFT_PRESSED =
0x0010

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.debug_inputObject

Returns the value of attribute debug_input.



43
44
45
# File 'lib/ruby_rich/terminal.rb', line 43

def debug_input
  @debug_input
end

Class Method Details

.clearObject



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

def clear
  print "\e[2J\e[H"
  $stdout.flush
end

.disable_bracketed_pasteObject



99
100
101
102
# File 'lib/ruby_rich/terminal.rb', line 99

def disable_bracketed_paste
  print BRACKETED_PASTE_DISABLE
  $stdout.flush
end

.disable_mouseObject



73
74
75
76
77
# File 'lib/ruby_rich/terminal.rb', line 73

def disable_mouse
  @mouse_reporting_enabled = false
  print MOUSE_DISABLE
  $stdout.flush
end

.enable_bracketed_pasteObject



94
95
96
97
# File 'lib/ruby_rich/terminal.rb', line 94

def enable_bracketed_paste
  print BRACKETED_PASTE_ENABLE
  $stdout.flush
end

.enable_mouseObject



66
67
68
69
70
71
# File 'lib/ruby_rich/terminal.rb', line 66

def enable_mouse
  @mouse_reporting_enabled = true
  enable_windows_input_mode if windows?
  print MOUSE_ENABLE
  $stdout.flush
end

.enter_alt_screenObject



84
85
86
87
# File 'lib/ruby_rich/terminal.rb', line 84

def enter_alt_screen
  print ALT_SCREEN_ENABLE
  $stdout.flush
end

.leave_alt_screenObject



89
90
91
92
# File 'lib/ruby_rich/terminal.rb', line 89

def leave_alt_screen
  print ALT_SCREEN_DISABLE
  $stdout.flush
end

.prepare_inputObject



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

def prepare_input
  return unless windows?

  enable_windows_input_mode
end

.read_windows_input_eventObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby_rich/terminal.rb', line 138

def read_windows_input_event
  return nil unless windows?
  pending = next_windows_pending_event
  return pending if pending

  enable_windows_input_mode
  handle = get_std_handle.call(STD_INPUT_HANDLE)
  record = Fiddle::Pointer.malloc(INPUT_RECORD_SIZE)
  read_count = Fiddle::Pointer.malloc(4)

  loop do
    return nil unless read_console_input.call(handle, record, 1, read_count) != 0
    next unless read_count[0, 4].unpack1('L') == 1

    event = parse_windows_input_record(record)
    event = coalesce_windows_paste(handle, event) if paste_seed_event?(event)
    return event if event
  end
rescue
  nil
end

.restore(mouse: false, show_cursor: true, autowrap: true, alt_screen: false) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ruby_rich/terminal.rb', line 56

def restore(mouse: false, show_cursor: true, autowrap: true, alt_screen: false)
  disable_mouse if mouse
  disable_bracketed_paste
  set_autowrap(autowrap)
  leave_alt_screen if alt_screen
  restore_virtual_terminal_on_windows
  system("stty #{@original_state}") if @original_state && !windows?
  print TTY::Cursor.show if show_cursor
end

.set_autowrap(enabled) ⇒ Object



79
80
81
82
# File 'lib/ruby_rich/terminal.rb', line 79

def set_autowrap(enabled)
  print(enabled ? AUTOWRAP_ENABLE : AUTOWRAP_DISABLE)
  $stdout.flush
end

.setup(mouse: false, hide_cursor: true, autowrap: true, alt_screen: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_rich/terminal.rb', line 45

def setup(mouse: false, hide_cursor: true, autowrap: true, alt_screen: false)
  capture_state
  enable_virtual_terminal_on_windows
  system('stty -echo') unless windows?
  enter_alt_screen if alt_screen
  enable_bracketed_paste
  set_autowrap(autowrap)
  print TTY::Cursor.hide if hide_cursor
  enable_mouse if mouse
end

.windows?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/ruby_rich/terminal.rb', line 130

def windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end

.windows_input_modeObject



122
123
124
125
126
127
128
# File 'lib/ruby_rich/terminal.rb', line 122

def windows_input_mode
  return nil unless windows?

  console_mode(get_std_handle.call(STD_INPUT_HANDLE))
rescue
  nil
end

.windows_mouse_reporting?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/ruby_rich/terminal.rb', line 134

def windows_mouse_reporting?
  windows? && @mouse_reporting_enabled
end

.with_cooked(mouse: false, alt_screen: false) ⇒ Object



109
110
111
112
113
114
# File 'lib/ruby_rich/terminal.rb', line 109

def with_cooked(mouse: false, alt_screen: false)
  restore(mouse: mouse, alt_screen: alt_screen)
  yield
ensure
  setup(mouse: mouse, alt_screen: alt_screen, autowrap: false)
end