Class: RawLine::Line

Inherits:
Object
  • Object
show all
Includes:
HighLine::SystemExtensions
Defined in:
lib/rawline/line.rb

Overview

The Line class is used to represent the current line being processed and edited by RawLine::Editor. It keeps track of the characters typed, the cursor position, the current word and maintains an internal history to allow undos and redos.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(history_size) {|_self| ... } ⇒ Line

Create an instance of RawLine::Line. This method takes an optional block used to override the following instance attributes:

  • @text - the line text.

  • @history_size - the size of the line history buffer.

  • @position - the current cursor position within the line.

  • @prompt - a prompt to prepend to the line text.

Yields:

  • (_self)

Yield Parameters:

  • _self (RawLine::Line)

    the object that the method was called on



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rawline/line.rb', line 35

def initialize(history_size)
	@text = ""
	@history_size = history_size
	@position = 0
	@prompt = ""
	@word_separator = ' '
	yield self if block_given?
	@history = RawLine::HistoryBuffer.new(@history_size)
	@history << "" # Add empty line for complete undo...
	@offset = @prompt.length
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



21
22
23
# File 'lib/rawline/line.rb', line 21

def history
  @history
end

#history_sizeObject

Returns the value of attribute history_size.



21
22
23
# File 'lib/rawline/line.rb', line 21

def history_size
  @history_size
end

#offsetObject (readonly)

Returns the value of attribute offset.



22
23
24
# File 'lib/rawline/line.rb', line 22

def offset
  @offset
end

#positionObject

Returns the value of attribute position.



21
22
23
# File 'lib/rawline/line.rb', line 21

def position
  @position
end

#promptObject

Returns the value of attribute prompt.



21
22
23
# File 'lib/rawline/line.rb', line 21

def prompt
  @prompt
end

#textObject

Returns the value of attribute text.



21
22
23
# File 'lib/rawline/line.rb', line 21

def text
  @text
end

#word_separatorObject

Returns the value of attribute word_separator.



21
22
23
# File 'lib/rawline/line.rb', line 21

def word_separator
  @word_separator
end

Instance Method Details

#<<(char) ⇒ Object

Add a character (expressed as a character code) to the line text.



143
144
145
# File 'lib/rawline/line.rb', line 143

def <<(char)
	@text << char.chr
end

#[](index) ⇒ Object

Access the line text at @index



150
151
152
# File 'lib/rawline/line.rb', line 150

def [](index)
	@text[index]
end

#[]=(index, chars) ⇒ Object

Modify the character(s) in the line text at @index



157
158
159
# File 'lib/rawline/line.rb', line 157

def []=(index, chars)
	@text[index] = chars
end

#bolObject

Return the position corresponding to the beginning of the line.



101
102
103
# File 'lib/rawline/line.rb', line 101

def bol
 0	
end

#bol?Boolean

Return true if the cursor is at the beginning of the line.

Returns:

  • (Boolean)


108
109
110
# File 'lib/rawline/line.rb', line 108

def bol?
	@position<=bol
end

#eolObject

Return the position corresponding to the end of the line.



115
116
117
# File 'lib/rawline/line.rb', line 115

def eol
	@text.length-1
end

#eol?Boolean

Return true if the cursor is at the end of the line.

Returns:

  • (Boolean)


122
123
124
# File 'lib/rawline/line.rb', line 122

def eol?
	@position>=eol
end

#left(offset = 1) ⇒ Object

Decrement the line position by offset



129
130
131
# File 'lib/rawline/line.rb', line 129

def left(offset=1)
	@position = (@position-offset <= 0) ? 0 : @position-offset
end

#lengthObject

Return the length of the line text.



164
165
166
# File 'lib/rawline/line.rb', line 164

def length
	@text.length
end

#max_lengthObject

Return the maximum line length. By default, it corresponds to the terminal’s width minus the length of the line prompt.



51
52
53
# File 'lib/rawline/line.rb', line 51

def max_length
	terminal_size[0]-@offset
end

#right(offset = 1) ⇒ Object

Increment the line position by offset



136
137
138
# File 'lib/rawline/line.rb', line 136

def right(offset=1)
	@position = (@position+offset >= max_length) ? max_length : @position+offset
end

#wordObject

Return information about the current word, as a Hash composed by the following elements:

  • :start: The position in the line corresponding to the word start

  • :end: The position in the line corresponding to the word end

  • :text: The word text.



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
# File 'lib/rawline/line.rb', line 61

def word
	return {:start => bol, :end => eol+1, :text => @text} if @word_separator.to_s == ''
	last = @text.index(@word_separator, @position)
	first = @text.rindex(@word_separator, @position)
	# Trim word separators and handle EOL and BOL
	if first then
	 	first +=1 
	else 
		first = bol
	end
	if last then
	 	last -=1 
	else 
		last = eol+1 unless last
	end
	# Swap if overlapping
	last, first = first, last if last < first
	text = @text[first..last].to_s
	# Repeat the search if within word separator
	if text.match @word_separator then
		last = first
		first = @text.rindex(@word_separator, first)
	 	if first then first+=1
		else first =	bol 
		end
		text = @text[first..last]
	end
	{:start => first, :end => last, :text => text}
end

#wordsObject

Return an array containing the words present in the current line



94
95
96
# File 'lib/rawline/line.rb', line 94

def words
	@text.split @word_separator
end