fancy_buff 2.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fancy_buff.rb +75 -24
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8121c289daee5a5f2aabbcae53048661536428bfc90b8fbca1f49f2722dea5c
4
- data.tar.gz: ce3507432e0816fb69634ec64086c03543ae79908ebd5aef7c3749119750cdb3
3
+ metadata.gz: 24ca22ff6ce4b91274d9158e71ddb51dbf78ed644bdc4eb0affc665e8fc7b9a9
4
+ data.tar.gz: 8027d3a0e02558e96a7f12c4746b6f9b9ca8b3e896c121d64fb1e841e08da65a
5
5
  SHA512:
6
- metadata.gz: 39155c9822923ebf3a33f676a66eafcc86004d22c760f1d116e8da0c0f80de1c76119753374b7ec8a84933263da1a8c56b96741f8366bbcad86a629f21a2c9ff
7
- data.tar.gz: 0171ca4fa6000da425da34143c1479352c6410e4ffb22ea676893858ac3ec1294ba8bf20491a70b8d76d3959b18e7201ae5d9968059b4e09fa2a1098b14f01f8
6
+ metadata.gz: 7c9f91c3e37fded5bb22351c276a47d2d0baa03e516d1eaa7bbc1ebe1ae85b48e00610e7ef3c75d04ffa11d5907bf7e1fcfc82be1d34b3bca987a55dd93c8340
7
+ data.tar.gz: c707b62adb80e8506aaaf03d67eb7913caeb98ca03faa8cd9a6839e9b8dabf7d70fa9e83d2b6d6a736195bf26ff81ff2d1b3d9f35df0dfd3d5902c625fca29e1
data/lib/fancy_buff.rb CHANGED
@@ -4,14 +4,13 @@ class FancyBuff
4
4
  :bytes,
5
5
  :lines,
6
6
  :length,
7
+ :line_no_width,
7
8
  :max_char_width,
9
+ :win,
10
+ :caret,
8
11
  :marks,
9
12
  :selections
10
13
 
11
- # allows the consuming application to set the window size, since that
12
- # application is probably mananging the other buffers in use
13
- attr_accessor :win
14
-
15
14
  # gives you a default, empty, zero slice
16
15
  #
17
16
  # formatter - a Rouge formatter
@@ -19,26 +18,66 @@ class FancyBuff
19
18
  def initialize(formatter, lexer)
20
19
  @formatter = formatter
21
20
  @lexer = lexer
21
+ @win = [0, 0, 0, 0] # the default slice is at the beginning of the buffer, and has a zero size
22
+ @caret = [c, r]
22
23
 
23
24
  # size tracking
24
25
  @chars = 0 # the number of characters in the buffer (not the same as the number of bytes)
25
26
  @bytes = 0 # the number of bytes in the buffer (not the same as the number of characters)
26
27
  @lines = []
28
+ @line_no_width = 1
29
+ @rendered_lines = [] # fully formatted, syntax highlighted, and transformed
30
+ @edited_since_last_render = true
27
31
  @max_char_width = 0
28
32
  @all_buff = @lines.join("\n")
29
33
 
30
34
  @marks = {}
31
35
  @selections = {}
32
- @win = [0, 0, 0, 0] # the default slice is at the beginning of the buffer, and has a zero size
33
36
  end
34
37
 
35
- # index of first visible row
36
- def r
37
- @win[0]
38
+ # move the window coordinates, and possibly the caret as well, if moving the
39
+ # window would cause the caret to be out-of-bounds
40
+ def win=(coords)
41
+ @win = coords
42
+
43
+ reset_caret!
44
+ end
45
+
46
+ def reset_caret!
47
+ cx, cy = @caret
48
+ new_cx = if cx < c
49
+ c
50
+ elsif cx > c + w
51
+ w
52
+ else
53
+ cx
54
+ end
55
+
56
+ new_cy = if cy < r
57
+ r
58
+ elsif cy > (r + h - 1)
59
+ r + h - 1
60
+ else
61
+ cy
62
+ end
63
+
64
+ @caret = [new_cx, new_cy]
65
+ end
66
+
67
+ def visual_caret
68
+ [
69
+ caret[0] - c + line_no_width + 1, # '+ 1' is to account for a space between line numbers and caret
70
+ caret[1] - r
71
+ ]
38
72
  end
