fancy_buff 2.4.0 → 3.1.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 +102 -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: 195debbfecfec78a6e9efe04ca6b0cb0dcb828f3cc0f9a957f8342422f00c925
4
+ data.tar.gz: 0e2b2635f5330e5b7dd08a1135a0810d1e38d6695d289598826b0618798303ea
5
5
  SHA512:
6
- metadata.gz: 68953497b15fc893d68b270dde92799394fbeff68b2f39ce500e90a1a212fcafa0c29c69250bd7242a771901b5467a97535c0339f8e5fb9005688791892d0a30
7
- data.tar.gz: 0d2b82addc499d165882758b688868495003572975227d3f8a8a9602ea51ff9c5722ef3e5a33714ff629305b8b6efbd7a1bcc87edefcb703ff9ed53a0c8eb7c4
6
+ metadata.gz: 6b5cef70ff2eee10fd6f7cd33af401b36fcf7478f5493795fd694c6446b0a52d5f7a598bc112b83711204ab0c835d91d1b2b6535a3320f91228a7d6848c2e6de
7
+ data.tar.gz: 90ad2a3a7148f2669a7b1274d482ca08bd17d7068622ebc19aaf6d827c283b18c7da6fe217aba91e0c501e0419f8b49099bbfdffa1ef920d6abe11fee5342fac
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,23 @@ 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
+ adjust_caret!
40
44
  end
41
45
 
42
46
  # index of first visible column
43
47
  def c
48
+ @win[0]
49
+ end
50
+
51
+ # index of first visible row
52
+ def r
44
53
  @win[1]
45
54
  end
46
55
 
@@ -54,14 +63,83 @@ class FancyBuff
54
63
  @win[3]
55
64
  end
56
65
 
66
+ def adjust_caret!
67
+ cx, cy = @caret
68
+ new_cx = if cx < c
69
+ c
70
+ elsif cx > c + w
71
+ w
72
+ else
73
+ cx
74
+ end
75
+
76
+ new_cy = if cy < r
77
+ r
78
+ elsif cy > (r + h - 1)
79
+ r + h - 1
80
+ else
81
+ cy
82
+ end
83
+
84
+ @caret = [new_cx, new_cy]
85
+ end
86
+
87
+ def adjust_win!
88
+ cx, cy = visual_caret
89
+ wx, wy = @win[0], @win[1]
90
+
91
+ new_wx = if cx > (c + w - 1)
92
+ wx + 1
93
+ elsif cx < 0
94
+ wx - 1
95
+ else
96
+ wx
97
+ end
98
+
99
+ new_wy = if cy > (r + h - 1)
100
+ wy + 1
101
+ elsif cy < 0
102
+ wy - 1
103
+ else
104
+ wy
105
+ end
106
+
107
+ @win[0] = wx
108
+ @win[1] = wy
109
+ end
110
+
111
+ def caret_down!
112
+ @caret[1] = [caret[1] + 1, @lines.length - 1].min
113
+ adjust_win!
114
+ end
115
+
116
+ def caret_up!
117
+ @caret[1] = [caret[1] - 1, 0].max
118
+ adjust_win!
119
+ end
120
+
121
+ def caret_left!
122
+ @caret[0] = [caret[0] - 1, 0].max
123
+ adjust_win!
124
+ end
125
+
126
+ def caret_right!
127
+ @caret[0] = [caret[0] + 1, @lines[caret[1]].length - 1].min
128
+ adjust_win!
129
+ end
130
+
131
+ def visual_caret
132
+ [
133
+ caret[0] - c + line_no_width + 1, # '+ 1' is to account for a space between line numbers and caret
134
+ caret[1] - r
135
+ ]
136
+ end
57
137
  # returns an array of strings representing the visible characters from this
58
138
  # FancyBuffer's @rect
59
139
  def win_s
60
- r, c, w, h = @win
61
-
62
140
  return [] if h == 0 || w == 0
63
141
 
64
- line_no_width = @lines.length.to_s.length
142
+ @line_no_width = @lines.length.to_s.length
65
143
  if @edited_since_last_render
66
144
  @rendered_lines = @formatter
67
145
  .format(@lexer.lex(@lines.join("\n")))
@@ -74,7 +152,7 @@ class FancyBuff
74
152
  end
75
153
 
76
154
  @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)}" }
155
+ .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
156
  .map{|l| "#{l}\e[0K" } +
79
157
  Array.new(blank_lines) { "\e[0K" }
80
158
  end
@@ -118,23 +196,27 @@ class FancyBuff
118
196
  end
119
197
 
120
198
  # scrolls the visible window up
121
- def win_up(n=1)
122
- @win[0] = [@win[0] - n, 0].max
199
+ def win_up!(n=1)
200
+ @win[1] = [@win[1] - n, 0].max
201
+ adjust_caret!
123
202
  end
124
203
 
125
204
  # scrolls the visible window down
126
- def win_down(n=1)
127
- @win[0] = [@win[0] + n, @lines.length - 1].min
205
+ def win_down!(n=1)
206
+ @win[1] = [@win[1] + n, @lines.length - 1].min
207
+ adjust_caret!
128
208
  end
129
209
 
130
210
  # scrolls the visible window left
131
- def win_left(n=1)
132
- @win[1] = [@win[1] - n, 0].max
211
+ def win_left!(n=1)
212
+ @win[0] = [@win[0] - n, 0].max
213
+ adjust_caret!
133
214
  end
134
215
 
135
216
  # scrolls the visible window right
136
- def win_right(n=1)
137
- @win[1] = [@win[1] + n, max_char_width - 1].min
217
+ def win_right!(n=1)
218
+ @win[0] = [@win[0] + n, max_char_width - 1].min
219
+ adjust_caret!
138
220
  end
139
221
 
140
222
  # 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.1.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: []