picky 2.1.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,21 +2,31 @@ class Terminal
2
2
 
3
3
  attr_reader :client
4
4
 
5
- def initialize given_uri
5
+ def initialize given_uri, id_amount = nil
6
6
  check_highline_gem
7
7
  check_picky_client_gem
8
8
 
9
9
  require 'uri'
10
10
  uri = URI.parse given_uri
11
+
12
+ # If the user gave a whole url without http, add that and reparse.
13
+ #
11
14
  unless uri.path
12
15
  uri = URI.parse "http://#{given_uri}"
13
16
  end
17
+
18
+ # If the user gave a path without / in front, add one.
19
+ #
14
20
  unless uri.path =~ /^\//
15
21
  uri.path = "/#{uri.path}"
16
22
  end
17
23
 
18
24
  @searches = 0
19
25
  @durations = 0
26
+ @current_text = ''
27
+ @cursor_offset = 0
28
+ @last_ids = ''
29
+ @id_amount = id_amount && Integer(id_amount) || 20
20
30
  @client = Picky::Client.new :host => (uri.host || 'localhost'), :port => (uri.port || 8080), :path => uri.path
21
31
 
22
32
  install_trap
@@ -35,6 +45,8 @@ class Terminal
35
45
  exit 1
36
46
  end
37
47
 
48
+ # Install the Ctrl-C handler.
49
+ #
38
50
  def install_trap
39
51
  Signal.trap('INT') do
40
52
  print "\e[100D"
@@ -47,17 +59,28 @@ class Terminal
47
59
  end
48
60
  end
49
61
 
62
+ # Flush to STDOUT.
63
+ #
50
64
  def flush
51
65
  STDOUT.flush
52
66
  end
67
+
68
+ # Position cursor amount to the left.
69
+ #
53
70
  def left amount = 1
54
71
  print "\e[#{amount}D"
55
72
  flush
56
73
  end
74
+
75
+ # Position cursor amount to the right.
76
+ #
57
77
  def right amount = 1
58
78
  print "\e[#{amount}C"
59
79
  flush
60
80
  end
81
+
82
+ # Move cursor to position.
83
+ #
61
84
  def move_to position
62
85
  relative = position - @cursor_offset
63
86
  if relative > 0
@@ -68,45 +91,90 @@ class Terminal
68
91
  @cursor_offset = position
69
92
  flush
70
93
  end
94
+
95
+ # Delete one character.
96
+ #
71
97
  def backspace
72
- @current_text.chop!
73
- print "\e[1D"
74
- print " "
75
- print "\e[1D"
98
+ chop_text
99
+ print "\e[1D \e[1D"
76
100
  flush
77
101
  end
102
+
103
+ # Write the text to the input area.
104
+ #
78
105
  def write text
79
- print text
80
106
  @cursor_offset += text.size
107
+ print text
81
108
  flush
82
109
  end
110
+
111
+ # Chop off one character.
112
+ #
113
+ def chop_text
114
+ @current_text.chop!
115
+ end
116
+
117
+ # Add the given text to the current text.
118
+ #
119
+ def add_text text
120
+ @current_text << text
121
+ end
122
+
123
+ # Type the given text into the input area.
124
+ #
83
125
  def type_search character
84
- @current_text << character
126
+ add_text character
85
127
  write character
86
128
  end
129
+
130
+ # Write the amount of result ids.
131
+ #
87
132
  def write_results results
88
133
  move_to 0
89
134
  write "%9d" % (results && results.total || 0)
90
135
  move_to 10 + @current_text.size
91
136
  end
137
+
138
+ # Move to the id area.
139
+ #
92
140
  def move_to_ids
93
- move_to 10 + @current_text.size + 2
141
+ move_to 12 + @current_text.size
94
142
  end
143
+
144
+ # Write the result ids.
145
+ #
95
146
  def write_ids results
96
147
  move_to_ids
97
- write "=> #{results.total ? results.ids : []}"
148
+ write "=> #{results.total ? results.ids(@id_amount) : []}"
149
+ rescue StandardError => e
150
+ p e.message
151
+ p e.backtrace
98
152
  end
153
+
154
+ # Clear the result ids.
155
+ #
99
156
  def clear_ids
100
157
  move_to_ids
101
- write " "*200
158
+ write @ids_clearing_string ||= " "*200
102
159
  end
160
+
161
+ # Log a search.
162
+ #
103
163
  def log results
104
164
  @searches += 1
105
165
  @durations += (results[:duration] || 0)
106
166
  end
167
+
168
+ # Perform a search.
169
+ #
107
170
  def search full = false
108
- client.search @current_text, :ids => (full ? 20 : 0)
171
+ client.search @current_text, :ids => (full ? @id_amount : 0)
109
172
  end
173
+
174
+ # Perform a search and write the results.
175
+ #
176
+ # Handles 404s and connection problems.
177
+ #
110
178
  def search_and_write full = false
111
179
  results = search full
112
180
  results.extend Picky::Convenience
@@ -116,16 +184,20 @@ class Terminal
116
184
  full ? write_ids(results) : clear_ids
117
185
 
118
186
  write_results results
187
+ rescue Errno::ECONNREFUSED => e
188
+ write "Please start a Picky server listening to #{@client.path}."
189
+ rescue Yajl::ParseError => e
190
+ write "Got a 404. Maybe the path #{@client.path} isn't a correct one?"
119
191
  end
120
192
 
193
+ # Run the terminal.
194
+ #
195
+ # Note: Uses a simple loop to handle input.
196
+ #
121
197
  def run
