Class: Foruiman::TUI::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/tui/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, terminal: Terminal.new, renderer: Renderer.new) ⇒ Application

Returns a new instance of Application.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/foruiman/tui/application.rb', line 12

def initialize(engine, terminal: Terminal.new, renderer: Renderer.new)
  @engine = engine
  @terminal = terminal
  @renderer = renderer
  @keyboard = Keyboard.new
  @state = State.new(engine.processes.map(&:name))
  @last_frame = nil
  @next_frame_at = 0
  @was_shutting_down = false
  @feedback_until = 0
end

Instance Attribute Details

#state ⇒ Object (readonly)

Returns the value of attribute state.



10
11
12
# File 'lib/foruiman/tui/application.rb', line 10

def state
  @state
end

Instance Method Details

#handle(key, height) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/foruiman/tui/application.rb', line 52

def handle(key, height)
  return if @engine.shutting_down?

  if key.is_a?(Integer)
    state.select(key.zero? ? state.tabs.size - 1 : key - 1)
    return
  end
  buffer = @engine.logs[state.name]
  case key
  when :next then state.move(1)
  when :previous then state.move(-1)
  when :up then state.viewport.scroll(-1, buffer, height)
  when :down then state.viewport.scroll(1, buffer, height)
  when :page_up then state.viewport.scroll(-height, buffer, height)
  when :page_down then state.viewport.scroll(height, buffer, height)
  when :home then state.viewport.home(buffer)
  when :end, :follow then state.viewport.follow
  when :toggle_follow then state.viewport.toggle(buffer, height)
  when :help then state.help = !state.help
  when :escape then state.help = false
  when :restart then control(key)
  when :stop then toggle_process
  when :input then begin_input
  when :restart_all, :stop_all
    control_all(key == :restart_all ? :restart : :stop)
  when :quit then @engine.shutdown
  end
end

#run ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/foruiman/tui/application.rb', line 24

def run
  @engine.manage_input!
  @terminal.session do
    @engine.run(keep_open: true) do
      rows, columns = @terminal.size
      @engine.resize_inputs(rows, columns)
      detach_unavailable_input
      input = @terminal.read
      @engine.shutdown if input.nil?
      height = [Renderer.log_height(rows: rows, columns: columns, input: state.input?), 1].max
      read_keys(input.is_a?(String) ? input : "", height)
      state.feedback = nil if monotonic >= @feedback_until
      next if monotonic < @next_frame_at && @was_shutting_down == @engine.shutting_down?

      @was_shutting_down = @engine.shutting_down?

      @next_frame_at = monotonic + (1.0 / 30)
      frame = @renderer.render(state, @engine, rows: rows, columns: columns)
      if frame != @last_frame
        @terminal.draw(frame)
        @last_frame = frame
      end
    end
  end
ensure
  @engine.close
end