tty_string 2.0.1 → 2.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09c75d4e226fcdb40060ba61420acdbd45496ba321ab76e63e6f3cfaff6c1769'
4
- data.tar.gz: 12cdd2deb8a45550bc3225aafdd02982039d2293ea90e9f82063809512306f2b
3
+ metadata.gz: fed365cd62d837aec3621a4c1a1f9055871aaf7050d4c136206ca1b6ef6e6a5b
4
+ data.tar.gz: adfbc75cc2f3ec31be690d30a26848ba9ca6b43f83612f8a9ccf698bfc8a7d1e
5
5
  SHA512:
6
- metadata.gz: d01fea8ea93629dc0467ecfb4d37d28a23f53cb4fa3e61843ae1a189e5f1391778d2d79efaacee59dc9855678fda48f1d0434d6bcf6c43f120881399eaed0835
7
- data.tar.gz: 3b06c7653d84ec1bf2864007daee5a958b968ef30a75c0b9463cf22f04aab0ac0d0b24560b9282dcda3c065424503d224324ae7d5b067afc0bec85a22def3b24
6
+ metadata.gz: edf12a947a46e36cdb82ec8f2b8ba26c111b189a78d1f35e7136771499605d4bfba8c96b251595555c5b83bfaa0f3059aec868958590d3ae5d012c37772a3145
7
+ data.tar.gz: 3a4487855eecdb533dd8f3382752c3f7b35bcd119a2516c7a02dba077580dff64837f1374a86bb4aa88628dc93eb51a1b9296873df53c19022a8ab75ba8505f7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v2.0.2
2
+ - Improve whitespace handling
3
+
1
4
  # v2.0.1
2
5
  - Fix warning
3
6
 
@@ -8,9 +8,5 @@ module TTYString
8
8
  @style = style
9
9
  @value = value
10
10
  end
11
-
12
- def to_s(style_context: NullStyle)
13
- "#{style.to_s(context: style_context)}#{value}"
14
- end
15
11
  end
16
12
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTYString
4
+ class Row
5
+ include Enumerable
6
+
7
+ attr_accessor :newline_style
8
+
9
+ def initialize(newline_style: NullStyle)
10
+ @array = []
11
+ self.newline_style = newline_style
12
+ end
13
+
14
+ def each(&block)
15
+ @array.each(&block)
16
+ end
17
+
18
+ def []=(index, value)
19
+ @array[index] = value
20
+ end
21
+
22
+ def slice!(*args)
23
+ @array.slice!(*args)
24
+ end
25
+
26
+ def fill(*args)
27
+ @array.fill(*args)
28
+ end
29
+ end
30
+ end
@@ -3,6 +3,7 @@
3
3
  require_relative 'cursor'
4
4
  require_relative 'cell'
5
5
  require_relative 'style'
6
+ require_relative 'row'
6
7
 
7
8
  module TTYString
8
9
  # a grid to draw on
@@ -15,40 +16,49 @@ module TTYString
15
16
  @current_style = @initial_style = initial_style
16
17
  end
17
18
 
18
- def to_s # rubocop:disable Metrics/MethodLength
19
+ def to_s # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
19
20
  style_context = initial_style
20
- screen.map do |row|
21
- Array(row).map do |cell|
21
+ str = +''
22
+ screen.each_with_index do |row, index|
23
+ unless index.zero?
24
+ str << row.newline_style.to_s(context: style_context) if row
25
+ str << "\n"
26
+ style_context = row.newline_style if row
27
+ end
28
+
29
+ Array(row).each do |cell|
22
30
  if cell
23
- value = cell.to_s(style_context: style_context)
31
+ str << cell.style.to_s(context: style_context)
32
+ str << cell.value
24
33
  style_context = cell.style
25
- value
26
34
  else
27
- ' '
35
+ str << ' '
28
36
  end
29
- end.join.rstrip
30
- end.join("\n") + current_style.to_s(context: style_context)
37
+ end
38
+ end
39
+ str << current_style.to_s(context: style_context)
40
+ str
31
41
  end
32
42
 
33
43
  def []=((row, col), value)
34
- screen[row] ||= []
44
+ screen[row] ||= Row.new(newline_style: current_style)
35
45
  screen[row][col] = value
36
46
  end
37
47
 
38
48
  def clear_at_cursor
39
- self[cursor] = nil
49
+ self[cursor] = Cell.new(' ', style: current_style)
40
50
  end
41
51
 
42
52
  def clear_line_forward
43
- screen[row].fill(nil, col..-1)
53
+ screen[row].slice!(col..-1)
44
54
  end
45
55
 
46
56
  def clear_line_backward
47
- screen[row].fill(nil, 0..col)
57
+ screen[row].fill(Cell.new(' ', style: current_style), 0..col)
48
58
  end
49
59
 
50
60
  def clear_line
51
- screen[row] = []
61
+ screen[row] = Row.new(newline_style: current_style)
52
62
  end
53
63
 
54
64
  def clear
@@ -56,17 +66,17 @@ module TTYString
56
66
  end
57
67
 
58
68
  def scroll_up
59
- screen.push([])
69
+ screen.push(Row.new(newline_style: current_style))
60
70
  screen.shift
61
71
  end
62
72
 
63
73
  def scroll_down
64
- screen.unshift([])
74
+ screen.unshift(Row.new(newline_style: current_style))
65
75
  screen.pop
66
76
  end
67
77
 
68
78
  def clear_lines_before
69
- screen.fill([], 0...row)
79
+ screen.fill(Row.new(newline_style: current_style), 0...row)
70
80
  end
71
81
 
72
82
  def clear_lines_after
@@ -84,7 +94,7 @@ module TTYString
84
94
  end
85
95
 
86
96
  def ensure_row
87
- screen[row] ||= []
97
+ screen[row] ||= Row.new(newline_style: current_style)
88
98
  end
89
99
 
90
100
  def write(string)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TTYString
4
- VERSION = '2.0.1'
4
+ VERSION = '2.0.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty_string
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dana Sherson
@@ -210,6 +210,7 @@ files:
210
210
  - lib/tty_string/cursor.rb
211
211
  - lib/tty_string/null_style.rb
212
212
  - lib/tty_string/parser.rb
213
+ - lib/tty_string/row.rb
213
214
  - lib/tty_string/screen.rb
214
215
  - lib/tty_string/style.rb
215
216
  - lib/tty_string/version.rb