Class: Picky::Terminal
Overview
A simple terminal based search.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#add_text(text) ⇒ Object
Add the given text to the current text.
-
#backspace ⇒ Object
Delete one character.
-
#check_highline_gem ⇒ Object
:nodoc:.
-
#check_picky_client_gem ⇒ Object
:nodoc:.
-
#chop_text ⇒ Object
Chop off one character.
-
#clear_ids ⇒ Object
Clear the result ids.
-
#flush ⇒ Object
Flush to STDOUT.
-
#initialize(given_uri, id_amount = nil) ⇒ Terminal
constructor
A new instance of Terminal.
-
#install_trap ⇒ Object
Install the Ctrl-C handler.
-
#left(amount = 1) ⇒ Object
Position cursor amount to the left.
-
#log(results) ⇒ Object
Log a search.
-
#move_to(position) ⇒ Object
Move cursor to position.
-
#move_to_ids ⇒ Object
Move to the id area.
-
#right(amount = 1) ⇒ Object
Position cursor amount to the right.
-
#run ⇒ Object
Run the terminal.
-
#search(full = false) ⇒ Object
Perform a search.
-
#search_and_write(full = false) ⇒ Object
Perform a search and write the results.
-
#type_search(character) ⇒ Object
Type the given text into the input area.
-
#write(text) ⇒ Object
Write the text to the input area.
-
#write_ids(results) ⇒ Object
Write the result ids.
-
#write_results(results) ⇒ Object
Write the amount of result ids.
Constructor Details
#initialize(given_uri, id_amount = nil) ⇒ Terminal
Returns a new instance of Terminal.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/picky-client/tools/terminal.rb', line 9 def initialize given_uri, id_amount = nil check_highline_gem check_picky_client_gem require 'uri' uri = URI.parse given_uri # If the user gave a whole url without http, add that and reparse. # unless uri.path uri = URI.parse "http://#{given_uri}" end # If the user gave a path without / in front, add one. # unless uri.path =~ /^\// uri.path = "/#{uri.path}" end @searches = 0 @durations = 0 @current_text = '' @cursor_offset = 0 @last_ids = '' @id_amount = id_amount && Integer(id_amount) || 20 @client = Picky::Client.new uri install_trap end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/picky-client/tools/terminal.rb', line 7 def client @client end |
Instance Method Details
#add_text(text) ⇒ Object
Add the given text to the current text.
123 124 125 |
# File 'lib/picky-client/tools/terminal.rb', line 123 def add_text text @current_text << text end |
#backspace ⇒ Object
Delete one character.
101 102 103 104 105 |
# File 'lib/picky-client/tools/terminal.rb', line 101 def backspace chop_text print "\e[1D \e[1D" flush end |
#check_highline_gem ⇒ Object
:nodoc:
38 39 40 41 42 43 44 |
# File 'lib/picky-client/tools/terminal.rb', line 38 def check_highline_gem # :nodoc: require "highline/system_extensions" extend HighLine::SystemExtensions rescue LoadError warn_gem_missing 'highline', 'the terminal interface' exit 1 end |
#check_picky_client_gem ⇒ Object
:nodoc:
45 46 47 48 49 50 |
# File 'lib/picky-client/tools/terminal.rb', line 45 def check_picky_client_gem # :nodoc: require 'picky-client' rescue LoadError warn_gem_missing 'picky-client', 'the terminal interface' exit 1 end |
#chop_text ⇒ Object
Chop off one character.
117 118 119 |
# File 'lib/picky-client/tools/terminal.rb', line 117 def chop_text @current_text.chop! end |
#clear_ids ⇒ Object
Clear the result ids.
160 161 162 163 |
# File 'lib/picky-client/tools/terminal.rb', line 160 def clear_ids move_to_ids write @ids_clearing_string ||= " "*200 end |
#flush ⇒ Object
Flush to STDOUT.
68 69 70 |
# File 'lib/picky-client/tools/terminal.rb', line 68 def flush STDOUT.flush end |
#install_trap ⇒ Object
Install the Ctrl-C handler.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/picky-client/tools/terminal.rb', line 54 def install_trap Signal.trap('INT') do print "\e[100D" flush puts "\n" puts "You performed #{@searches} searches, totalling #{"%.3f" % @durations} seconds." print "\e[100D" flush exit end end |
#left(amount = 1) ⇒ Object
Position cursor amount to the left.
74 75 76 77 |
# File 'lib/picky-client/tools/terminal.rb', line 74 def left amount = 1 print "\e[#{amount}D" flush end |
#log(results) ⇒ Object
Log a search.
167 168 169 170 |
# File 'lib/picky-client/tools/terminal.rb', line 167 def log results @searches += 1 @durations += (results[:duration] || 0) end |
#move_to(position) ⇒ Object
Move cursor to position.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/picky-client/tools/terminal.rb', line 88 def move_to position relative = position - @cursor_offset if relative > 0 right relative else left relative end @cursor_offset = position flush end |
#move_to_ids ⇒ Object
Move to the id area.
144 145 146 |
# File 'lib/picky-client/tools/terminal.rb', line 144 def move_to_ids move_to 12 + @current_text.size end |
#right(amount = 1) ⇒ Object
Position cursor amount to the right.
81 82 83 84 |
# File 'lib/picky-client/tools/terminal.rb', line 81 def right amount = 1 print "\e[#{amount}C" flush end |
#run ⇒ Object
Run the terminal.
Note: Uses a simple loop to handle input.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/picky-client/tools/terminal.rb', line 201 def run puts "Type and see the result count update. Press enter for the first #{@id_amount} result ids." puts "Break with Ctrl-C." search_and_write loop do input = get_character case input when 127 backspace search_and_write when 13 search_and_write true else # All other. type_search input.chr search_and_write end end end |
#search(full = false) ⇒ Object
Perform a search.
174 175 176 |
# File 'lib/picky-client/tools/terminal.rb', line 174 def search full = false client.search @current_text, :ids => (full ? @id_amount : 0) end |
#search_and_write(full = false) ⇒ Object
Perform a search and write the results.
Handles 404s and connection problems.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/picky-client/tools/terminal.rb', line 182 def search_and_write full = false results = search full results.extend Picky::Convenience log results full ? write_ids(results) : clear_ids write_results results rescue Errno::ECONNREFUSED => e write "Please start a Picky server listening to #{@client.path}." rescue Yajl::ParseError => e write "Got a 404. Maybe the path #{@client.path} isn't a correct one?" end |
#type_search(character) ⇒ Object
Type the given text into the input area.
129 130 131 132 |
# File 'lib/picky-client/tools/terminal.rb', line 129 def type_search character add_text character write character end |
#write(text) ⇒ Object
Write the text to the input area.
109 110 111 112 113 |
# File 'lib/picky-client/tools/terminal.rb', line 109 def write text @cursor_offset += text.size print text flush end |
#write_ids(results) ⇒ Object
Write the result ids.
150 151 152 153 154 155 156 |
# File 'lib/picky-client/tools/terminal.rb', line 150 def write_ids results move_to_ids write "=> #{(results.total ? results.ids(@id_amount) : []).inspect}" rescue StandardError => e p e. p e.backtrace end |
#write_results(results) ⇒ Object
Write the amount of result ids.
136 137 138 139 140 |
# File 'lib/picky-client/tools/terminal.rb', line 136 def write_results results move_to 0 write "%9d" % (results && results.total || 0) move_to 10 + @current_text.size end |