Class: Muby::InputWindow

Inherits:
Object
  • Object
show all
Includes:
Configurable, Displayer, HelperMethods, Logger, Styled, UserMethods
Defined in:
lib/muby/inputwindow.rb

Overview

The fancy window that gets our input.

The class handles everything to do with the input window itself, the user callable methods are in the UserMethods module.

All commands entered with conf.escape_character will be eval’d in this class scope.

Constant Summary

Constants included from Styled

Styled::ALTCHARSET, Styled::BLACK, Styled::BLINK, Styled::BLUE, Styled::BOLD, Styled::CHARTEXT, Styled::CYAN, Styled::DIM, Styled::GREEN, Styled::INVIS, Styled::MAGENTA, Styled::NORMAL, Styled::PROTECT, Styled::RED, Styled::REVERSE, Styled::STANDOUT, Styled::UNDERLINE, Styled::WHITE, Styled::YELLOW

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#do_execute, #execute, #execute_with_verbosity

Methods included from UserMethods

#append_buffer!, #backspace_buffer!, #complete!, #connect, #connected?, #delete_buffer!, #disable_history_search!, #disconnect, #echo, #enable_history_search!, #end_buffer!, #execute_command!, #fake_connect, #get_message_line, #get_status_line, #help, #history_search!, #home_buffer!, #ignore_command!, #next_character_buffer!, #next_history_buffer!, #next_word_buffer!, #previous_character_buffer!, #previous_history_buffer!, #previous_word_buffer!, #print, #process_buffer!, #quit, #receive, #reconnect, #reload_application!, #rescue_thread, #resize_application!, #scroll_down!, #scroll_up!, #send, #sendn, #set_message_line, #set_status_line, #shell_command!, #toggle_history_search!, #toggle_verbosity!, #two_step_quit!, #update_history_buffer!, #update_history_search!, #word_backspace_buffer!

Methods included from Displayer

debug, error, exception, info, trace, warn

Methods included from Configurable

#conf

Methods included from Logger

#log_input, #log_output

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



29
30
31
# File 'lib/muby/inputwindow.rb', line 29

def history
  @history
end

Class Method Details

.get_instanceObject



25
26
27
# File 'lib/muby/inputwindow.rb', line 25

def self.get_instance
  @@instance ||= Muby::InputWindow.new
end

Instance Method Details

#do_startup_triggersObject



174
175
176
177
178
# File 'lib/muby/inputwindow.rb', line 174

def do_startup_triggers
  conf.startup_triggers.each do |trigger|
    execute(trigger, self, Muby::OutputWindow.get_instance)
  end
end

#goObject

Start working!



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/muby/inputwindow.rb', line 34

def go
  # Ensure our size is calculable
  begin
    height
    width
    top
    left
  rescue Exception => e
    Ncurses.endwin
    raise "Unable to calculate input window geometry: #{e}\n\nCheck your config file (#{conf.loaded_rc_file}) for errors in 'conf.input_window_geometry!\n\n"
  end

  # Init all the default values
  @buffer = ""
  @cursorPosition = 0
  @history = []
  @historyPointer = nil
  @connection = nil
  @statusLine = ""
  @messageLine = ""
  @recent_escape = []
  @handle_mode = :append_buffer!

  resize!

  help unless conf.user_edited_config_file
end

#handle(c, hash) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/muby/inputwindow.rb', line 151

def handle(c, hash)
  # Echo the keycode we got if we have conf.echo_keycodes == true (good for setting up conf.key_commands)
  if conf.echo_keycodes && c != Ncurses.const_get("ERR")
    info(c.to_s)
  end
  if hash.include?(c)
    value = hash[c]
    if Hash === value
      c = @inputWindow.wgetch
      if value.include?(c)
        handle(c, value)
      else
        handle(c, conf.key_commands)
      end
    else
      execute(value, self, Muby::OutputWindow.get_instance, c)
    end
  elsif 0 < c && c < 265
    method = self.method(@handle_mode)
    method.call(c)
  end
end

#heightObject



93
94
95
# File 'lib/muby/inputwindow.rb', line 93

def height
  do_execute(conf.input_window_geometry[:height])
end

#leftObject



81
82
83
# File 'lib/muby/inputwindow.rb', line 81

def left
  do_execute(conf.input_window_geometry[:left])
end

#loadHistoryObject

Load our history from the history file.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/muby/inputwindow.rb', line 134

def loadHistory
  begin
    if FileTest.readable?(conf.history_file)
      info("Reading #{conf.history_file}")
      Kernel::open(conf.history_file) do |input|
        @history = Marshal.load(input.read)
      end
      @history.each do |line|
        Muby::Completer.get_instance.store(line)
      end if conf.feed_completer_with_history
    end
  rescue Exception => e
    exception(e)
  end
  @history ||= []
end

#processObject

Just keep listening to the keyboard input.



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/muby/inputwindow.rb', line 183

def process
  loadHistory
  do_startup_triggers
  # We need to check one key at a time due to triggers and conf.key_commands
  while c = @inputWindow.wgetch
    if c == Ncurses.const_get("ERR")
      sleep(0.01)
    else
      handle(c, conf.key_commands)
      update
    end
  end # while c = @inputWindow.wgetch
end

#reload!Object



77
78
79
# File 'lib/muby/inputwindow.rb', line 77

def reload!
  do_startup_triggers
end

#resize!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/muby/inputwindow.rb', line 62

def resize!
  # Init the input box
  @inputBorder = Ncurses.newwin(height, width, top, left)
  @inputBorder.box(0,0)
  @inputBorder.keypad(true)
  @inputBorder.refresh

  # Init the window itself
  @inputWindow = Ncurses.newwin(height - 2, width - 2, top + 1, left + 1)
  @inputWindow.keypad(true)
  @inputWindow.scrollok(true)
  @inputWindow.nodelay(true)
  @inputWindow.refresh
end

#saveHistoryObject

Save our history into the history file.



121
122
123
124
125
126
127
128
129
# File 'lib/muby/inputwindow.rb', line 121

def saveHistory
  # This should be wrapped in something else, which checks for sanity (writability)
  dir = Pathname.new(conf.history_file).parent
  dir.mkpath unless dir.exist?
  file = Pathname.new(conf.history_file)
  file.open("w") do |output|
    Marshal.dump(@history, output)
  end
end

#topObject



85
86
87
# File 'lib/muby/inputwindow.rb', line 85

def top
  do_execute(conf.input_window_geometry[:top])
end

#updateObject

Update the input box, redrawing borders and text etc.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/muby/inputwindow.rb', line 100

def update
  @inputBorder.box(0,0)
  @inputBorder.mvwprintw(0,1,"%s", @statusLine)
  @inputBorder.mvwprintw(height - 1, 1, "%s", @messageLine)
  @inputBorder.refresh

  @inputWindow.werase
  row = 0
  col = @cursorPosition
  while col > width - 2
    row = row + 1
    col = col - width - 2
  end
  @inputWindow.mvwprintw(row,col,"%s", @buffer[@cursorPosition,@buffer.size - @cursorPosition])
  @inputWindow.mvwprintw(0,0,"%s", @buffer[0, @cursorPosition])
  @inputWindow.refresh
end

#widthObject



89
90
91
# File 'lib/muby/inputwindow.rb', line 89

def width
  do_execute(conf.input_window_geometry[:width])
end