fancy_buff 2.2.0 → 2.4.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 +4 -4
  2. data/lib/fancy_buff.rb +46 -5
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 919af51ebb70ff27b989e17db097db51eaae6c588200d86cc081c5e2c1e8d523
4
- data.tar.gz: '049b9b7c4233e5aee2e807e16e5027fd703da72b6683231e348645df141889ee'
3
+ metadata.gz: 892b50af936bb97a23a873b029d23410320cde8ecfbee5952ca6693f1b2f6e5c
4
+ data.tar.gz: cd5c61bd3f0be2e4463686d0e72fd6594aa362048dfea27423bef03c7a6c46a9
5
5
  SHA512:
6
- metadata.gz: fcb544d92688baa82919afb8fafcbc7761bc83db052759a0b6d25f15532f3b0a5d23a93628aecc1882dfcbc3a856b38241e9c846d7edae3536a1629ad18513e3
7
- data.tar.gz: 4aa39cc8ac44c11c0bc3b31bd9f9ba882611e752bed58248301cfec5e3ad85e71c85c1e88b1312beeed36552c9894980557c06a230b74768832729bc158860e8
6
+ metadata.gz: 68953497b15fc893d68b270dde92799394fbeff68b2f39ce500e90a1a212fcafa0c29c69250bd7242a771901b5467a97535c0339f8e5fb9005688791892d0a30
7
+ data.tar.gz: 0d2b82addc499d165882758b688868495003572975227d3f8a8a9602ea51ff9c5722ef3e5a33714ff629305b8b6efbd7a1bcc87edefcb703ff9ed53a0c8eb7c4
data/lib/fancy_buff.rb CHANGED
@@ -24,6 +24,8 @@ class FancyBuff
24
24
  @chars = 0 # the number of characters in the buffer (not the same as the number of bytes)
25
25
  @bytes = 0 # the number of bytes in the buffer (not the same as the number of characters)
26
26
  @lines = []
27
+ @rendered_lines = [] # fully formatted, syntax highlighted, and transformed
28
+ @edited_since_last_render = true
27
29
  @max_char_width = 0
28
30
  @all_buff = @lines.join("\n")
29
31
 
@@ -59,14 +61,52 @@ class FancyBuff
59
61
 
60
62
  return [] if h == 0 || w == 0
61
63
 
62
- text = @formatter
63
- .format(@lexer.lex(@lines.join("\n")))
64
- .lines[r..(r + visible_lines - 1)]
65
- .map.with_index{|row, i| "#{(i + r + 1).to_s.rjust(3)} #{row.chars[c..(c + w - 1 - 4)]&.join}" }
66
- .map{|l| l.chomp + "\e[0K" } +
64
+ line_no_width = @lines.length.to_s.length
65
+ if @edited_since_last_render
66
+ @rendered_lines = @formatter
67
+ .format(@lexer.lex(@lines.join("\n")))
68
+ .lines
69
+ .map(&:chomp)
70
+
71
+ @edited_since_last_render = false
72
+ else
73
+ @rendered_lines[r..(r + visible_lines - 1)]
74
+ end
75
+
76
+ @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)}" }
78
+ .map{|l| "#{l}\e[0K" } +
67
79
  Array.new(blank_lines) { "\e[0K" }
68
80
  end
69
81
 
82
+ # input - a String that may or may not contain ANSI color codes
83
+ # start - the starting index of printable characters to keep
84
+ # finish - the ending index of printable characters to keep
85
+ #
86
+ # treats `input' like a String that does
87
+ def substr_with_color(input, start, finish)
88
+ ansi_pattern = /\A\e\[[0-9;]+m/
89
+ printable_counter = 0
90
+ remaining = input.clone.chomp
91
+ result = ''
92
+
93
+ loop do
94
+ break if remaining.empty? || printable_counter > finish
95
+
96
+ match = remaining.match(ansi_pattern)
97
+ if match
98
+ result += match[0]
99
+ remaining = remaining.sub(match[0], '')
100
+ else
101
+ result += remaining[0] if printable_counter >= start
102
+ remaining = remaining[1..-1]
103
+ printable_counter += 1
104
+ end
105
+ end
106
+
107
+ result + "\e[0m"
108
+ end
109
+
70
110
  # the number of visible lines from @lines at any given time
71
111
  def visible_lines
72
112
  [h, @lines.length - r].min
@@ -149,6 +189,7 @@ class FancyBuff
149
189
  @chars += line.chars.length
150
190
  @max_char_width = line.chars.length if line.chars.length > @max_char_width
151
191
 
192
+ @edited_since_last_render = true
152
193
  nil
153
194
  end
154
195
  #
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.2.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt