yap-rawline 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76acdc9b7eb8468db86e98f86c2eb40383b61497
4
+ data.tar.gz: 54fe99a5a89a602cdcbe0ad54cbf139ff041423a
5
+ SHA512:
6
+ metadata.gz: 6000eefda9f5121e4d2f697f09b9dcd95980b377b685192a23fe6e62934ff06224f6ed3874532974eae7d25dfb9093284c3fe3853ae8245307f2d3b547be26c0
7
+ data.tar.gz: d982956b2abe6c8a0531060072117f15513809dc5ba1cbd6260236376f4a7753f0327da46a1046a96b539e308feb0dfffd34d98bf4b1ad62e97dfbaa458ca2ad
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ == Version 0.3.1
2
+
3
+ * Renamed completion_append_char to complation_append_character (Readline compatibility).
4
+ * Fixed minor bugs in HistoryBuffer (push and << methods are now redefined properly).
5
+ * The string returned by Editor#read (and Rawline#readline) does not contain a trailing newline character anymore (Readline compatibility)
6
+ * It is now possible to set Line#word_separator to an empty string or nil (Readline compatibility).
7
+ * Fixed #6 (Fixnum#ord not defined on Ruby 1.8.6)
8
+ * Fixed #7 (Win32Console gem not required)
9
+ * Fixed #8 (Rawline.version not defined)
10
+ * Fixed #9 (Readline compatibility issues)
11
+
12
+
13
+ == Version 0.3.0
14
+
15
+ * Fixed linux-specific bugs.
16
+ * Added RawLine::Line#words method, which returns the words typed in the current line.
17
+ * undo and redo action now bound to C-u and C-r instead of C-z and C-y to avoid conflicts on Linux.
18
+ * test file and directory renaming.
19
+ * Fixed #1 [#21300] bad requirement because sensitive case error.
20
+ * Fixed #2 [#21301] unitialized constant when no WIN32CONSOLE.
21
+ * Added Ruby 1.9.1 support.
22
+ * Editor#read now takes an optional parameter (default: false) to enable or disable history.
23
+ * Line is no longer added to history when calling Editor#history_back.
24
+ * Added Editor#filename_completion_proc.
25
+ * Editor#completion_proc defaults to Editor#filename_completion_proc.
26
+ * RawLine is now a drop-in replacement for Readline.
27
+ * Added examples/readline_emulation.rb.
28
+ * Moved repository to GitHub.
29
+
30
+ == Version 0.2.0
31
+
32
+ * Added /examples and /test directory to gem.
33
+ * Escape codes can now be used in prompt.
34
+ * It is now possible to use bind(key, &block) with a String as key, even if the corresponding escape sequence is not defined.
35
+ * Added Editor#write_line(string) to print a any string (and "hit return").
36
+ * Library name changed to "RawLine" to avoid name collision issues (Bug 18879: http://rubyforge.org/tracker/?func=detail&aid=18879&group_id=5622&atid=21788).
37
+ * Provided alternative implementation for left and right arrows if terminal
38
+ supports escape sequences (on Windows, it requires the Win32Console gem).
39
+
40
+ == Version 0.1.0
41
+
42
+ First preview release of InLine, implementing some of the functionality provided by the ReadLine library such as basic line editing, history and word completion.
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2008-2013, Fabio Cevasco
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+ - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+
8
+ Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11
+
12
+
data/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = RawLine
2
+
3
+ RawLine was created to provide a 100% Ruby alternative to the ReadLine library, providing some of its most popular features such as:
4
+
5
+ * Basic line editing operations
6
+ * Word completion
7
+ * History Management
8
+ * Custom key/key sequences bindings
9
+
10
+ == Installation
11
+
12
+ The simplest method to install RawLine is to install the gem:
13
+
14
+ gem install rawline
15
+
16
+ == Usage
17
+
18
+ Editor initialization:
19
+
20
+ require 'rawline'
21
+ editor = RawLine::Editor.new
22
+
23
+ Key binding:
24
+
25
+ editor.bind(:ctrl_z) { editor.undo }
26
+ editor.bind(:up_arrow) { editor.history_back }
27
+ editor.bind(:ctrl_x) { puts "Exiting..."; exit }
28
+
29
+ Setup word completion
30
+
31
+ editor.completion_proc = lambda do |word|
32
+ if word
33
+ ['select', 'update', 'delete', 'debug', 'destroy'].find_all { |e| e.match(/^#{Regexp.escape(word)}/) }
34
+ end
35
+ end
36
+ editor.completion_append_string = " "
37
+
38
+ Read input:
39
+
40
+ editor.read("=> ", true)
41
+
42
+ == Replacing Readline
43
+
44
+ Simply include the RawLine (or Rawline) module:
45
+
46
+ include Rawline
47
+
48
+ ...and you'll get:
49
+
50
+ readline(prompt, add_history) # RawLine::Editor#read(prompt, add_history)
51
+ HISTORY # RawLine::Editor#history
52
+ FILENAME_COMPLETION_PROC # Rawline::Editor#filename_completion_proc
53
+ ...
54
+
55
+ but also:
56
+
57
+ Rawline.editor # RawLine::Editor
58
+
59
+ ...which opens a world of endless possibilities! ;-)
60
+
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'highline/system_extensions'
5
+
6
+ include HighLine::SystemExtensions
7
+
8
+ puts "Press a key to view the corresponding ASCII code(s) (or CTRL-X to exit)."
9
+
10
+ loop do
11
+
12
+ print "=> "
13
+ char = get_character.ord rescue nil
14
+ case char
15
+ when ?\C-x.ord then
16
+ puts "Exiting..."; exit;
17
+ else
18
+ puts "#{char.chr} [#{char}] (hex: #{char.to_s(16)})";
19
+ end
20
+
21
+ end
@@ -0,0 +1,28 @@
1
+ #!usr/bin/env ruby
2
+
3
+ require 'irb'
4
+ require 'irb/completion'
5
+ require File.dirname(File.expand_path(__FILE__))+'/../lib/rawline'
6
+
7
+ Rawline.basic_word_break_characters= " \t\n\"\\'`><;|&{("
8
+ Rawline.completion_append_character = nil
9
+ Rawline.completion_proc = IRB::InputCompletor::CompletionProc
10
+
11
+ class RawlineInputMethod < IRB::ReadlineInputMethod
12
+ include Rawline
13
+ def gets
14
+ if l = readline(@prompt, false)
15
+ HISTORY.push(l) if !l.empty?
16
+ @line[@line_no += 1] = l + "\n"
17
+ else
18
+ @eof = true
19
+ l
20
+ end
21
+ end
22
+ end
23
+
24
+ module IRB
25
+ @CONF[:LC_MESSAGES] = Locale.new
26
+ @CONF[:SCRIPT] = RawlineInputMethod.new
27
+ end
28
+ IRB.start
@@ -0,0 +1,27 @@
1
+ #!usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'rush'
5
+ require File.dirname(File.expand_path(__FILE__))+'/../lib/rawline'
6
+
7
+ class RawlineRush < Rush::Shell
8
+
9
+ def initialize
10
+ Rawline.basic_word_break_characters = ""
11
+ Rawline.completion_proc = completion_proc
12
+ super
13
+ end
14
+
15
+ def run
16
+ loop do
17
+ cmd = Rawline.readline('rawline_rush> ')
18
+ finish if cmd.nil? or cmd == 'exit'
19
+ next if cmd == ""
20
+ Rawline::HISTORY.push(cmd)
21
+ execute(cmd)
22
+ end
23
+ end
24
+ end
25
+
26
+ shell = RawlineRush.new.run
27
+
@@ -0,0 +1,189 @@
1
+ #!usr/bin/env ruby
2
+
3
+ require File.dirname(File.expand_path(__FILE__))+'/../lib/rawline'
4
+ require 'io/console'
5
+ require 'term/ansicolor'
6
+
7
+ $z = File.open("/tmp/z.log", "w+")
8
+ $z.sync = true
9
+
10
+ # puts "*** Rawline Editor Test Shell ***"
11
+ # puts " * Press CTRL+X to exit"
12
+ # puts " * Press CTRL+G to clear command history"
13
+ # puts " * Press CTRL+D for line-related information"
14
+ # puts " * Press CTRL+E to view command history"
15
+
16
+ editor = RawLine::Editor.new
17
+ kill_ring = []
18
+
19
+ editor.terminal.keys.merge!(enter: [13])
20
+ editor.bind(:return){ editor.newline }
21
+
22
+ # Move to beginning of line
23
+ editor.bind(:ctrl_a) { editor.move_to_beginning_of_input }
24
+
25
+ # Move to end of line
26
+ editor.bind(:ctrl_e) { editor.move_to_end_of_input }
27
+
28
+ # Move backward one word at a time
29
+ editor.bind(:ctrl_b) {
30
+ text = editor.line.text[0...editor.line.position].reverse
31
+ position = text.index(/\s+/, 1)
32
+ position = position ? (text.length - position) : 0
33
+ editor.move_to_position position
34
+ }
35
+
36
+ # Move forward one word at a time
37
+ editor.bind(:ctrl_f) {
38
+ text = editor.line.text
39
+ position = text.index(/\s+/, editor.line.position)
40
+ position = position ? (position + 1) : text.length
41
+ editor.move_to_position position
42
+ }
43
+
44
+ # Yank text from the kill ring and insert it at the cursor position
45
+ editor.bind(:ctrl_y){
46
+ text = kill_ring[-1]
47
+ if text
48
+ editor.yank_forward text
49
+ end
50
+ }
51
+
52
+ # Backwards delete one word
53
+ editor.bind(:ctrl_w){
54
+ before_text = editor.line.text[0...editor.line.position]
55
+ after_text = editor.line.text[editor.line.position..-1]
56
+
57
+ have_only_seen_whitespace = true
58
+ position = 0
59
+
60
+ before_text.reverse.each_char.with_index do |ch, i|
61
+ if ch =~ /\s/ && !have_only_seen_whitespace
62
+ position = before_text.length - i
63
+ break
64
+ else
65
+ have_only_seen_whitespace = false
66
+ end
67
+ end
68
+
69
+ killed_text = before_text[position...editor.line.position]
70
+ kill_ring.push killed_text
71
+
72
+ text = [before_text.slice(0, position), after_text].join
73
+ editor.overwrite_line text
74
+ editor.move_to_position position
75
+ }
76
+
77
+ # History forward, but if at the end of the history then give user a
78
+ # blank line rather than remain on the last command
79
+ editor.bind(:down_arrow) {
80
+ if editor.history.searching? && !editor.history.end?
81
+ editor.history_forward
82
+ else
83
+ editor.overwrite_line ""
84
+ end
85
+ }
86
+ editor.bind(:up_arrow) { editor.history_back }
87
+
88
+ editor.bind(:enter) { editor.newline }
89
+ editor.bind(:tab) { editor.complete }
90
+ editor.bind(:backspace) { editor.delete_left_character }
91
+
92
+ # Delete to end of line from cursor position
93
+ editor.bind(:ctrl_k) {
94
+ kill_ring.push editor.kill_forward
95
+ }
96
+
97
+ # Delete to beginning of line from cursor position
98
+ editor.bind(:ctrl_u) {
99
+ kill_ring.push editor.line.text[0...editor.line.position]
100
+ editor.overwrite_line editor.line.text[editor.line.position..-1]
101
+ editor.move_to_position 0
102
+ }
103
+
104
+ # Forward delete a character, leaving the cursor in place
105
+ editor.bind("\e[3~") {
106
+ before_text = editor.line.text[0...editor.line.position]
107
+ after_text = editor.line.text[(editor.line.position+1)..-1]
108
+ text = [before_text, after_text].join
109
+ position = editor.line.position
110
+ editor.overwrite_line text
111
+ editor.move_to_position position
112
+ }
113
+
114
+ editor.bind(:ctrl_l){
115
+ editor.clear_screen
116
+ }
117
+
118
+ editor.bind(:ctrl_r) {
119
+ editor.redo
120
+ }
121
+ editor.bind(:left_arrow) { editor.move_left }
122
+ editor.bind(:right_arrow) { editor.move_right }
123
+ editor.bind(:up_arrow) { editor.history_back }
124
+ editor.bind(:down_arrow) { editor.history_forward }
125
+ editor.bind(:delete) { editor.delete_character }
126
+ editor.bind(:insert) { editor.toggle_mode }
127
+
128
+ editor.bind(:ctrl_g) { editor.clear_history }
129
+ # editor.bind(:ctrl_l) { editor.debug_line }
130
+ editor.bind(:ctrl_h) { editor.show_history }
131
+ editor.bind(:ctrl_d) { puts; puts "Exiting..."; exit }
132
+
133
+ # character-search; wraps around as necessary
134
+ editor.bind(:ctrl_n) {
135
+ line = editor.line
136
+ text, start_position = line.text, line.position
137
+ i, new_position = start_position, nil
138
+
139
+ break_on_bytes = [editor.terminal.keys[:ctrl_c]].flatten
140
+ byte = [editor.read_character].flatten.first
141
+
142
+ unless break_on_bytes.include?(byte)
143
+ loop do
144
+ i += 1
145
+ i = 0 if i >= text.length # wrap-around to the beginning
146
+ break if i == start_position # back to where we started
147
+ (editor.move_to_position(i) ; break) if text[i] == byte.chr # found a match; move and break
148
+ end
149
+ end
150
+ }
151
+
152
+ editor.completion_proc = lambda do |word|
153
+ if word
154
+ ['select', 'settle', 'seinfeld', 'sediment', 'selective', 'update', 'delete', 'debug', 'destroy'].find_all { |e| e.match(/^#{Regexp.escape(word)}/) }
155
+ end
156
+ end
157
+
158
+ editor.on_word_complete do |event|
159
+ sub_word = event[:payload][:sub_word]
160
+ word = event[:payload][:word]
161
+ actual_completion = event[:payload][:completion]
162
+ possible_completions = event[:payload][:possible_completions]
163
+
164
+ editor.content_box.content = possible_completions.map do |completion|
165
+ if completion == actual_completion
166
+ Term::ANSIColor.negative(completion)
167
+ else
168
+ completion
169
+ end
170
+ end.join(" ")
171
+ end
172
+
173
+ editor.on_word_complete_no_match do |event|
174
+ sub_word = event[:payload][:sub_word]
175
+ word = event[:payload][:word]
176
+ editor.content_box.content = "Failed to find a match to complete #{sub_word} portion of #{word}"
177
+ end
178
+
179
+ editor.on_read_line do |event|
180
+ line = event[:payload][:line]
181
+ puts "You typed: [#{line}]"
182
+ editor.reset_line
183
+ end
184
+
185
+ editor.on_word_complete_done do |event|
186
+ editor.content_box.content = ""
187
+ end
188
+
189
+ editor.start
@@ -0,0 +1,17 @@
1
+ #!usr/bin/env ruby
2
+
3
+ require File.dirname(File.expand_path(__FILE__))+'/../lib/rawline'
4
+
5
+ puts "*** Readline emulation Test Shell ***"
6
+ puts " * Press CTRL+X to exit"
7
+ puts " * Press <TAB> for file completion"
8
+
9
+ Rawline.editor.bind(:ctrl_x) { puts; puts "Exiting..."; exit }
10
+
11
+ Dir.chdir '..'
12
+
13
+ include Rawline
14
+
15
+ loop do
16
+ puts "You typed: [#{readline("=> ", true)}]"
17
+ end
data/lib/rawline.rb ADDED
@@ -0,0 +1,60 @@
1
+ #!usr/bin/env ruby
2
+
3
+ #
4
+ # RawLine.rb
5
+ #
6
+ # Created by Fabio Cevasco on 2008-03-01.
7
+ # Copyright (c) 2008 Fabio Cevasco. All rights reserved.
8
+ #
9
+ # This is Free Software. See LICENSE for details.
10
+ #
11
+
12
+ require "rubygems"
13
+
14
+ #
15
+ # The RawLine (or Rawline) module can be used in the same way
16
+ # as the Readline one.
17
+ #
18
+ module RawLine
19
+
20
+ def self.rawline_version
21
+ "0.3.2"
22
+ end
23
+
24
+ class BindingException < RuntimeError; end
25
+
26
+ if RUBY_PLATFORM.match(/mswin/i) then
27
+ begin
28
+ require 'win32console'
29
+ def self.win32console?; true; end
30
+ def self.ansi?; true; end
31
+ rescue Exception
32
+ def self.win32console?; false; end
33
+ def self.ansi?; false; end
34
+ end
35
+ else # Unix-like
36
+ def self.ansi?; true; end
37
+ end
38
+ end
39
+
40
+ # Adding Fixnum#ord for Ruby 1.8.6
41
+ class Fixnum; def ord; self; end; end unless Fixnum.method_defined? :ord
42
+
43
+ Rawline = RawLine
44
+
45
+ dir = File.dirname(File.expand_path(__FILE__))
46
+ require "highline"
47
+ require "#{dir}/rawline/terminal"
48
+ require "#{dir}/rawline/terminal/windows_terminal"
49
+ require "#{dir}/rawline/terminal/vt220_terminal"
50
+ require "#{dir}/rawline/history_buffer"
51
+ require "#{dir}/rawline/line"
52
+ require "#{dir}/rawline/prompt"
53
+ require "#{dir}/rawline/completer"
54
+ require "#{dir}/rawline/event_loop"
55
+ require "#{dir}/rawline/event_registry"
56
+ require "#{dir}/rawline/keycode_parser"
57
+ require "#{dir}/rawline/editor"
58
+
59
+ module RawLine
60
+ end