fancy_buff 2.2.0 → 2.3.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 +30 -1
  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: c8121c289daee5a5f2aabbcae53048661536428bfc90b8fbca1f49f2722dea5c
4
+ data.tar.gz: ce3507432e0816fb69634ec64086c03543ae79908ebd5aef7c3749119750cdb3
5
5
  SHA512:
6
- metadata.gz: fcb544d92688baa82919afb8fafcbc7761bc83db052759a0b6d25f15532f3b0a5d23a93628aecc1882dfcbc3a856b38241e9c846d7edae3536a1629ad18513e3
7
- data.tar.gz: 4aa39cc8ac44c11c0bc3b31bd9f9ba882611e752bed58248301cfec5e3ad85e71c85c1e88b1312beeed36552c9894980557c06a230b74768832729bc158860e8
6
+ metadata.gz: 39155c9822923ebf3a33f676a66eafcc86004d22c760f1d116e8da0c0f80de1c76119753374b7ec8a84933263da1a8c56b96741f8366bbcad86a629f21a2c9ff
7
+ data.tar.gz: 0171ca4fa6000da425da34143c1479352c6410e4ffb22ea676893858ac3ec1294ba8bf20491a70b8d76d3959b18e7201ae5d9968059b4e09fa2a1098b14f01f8
data/lib/fancy_buff.rb CHANGED
@@ -59,14 +59,43 @@ class FancyBuff
59
59
 
60
60
  return [] if h == 0 || w == 0
61
61
 
62
+ line_no_width = @lines.length.to_s.length
62
63
  text = @formatter
63
64
  .format(@lexer.lex(@lines.join("\n")))
64
65
  .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.with_index{|row, i| "#{(i + r + 1).to_s.rjust(line_no_width)} #{substr_with_color(row, c, c + w - line_no_width - 2)}" }
66
67
  .map{|l| l.chomp + "\e[0K" } +
67
68
  Array.new(blank_lines) { "\e[0K" }
68
69
  end
69
70
 
71
+ # input - a String that may or may not contain ANSI color codes
72
+ # start - the starting index of printable characters to keep
73
+ # finish - the ending index of printable characters to keep
74
+ #
75
+ # treats `input' like a String that does
76
+ def substr_with_color(input, start, finish)
77
+ ansi_pattern = /\A\e\[[0-9;]+m/
78
+ printable_counter = 0
79
+ remaining = input.clone.chomp
80
+ result = ''
81
+
82
+ loop do
83
+ break if remaining.empty? || printable_counter > finish
84
+
85
+ match = remaining.match(ansi_pattern)
86
+ if match
87
+ result += match[0]
88
+ remaining = remaining.sub(match[0], '')
89
+ else
90
+ result += remaining[0] if printable_counter >= start
91
+ remaining = remaining[1..-1]
92
+ printable_counter += 1
93
+ end
94
+ end
95
+
96
+ result + "\e[0m"
97
+ end
98
+
70
99
  # the number of visible lines from @lines at any given time
71
100
  def visible_lines
72
101
  [h, @lines.length - r].min
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.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt