Class: TuiTui::TerminalSession

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

Overview

Owns raw mode, alternate screen, and restoration traps for one TUI session.

Constant Summary collapse

RESTORE_SIGNALS =
%w[TERM HUP INT].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console:, output:, events:, mouse:) ⇒ TerminalSession

Returns a new instance of TerminalSession.



10
11
12
13
14
15
16
# File 'lib/tui_tui/terminal_session.rb', line 10

def initialize(console:, output:, events:, mouse:)
  @console = console
  @output = output
  @events = events
  @mouse = mouse
  @closed = false
end

Instance Attribute Details

#mouseObject

Returns the value of attribute mouse.



28
29
30
# File 'lib/tui_tui/terminal_session.rb', line 28

def mouse
  @mouse
end

Instance Method Details

#closeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tui_tui/terminal_session.rb', line 42

def close
  # Close is called from ensure, at_exit, and signal traps.
  return if @closed

  @closed = true
  @output.write((@mouse ? Ansi::MOUSE_OFF : "") + Ansi::SHOW + Ansi::ALT_OFF)
  @output.flush
  begin
    @console.cooked!
  rescue StandardError
    nil
  end

  trap("WINCH", @prev_winch || "DEFAULT") if defined?(@prev_winch)
  restore_signal_traps
end

#startObject



18
19
20
21
22
23
24
25
26
# File 'lib/tui_tui/terminal_session.rb', line 18

def start
  # `intr: true` keeps the interrupt/quit/suspend characters live in raw mode, so Ctrl-C raises SIGINT.
  @console.raw!(intr: true)
  @output.write(Ansi::ALT_ON + Ansi::HIDE + Ansi::CLEAR + (@mouse ? Ansi::MOUSE_ON : ""))
  @output.flush
  @prev_winch = trap("WINCH") { @events.resized! }
  install_restore_traps
  at_exit { close }
end