122
- puts "Type and see the result count update. Press enter for the first 20 result ids."
198
+ puts "Type and see the result count update. Press enter for the first #{@id_amount} result ids."
123
199
  puts "Break with Ctrl-C."
124
200
 
125
- @current_text = ''
126
- @cursor_offset = 0
127
- @last_ids = ''
128
- move_to 10
129
201
  search_and_write
130
202
 
131
203
  loop do
@@ -137,7 +209,7 @@ class Terminal
137
209
  search_and_write
138
210
  when 13
139
211
  search_and_write true
140
- else
212
+ else # All other.
141
213
  type_search input.chr
142
214
  search_and_write
143
215
  end
@@ -1,6 +1,6 @@
1
1
  # Routing tasks.
2
2
  #
3
- desc "Shows the available URL paths"
3
+ desc "Shows the available URL paths."
4
4
  task :routes => :application do
5
5
  Application.apps.each do |app|
6
6
  puts app.to_routes
@@ -1,7 +1,10 @@
1
1
  # Tasks for testing your engine configuration in the terminal.
2
2
  #
3
+ desc 'Simple terminal search - pass it an URL to search on, e.g. /books.'
3
4
  task :search do
4
5
  load File.expand_path '../../picky/auxiliary/terminal.rb', __FILE__
5
- terminal = Terminal.new ARGV[1] || raise("Usage:\n rake search <URL>\n E.g. rake search /books\n rake search localhost:8080/books")
6
+ url_or_path = ARGV[1] || raise("Usage:\n rake search <URL> [<result ids>]\n E.g. rake search /books\n rake search localhost:8080/books 10")
7
+ ids = ARGV[2]
8
+ terminal = Terminal.new url_or_path, ids
6
9
  terminal.run
7
10
  end
@@ -11,8 +11,102 @@ describe Terminal do
11
11
 
12
12
  let(:terminal) { described_class.new('/some/url') }
13
13
 
14
- before(:each) do
15
- terminal.stub! :search => { :total => 0, :duration => 0.01 }
14
+ describe 'backspace' do
15
+ it 'works correctly' do
16
+ terminal.should_receive(:chop_text).once.ordered.with()
17
+ terminal.should_receive(:print).once.ordered.with "\e[1D \e[1D"
18
+ terminal.should_receive(:flush).once.ordered.with()
19
+
20
+ terminal.backspace
21
+ end
22
+ end
23
+
24
+ describe 'write_results' do
25
+ it 'works correctly' do
26
+ terminal.should_receive(:move_to).once.ordered.with 0
27
+ terminal.should_receive(:write).once.ordered.with " 0"
28
+ terminal.should_receive(:move_to).once.ordered.with 10
29
+
30
+ terminal.write_results nil
31
+ end
32
+ it 'works correctly' do
33
+ terminal.should_receive(:move_to).once.ordered.with 0
34
+ terminal.should_receive(:write).once.ordered.with "123456789"
35
+ terminal.should_receive(:move_to).once.ordered.with 10
36
+
37
+ terminal.write_results stub(:results, :total => 123456789)
38
+ end
39
+ end
40
+
41
+ describe 'search' do
42
+ before(:each) do
43
+ @client = stub :client
44
+ terminal.stub! :client => @client
45
+
46
+ terminal.add_text 'hello'
47
+ end
48
+ it 'searches full correctly' do
49
+ @client.should_receive(:search).once.with 'hello', :ids => 20
50
+
51
+ terminal.search true
52
+ end
53
+ it 'searches full correctly' do
54
+ terminal = described_class.new('/some/url', 33)
55
+ terminal.stub! :client => @client
56
+
57
+ @client.should_receive(:search).once.with '', :ids => 33
58
+
59
+ terminal.search true
60
+ end
61
+ it 'searches live correctly' do
62
+ @client.should_receive(:search).once.with 'hello', :ids => 0
63
+
64
+ terminal.search
65
+ end
66
+ it 'searches live correctly' do
67
+ @client.should_receive(:search).once.with 'hello', :ids => 0
68
+
69
+ terminal.search false
70
+ end
71
+ end
72
+
73
+ describe 'clear_ids' do
74
+ it 'moves to the ids, then clears all' do
75
+ terminal.should_receive(:move_to_ids).once.with()
76
+ terminal.should_receive(:write).once.with " "*200
77
+
78
+ terminal.clear_ids
79
+ end
80
+ end
81
+
82
+ describe 'write_ids' do
83
+ it 'writes the result\'s ids' do
84
+ terminal.should_receive(:move_to_ids).once.with()
85
+ terminal.should_receive(:write).once.with "=> []"
86
+
87
+ terminal.write_ids stub(:results, :total => nil)
88
+ end
89
+ it 'writes the result\'s ids' do
90
+ terminal.should_receive(:move_to_ids).once.with()
91
+ terminal.should_receive(:write).once.with "=> [1, 2, 3]"
92
+
93
+ terminal.write_ids stub(:results, :total => 3, :ids => [1, 2, 3])
94
+ end
95
+ end
96
+
97
+ describe 'move_to_ids' do
98
+ it 'moves to a specific place' do
99
+ terminal.should_receive(:move_to).once.with 12
100
+
101
+ terminal.move_to_ids
102
+ end
103
+ it 'moves to a specific place' do
104
+ terminal.add_text 'test'
105
+
106
+ terminal.should_receive(:move_to).once.with 16
107
+
108
+ terminal.move_to_ids
109
+ end
16
110
  end
17
111
 
18
112
  describe 'left' do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.1.1
5
+ version: 2.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florian Hanke