Skip to content

Commit dfce548

Browse files
committed
Fix whitespace issues
1 parent 1b45589 commit dfce548

6 files changed

Lines changed: 75 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v2.0.2
2+
- Improve whitespace handling
3+
14
# v2.0.1
25
- Fix warning
36

lib/tty_string/cell.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,5 @@ def initialize(value, style: NullStyle)
88
@style = style
99
@value = value
1010
end
11-
12-
def to_s(style_context: NullStyle)
13-
"#{style.to_s(context: style_context)}#{value}"
14-
end
1511
end
1612
end

lib/tty_string/row.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/tty_string/screen.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'cursor'
44
require_relative 'cell'
55
require_relative 'style'
6+
require_relative 'row'
67

78
module 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)

lib/tty_string/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module TTYString
4-
VERSION = '2.0.1'
4+
VERSION = '2.0.2'
55
end

spec/tty_string_spec.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@
397397

398398
it 'removes unnecessary reset codes' do
399399
expect("\e[31mred\e[0m\e[32m green\e[0m \e[0m")
400-
.to render_with_style_as "\e[31mred\e[32m green\e[0m"
400+
.to render_with_style_as "\e[31mred\e[32m green\e[0m "
401401
end
402402

403403
it 'removes unnecessary color code overriding previous ones' do
@@ -412,7 +412,7 @@
412412

413413
it 'assumes the string begins reset' do
414414
expect(" \e[31mred \r\e[0mplain")
415-
.to render_with_style_as "plain\e[31mred\e[0m"
415+
.to render_with_style_as "plain\e[31mred \e[0m"
416416
end
417417

418418
it 'renders 24bit color codes' do
@@ -472,6 +472,18 @@
472472
unknown: described_class::RAISE)
473473
end.to raise_error described_class::UnknownCodeError, 'Unknown style code "256" in "\e[38;5;256m"'
474474
end
475+
476+
it 'puts the newline in the right place' do
477+
expect("dog \e[1;31mcat\e[0m\n").to render_with_style_as(
478+
"dog \e[1;31mcat\e[0m\n"
479+
)
480+
end
481+
482+
it 'fills clears with styled spaces' do
483+
expect("abcd\033[2D\033[41m\033[1K\033[0m").to render_with_style_as(
484+
"\e[41m \e[0md"
485+
)
486+
end
475487
end
476488

477489
describe 'h' do

0 commit comments

Comments
 (0)