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.
- checksums.yaml +4 -4
- data/lib/fancy_buff.rb +46 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 892b50af936bb97a23a873b029d23410320cde8ecfbee5952ca6693f1b2f6e5c
|
4
|
+
data.tar.gz: cd5c61bd3f0be2e4463686d0e72fd6594aa362048dfea27423bef03c7a6c46a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
#
|