@@ -7,21 +7,17 @@ pub struct StyledBuffer {
77 lines : Vec < Vec < StyledChar > > ,
88}
99
10- #[ derive( Debug ) ]
10+ #[ derive( Debug , Clone ) ]
1111struct StyledChar {
1212 chr : char ,
1313 style : Style ,
1414}
1515
1616impl StyledChar {
17- fn new ( chr : char , style : Style ) -> Self {
18- StyledChar { chr, style }
19- }
20- }
17+ const SPACE : Self = StyledChar :: new ( ' ' , Style :: NoStyle ) ;
2118
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 }
2521 }
2622}
2723
@@ -66,31 +62,25 @@ impl StyledBuffer {
6662 }
6763
6864 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 ( ) ) ;
7167 }
7268 }
7369
7470 /// 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.
7773 pub fn putc ( & mut self , line : usize , col : usize , chr : char , style : Style ) {
7874 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 ) ;
8877 }
78+ self . lines [ line] [ col] = StyledChar :: new ( chr, style) ;
8979 }
9080
9181 /// 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.
9484 pub fn puts ( & mut self , line : usize , col : usize , string : & str , style : Style ) {
9585 let mut n = col;
9686 for c in string. chars ( ) {
@@ -108,7 +98,7 @@ impl StyledBuffer {
10898 if !self . lines [ line] . is_empty ( ) {
10999 // Push the old content over to make room for new content
110100 for _ in 0 ..string_len {
111- self . lines [ line] . insert ( 0 , StyledChar :: default ( ) ) ;
101+ self . lines [ line] . insert ( 0 , StyledChar :: SPACE ) ;
112102 }
113103 }
114104
0 commit comments