Class: Muxr::InputHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/muxr/input_handler.rb

Overview

Translates raw keystrokes into either commands (when the Ctrl-a prefix is active) or passthrough bytes to the focused pane. The handler is a small state machine: :idle → :prefix → :idle, with a separate :command branch for the “:”-driven mini-command line.

Constant Summary collapse

PREFIX =

Ctrl-a

"\x01".freeze
PREFIX_BINDINGS =
{
  "c"    => :new_pane,
  "n"    => :focus_next,
  "p"    => :focus_prev,
  "a"    => :focus_last,
  "k"    => :close_focused,
  "\t"   => :cycle_layout,
  "\r"   => :promote_master,
  "\n"   => :promote_master,
  "~"    => :toggle_drawer,
  "d"    => :detach,
  "?"    => :show_help,
  "q"    => :quit_immediate,
  "["    => :enter_scrollback,
  "]"    => :paste_from_buffer
}.freeze
SCROLLBACK_BINDINGS =
{
  "j"    => :line_forward,
  "k"    => :line_back,
  "\x04" => :half_forward, # Ctrl-d
  "\x15" => :half_back,    # Ctrl-u
  "d"    => :half_forward,
  "u"    => :half_back,
  "\x06" => :full_forward, # Ctrl-f
  "\x02" => :full_back,    # Ctrl-b
  "f"    => :full_forward,
  "b"    => :full_back,
  " "    => :full_forward,
  "g"    => :top,
  "G"    => :bottom
}.freeze
SCROLLBACK_EXITS =

q, Esc, Ctrl-c

["q", "\e", "\x03"].freeze
SELECTION_BINDINGS =
{
  "h"    => :left,
  "l"    => :right,
  "j"    => :down,
  "k"    => :up,
  "0"    => :line_start,
  "$"    => :line_end,
  "g"    => :top,
  "G"    => :bottom,
  "\x04" => :half_down, # Ctrl-d
  "\x15" => :half_up,   # Ctrl-u
  "d"    => :half_down,
  "u"    => :half_up,
  "\x06" => :full_down, # Ctrl-f
  "\x02" => :full_up,   # Ctrl-b
  "f"    => :full_down,
  "b"    => :full_up,
  " "    => :full_down
}.freeze
SELECTION_YANK =
["\r", "\n", "y"].freeze
SELECTION_CANCEL =

q, Esc, Ctrl-c

["q", "\e", "\x03"].freeze
DIGIT_RE =
/\A[1-9]\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ InputHandler

Returns a new instance of InputHandler.



71
72
73
74
75
# File 'lib/muxr/input_handler.rb', line 71

def initialize(app)
  @app = app
  @state = :idle
  @command_buffer = +""
end

Instance Attribute Details

#command_bufferObject (readonly)

Returns the value of attribute command_buffer.



69
70
71
# File 'lib/muxr/input_handler.rb', line 69

def command_buffer
  @command_buffer
end

#stateObject (readonly)

Returns the value of attribute state.



69
70
71
# File 'lib/muxr/input_handler.rb', line 69

def state
  @state
end

Instance Method Details

#cancelObject



123
124
125
126
# File 'lib/muxr/input_handler.rb', line 123

def cancel
  @state = :idle
  @command_buffer = +""
end

#enter_confirm_quitObject



107
108
109
# File 'lib/muxr/input_handler.rb', line 107

def enter_confirm_quit
  @state = :confirm_quit
end

#enter_help_modeObject



103
104
105
# File 'lib/muxr/input_handler.rb', line 103

def enter_help_mode
  @state = :help
end

#enter_idle_modeObject



119
120
121
# File 'lib/muxr/input_handler.rb', line 119

def enter_idle_mode
  @state = :idle
end

#enter_scrollback_modeObject



111
112
113
# File 'lib/muxr/input_handler.rb', line 111

def enter_scrollback_mode
  @state = :scrollback
end

#enter_selection_modeObject



115
116
117
# File 'lib/muxr/input_handler.rb', line 115

def enter_selection_mode
  @state = :selection
end

#feed(data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/muxr/input_handler.rb', line 77

def feed(data)
  data.each_char do |ch|
    case @state
    when :help
      @app.dismiss_help
      @state = :idle
    when :confirm_quit
      handle_confirm_quit(ch)
    when :idle
      if ch == PREFIX
        @state = :prefix
      else
        @app.send_to_focused(ch)
      end
    when :prefix
      handle_prefix(ch)
    when :command
      handle_command_input(ch)
    when :scrollback
      handle_scrollback_input(ch)
    when :selection
      handle_selection_input(ch)
    end
  end
end