Class: RCommand
- Inherits:
-
Object
- Object
- RCommand
- Defined in:
- lib/rcommand.rb,
lib/rcommand/version.rb,
lib/rcommand/io_methods.rb
Overview
– Copyright © 2005 Robert Aman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Defined Under Namespace
Modules: RCOMMAND_VERSION Classes: IOMethod, RawIOMethod, StandardIOMethod
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#history_index ⇒ Object
Returns the value of attribute history_index.
-
#io_method ⇒ Object
Returns the value of attribute io_method.
-
#io_read ⇒ Object
readonly
Returns the value of attribute io_read.
-
#io_write ⇒ Object
readonly
Returns the value of attribute io_write.
-
#procs ⇒ Object
readonly
Returns the value of attribute procs.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
Class Method Summary collapse
-
.common_prefix(command_list) ⇒ Object
Returns any prefix shared by all commands in a list, or the empty string if there was no prefix.
-
.matches(partial, command_list) ⇒ Object
Returns all commands in a command list that match a partial command.
Instance Method Summary collapse
-
#<<(command) ⇒ Object
Adds a command to the command line.
-
#completions_for(partial) ⇒ Object
Returns an array of completions for the given partial command.
-
#initialize(io_read, io_write) ⇒ RCommand
constructor
Creates a new RCommand interface object.
-
#next ⇒ Object
Sets the command line to the next command.
- #on_completion(&block) ⇒ Object
- #on_double_tab(&block) ⇒ Object
- #on_interupt(&block) ⇒ Object
- #on_key_down(&block) ⇒ Object
- #on_key_left(&block) ⇒ Object
- #on_key_right(&block) ⇒ Object
- #on_key_up(&block) ⇒ Object
- #on_single_tab(&block) ⇒ Object
-
#prev ⇒ Object
Sets the command line to the previous command.
-
#readline ⇒ Object
Returns the command string entered by the user.
Constructor Details
#initialize(io_read, io_write) ⇒ RCommand
Creates a new RCommand interface object.
69 70 71 72 73 74 75 76 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rcommand.rb', line 69 def initialize(io_read, io_write) unless io_read.kind_of? IO raise "Expecting object of type IO, got #{io_read.class.name}." end unless io_write.kind_of? IO raise "Expecting object of type IO, got #{io_write.class.name}." end @io_read = io_read @io_write = io_write @io_method = nil @history = [] @history_index = nil @procs = {} @procs[:completion] = lambda do |partial| RCommand.matches(partial, (self.history.collect do |line| line.split(' ') end).flatten.uniq) end @procs[:single_tab] = lambda do |partial| matches = self.completions_for(partial) common_prefix = RCommand.common_prefix(matches) common_prefix.nil? ? partial : common_prefix end @procs[:double_tab] = lambda do |partial| matches = self.completions_for(partial) if matches.size > 1 self.io_write.puts() for match in matches.uniq self.io_write.puts(match) end self.prompt() self.io_write.print(partial) end partial end @procs[:key_up] = lambda do |partial| self.prev() end @procs[:key_down] = lambda do |partial| self.next() end @procs[:key_left] = nil @procs[:key_left] = nil @procs[:interupt] = lambda do self.io_write.print("\n") system("stty sane") exit end initialize_io_method() end |
Instance Attribute Details
#history ⇒ Object (readonly)
Returns the value of attribute history.
130 131 132 |
# File 'lib/rcommand.rb', line 130 def history @history end |
#history_index ⇒ Object
Returns the value of attribute history_index.
131 132 133 |
# File 'lib/rcommand.rb', line 131 def history_index @history_index end |
#io_method ⇒ Object
Returns the value of attribute io_method.
126 127 128 |
# File 'lib/rcommand.rb', line 126 def io_method @io_method end |
#io_read ⇒ Object (readonly)
Returns the value of attribute io_read.
124 125 126 |
# File 'lib/rcommand.rb', line 124 def io_read @io_read end |
#io_write ⇒ Object (readonly)
Returns the value of attribute io_write.
125 126 127 |
# File 'lib/rcommand.rb', line 125 def io_write @io_write end |
#procs ⇒ Object (readonly)
Returns the value of attribute procs.
128 129 130 |
# File 'lib/rcommand.rb', line 128 def procs @procs end |
#prompt ⇒ Object
Returns the value of attribute prompt.
133 134 135 |
# File 'lib/rcommand.rb', line 133 def prompt @prompt end |
Class Method Details
.common_prefix(command_list) ⇒ Object
Returns any prefix shared by all commands in a list, or the empty string if there was no prefix.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rcommand.rb', line 47 def self.common_prefix(command_list) return "" unless command_list.kind_of? Enumerable return "" if command_list.size == 0 command_list.each do |command| unless command.kind_of? String raise "Expecting String, got #{command.class.name} instead." end end prefix = "" command_list = command_list.sort do |a, b| a.size <=> b.size end for i in 0..command_list[0].size for command in command_list return prefix if command_list[0][i] != command[i] end prefix = command_list[0][0..i] end return prefix end |
.matches(partial, command_list) ⇒ Object
Returns all commands in a command list that match a partial command.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rcommand.rb', line 34 def self.matches(partial, command_list) matches = [] for command in command_list unless command.kind_of? String raise "Expecting String, got #{command.class.name} instead." end matches << command if command[0...(partial.size)] == partial end return matches end |
Instance Method Details
#<<(command) ⇒ Object
Adds a command to the command line.
181 182 183 184 185 186 187 188 189 |
# File 'lib/rcommand.rb', line 181 def <<(command) command = command[0..-1] if command[-1] == "\n" command = command[0..-1] if command[-1] == "\r" unless command.empty? self.history.pop() if self.history[-1] == "" self.history << command end self.history_index = nil end |
#completions_for(partial) ⇒ Object
Returns an array of completions for the given partial command. This method tries to use the on_completion
block if available.
146 147 148 149 150 151 152 |
# File 'lib/rcommand.rb', line 146 def completions_for(partial) if @procs[:completion] != nil return @procs[:completion].call(partial) else return RCommand.matches(partial, self.history) end end |
#next ⇒ Object
Sets the command line to the next command.
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rcommand.rb', line 166 def next() if self.history_index.nil? nil else self.history_index += 1 if self.history_index > self.history.size - 1 self.history_index = self.history.size - 1 "" else self.history[self.history_index] end end end |
#on_completion(&block) ⇒ Object
135 |
# File 'lib/rcommand.rb', line 135 def on_completion(&block); @procs[:completion] = block; end |
#on_double_tab(&block) ⇒ Object
137 |
# File 'lib/rcommand.rb', line 137 def on_double_tab(&block); @procs[:double_tab] = block; end |
#on_interupt(&block) ⇒ Object
142 |
# File 'lib/rcommand.rb', line 142 def on_interupt(&block); @procs[:interupt] = block; end |
#on_key_down(&block) ⇒ Object
139 |
# File 'lib/rcommand.rb', line 139 def on_key_down(&block); @procs[:key_down] = block; end |
#on_key_left(&block) ⇒ Object
140 |
# File 'lib/rcommand.rb', line 140 def on_key_left(&block); @procs[:key_left] = block; end |
#on_key_right(&block) ⇒ Object
141 |
# File 'lib/rcommand.rb', line 141 def on_key_right(&block); @procs[:key_right] = block; end |
#on_key_up(&block) ⇒ Object
138 |
# File 'lib/rcommand.rb', line 138 def on_key_up(&block); @procs[:key_up] = block; end |
#on_single_tab(&block) ⇒ Object
136 |
# File 'lib/rcommand.rb', line 136 def on_single_tab(&block); @procs[:single_tab] = block; end |
#prev ⇒ Object
Sets the command line to the previous command.
155 156 157 158 159 160 161 162 163 |
# File 'lib/rcommand.rb', line 155 def prev() if self.history_index.nil? self.history_index = self.history.size - 1 else self.history_index -= 1 self.history_index = 0 if self.history_index < 0 end self.history[self.history_index] end |
#readline ⇒ Object
Returns the command string entered by the user.
192 193 194 195 196 197 198 |
# File 'lib/rcommand.rb', line 192 def readline() line = self.io_method.readline() line = line[0..-1] if line[-1] == "\n" line = line[0..-1] if line[-1] == "\r" self << line return line + "\n" end |