rawline 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +42 -30
- data/examples/rawline_irb.rb +27 -0
- data/examples/rawline_rush.rb +27 -0
- data/examples/rawline_shell.rb +26 -26
- data/examples/readline_emulation.rb +17 -17
- data/lib/rawline/editor.rb +705 -704
- data/lib/rawline/history_buffer.rb +134 -134
- data/lib/rawline/line.rb +169 -168
- data/lib/rawline.rb +109 -119
- data/spec/editor_spec.rb +167 -167
- metadata +4 -2
@@ -1,134 +1,134 @@
|
|
1
|
-
#!usr/bin/env ruby
|
2
|
-
|
3
|
-
#
|
4
|
-
# history_buffer.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
|
-
#
|
13
|
-
module RawLine
|
14
|
-
|
15
|
-
#
|
16
|
-
# The HistoryBuffer class is used to hold the editor and line histories, as well
|
17
|
-
# as word completion matches.
|
18
|
-
#
|
19
|
-
class HistoryBuffer < Array
|
20
|
-
|
21
|
-
attr_reader :position, :size
|
22
|
-
attr_accessor :duplicates, :exclude, :cycle
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# * <tt>@
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@
|
36
|
-
|
37
|
-
@
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
#
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
#
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
when
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
when
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
#
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
1
|
+
#!usr/bin/env ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# history_buffer.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
|
+
#
|
13
|
+
module RawLine
|
14
|
+
|
15
|
+
#
|
16
|
+
# The HistoryBuffer class is used to hold the editor and line histories, as well
|
17
|
+
# as word completion matches.
|
18
|
+
#
|
19
|
+
class HistoryBuffer < Array
|
20
|
+
|
21
|
+
attr_reader :position, :size
|
22
|
+
attr_accessor :duplicates, :exclude, :cycle
|
23
|
+
|
24
|
+
#
|
25
|
+
# Create an instance of RawLine::HistoryBuffer.
|
26
|
+
# This method takes an optional block used to override the
|
27
|
+
# following instance attributes:
|
28
|
+
# * <tt>@duplicates</tt> - whether or not duplicate items will be stored in the buffer.
|
29
|
+
# * <tt>@exclude</tt> - a Proc object defining exclusion rules to prevent items from being added to the buffer.
|
30
|
+
# * <tt>@cycle</tt> - Whether or not the buffer is cyclic.
|
31
|
+
#
|
32
|
+
def initialize(size)
|
33
|
+
@duplicates = true
|
34
|
+
@exclude = lambda{|a|}
|
35
|
+
@cycle = false
|
36
|
+
yield self if block_given?
|
37
|
+
@size = size
|
38
|
+
@position = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Resize the buffer, resetting <tt>@position</tt> to nil.
|
43
|
+
#
|
44
|
+
def resize(new_size)
|
45
|
+
if new_size < @size
|
46
|
+
@size-new_size.times { pop }
|
47
|
+
end
|
48
|
+
@size = new_size
|
49
|
+
@position = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Clear the content of the buffer and reset <tt>@position</tt> to nil.
|
54
|
+
#
|
55
|
+
def empty
|
56
|
+
@position = nil
|
57
|
+
clear
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Retrieve the element at <tt>@position</tt>.
|
62
|
+
#
|
63
|
+
def get
|
64
|
+
return nil unless length > 0
|
65
|
+
@position = length-1 unless @position
|
66
|
+
at @position
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Return true if <tt>@position</tt> is at the end of the buffer.
|
71
|
+
#
|
72
|
+
def end?
|
73
|
+
@position == length-1
|
74
|
+
end
|
75
|
+
|
76
|
+
#
|
77
|
+
# Return true if <tt>@position</tt> is at the start of the buffer.
|
78
|
+
#
|
79
|
+
def start?
|
80
|
+
@position == 0
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Decrement <tt>@position</tt>.
|
85
|
+
#
|
86
|
+
def back
|
87
|
+
return nil unless length > 0
|
88
|
+
case @position
|
89
|
+
when nil then
|
90
|
+
@position = length-1
|
91
|
+
when 0 then
|
92
|
+
@position = length-1 if @cycle
|
93
|
+
else
|
94
|
+
@position -= 1
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Increment <tt>@position</tt>.
|
100
|
+
#
|
101
|
+
def forward
|
102
|
+
return nil unless length > 0
|
103
|
+
case @position
|
104
|
+
when nil then
|
105
|
+
@position = length-1
|
106
|
+
when length-1 then
|
107
|
+
@position = 0 if @cycle
|
108
|
+
else
|
109
|
+
@position += 1
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# Add a new item to the buffer.
|
115
|
+
#
|
116
|
+
def push(item)
|
117
|
+
delete(item) unless @duplicates
|
118
|
+
unless @exclude.call(item)
|
119
|
+
# Remove the oldest element if size is exceeded
|
120
|
+
if @size <= length
|
121
|
+
reverse!.pop
|
122
|
+
reverse!
|
123
|
+
end
|
124
|
+
# Add the new item and reset the position
|
125
|
+
super(item)
|
126
|
+
@position = nil
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
alias << push
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
data/lib/rawline/line.rb
CHANGED
@@ -1,168 +1,169 @@
|
|
1
|
-
#!usr/bin/env ruby
|
2
|
-
|
3
|
-
#
|
4
|
-
# line.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
|
-
module RawLine
|
13
|
-
|
14
|
-
#
|
15
|
-
# The Line class is used to represent the current line being processed and edited
|
16
|
-
# by RawLine::Editor. It keeps track of the characters typed, the cursor position,
|
17
|
-
# the current word and maintains an internal history to allow undos and redos.
|
18
|
-
#
|
19
|
-
class Line
|
20
|
-
|
21
|
-
attr_accessor :text, :position, :history, :prompt, :history_size, :word_separator
|
22
|
-
attr_reader :offset
|
23
|
-
|
24
|
-
include HighLine::SystemExtensions
|
25
|
-
|
26
|
-
#
|
27
|
-
# Create an instance of RawLine::Line.
|
28
|
-
# This method takes an optional block used to override the
|
29
|
-
# following instance attributes:
|
30
|
-
# * <tt>@text</tt> - the line text.
|
31
|
-
# * <tt>@history_size</tt> - the size of the line history buffer.
|
32
|
-
# * <tt>@position</tt> - the current cursor position within the line.
|
33
|
-
# * <tt>@prompt</tt> - a prompt to prepend to the line text.
|
34
|
-
#
|
35
|
-
def initialize(history_size)
|
36
|
-
@text = ""
|
37
|
-
@history_size = history_size
|
38
|
-
@position = 0
|
39
|
-
@prompt = ""
|
40
|
-
@word_separator = ' '
|
41
|
-
yield self if block_given?
|
42
|
-
@history = RawLine::HistoryBuffer.new(@history_size)
|
43
|
-
@history << "" # Add empty line for complete undo...
|
44
|
-
@offset = @prompt.length
|
45
|
-
end
|
46
|
-
|
47
|
-
#
|
48
|
-
# Return the maximum line length. By default, it corresponds to the terminal's
|
49
|
-
# width minus the length of the line prompt.
|
50
|
-
#
|
51
|
-
def max_length
|
52
|
-
terminal_size[0]-@offset
|
53
|
-
end
|
54
|
-
|
55
|
-
#
|
56
|
-
# Return information about the current word, as a Hash composed by the following
|
57
|
-
# elements:
|
58
|
-
# * <tt>:start</tt>: The position in the line corresponding to the word start
|
59
|
-
# * <tt>:end</tt>: The position in the line corresponding to the word end
|
60
|
-
# * <tt>:text</tt>: The word text.
|
61
|
-
def word
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
if
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
#
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
#
|
99
|
-
#
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#
|
106
|
-
#
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
#
|
113
|
-
#
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#
|
120
|
-
#
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
#
|
127
|
-
#
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
#
|
134
|
-
#
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
#
|
141
|
-
#
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
#
|
148
|
-
#
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
#
|
155
|
-
#
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
#
|
162
|
-
#
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
end
|
1
|
+
#!usr/bin/env ruby
|
2
|
+
|
3
|
+
#
|
4
|
+
# line.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
|
+
module RawLine
|
13
|
+
|
14
|
+
#
|
15
|
+
# The Line class is used to represent the current line being processed and edited
|
16
|
+
# by RawLine::Editor. It keeps track of the characters typed, the cursor position,
|
17
|
+
# the current word and maintains an internal history to allow undos and redos.
|
18
|
+
#
|
19
|
+
class Line
|
20
|
+
|
21
|
+
attr_accessor :text, :position, :history, :prompt, :history_size, :word_separator
|
22
|
+
attr_reader :offset
|
23
|
+
|
24
|
+
include HighLine::SystemExtensions
|
25
|
+
|
26
|
+
#
|
27
|
+
# Create an instance of RawLine::Line.
|
28
|
+
# This method takes an optional block used to override the
|
29
|
+
# following instance attributes:
|
30
|
+
# * <tt>@text</tt> - the line text.
|
31
|
+
# * <tt>@history_size</tt> - the size of the line history buffer.
|
32
|
+
# * <tt>@position</tt> - the current cursor position within the line.
|
33
|
+
# * <tt>@prompt</tt> - a prompt to prepend to the line text.
|
34
|
+
#
|
35
|
+
def initialize(history_size)
|
36
|
+
@text = ""
|
37
|
+
@history_size = history_size
|
38
|
+
@position = 0
|
39
|
+
@prompt = ""
|
40
|
+
@word_separator = ' '
|
41
|
+
yield self if block_given?
|
42
|
+
@history = RawLine::HistoryBuffer.new(@history_size)
|
43
|
+
@history << "" # Add empty line for complete undo...
|
44
|
+
@offset = @prompt.length
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Return the maximum line length. By default, it corresponds to the terminal's
|
49
|
+
# width minus the length of the line prompt.
|
50
|
+
#
|
51
|
+
def max_length
|
52
|
+
terminal_size[0]-@offset
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Return information about the current word, as a Hash composed by the following
|
57
|
+
# elements:
|
58
|
+
# * <tt>:start</tt>: The position in the line corresponding to the word start
|
59
|
+
# * <tt>:end</tt>: The position in the line corresponding to the word end
|
60
|
+
# * <tt>:text</tt>: The word text.
|
61
|
+
def word
|
62
|
+
return {:start => bol, :end => eol+1, :text => @text} if @word_separator.to_s == ''
|
63
|
+
last = @text.index(@word_separator, @position)
|
64
|
+
first = @text.rindex(@word_separator, @position)
|
65
|
+
# Trim word separators and handle EOL and BOL
|
66
|
+
if first then
|
67
|
+
first +=1
|
68
|
+
else
|
69
|
+
first = bol
|
70
|
+
end
|
71
|
+
if last then
|
72
|
+
last -=1
|
73
|
+
else
|
74
|
+
last = eol+1 unless last
|
75
|
+
end
|
76
|
+
# Swap if overlapping
|
77
|
+
last, first = first, last if last < first
|
78
|
+
text = @text[first..last].to_s
|
79
|
+
# Repeat the search if within word separator
|
80
|
+
if text.match @word_separator then
|
81
|
+
last = first
|
82
|
+
first = @text.rindex(@word_separator, first)
|
83
|
+
if first then first+=1
|
84
|
+
else first = bol
|
85
|
+
end
|
86
|
+
text = @text[first..last]
|
87
|
+
end
|
88
|
+
{:start => first, :end => last, :text => text}
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# Return an array containing the words present in the current line
|
93
|
+
#
|
94
|
+
def words
|
95
|
+
@text.split @word_separator
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Return the position corresponding to the beginning of the line.
|
100
|
+
#
|
101
|
+
def bol
|
102
|
+
0
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# Return true if the cursor is at the beginning of the line.
|
107
|
+
#
|
108
|
+
def bol?
|
109
|
+
@position<=bol
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
# Return the position corresponding to the end of the line.
|
114
|
+
#
|
115
|
+
def eol
|
116
|
+
@text.length-1
|
117
|
+
end
|
118
|
+
|
119
|
+
#
|
120
|
+
# Return true if the cursor is at the end of the line.
|
121
|
+
#
|
122
|
+
def eol?
|
123
|
+
@position>=eol
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Decrement the line position by <tt>offset</tt>
|
128
|
+
#
|
129
|
+
def left(offset=1)
|
130
|
+
@position = (@position-offset <= 0) ? 0 : @position-offset
|
131
|
+
end
|
132
|
+
|
133
|
+
#
|
134
|
+
# Increment the line position by <tt>offset</tt>
|
135
|
+
#
|
136
|
+
def right(offset=1)
|
137
|
+
@position = (@position+offset >= max_length) ? max_length : @position+offset
|
138
|
+
end
|
139
|
+
|
140
|
+
#
|
141
|
+
# Add a character (expressed as a character code) to the line text.
|
142
|
+
#
|
143
|
+
def <<(char)
|
144
|
+
@text << char.chr
|
145
|
+
end
|
146
|
+
|
147
|
+
#
|
148
|
+
# Access the line text at <tt>@index</tt>
|
149
|
+
#
|
150
|
+
def [](index)
|
151
|
+
@text[index]
|
152
|
+
end
|
153
|
+
|
154
|
+
#
|
155
|
+
# Modify the character(s) in the line text at <tt>@index</tt>
|
156
|
+
#
|
157
|
+
def []=(index, chars)
|
158
|
+
@text[index] = chars
|
159
|
+
end
|
160
|
+
|
161
|
+
#
|
162
|
+
# Return the length of the line text.
|
163
|
+
#
|
164
|
+
def length
|
165
|
+
@text.length
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
end
|