coolline 0.1.0 → 0.2.0

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.
@@ -24,38 +24,38 @@ class Coolline
24
24
  :word_boundaries => [" ", "-", "_"],
25
25
 
26
26
  :handlers =>
27
- [
28
- Handler.new(/\A(?:\C-h|\x7F)\z/, &:kill_backward_char),
29
- Handler.new("\C-a", &:beginning_of_line),
30
- Handler.new("\C-e", &:end_of_line),
31
- Handler.new("\C-k", &:kill_line),
32
- Handler.new("\C-f", &:forward_char),
33
- Handler.new("\C-b", &:backward_char),
34
- Handler.new("\C-d", &:kill_current_char_or_leave),
35
- Handler.new("\C-c") { Process.kill(:INT, Process.pid) },
36
- Handler.new("\C-w", &:kill_backward_word),
37
- Handler.new("\C-t", &:transpose_chars),
38
- Handler.new("\C-n", &:next_history_line),
39
- Handler.new("\C-p", &:previous_history_line),
40
- Handler.new("\C-r", &:interactive_search),
41
- Handler.new("\t", &:complete),
42
- Handler.new("\C-a".."\C-z") {},
43
-
44
- Handler.new(/\A\e(?:\C-h|\x7F)\z/, &:kill_backward_word),
45
- Handler.new("\eb", &:backward_word),
46
- Handler.new("\ef", &:forward_word),
47
- Handler.new("\e[A", &:previous_history_line),
48
- Handler.new("\e[B", &:next_history_line),
49
- Handler.new("\e[5~", &:previous_history_line),
50
- Handler.new("\e[6~", &:next_history_line),
51
- Handler.new("\e[C", &:forward_char),
52
- Handler.new("\e[D", &:backward_char),
53
- Handler.new("\et", &:transpose_words),
54
- Handler.new("\ec", &:capitalize_word),
55
- Handler.new("\eu", &:uppercase_word),
56
- Handler.new("\el", &:lowercase_word),
57
-
58
- Handler.new(/\e.+/) {},
27
+ [
28
+ Handler.new(/\A(?:\C-h|\x7F)\z/, &:kill_backward_char),
29
+ Handler.new(?\C-a, &:beginning_of_line),
30
+ Handler.new(?\C-e, &:end_of_line),
31
+ Handler.new(?\C-k, &:kill_line),
32
+ Handler.new(?\C-f, &:forward_char),
33
+ Handler.new(?\C-b, &:backward_char),
34
+ Handler.new(?\C-d, &:kill_current_char_or_leave),
35
+ Handler.new(?\C-c) { Process.kill(:INT, Process.pid) },
36
+ Handler.new(?\C-w, &:kill_backward_word),
37
+ Handler.new(?\C-t, &:transpose_chars),
38
+ Handler.new(?\C-n, &:next_history_line),
39
+ Handler.new(?\C-p, &:previous_history_line),
40
+ Handler.new(?\C-r, &:interactive_search),
41
+ Handler.new(?\t, &:complete),
42
+ Handler.new(?\C-a..?\C-z) {},
43
+
44
+ Handler.new(/\A\e(?:\C-h|\x7F)\z/, &:kill_backward_word),
45
+ Handler.new("\eb", &:backward_word),
46
+ Handler.new("\ef", &:forward_word),
47
+ Handler.new("\e[A", &:previous_history_line),
48
+ Handler.new("\e[B", &:next_history_line),
49
+ Handler.new("\e[5~", &:previous_history_line),
50
+ Handler.new("\e[6~", &:next_history_line),
51
+ Handler.new("\e[C", &:forward_char),
52
+ Handler.new("\e[D", &:backward_char),
53
+ Handler.new("\et", &:transpose_words),
54
+ Handler.new("\ec", &:capitalize_word),
55
+ Handler.new("\eu", &:uppercase_word),
56
+ Handler.new("\el", &:lowercase_word),
57
+
58
+ Handler.new(/\e.+/) {},
59
59
  ],
60
60
 
61
61
  :unknown_char_proc => :insert_string.to_proc,
@@ -155,11 +155,12 @@ class Coolline
155
155
  def readline(prompt = ">> ")
156
156
  @prompt = prompt
157
157
 
158
+ @history.delete_empty
159
+
158
160
  @line = ""
159
161
  @pos = 0
160
162
  @accumulator = nil
161
163
 
162
- @history_index = @history.size
163
164
  @history_moved = false
164
165
 
165
166
  @should_exit = false
@@ -167,19 +168,20 @@ class Coolline
167
168
  print "\r\e[0m\e[0K"
168
169
  print @prompt
169
170
 
171
+ @history.index = @history.size - 1
172
+ @history << @line
173
+
170
174
  until (char = @input.getch) == "\r"
171
175
  handle(char)
172
176
  return if @should_exit
173
177
 
174
178
  if @history_moved
175
179
  @history_moved = false
176
- else
177
- @history_index = @history.size
178
180
  end
179
181
 
180
- width = @input.winsize[1]
181
- prompt_size = strip_ansi_codes(@prompt).size
182
- line = transform(@line)
182
+ width = @input.winsize[1]
183
+ prompt_size = strip_ansi_codes(@prompt).size
184
+ line = transform(@line)
183
185
 
184
186
  stripped_line_width = strip_ansi_codes(line).size
185
187
  line << " " * [width - stripped_line_width - prompt_size, 0].max
@@ -214,7 +216,6 @@ class Coolline
214
216
  break if i >= end_index
215
217
  end
216
218
  end
217
-
218
219
  if @pos < left_width + 1
219
220
  print "\e[#{prompt_size + @pos + 1}G"
220
221
  end
@@ -223,7 +224,8 @@ class Coolline
223
224
 
224
225
  print "\n"
225
226
 
226
- @history << @line
227
+ @history[-1] = @line if @history.size != 0
228
+ @history.index = @history.size
227
229
 
228
230
  @line + "\n"
229
231
  end
@@ -240,14 +242,13 @@ class Coolline
240
242
 
241
243
  # Selects the previous line in history (if any)
242
244
  def previous_history_line
243
- if @history_index - 1 >= 0
244
- @line.replace @history[@history_index - 1]
245
- @pos = [@line.size, @pos].min
246
-
247
- @history_index -= 1
245
+ if @history.index >= 0
246
+ @line.replace @history[@history.index]
247
+ @history.index -= 1
248
248
  end
249
249
 
250
250
  @history_moved = true
251
+ end_of_line
251
252
  end
252
253
 
253
254
  # Selects the next line in history (if any).
@@ -255,19 +256,22 @@ class Coolline
255
256
  # When on the last line, this method replaces the current line with an empty
256
257
  # string.
257
258
  def next_history_line
258
- if @history_index + 1 <= @history.size
259
- @line.replace @history[@history_index + 1] || ""
260
- @pos = [@line.size, @pos].min
261
-
262
- @history_index += 1
259
+ if @history.index + 2 < @history.size
260
+ @history.index += 1
261
+ @line.replace @history[@history.index + 1] || @history.current
262
+ else
263
+ @line.replace @history[-1]
264
+ @history.index = @history.size - 2
263
265
  end
264
266
 
265
267
  @history_moved = true
268
+
269
+ end_of_line
266
270
  end
267
271
 
268
272
  # Prompts the user to search for a line
269
273
  def interactive_search
270
- found_index = @history_index
274
+ found_index = @history.index
271
275
 
272
276
  # Use another coolline instance for the search! :D
273
277
  Coolline.new { |c|
@@ -278,7 +282,7 @@ class Coolline
278
282
  c.transform_proc = proc do
279
283
  pattern = Regexp.new Regexp.escape(c.line)
280
284
 
281
- line, found_index = @history.search(pattern, @history_index).first
285
+ line, found_index = @history.search(pattern, @history.index).first
282
286
 
283
287
  if line
284
288
  "#{c.line}): #{line}"
@@ -292,10 +296,12 @@ class Coolline
292
296
  c.history_size = 0
293
297
  }.readline("(search:")
294
298
 
299
+ found_index ||= @history.index
300
+
295
301
  @line.replace @history[found_index]
296
302
  @pos = [@line.size, @pos].min
297
303
 
298
- @history_index = found_index
304
+ @history.index = found_index
299
305
  @history_moved = true
300
306
  end
301
307
 
@@ -2,6 +2,8 @@ class Coolline
2
2
  # Class used to keep track of input. It keeps a certain amount of lines at
3
3
  # most in memory, and stores them in a file.
4
4
  class History
5
+ attr_accessor :index
6
+
5
7
  def initialize(filename, max_size = 5000)
6
8
  @io = File.open(filename, 'a+')
7
9
  @max_size = max_size
@@ -9,6 +11,8 @@ class Coolline
9
11
  @lines = []
10
12
 
11
13
  load_lines
14
+ delete_empty
15
+ @index = size
12
16
  end
13
17
 
14
18
  def reopen(filename)
@@ -33,6 +37,10 @@ class Coolline
33
37
  end
34
38
  end
35
39
 
40
+ def delete_empty
41
+ @lines.reject!(&:empty?)
42
+ end
43
+
36
44
  def <<(el)
37
45
  @io.puts el
38
46
  @io.flush
@@ -43,10 +51,18 @@ class Coolline
43
51
  self
44
52
  end
45
53
 
54
+ def current
55
+ self[@index]
56
+ end
57
+
46
58
  def [](id)
47
59
  @lines[id]
48
60
  end
49
61
 
62
+ def []=(id, val)
63
+ @lines[id] = val
64
+ end
65
+
50
66
  def size
51
67
  @lines.size
52
68
  end
@@ -1,3 +1,3 @@
1
1
  class Coolline
2
- Version = "0.1.0"
2
+ Version = "0.2.0"
3
3
  end
data/repl.rb CHANGED
@@ -20,7 +20,6 @@ end
20
20
 
21
21
  loop do
22
22
  line = cool.readline
23
-
24
23
  obj = eval(line)
25
24
 
26
25
  print "=> "
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coolline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-08 00:00:00.000000000 Z
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: riot
16
- requirement: &23189860 !ruby/object:Gem::Requirement
16
+ requirement: &12993820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *23189860
24
+ version_requirements: *12993820
25
25
  description: ! 'A readline-like library that allows to change how the input
26
26
 
27
27
  is displayed.
@@ -69,3 +69,4 @@ signing_key:
69
69
  specification_version: 3
70
70
  summary: Sounds like readline, smells like readline, but isn't readline
71
71
  test_files: []
72
+ has_rdoc: