33require_relative 'cursor'
44require_relative 'cell'
55require_relative 'style'
6+ require_relative 'row'
67
78module TTYString
89 # a grid to draw on
@@ -15,58 +16,67 @@ def initialize(initial_style:)
1516 @current_style = @initial_style = initial_style
1617 end
1718
18- def to_s # rubocop:disable Metrics/MethodLength
19+ def to_s # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
1920 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 |
2230 if cell
23- value = cell . to_s ( style_context : style_context )
31+ str << cell . style . to_s ( context : style_context )
32+ str << cell . value
2433 style_context = cell . style
25- value
2634 else
27- ' '
35+ str << ' '
2836 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
3141 end
3242
3343 def []=( ( row , col ) , value )
34- screen [ row ] ||= [ ]
44+ screen [ row ] ||= Row . new ( newline_style : current_style )
3545 screen [ row ] [ col ] = value
3646 end
3747
3848 def clear_at_cursor
39- self [ cursor ] = nil
49+ self [ cursor ] = Cell . new ( ' ' , style : current_style )
4050 end
4151
4252 def clear_line_forward
43- screen [ row ] . fill ( nil , col ..-1 )
53+ screen [ row ] . slice! ( col ..-1 )
4454 end
4555
4656 def clear_line_backward
47- screen [ row ] . fill ( nil , 0 ..col )
57+ screen [ row ] . fill ( Cell . new ( ' ' , style : current_style ) , 0 ..col )
4858 end
4959
5060 def clear_line
51- screen [ row ] = [ ]
61+ screen [ row ] = Row . new ( newline_style : current_style )
5262 end
5363
5464 def clear
5565 @screen = [ ]
5666 end
5767
5868 def scroll_up
59- screen . push ( [ ] )
69+ screen . push ( Row . new ( newline_style : current_style ) )
6070 screen . shift
6171 end
6272
6373 def scroll_down
64- screen . unshift ( [ ] )
74+ screen . unshift ( Row . new ( newline_style : current_style ) )
6575 screen . pop
6676 end
6777
6878 def clear_lines_before
69- screen . fill ( [ ] , 0 ...row )
79+ screen . fill ( Row . new ( newline_style : current_style ) , 0 ...row )
7080 end
7181
7282 def clear_lines_after
@@ -84,7 +94,7 @@ def clear_forward
8494 end
8595
8696 def ensure_row
87- screen [ row ] ||= [ ]
97+ screen [ row ] ||= Row . new ( newline_style : current_style )
8898 end
8999
90100 def write ( string )
0 commit comments