Class: Picky::Cursed
- Includes:
- Curses
- Defined in:
- lib/picky-client/tools/cursed.rb
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_picky_client_gem ⇒ Object
:nodoc:.
-
#chop_text ⇒ Object
Chop off one character.
- #clear_error ⇒ Object
-
#clear_results ⇒ Object
Clear the result ids.
-
#error(text) ⇒ Object
Display an error text.
-
#initialize(given_uri, id_amount = nil) ⇒ Cursed
constructor
A new instance of Cursed.
-
#install_trap ⇒ Object
Install the Ctrl-C handler.
-
#intro ⇒ Object
Display an intro text.
-
#log(results) ⇒ Object
Log a search.
-
#move_to_counts ⇒ Object
Positioning.
- #move_to_error ⇒ Object
- #move_to_input ⇒ Object
- #move_to_results ⇒ Object
-
#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_counts(results) ⇒ Object
Write the amount of result ids.
-
#write_results(results) ⇒ Object
Write the result ids.
Constructor Details
#initialize(given_uri, id_amount = nil) ⇒ Cursed
Returns a new instance of Cursed.
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 38 39 40 41 42 |
# File 'lib/picky-client/tools/cursed.rb', line 12 def initialize given_uri, id_amount = nil check_picky_client_gem init_screen curs_set 1 stdscr.keypad(true) 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 = '' @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.
10 11 12 |
# File 'lib/picky-client/tools/cursed.rb', line 10 def client @client end |
Instance Method Details
#add_text(text) ⇒ Object
Add the given text to the current text.
91 92 93 |
# File 'lib/picky-client/tools/cursed.rb', line 91 def add_text text @current_text << text end |
#backspace ⇒ Object
Delete one character.
78 79 80 81 82 |
# File 'lib/picky-client/tools/cursed.rb', line 78 def backspace chop_text move_to_input clrtoeol end |
#check_picky_client_gem ⇒ Object
:nodoc:
43 44 45 46 47 48 |
# File 'lib/picky-client/tools/cursed.rb', line 43 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.
85 86 87 |
# File 'lib/picky-client/tools/cursed.rb', line 85 def chop_text @current_text.chop! end |
#clear_error ⇒ Object
169 170 171 172 173 |
# File 'lib/picky-client/tools/cursed.rb', line 169 def clear_error move_to_error addstr @error_clear_string ||= " "*80 move_to_input end |
#clear_results ⇒ Object
Clear the result ids.
113 114 115 116 117 |
# File 'lib/picky-client/tools/cursed.rb', line 113 def clear_results move_to_results clrtoeol move_to_input end |
#error(text) ⇒ Object
Display an error text.
163 164 165 166 167 168 |
# File 'lib/picky-client/tools/cursed.rb', line 163 def error text move_to_error flash addstr text move_to_input end |
#install_trap ⇒ Object
Install the Ctrl-C handler.
52 53 54 55 56 57 58 59 |
# File 'lib/picky-client/tools/cursed.rb', line 52 def install_trap Signal.trap('INT') do move_to_error puts "Performed #{@searches} searches (#{"%.3f" % @durations} seconds)." sleep 1 exit end end |
#intro ⇒ Object
Display an intro text.
177 178 179 180 181 182 |
# File 'lib/picky-client/tools/cursed.rb', line 177 def intro addstr "Type and see the result count update. Press enter for the first #{@id_amount} result ids." setpos 1, 0 addstr "Break with Ctrl-C." setpos 2, 0 end |
#log(results) ⇒ Object
Log a search.
129 130 131 132 |
# File 'lib/picky-client/tools/cursed.rb', line 129 def log results @searches += 1 @durations += (results[:duration] || 0) end |
#move_to_counts ⇒ Object
Positioning.
63 64 65 |
# File 'lib/picky-client/tools/cursed.rb', line 63 def move_to_counts setpos 3, 0 end |
#move_to_error ⇒ Object
72 73 74 |
# File 'lib/picky-client/tools/cursed.rb', line 72 def move_to_error setpos 5, 0 end |
#move_to_input ⇒ Object
66 67 68 |
# File 'lib/picky-client/tools/cursed.rb', line 66 def move_to_input setpos 3, (12 + @current_text.size) end |
#move_to_results ⇒ Object
69 70 71 |
# File 'lib/picky-client/tools/cursed.rb', line 69 def move_to_results setpos 4, 12 end |
#run ⇒ Object
Run the terminal.
Note: Uses a simple loop to handle input.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/picky-client/tools/cursed.rb', line 188 def run intro move_to_input search_and_write loop do input = getch case input when 10 # Curses::Key::ENTER search_and_write true when 127 # Curses::Key::BACKSPACE delch backspace search_and_write when (256..1000000) else type_search input.chr search_and_write end end end |
#search(full = false) ⇒ Object
Perform a search.
136 137 138 |
# File 'lib/picky-client/tools/cursed.rb', line 136 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/picky-client/tools/cursed.rb', line 144 def search_and_write full = false results = search full results.extend Picky::Convenience clear_error log results full ? write_results(results) : clear_results write_counts results move_to_input rescue Errno::ECONNREFUSED => e error "Please start a Picky server listening to #{@client.path}." rescue Yajl::ParseError => e error "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.
97 98 99 |
# File 'lib/picky-client/tools/cursed.rb', line 97 def type_search character add_text character end |
#write_counts(results) ⇒ Object
Write the amount of result ids.
121 122 123 124 125 |
# File 'lib/picky-client/tools/cursed.rb', line 121 def write_counts results move_to_counts addstr "%11d" % (results && results.total || 0) move_to_input end |
#write_results(results) ⇒ Object
Write the result ids.
103 104 105 106 107 108 109 110 |
# File 'lib/picky-client/tools/cursed.rb', line 103 def write_results results move_to_results addstr "#{results.total ? results.ids(@id_amount) : []}" move_to_input rescue StandardError => e p e. p e.backtrace end |