39
73
 
40
74
  # index of first visible column
41
75
  def c
76
+ @win[0]
77
+ end
78
+
79
+ # index of first visible row
80
+ def r
42
81
  @win[1]
43
82
  end
44
83
 
@@ -55,16 +94,23 @@ class FancyBuff
55
94
  # returns an array of strings representing the visible characters from this
56
95
  # FancyBuffer's @rect
57
96
  def win_s
58
- r, c, w, h = @win
59
-
60
97
  return [] if h == 0 || w == 0
61
98
 
62
- line_no_width = @lines.length.to_s.length
63
- text = @formatter
64
- .format(@lexer.lex(@lines.join("\n")))
65
- .lines[r..(r + visible_lines - 1)]
66
- .map.with_index{|row, i| "#{(i + r + 1).to_s.rjust(line_no_width)} #{substr_with_color(row, c, c + w - line_no_width - 2)}" }
67
- .map{|l| l.chomp + "\e[0K" } +
99
+ @line_no_width = @lines.length.to_s.length
100
+ if @edited_since_last_render
101
+ @rendered_lines = @formatter
102
+ .format(@lexer.lex(@lines.join("\n")))
103
+ .lines
104
+ .map(&:chomp)
105
+
106
+ @edited_since_last_render = false
107
+ else
108
+ @rendered_lines[r..(r + visible_lines - 1)]
109
+ end
110
+
111
+ @rendered_lines[r..(r + visible_lines - 1)]
112
+ .map.with_index{|row, i| "#{(i + r + 1).to_s.rjust(@line_no_width)} #{substr_with_color(row, c, c + w - @line_no_width - 2)}" }
113
+ .map{|l| "#{l}\e[0K" } +
68
114
  Array.new(blank_lines) { "\e[0K" }
69
115
  end
70
116
 
@@ -107,23 +153,27 @@ class FancyBuff
107
153
  end
108
154
 
109
155
  # scrolls the visible window up
110
- def win_up(n=1)
111
- @win[0] = [@win[0] - n, 0].max
156
+ def win_up!(n=1)
157
+ @win[1] = [@win[1] - n, 0].max
158
+ reset_caret!
112
159
  end
113
160
 
114
161
  # scrolls the visible window down
115
- def win_down(n=1)
116
- @win[0] = [@win[0] + n, @lines.length - 1].min
162
+ def win_down!(n=1)
163
+ @win[1] = [@win[1] + n, @lines.length - 1].min
164
+ reset_caret!
117
165
  end
118
166
 
119
167
  # scrolls the visible window left
120
- def win_left(n=1)
121
- @win[1] = [@win[1] - n, 0].max
168
+ def win_left!(n=1)
169
+ @win[0] = [@win[0] - n, 0].max
170
+ reset_caret!
122
171
  end
123
172
 
124
173
  # scrolls the visible window right
125
- def win_right(n=1)
126
- @win[1] = [@win[1] + n, max_char_width - 1].min
174
+ def win_right!(n=1)
175
+ @win[0] = [@win[0] + n, max_char_width - 1].min
176
+ reset_caret!
127
177
  end
128
178
 
129
179
  # set a mark, as in the Vim sense
@@ -178,6 +228,7 @@ class FancyBuff
178
228
  @chars += line.chars.length
179
229
  @max_char_width = line.chars.length if line.chars.length > @max_char_width
180
230
 
231
+ @edited_since_last_render = true
181
232
  nil
182
233
  end
183
234
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fancy_buff
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '5.18'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '5.18'
41
41
  description: a text buffer with marks, selections, and simple insert/delete
42
42
  email: jefflunt@gmail.com
43
43
  executables: []