mini_readline 0.0.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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +207 -0
  6. data/Rakefile +40 -0
  7. data/lib/mini_readline.rb +28 -0
  8. data/lib/mini_readline/options.rb +19 -0
  9. data/lib/mini_readline/raw_term.rb +14 -0
  10. data/lib/mini_readline/raw_term/other.rb +67 -0
  11. data/lib/mini_readline/raw_term/other/map.rb +75 -0
  12. data/lib/mini_readline/raw_term/other/set_posn.rb +22 -0
  13. data/lib/mini_readline/raw_term/windows.rb +88 -0
  14. data/lib/mini_readline/raw_term/windows/map.rb +71 -0
  15. data/lib/mini_readline/raw_term/windows/set_posn.rb +40 -0
  16. data/lib/mini_readline/raw_term/windows/win_32_api.rb +47 -0
  17. data/lib/mini_readline/read_line.rb +65 -0
  18. data/lib/mini_readline/read_line/edit.rb +87 -0
  19. data/lib/mini_readline/read_line/edit/cancel.rb +15 -0
  20. data/lib/mini_readline/read_line/edit/delete_left.rb +21 -0
  21. data/lib/mini_readline/read_line/edit/delete_right.rb +19 -0
  22. data/lib/mini_readline/read_line/edit/enter.rb +14 -0
  23. data/lib/mini_readline/read_line/edit/go_end.rb +14 -0
  24. data/lib/mini_readline/read_line/edit/go_home.rb +14 -0
  25. data/lib/mini_readline/read_line/edit/go_left.rb +18 -0
  26. data/lib/mini_readline/read_line/edit/go_right.rb +18 -0
  27. data/lib/mini_readline/read_line/edit/insert_text.rb +18 -0
  28. data/lib/mini_readline/read_line/edit/next_history.rb +19 -0
  29. data/lib/mini_readline/read_line/edit/previous_history.rb +19 -0
  30. data/lib/mini_readline/read_line/edit/unmapped.rb +18 -0
  31. data/lib/mini_readline/read_line/edit_window.rb +72 -0
  32. data/lib/mini_readline/read_line/edit_window/sync_cursor.rb +15 -0
  33. data/lib/mini_readline/read_line/edit_window/sync_window.rb +46 -0
  34. data/lib/mini_readline/read_line/history.rb +61 -0
  35. data/lib/mini_readline/version.rb +4 -0
  36. data/mini_readline.gemspec +26 -0
  37. data/reek.txt +1 -0
  38. data/sire.rb +102 -0
  39. data/tests/mini_readline_tests.rb +55 -0
  40. metadata +110 -0
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/delete_left.rb - Process :delete_left
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/delete_left.rb - Process :delete_left
7
+ class Edit
8
+
9
+ #The delete to the left command
10
+ def delete_left(_keyboard_args)
11
+ if @edit_posn > 0
12
+ @edit_buffer = @edit_buffer[0...(@edit_posn-1)] +
13
+ @edit_buffer[@edit_posn..-1]
14
+
15
+ @edit_posn -= 1
16
+ else
17
+ @term.beep
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/delete_right.rb - Process :delete_right
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/delete_right.rb - Process :delete_right
7
+ class Edit
8
+
9
+ #The delete to the right
10
+ def delete_right(_keyboard_args)
11
+ if @edit_posn < self.length
12
+ @edit_buffer = @edit_buffer[0...(@edit_posn)] +
13
+ @edit_buffer[@edit_posn+1..-1]
14
+ else
15
+ @term.beep
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/enter.rb - Process :enter
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/enter.rb - Process :enter
7
+ class Edit
8
+
9
+ #The insert_text command. We are DONE!
10
+ def enter(_keyboard_args)
11
+ @working = false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/go_home.rb - Process :go_end
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/go_home.rb - Process :go_end
7
+ class Edit
8
+
9
+ #A lot to the right please!
10
+ def go_end(_keyboard_args)
11
+ @edit_posn = length
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/go_home.rb - Process :go_home
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/go_home.rb - Process :go_home
7
+ class Edit
8
+
9
+ #A lot to the left please!
10
+ def go_home(_keyboard_args)
11
+ @edit_posn = 0
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/go_left.rb - Process :go_left
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/go_left.rb - Process :go_left
7
+ class Edit
8
+
9
+ #A little to the left please!
10
+ def go_left(_keyboard_args)
11
+ if @edit_posn > 0
12
+ @edit_posn -= 1
13
+ else
14
+ @term.beep
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/go_right.rb - Process :go_right
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/go_right.rb - Process :go_right
7
+ class Edit
8
+
9
+ #A little to the right please!
10
+ def go_right(_keyboard_args)
11
+ if @edit_posn < edit_buffer.length
12
+ @edit_posn += 1
13
+ else
14
+ @term.beep
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/insert_text.rb - Process :insert_text
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/insert_text.rb - Process :insert_text
7
+ class Edit
8
+
9
+ #The insert_text command
10
+ def insert_text(keyboard_args)
11
+ @edit_buffer = @edit_buffer[0...@edit_posn] +
12
+ keyboard_args[1] +
13
+ @edit_buffer[@edit_posn..-1]
14
+
15
+ @edit_posn += 1
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/next_history.rb - Process :next_history
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/next_history.rb - Process :next_history
7
+ class Edit
8
+
9
+ #The insert_text command. We are DONE!
10
+ def next_history(_keyboard_args)
11
+ if (temp = @history.get_next_history)
12
+ @edit_buffer = temp
13
+ @edit_posn = temp.length
14
+ else
15
+ @term.beep
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/previous_history.rb - Process :previous_history
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/previous_history.rb - Process :previous_history
7
+ class Edit
8
+
9
+ #The insert_text command. We are DONE!
10
+ def previous_history(_keyboard_args)
11
+ if (temp = @history.get_previous_history)
12
+ @edit_buffer = temp
13
+ @edit_posn = temp.length
14
+ else
15
+ @term.beep
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/edit/unmapped.rb - Process :unmapped
4
+ module MiniReadline
5
+
6
+ #* read_line/window/edit/unmapped.rb - Process :unmapped
7
+ class Edit
8
+
9
+ #An unmapped key was pressed. Beep!
10
+ def unmapped(keyboard_args)
11
+ if @options[:debug]
12
+ puts keyboard_args.inspect
13
+ end
14
+
15
+ @term.beep
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,72 @@
1
+ # coding: utf-8
2
+
3
+ require_relative 'edit_window/sync_window'
4
+ require_relative 'edit_window/sync_cursor'
5
+
6
+ #* read_line/edit_window.rb - Edit window support.
7
+ module MiniReadline
8
+
9
+ #* read_line/edit_window.rb - Support for the edit window.
10
+ class EditWindow
11
+
12
+ #Determine the edit window limits.
13
+ def initialize_parms(options)
14
+ @options = options
15
+ @base_width = window_width - @options[:base_prompt].length
16
+ @scroll_width = window_width - @options[:scroll_prompt].length
17
+ @term = @options[:term]
18
+
19
+ @left_margin, @window_buffer = 0, ""
20
+ end
21
+
22
+ #What is the offset of the window's left margin?
23
+ attr_accessor :left_margin
24
+
25
+ #What is the offset of the window's right margin?
26
+ def right_margin
27
+ left_margin + active_width - 1
28
+ end
29
+
30
+ #Set the right margin
31
+ #<br>Notes
32
+ #* If the right_margin is being set, then we must be scrolling. That is
33
+ # why the scroll_width is used instead of active_width here.
34
+ def right_margin=(value)
35
+ @left_margin = value - scroll_width + 1
36
+ end
37
+
38
+ #Is the window currently in the scrolled state?
39
+ def window_scrolled?
40
+ left_margin > 0
41
+ end
42
+
43
+ #The shadow copy of what is actually on the screen?
44
+ attr_accessor :window_buffer
45
+
46
+ #The width of the window with the base prompt
47
+ attr_reader :base_width
48
+
49
+ #The width of the window with the alternate prompt
50
+ attr_reader :scroll_width
51
+
52
+ #What is the full window width?
53
+ def window_width
54
+ @options[:window_width]
55
+ end
56
+
57
+ #How wide is the active region of the window now?
58
+ def active_width
59
+ window_scrolled? ? scroll_width : base_width
60
+ end
61
+
62
+ #What is the current prompt?
63
+ def prompt
64
+ window_scrolled? ? @options[:scroll_prompt] : @options[:base_prompt]
65
+ end
66
+
67
+ #What is the scroll step?
68
+ def scroll_step
69
+ @options[:scroll_step]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,15 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/sync_cursor.rb - Keep the cursor in sync.
4
+ module MiniReadline
5
+
6
+ #* read_line/window/sync_cursor.rb - Keep the cursor in sync.
7
+ class EditWindow
8
+
9
+ #Keep the cursor in sync!
10
+ def sync_cursor(edit_posn)
11
+ @term.set_posn(edit_posn - left_margin + prompt.length)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/window/sync_window.rb - Keeping the screen in sync.
4
+ module MiniReadline
5
+
6
+ #* read_line/window/sync_window.rb - Keeping the screen in sync.
7
+ class EditWindow
8
+
9
+ #Keep the edit window in sync!
10
+ def sync_window(edit_buffer, edit_posn)
11
+ window_buffer.clear unless check_margins(edit_posn)
12
+ image = build_screen_image(edit_buffer)
13
+ update_screen(image)
14
+ @window_buffer = image
15
+ end
16
+
17
+ #Verify/update the window margins. Returns true if they're fine.
18
+ def check_margins(edit_posn)
19
+ old_margins = [left_margin, right_margin]
20
+
21
+ if edit_posn < left_margin
22
+ self.left_margin = [edit_posn - scroll_step, 0].max
23
+ elsif edit_posn > right_margin
24
+ self.right_margin = edit_posn + scroll_step
25
+ end
26
+
27
+ old_margins == [left_margin, right_margin]
28
+ end
29
+
30
+ #Compute what should be on the screen.
31
+ def build_screen_image(edit_buffer)
32
+ prompt + edit_buffer[left_margin..right_margin].ljust(window_width)
33
+ end
34
+
35
+ #Bring the screen into agreement with the image.
36
+ def update_screen(image)
37
+ (0...window_width).each do |index|
38
+ if (image_char = image[index]) != window_buffer[index]
39
+ @term.set_posn(index)
40
+ @term.put_string(image_char)
41
+ end
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,61 @@
1
+ # coding: utf-8
2
+
3
+ #* read_line/history.rb - Edit history support
4
+ module MiniReadline
5
+
6
+ #* read_line/history.rb - Support for the edit history.
7
+ class History
8
+
9
+ #Setup the history array of the mini line editor.
10
+ def initialize(buffer)
11
+ @_buffer = buffer
12
+ goto_end_of_history
13
+ end
14
+
15
+ #Get the history object ready for the next read line operation.
16
+ def initialize_parms(options)
17
+ @options = options
18
+ goto_end_of_history
19
+ end
20
+
21
+ #Go to the end of the history array.
22
+ def goto_end_of_history
23
+ @history_cursor = history.length
24
+ end
25
+
26
+ #Get the previous history string.
27
+ def get_previous_history
28
+ if @history_cursor > 0
29
+ @history_cursor -= 1
30
+ self.history[@history_cursor]
31
+ else
32
+ false
33
+ end
34
+ end
35
+
36
+ #Get the next history string
37
+ def get_next_history
38
+ if @history_cursor < history.length
39
+ @history_cursor += 1
40
+ history[@history_cursor] || ""
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ #Append a string to the history buffer if enabled.
47
+ def append_history(str)
48
+ str_strip = str.strip
49
+
50
+ return if (str_strip == '') && @options[:no_blanks]
51
+ return if (history.include?(str_strip)) && @options[:no_dups]
52
+
53
+ history << str
54
+ end
55
+
56
+ #Get the history buffer associated with this instance.
57
+ def history
58
+ @_buffer || []
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ module MiniReadline
2
+ #The current version of the mini_readline gem.
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mini_readline/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mini_readline"
8
+ spec.version = MiniReadline::VERSION
9
+ spec.authors = ["Peter Camilleri"]
10
+ spec.email = ["peter.c.camilleri@gmail.com"]
11
+
12
+ spec.summary = "A simplified replacement for readline."
13
+ spec.description = "A gem for console command entry with line edit and history."
14
+ spec.homepage = "http://teuthida-technologies.com/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>=1.9.3'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end