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 = ANSIString.new("")
  @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.



147
148
149
# File 'lib/rawline/line.rb', line 147

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

#[](index) ⇒ Object

Access the line text at @index



154
155
156
# File 'lib/rawline/line.rb', line 154

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

#[]=(index, chars) ⇒ Object

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



161
162
163
# File 'lib/rawline/line.rb', line 161

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

#bolObject

Return the position corresponding to the beginning of the line.



105
106
107
# File 'lib/rawline/line.rb', line 105

def bol
 0
end

#bol?Boolean

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/rawline/line.rb', line 112

def bol?
  @position<=bol
end

#eolObject

Return the position corresponding to the end of the line.



119
120
121
# File 'lib/rawline/line.rb', line 119

def eol
  @text.length-1
end

#eol?Boolean

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

Returns:

  • (Boolean)


126
127
128
# File 'lib/rawline/line.rb', line 126

def eol?
  @position>=eol
end

#left(offset = 1) ⇒ Object

Decrement the line position by offset



133
134
135
# File 'lib/rawline/line.rb', line 133

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

#lengthObject

Return the length of the line text.



168
169
170
# File 'lib/rawline/line.rb', line 168

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



140
141
142
# File 'lib/rawline/line.rb', line 140

def right(offset=1)
  @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.



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

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



98
99
100
# File 'lib/rawline/line.rb', line 98

def words
  @text.split @word_separator
end