picky-client 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/picky-client/aux/cursed.rb +215 -0
- metadata +3 -2
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
module Picky
|
|
2
|
+
|
|
3
|
+
# A simple terminal based search.
|
|
4
|
+
#
|
|
5
|
+
class Cursed # :nodoc:all
|
|
6
|
+
|
|
7
|
+
require 'curses'
|
|
8
|
+
include Curses
|
|
9
|
+
|
|
10
|
+
attr_reader :client
|
|
11
|
+
|
|
12
|
+
def initialize given_uri, id_amount = nil
|
|
13
|
+
check_picky_client_gem
|
|
14
|
+
|
|
15
|
+
init_screen
|
|
16
|
+
curs_set 1
|
|
17
|
+
stdscr.keypad(true)
|
|
18
|
+
|
|
19
|
+
require 'uri'
|
|
20
|
+
uri = URI.parse given_uri
|
|
21
|
+
|
|
22
|
+
# If the user gave a whole url without http, add that and reparse.
|
|
23
|
+
#
|
|
24
|
+
unless uri.path
|
|
25
|
+
uri = URI.parse "http://#{given_uri}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# If the user gave a path without / in front, add one.
|
|
29
|
+
#
|
|
30
|
+
unless uri.path =~ /^\//
|
|
31
|
+
uri.path = "/#{uri.path}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
@searches = 0
|
|
35
|
+
@durations = 0
|
|
36
|
+
|
|
37
|
+
@current_text = ''
|
|
38
|
+
@id_amount = id_amount && Integer(id_amount) || 20
|
|
39
|
+
@client = Picky::Client.new :host => (uri.host || 'localhost'), :port => (uri.port || 8080), :path => uri.path
|
|
40
|
+
|
|
41
|
+
install_trap
|
|
42
|
+
end
|
|
43
|
+
def check_picky_client_gem # :nodoc:
|
|
44
|
+
require 'picky-client'
|
|
45
|
+
rescue LoadError
|
|
46
|
+
warn_gem_missing 'picky-client', 'the terminal interface'
|
|
47
|
+
exit 1
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Install the Ctrl-C handler.
|
|
51
|
+
#
|
|
52
|
+
def install_trap
|
|
53
|
+
Signal.trap('INT') do
|
|
54
|
+
move_to_error
|
|
55
|
+
puts "Performed #{@searches} searches (#{"%.3f" % @durations} seconds)."
|
|
56
|
+
sleep 1
|
|
57
|
+
exit
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Positioning.
|
|
62
|
+
#
|
|
63
|
+
def move_to_counts
|
|
64
|
+
setpos 3, 0
|
|
65
|
+
end
|
|
66
|
+
def move_to_input
|
|
67
|
+
setpos 3, (12 + @current_text.size)
|
|
68
|
+
end
|
|
69
|
+
def move_to_results
|
|
70
|
+
setpos 4, 12
|
|
71
|
+
end
|
|
72
|
+
def move_to_error
|
|
73
|
+
setpos 5, 0
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Delete one character.
|
|
77
|
+
#
|
|
78
|
+
def backspace
|
|
79
|
+
chop_text
|
|
80
|
+
move_to_input
|
|
81
|
+
clrtoeol
|
|
82
|
+
end
|
|
83
|
+
# Chop off one character.
|
|
84
|
+
#
|
|
85
|
+
def chop_text
|
|
86
|
+
@current_text.chop!
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Add the given text to the current text.
|
|
90
|
+
#
|
|
91
|
+
def add_text text
|
|
92
|
+
@current_text << text
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Type the given text into the input area.
|
|
96
|
+
#
|
|
97
|
+
def type_search character
|
|
98
|
+
add_text character
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Write the result ids.
|
|
102
|
+
#
|
|
103
|
+
def write_results results
|
|
104
|
+
move_to_results
|
|
105
|
+
addstr "#{results.total ? results.ids(@id_amount) : []}"
|
|
106
|
+
move_to_input
|
|
107
|
+
rescue StandardError => e
|
|
108
|
+
p e.message
|
|
109
|
+
p e.backtrace
|
|
110
|
+
end
|
|
111
|
+
# Clear the result ids.
|
|
112
|
+
#
|
|
113
|
+
def clear_results
|
|
114
|
+
move_to_results
|
|
115
|
+
clrtoeol
|
|
116
|
+
move_to_input
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Write the amount of result ids.
|
|
120
|
+
#
|
|
121
|
+
def write_counts results
|
|
122
|
+
move_to_counts
|
|
123
|
+
addstr "%11d" % (results && results.total || 0)
|
|
124
|
+
move_to_input
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Log a search.
|
|
128
|
+
#
|
|
129
|
+
def log results
|
|
130
|
+
@searches += 1
|
|
131
|
+
@durations += (results[:duration] || 0)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Perform a search.
|
|
135
|
+
#
|
|
136
|
+
def search full = false
|
|
137
|
+
client.search @current_text, :ids => (full ? @id_amount : 0)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Perform a search and write the results.
|
|
141
|
+
#
|
|
142
|
+
# Handles 404s and connection problems.
|
|
143
|
+
#
|
|
144
|
+
def search_and_write full = false
|
|
145
|
+
results = search full
|
|
146
|
+
results.extend Picky::Convenience
|
|
147
|
+
|
|
148
|
+
clear_error
|
|
149
|
+
log results
|
|
150
|
+
|
|
151
|
+
full ? write_results(results) : clear_results
|
|
152
|
+
|
|
153
|
+
write_counts results
|
|
154
|
+
move_to_input
|
|
155
|
+
rescue Errno::ECONNREFUSED => e
|
|
156
|
+
error "Please start a Picky server listening to #{@client.path}."
|
|
157
|
+
rescue Yajl::ParseError => e
|
|
158
|
+
error "Got a 404. Maybe the path #{@client.path} isn't a correct one?"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Display an error text.
|
|
162
|
+
#
|
|
163
|
+
def error text
|
|
164
|
+
move_to_error
|
|
165
|
+
flash
|
|
166
|
+
addstr text
|
|
167
|
+
move_to_input
|
|
168
|
+
end
|
|
169
|
+
def clear_error
|
|
170
|
+
move_to_error
|
|
171
|
+
addstr @error_clear_string ||= " "*80
|
|
172
|
+
move_to_input
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Display an intro text.
|
|
176
|
+
#
|
|
177
|
+
def intro
|
|
178
|
+
addstr "Type and see the result count update. Press enter for the first #{@id_amount} result ids."
|
|
179
|
+
setpos 1, 0
|
|
180
|
+
addstr "Break with Ctrl-C."
|
|
181
|
+
setpos 2, 0
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Run the terminal.
|
|
185
|
+
#
|
|
186
|
+
# Note: Uses a simple loop to handle input.
|
|
187
|
+
#
|
|
188
|
+
def run
|
|
189
|
+
intro
|
|
190
|
+
|
|
191
|
+
move_to_input
|
|
192
|
+
search_and_write
|
|
193
|
+
|
|
194
|
+
loop do
|
|
195
|
+
input = getch
|
|
196
|
+
|
|
197
|
+
case input
|
|
198
|
+
when 10 # Curses::Key::ENTER
|
|
199
|
+
search_and_write true
|
|
200
|
+
when 127 # Curses::Key::BACKSPACE
|
|
201
|
+
delch
|
|
202
|
+
backspace
|
|
203
|
+
search_and_write
|
|
204
|
+
when (256..1000000)
|
|
205
|
+
|
|
206
|
+
else
|
|
207
|
+
type_search input.chr
|
|
208
|
+
search_and_write
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
end
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: picky-client
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 2.2.
|
|
5
|
+
version: 2.2.1
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Florian Hanke
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2011-04-
|
|
13
|
+
date: 2011-04-15 00:00:00 +10:00
|
|
14
14
|
default_executable:
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|
|
@@ -33,6 +33,7 @@ extensions: []
|
|
|
33
33
|
extra_rdoc_files:
|
|
34
34
|
- README.rdoc
|
|
35
35
|
files:
|
|
36
|
+
- lib/picky-client/aux/cursed.rb
|
|
36
37
|
- lib/picky-client/aux/terminal.rb
|
|
37
38
|
- lib/picky-client/client.rb
|
|
38
39
|
- lib/picky-client/convenience.rb
|