fancy_buff 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/fancy_buff.rb +118 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 693126b181dc6a997dd15d46158a437d31e3957c88fd314edf557c16c905a36e
4
+ data.tar.gz: 5f88475a875d86e13192b397f35c7970cfcb11a12390315114dedfe38d82d13b
5
+ SHA512:
6
+ metadata.gz: fd662a50783b6a3af23bdbd70f3065952a7d1faac12af6208a699ee53527d26d60f5958fcf9b045e6ba0fb196744db151a38395966332aee70ae8eb383d30828
7
+ data.tar.gz: befd4e7240600a0001697c647eaef1efef60a0635cc5db27988b580ad0930d3538de02d4f6a0390476c444753d29b12c63d47919d7d7ee0869933e42e37d274f
data/lib/fancy_buff.rb ADDED
@@ -0,0 +1,118 @@
1
+ # a text buffer with marks, selections, and rudimentary editing
2
+ class TinyBuff
3
+ attr_reader :length
4
+
5
+ # content: the starting content of the buffer as a String
6
+ def initialize(content='')
7
+ @content = content.lines.map(&:chomp)
8
+ @marks = {}
9
+ @selections = {}
10
+
11
+ @length = @content
12
+ .map{|l| l.length }
13
+ .sum
14
+ end
15
+
16
+ # returns the number of lines
17
+ def lines
18
+ @content.length
19
+ end
20
+
21
+ # line_range: the Range of lines to return (NOT line indexes numbers)
22
+ # max_len (optional): the maximum length of each line to return; useful when you want to
23
+ # return a rectangular region of text for display, for example
24
+ def [](line_range, max_len=nil)
25
+ !!max_len ? @content[line_range] : @content[line_range].map{|l| l[..max_len] }
26
+ end
27
+
28
+ # col: starting column (zero-based)
29
+ # row: starting line (zero-based)
30
+ # wid: the number of characters per line
31
+ # hgt: the number of rows
32
+ def rect(col, row, wid, hgt)
33
+ @content[row..(row + hgt - 1)]
34
+ .map{|row| row.chomp.chars[col..(col + wid - 1)].join }
35
+ end
36
+
37
+ # set a mark, as in the Vim sense
38
+ #
39
+ # sym: the name of the mark
40
+ # char_num: the number of characters from the top of the buffer to set the
41
+ # mark
42
+ def mark(sym, char_num)
43
+ @marks[sym] = char_num
44
+
45
+ nil
46
+ end
47
+
48
+ # selects a named range of characters
49
+ #
50
+ # sym: the name of the range
51
+ # char_range: a Range representing the starting and ending byte of the
52
+ # selection
53
+ def select(sym, char_range)
54
+ @selections[sym] = char_range
55
+
56
+ nil
57
+ end
58
+
59
+ # line: the line to insert
60
+ # index: the index to insert it at (NOT the 1-based line number)
61
+ def insert_line(line, index)
62
+ @content.insert(line, index)
63
+ @length += line.length
64
+ end
65
+
66
+ # str: the String to insert
67
+ # char_num: the char_num at which to start the insertion
68
+ def insert_text(str, char_num)
69
+ # TODO: this is tricky because if the string contains multiple lines then
70
+ # you're not just inserting in the middle of an existing line, you're
71
+ # inserting some text which may include multiple lines
72
+ end
73
+
74
+ # index: the indext of the line to delete (NOT the 1-based line number)
75
+ def delete_at(index)
76
+ @length -= @content[index].length
77
+ @content.delete_at(index)
78
+ end
79
+
80
+ # char_range: the range of characters to remove from this TinyBuff
81
+ def delete_range(char_range)
82
+ # TODO: this deletes the Range of characters, which may span multiple lines
83
+ end
84
+
85
+ # line: the line to add to the end of the buffer
86
+ def push(line)
87
+ @content << line
88
+ @length += line.length
89
+
90
+ nil
91
+ end
92
+ alias << push
93
+
94
+ # deletes and returns the last line of this buffer
95
+ def pop
96
+ @length -= (@content.pop).length
97
+
98
+ nil
99
+ end
100
+
101
+ # line: the line to be added to the beginning of the buffer
102
+ def unshift(line)
103
+ @content.unshift(line)
104
+ @length += line.length
105
+
106
+ nil
107
+ end
108
+ alias >> unshift
109
+
110
+ # deletes and returns the first line of the buffer
111
+ def shift
112
+ @length -= (@content.shift).length
113
+ end
114
+
115
+ def to_s
116
+ @content.join("\n")
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fancy_buff
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a text buffer with marks, selections, and simple insert/delete
14
+ email: jefflunt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/fancy_buff.rb
20
+ homepage: https://github.com/jefflunt/fancy_buff
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.7
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: want to have a small line-based text buffer with named marks and selections,
43
+ very rudimentary editing, but absolutely nothing else? then this library is for
44
+ you.
45
+ test_files: []