Class: TinyBuff
- Inherits:
-
Object
- Object
- TinyBuff
- Defined in:
- lib/fancy_buff.rb
Overview
a text buffer with marks, selections, and rudimentary editing
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#[](line_range, max_len = nil) ⇒ Object
line_range: the Range of lines to return (NOT line indexes numbers) max_len (optional): the maximum length of each line to return; useful when you want to return a rectangular region of text for display, for example.
-
#delete_at(index) ⇒ Object
index: the indext of the line to delete (NOT the 1-based line number).
-
#delete_range(char_range) ⇒ Object
char_range: the range of characters to remove from this TinyBuff.
-
#initialize(content = '') ⇒ TinyBuff
constructor
content: the starting content of the buffer as a String.
-
#insert_line(line, index) ⇒ Object
line: the line to insert index: the index to insert it at (NOT the 1-based line number).
-
#insert_text(str, char_num) ⇒ Object
str: the String to insert char_num: the char_num at which to start the insertion.
-
#lines ⇒ Object
returns the number of lines.
-
#mark(sym, char_num) ⇒ Object
set a mark, as in the Vim sense.
-
#pop ⇒ Object
deletes and returns the last line of this buffer.
-
#push(line) ⇒ Object
(also: #<<)
line: the line to add to the end of the buffer.
-
#rect(col, row, wid, hgt) ⇒ Object
col: starting column (zero-based) row: starting line (zero-based) wid: the number of characters per line hgt: the number of rows.
-
#select(sym, char_range) ⇒ Object
selects a named range of characters .
-
#shift ⇒ Object
deletes and returns the first line of the buffer.
- #to_s ⇒ Object
-
#unshift(line) ⇒ Object
(also: #>>)
line: the line to be added to the beginning of the buffer.
Constructor Details
#initialize(content = '') ⇒ TinyBuff
content: the starting content of the buffer as a String
6 7 8 9 10 11 12 13 14 |
# File 'lib/fancy_buff.rb', line 6 def initialize(content='') @content = content.lines.map(&:chomp) @marks = {} @selections = {} @length = @content .map{|l| l.length } .sum end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
3 4 5 |
# File 'lib/fancy_buff.rb', line 3 def length @length end |
Instance Method Details
#[](line_range, max_len = nil) ⇒ Object
line_range: the Range of lines to return (NOT line indexes numbers) max_len (optional): the maximum length of each line to return; useful when you want to
return a rectangular region of text for display, for example
24 25 26 |
# File 'lib/fancy_buff.rb', line 24 def [](line_range, max_len=nil) !!max_len ? @content[line_range] : @content[line_range].map{|l| l[..max_len] } end |
#delete_at(index) ⇒ Object
index: the indext of the line to delete (NOT the 1-based line number)
75 76 77 78 |
# File 'lib/fancy_buff.rb', line 75 def delete_at(index) @length -= @content[index].length @content.delete_at(index) end |
#delete_range(char_range) ⇒ Object
char_range: the range of characters to remove from this TinyBuff
81 82 83 |
# File 'lib/fancy_buff.rb', line 81 def delete_range(char_range) # TODO: this deletes the Range of characters, which may span multiple lines end |
#insert_line(line, index) ⇒ Object
line: the line to insert index: the index to insert it at (NOT the 1-based line number)
61 62 63 64 |
# File 'lib/fancy_buff.rb', line 61 def insert_line(line, index) @content.insert(line, index) @length += line.length end |
#insert_text(str, char_num) ⇒ Object
str: the String to insert char_num: the char_num at which to start the insertion
68 69 70 71 72 |
# File 'lib/fancy_buff.rb', line 68 def insert_text(str, char_num) # TODO: this is tricky because if the string contains multiple lines then # you're not just inserting in the middle of an existing line, you're # inserting some text which may include multiple lines end |
#lines ⇒ Object
returns the number of lines
17 18 19 |
# File 'lib/fancy_buff.rb', line 17 def lines @content.length end |
#mark(sym, char_num) ⇒ Object
set a mark, as in the Vim sense
sym: the name of the mark char_num: the number of characters from the top of the buffer to set the
mark
42 43 44 45 46 |
# File 'lib/fancy_buff.rb', line 42 def mark(sym, char_num) @marks[sym] = char_num nil end |
#pop ⇒ Object
deletes and returns the last line of this buffer
95 96 97 98 99 |
# File 'lib/fancy_buff.rb', line 95 def pop @length -= (@content.pop).length nil end |
#push(line) ⇒ Object Also known as: <<
line: the line to add to the end of the buffer
86 87 88 89 90 91 |
# File 'lib/fancy_buff.rb', line 86 def push(line) @content << line @length += line.length nil end |
#rect(col, row, wid, hgt) ⇒ Object
col: starting column (zero-based) row: starting line (zero-based) wid: the number of characters per line hgt: the number of rows
32 33 34 35 |
# File 'lib/fancy_buff.rb', line 32 def rect(col, row, wid, hgt) @content[row..(row + hgt - 1)] .map{|row| row.chomp.chars[col..(col + wid - 1)].join } end |
#select(sym, char_range) ⇒ Object
selects a named range of characters
sym: the name of the range char_range: a Range representing the starting and ending byte of the
selection
53 54 55 56 57 |
# File 'lib/fancy_buff.rb', line 53 def select(sym, char_range) @selections[sym] = char_range nil end |
#shift ⇒ Object
deletes and returns the first line of the buffer
111 112 113 |
# File 'lib/fancy_buff.rb', line 111 def shift @length -= (@content.shift).length end |
#to_s ⇒ Object
115 116 117 |
# File 'lib/fancy_buff.rb', line 115 def to_s @content.join("\n") end |
#unshift(line) ⇒ Object Also known as: >>
line: the line to be added to the beginning of the buffer
102 103 104 105 106 107 |
# File 'lib/fancy_buff.rb', line 102 def unshift(line) @content.unshift(line) @length += line.length nil end |