@@ -7,21 +7,17 @@ pub struct StyledBuffer {
7
7
lines : Vec < Vec < StyledChar > > ,
8
8
}
9
9
10
- #[ derive( Debug ) ]
10
+ #[ derive( Debug , Clone ) ]
11
11
struct StyledChar {
12
12
chr : char ,
13
13
style : Style ,
14
14
}
15
15
16
16
impl StyledChar {
17
- fn new ( chr : char , style : Style ) -> Self {
18
- StyledChar { chr, style }
19
- }
20
- }
17
+ const SPACE : Self = StyledChar :: new ( ' ' , Style :: NoStyle ) ;
21
18
22
- impl Default for StyledChar {
23
- fn default ( ) -> Self {
24
- StyledChar :: new ( ' ' , Style :: NoStyle )
19
+ const fn new ( chr : char , style : Style ) -> Self {
20
+ StyledChar { chr, style }
25
21
}
26
22
}
27
23
@@ -66,31 +62,25 @@ impl StyledBuffer {
66
62
}
67
63
68
64
fn ensure_lines ( & mut self , line : usize ) {
69
- while line >= self . lines . len ( ) {
70
- self . lines . push ( vec ! [ ] ) ;
65
+ if line >= self . lines . len ( ) {
66
+ self . lines . resize ( line + 1 , Vec :: new ( ) ) ;
71
67
}
72
68
}
73
69
74
70
/// Sets `chr` with `style` for given `line`, `col`.
75
- /// If line not exist in `StyledBuffer` , adds lines up to given
76
- /// and fills last line with spaces and `Style::NoStyle` style
71
+ /// If ` line` does not exist in our buffer , adds empty lines up to the given
72
+ /// and fills the last line with unstyled whitespace.
77
73
pub fn putc ( & mut self , line : usize , col : usize , chr : char , style : Style ) {
78
74
self . ensure_lines ( line) ;
79
- if col < self . lines [ line] . len ( ) {
80
- self . lines [ line] [ col] = StyledChar :: new ( chr, style) ;
81
- } else {
82
- let mut i = self . lines [ line] . len ( ) ;
83
- while i < col {
84
- self . lines [ line] . push ( StyledChar :: default ( ) ) ;
85
- i += 1 ;
86
- }
87
- self . lines [ line] . push ( StyledChar :: new ( chr, style) ) ;
75
+ if col >= self . lines [ line] . len ( ) {
76
+ self . lines [ line] . resize ( col + 1 , StyledChar :: SPACE ) ;
88
77
}
78
+ self . lines [ line] [ col] = StyledChar :: new ( chr, style) ;
89
79
}
90
80
91
81
/// Sets `string` with `style` for given `line`, starting from `col`.
92
- /// If line not exist in `StyledBuffer` , adds lines up to given
93
- /// and fills last line with spaces and `Style::NoStyle` style
82
+ /// If ` line` does not exist in our buffer , adds empty lines up to the given
83
+ /// and fills the last line with unstyled whitespace.
94
84
pub fn puts ( & mut self , line : usize , col : usize , string : & str , style : Style ) {
95
85
let mut n = col;
96
86
for c in string. chars ( ) {
@@ -108,7 +98,7 @@ impl StyledBuffer {
108
98
if !self . lines [ line] . is_empty ( ) {
109
99
// Push the old content over to make room for new content
110
100
for _ in 0 ..string_len {
111
- self . lines [ line] . insert ( 0 , StyledChar :: default ( ) ) ;
101
+ self . lines [ line] . insert ( 0 , StyledChar :: SPACE ) ;
112
102
}
113
103
}
114
104
0 commit comments