fancy_buff 2.4.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 +59 -20
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 892b50af936bb97a23a873b029d23410320cde8ecfbee5952ca6693f1b2f6e5c
4
- data.tar.gz: cd5c61bd3f0be2e4463686d0e72fd6594aa362048dfea27423bef03c7a6c46a9
3
+ metadata.gz: 24ca22ff6ce4b91274d9158e71ddb51dbf78ed644bdc4eb0affc665e8fc7b9a9
4
+ data.tar.gz: 8027d3a0e02558e96a7f12c4746b6f9b9ca8b3e896c121d64fb1e841e08da65a
5
5
  SHA512:
6
- metadata.gz: 68953497b15fc893d68b270dde92799394fbeff68b2f39ce500e90a1a212fcafa0c29c69250bd7242a771901b5467a97535c0339f8e5fb9005688791892d0a30
7
- data.tar.gz: 0d2b82addc499d165882758b688868495003572975227d3f8a8a9602ea51ff9c5722ef3e5a33714ff629305b8b6efbd7a1bcc87edefcb703ff9ed53a0c8eb7c4
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,11 +18,14 @@ 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
27
29
  @rendered_lines = [] # fully formatted, syntax highlighted, and transformed
28
30
  @edited_since_last_render = true
29
31
  @max_char_width = 0
@@ -31,16 +33,51 @@ class FancyBuff
31
33
 
32
34
  @marks = {}
33
35
  @selections = {}
34
- @win = [0, 0, 0, 0] # the default slice is at the beginning of the buffer, and has a zero size
35
36
  end
36
37
 
37
- # index of first visible row
38
- def r
39
- @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
+ ]
40
72
  end
41
73
 
42
74
  # index of first visible column
43
75
  def c
76
+ @win[0]
77
+ end
78
+
79
+ # index of first visible row
80
+ def r
44
81
  @win[1]
45
82
  end
46
83
 
@@ -57,11 +94,9 @@ class FancyBuff
57
94
  # returns an array of strings representing the visible characters from this
58
95
  # FancyBuffer's @rect
59
96
  def win_s
60
- r, c, w, h = @win
61
-
62
97
  return [] if h == 0 || w == 0
63
98
 
64
- line_no_width = @lines.length.to_s.length
99
+ @line_no_width = @lines.length.to_s.length
65
100
  if @edited_since_last_render
66
101
  @rendered_lines = @formatter
67
102
  .format(@lexer.lex(@lines.join("\n")))
@@ -74,7 +109,7 @@ class FancyBuff
74
109
  end
75
110
 
76
111
  @rendered_lines[r..(r + visible_lines - 1)]
77
- .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)}" }
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)}" }
78
113
  .map{|l| "#{l}\e[0K" } +
79
114
  Array.new(blank_lines) { "\e[0K" }
80
115
  end
@@ -118,23 +153,27 @@ class FancyBuff
118
153
  end
119
154
 
120
155
  # scrolls the visible window up
121
- def win_up(n=1)
122
- @win[0] = [@win[0] - n, 0].max
156
+ def win_up!(n=1)
157
+ @win[1] = [@win[1] - n, 0].max
158
+ reset_caret!
123
159
  end
124
160
 
125
161
  # scrolls the visible window down
126
- def win_down(n=1)
127
- @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!
128
165
  end
129
166
 
130
167
  # scrolls the visible window left
131
- def win_left(n=1)
132
- @win[1] = [@win[1] - n, 0].max
168
+ def win_left!(n=1)
169
+ @win[0] = [@win[0] - n, 0].max
170
+ reset_caret!
133
171
  end
134
172
 
135
173
  # scrolls the visible window right
136
- def win_right(n=1)
137
- @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!
138
177
  end
139
178
 
140
179
  # set a mark, as in the Vim sense
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.4.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: []