Class: TreeCursor
Instance Method Summary collapse
- #<<(lines) ⇒ Object
- #[](index) ⇒ Object
- #at_leaf? ⇒ Boolean
- #each(&block) ⇒ Object
- #i ⇒ Object
- #i=(num) ⇒ Object
- #index_after ⇒ Object
-
#initialize(txt, i = 0) ⇒ TreeCursor
constructor
A new instance of TreeCursor.
- #inspect ⇒ Object
- #length ⇒ Object
- #line ⇒ Object
- #select(line) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #txt ⇒ Object
- #under ⇒ Object
Constructor Details
#initialize(txt, i = 0) ⇒ TreeCursor
Returns a new instance of TreeCursor.
5 6 7 8 9 |
# File 'lib/xiki/tree_cursor.rb', line 5 def initialize txt, i=0 raise "1st parameter to TreeCursor.initialize must be a string" unless txt.is_a? String @i = i @txt = txt end |
Instance Method Details
#<<(lines) ⇒ Object
82 83 84 |
# File 'lib/xiki/tree_cursor.rb', line 82 def << lines @txt.insert index_after+1, lines end |
#[](index) ⇒ Object
53 54 55 |
# File 'lib/xiki/tree_cursor.rb', line 53 def [] index to_a[index] end |
#at_leaf? ⇒ Boolean
43 44 45 46 47 48 49 50 51 |
# File 'lib/xiki/tree_cursor.rb', line 43 def at_leaf? a = to_a following_line = a[@i+1] return true if following_line.nil? indent = Line.indent a[@i] following_indent = Line.indent a[@i+1] indent.length >= following_indent.length end |
#each(&block) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/xiki/tree_cursor.rb', line 34 def each &block @i = 0 while @i < length # while @i < @txt.split("\n").length block.call @i += 1 end end |
#i ⇒ Object
18 |
# File 'lib/xiki/tree_cursor.rb', line 18 def i; @i; end |
#i=(num) ⇒ Object
19 |
# File 'lib/xiki/tree_cursor.rb', line 19 def i= num; @i = num; end |
#index_after ⇒ Object
78 79 80 |
# File 'lib/xiki/tree_cursor.rb', line 78 def index_after to_a[0..@i].join("\n").length end |
#inspect ⇒ Object
14 15 16 |
# File 'lib/xiki/tree_cursor.rb', line 14 def inspect to_s end |
#length ⇒ Object
26 27 28 |
# File 'lib/xiki/tree_cursor.rb', line 26 def length @txt.strip.count("\n") + 1 end |
#line ⇒ Object
30 31 32 |
# File 'lib/xiki/tree_cursor.rb', line 30 def line to_a[@i] end |
#select(line) ⇒ Object
57 58 59 60 61 |
# File 'lib/xiki/tree_cursor.rb', line 57 def select line line = line.sub /^( *)\+ /, "\\1- " index = to_a.index{|o| o.sub(/^( *)\+ /, "\\1- ") == line} @i = index if index end |
#to_a ⇒ Object
22 23 24 |
# File 'lib/xiki/tree_cursor.rb', line 22 def to_a @txt.strip.split("\n", -1) end |
#to_s ⇒ Object
11 12 13 |
# File 'lib/xiki/tree_cursor.rb', line 11 def to_s "[#{@i}, #{@txt.inspect}]" end |
#txt ⇒ Object
20 |
# File 'lib/xiki/tree_cursor.rb', line 20 def txt; @txt; end |
#under ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/xiki/tree_cursor.rb', line 63 def under result = "" a = to_a target_indent = Line.indent(a[@i]).length i = @i + 1 while i < length indent = Line.indent(a[i]).length break if indent <= target_indent result << "#{a[i]}\n" i += 1 end result end |