fancy_buff 2.1.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 +42 -3
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54eb5fe670bbed8371d3d40a04a202820455b2563725c27e1b5639bedfdc9cb4
4
- data.tar.gz: 07703ee689b16d4544f889c68c7ecd5028829b9004a1a9081f76a332b9d1d724
3
+ metadata.gz: c8121c289daee5a5f2aabbcae53048661536428bfc90b8fbca1f49f2722dea5c
4
+ data.tar.gz: ce3507432e0816fb69634ec64086c03543ae79908ebd5aef7c3749119750cdb3
5
5
  SHA512:
6
- metadata.gz: 0d98a27f8aea897b1a3313e9fe830dc633b0b4f5e93978188a40dae468b4bdfbd35254a6ddaae8ef5188f9f13eeb6c1ec9f09909d081709ee3114efa28e3e408
7
- data.tar.gz: a3d70930ba010a6803cfb7db4aad39e18a00dae2c4eb97b0732212ba5a9d884505491373a50b4264d7baa9827cba4e24a5d03ca2e9fbdbda5a324d5af6c47b28
6
+ metadata.gz: 39155c9822923ebf3a33f676a66eafcc86004d22c760f1d116e8da0c0f80de1c76119753374b7ec8a84933263da1a8c56b96741f8366bbcad86a629f21a2c9ff
7
+ data.tar.gz: 0171ca4fa6000da425da34143c1479352c6410e4ffb22ea676893858ac3ec1294ba8bf20491a70b8d76d3959b18e7201ae5d9968059b4e09fa2a1098b14f01f8
data/lib/fancy_buff.rb CHANGED
@@ -13,12 +13,19 @@ class FancyBuff
13
13
  attr_accessor :win
14
14
 
15
15
  # gives you a default, empty, zero slice
16
- def initialize
16
+ #
17
+ # formatter - a Rouge formatter
18
+ # lexer - a Rouge lexer
19
+ def initialize(formatter, lexer)
20
+ @formatter = formatter
21
+ @lexer = lexer
22
+
17
23
  # size tracking
18
24
  @chars = 0 # the number of characters in the buffer (not the same as the number of bytes)
19
25
  @bytes = 0 # the number of bytes in the buffer (not the same as the number of characters)
20
26
  @lines = []
21
27
  @max_char_width = 0
28
+ @all_buff = @lines.join("\n")
22
29
 
23
30
  @marks = {}
24
31
  @selections = {}
@@ -52,11 +59,43 @@ class FancyBuff
52
59
 
53
60
  return [] if h == 0 || w == 0
54
61
 
55
- @lines[r..(r + visible_lines - 1)]
56
- .map.with_index{|row, i| "#{(i + r + 1).to_s.rjust(3)} #{row.chars[c..(c + w - 1 - 4)]&.join}\e[0K" } +
62
+ line_no_width = @lines.length.to_s.length
63
+ text = @formatter
64
+ .format(@lexer.lex(@lines.join("\n")))
65
+ .lines[r..(r + visible_lines - 1)]
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)}" }
67
+ .map{|l| l.chomp + "\e[0K" } +
57
68
  Array.new(blank_lines) { "\e[0K" }
58
69
  end
59
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
+
60
99
  # the number of visible lines from @lines at any given time
61
100
  def visible_lines
62
101
  [h, @lines.length - r].min
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fancy_buff
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-24 00:00:00.000000000 Z
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rouge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: minitest
15
29
  requirement: !ruby/object:Gem::Requirement