rawline 0.3.0 → 0.3.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.
@@ -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
- undef <<
25
-
26
- #
27
- # Create an instance of RawLine::HistoryBuffer.
28
- # This method takes an optional block used to override the
29
- # following instance attributes:
30
- # * <tt>@duplicates</tt> - whether or not duplicate items will be stored in the buffer.
31
- # * <tt>@exclude</tt> - a Proc object defining exclusion rules to prevent items from being added to the buffer.
32
- # * <tt>@cycle</tt> - Whether or not the buffer is cyclic.
33
- #
34
- def initialize(size)
35
- @duplicates = true
36
- @exclude = lambda{|a|}
37
- @cycle = false
38
- yield self if block_given?
39
- @size = size
40
- @position = nil
41
- end
42
-
43
- #
44
- # Resize the buffer, resetting <tt>@position</tt> to nil.
45
- #
46
- def resize(new_size)
47
- if new_size < @size
48
- @size-new_size.times { pop }
49
- end
50
- @size = new_size
51
- @position = nil
52
- end
53
-
54
- #
55
- # Clear the content of the buffer and reset <tt>@position</tt> to nil.
56
- #
57
- def empty
58
- @position = nil
59
- clear
60
- end
61
-
62
- #
63
- # Retrieve the element at <tt>@position</tt>.
64
- #
65
- def get
66
- return nil unless length > 0
67
- @position = length-1 unless @position
68
- at @position
69
- end
70
-
71
- #
72
- # Return true if <tt>@position</tt> is at the end of the buffer.
73
- #
74
- def end?
75
- @position == length-1
76
- end
77
-
78
- #
79
- # Return true if <tt>@position</tt> is at the start of the buffer.
80
- #
81
- def start?
82
- @position == 0
83
- end
84
-
85
- #
86
- # Decrement <tt>@position</tt>.
87
- #
88
- def back
89
- return nil unless length > 0
90
- case @position
91
- when nil then
92
- @position = length-1
93
- when 0 then
94
- @position = length-1 if @cycle
95
- else
96
- @position -= 1
97
- end
98
- end
99
-
100
- #
101
- # Increment <tt>@position</tt>.
102
- #
103
- def forward
104
- return nil unless length > 0
105
- case @position
106
- when nil then
107
- @position = length-1
108
- when length-1 then
109
- @position = 0 if @cycle
110
- else
111
- @position += 1
112
- end
113
- end
114
-
115
- #
116
- # Add a new item to the buffer.
117
- #
118
- def <<(item)
119
- delete(item) unless @duplicates
120
- unless @exclude.call(item)
121
- # Remove the oldest element if size is exceeded
122
- if @size <= length
123
- reverse!.pop
124
- reverse!
125
- end
126
- # Add the new item and reset the position
127
- push(item)
128
- @position = nil
129
- end
130
- end
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
- last = @text.index(@word_separator, @position)
63
- first = @text.rindex(@word_separator, @position)
64
- # Trim word separators and handle EOL and BOL
65
- if first then
66
- first +=1
67
- else
68
- first = bol
69
- end
70
- if last then
71
- last -=1
72
- else
73
- last = eol+1 unless last
74
- end
75
- # Swap if overlapping
76
- last, first = first, last if last < first
77
- text = @text[first..last]
78
- # Repeat the search if within word separator
79
- if text.match @word_separator then
80
- last = first
81
- first = @text.rindex(@word_separator, first)
82
- if first then first+=1
83
- else first = bol
84
- end
85
- text = @text[first..last]
86
- end
87
- {:start => first, :end => last, :text => text}
88
- end
89
-
90
- #
91
- # Return an array containing the words present in the current line
92
- #
93
- def words
94
- @text.split @word_separator
95
- end
96
-
97
- #
98
- # Return the position corresponding to the beginning of the line.
99
- #
100
- def bol
101
- 0
102
- end
103
-
104
- #
105
- # Return true if the cursor is at the beginning of the line.
106
- #
107
- def bol?
108
- @position<=bol
109
- end
110
-
111
- #
112
- # Return the position corresponding to the end of the line.
113
- #
114
- def eol
115
- @text.length-1
116
- end
117
-
118
- #
119
- # Return true if the cursor is at the end of the line.
120
- #
121
- def eol?
122
- @position>=eol
123
- end
124
-
125
- #
126
- # Decrement the line position by <tt>offset</tt>
127
- #
128
- def left(offset=1)
129
- @position = (@position-offset <= 0) ? 0 : @position-offset
130
- end
131
-
132
- #
133
- # Increment the line position by <tt>offset</tt>
134
- #
135
- def right(offset=1)
136
- @position = (@position+offset >= max_length) ? max_length : @position+offset
137
- end
138
-
139
- #
140
- # Add a character (expressed as a character code) to the line text.
141
- #
142
- def <<(char)
143
- @text << char.chr
144
- end
145
-
146
- #
147
- # Access the line text at <tt>@index</tt>
148
- #
149
- def [](index)
150
- @text[index]
151
- end
152
-
153
- #
154
- # Modify the character(s) in the line text at <tt>@index</tt>
155
- #
156
- def []=(index, chars)
157
- @text[index] = chars
158
- end
159
-
160
- #
161
- # Return the length of the line text.
162
- #
163
- def length
164
- @text.length
165
- end
166
-
167
- end
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