11use std:: cell:: RefCell ;
22
3+ use bitflags:: bitflags;
34use unicode_segmentation:: UnicodeSegmentation ;
45use unicode_width:: UnicodeWidthStr ;
56use xi_unicode:: EmojiExt ;
@@ -41,46 +42,63 @@ impl SegmentPartial {
4142 }
4243}
4344
45+ bitflags ! {
46+ /// Options for the `LineSegment` formatting
47+ #[ derive( Default , PartialEq , Eq , Debug , Clone , Copy ) ]
48+ pub ( crate ) struct LineSegmentOptions : u8 {
49+ /// None
50+ const NONE = 0b0000_0000 ;
51+ /// Dimmed
52+ const DIMMED = 0b0000_0001 ;
53+ /// Dimmed
54+ const REVERSED = 0b0000_0010 ;
55+ /// Dimmed
56+ const UNDERLINED = 0b0000_0100 ;
57+ }
58+ }
59+
60+ impl LineSegmentOptions {
61+ pub ( crate ) fn conditional ( condition : bool , options : Self ) -> Self {
62+ if condition { options } else { LineSegmentOptions :: NONE }
63+ }
64+ }
65+
4466/// Represents a segment in a larger line.
4567#[ derive( Clone , Debug ) ]
4668pub ( crate ) struct LineSegment {
4769 color : DisplayColor ,
48- dim : bool ,
49- reverse : bool ,
70+ options : LineSegmentOptions ,
5071 text : String ,
5172 length : usize ,
52- underline : bool ,
5373}
5474
5575impl LineSegment {
5676 /// Create a new instance with just the content.
5777 #[ must_use]
5878 pub ( crate ) fn new ( text : & str ) -> Self {
59- Self :: new_with_color_and_style ( text, DisplayColor :: Normal , false , false , false )
79+ Self :: new_with_color_and_style ( text, DisplayColor :: Normal , LineSegmentOptions :: NONE )
80+ }
81+
82+ /// Create a new instance with just the content.
83+ #[ must_use]
84+ pub ( crate ) fn new_copy_style ( text : & str , segment : & Self ) -> Self {
85+ Self :: new_with_color_and_style ( text, segment. color , segment. options )
6086 }
6187
6288 /// Create a new instance with added color.
6389 #[ must_use]
6490 pub ( crate ) fn new_with_color ( text : & str , color : DisplayColor ) -> Self {
65- Self :: new_with_color_and_style ( text, color, false , false , false )
91+ Self :: new_with_color_and_style ( text, color, LineSegmentOptions :: NONE )
6692 }
6793
6894 /// Create a new instance with added color and style.
6995 #[ must_use]
70- pub ( crate ) fn new_with_color_and_style (
71- text : & str ,
72- color : DisplayColor ,
73- dim : bool ,
74- underline : bool ,
75- reverse : bool ,
76- ) -> Self {
96+ pub ( crate ) fn new_with_color_and_style ( text : & str , color : DisplayColor , options : LineSegmentOptions ) -> Self {
7797 Self {
7898 text : String :: from ( text) ,
99+ options,
79100 color,
80- reverse,
81- dim,
82101 length : unicode_column_width ( text) ,
83- underline,
84102 }
85103 }
86104
@@ -93,15 +111,15 @@ impl LineSegment {
93111 }
94112
95113 pub ( crate ) const fn is_dimmed ( & self ) -> bool {
96- self . dim
114+ self . options . contains ( LineSegmentOptions :: DIMMED )
97115 }
98116
99117 pub ( crate ) const fn is_underlined ( & self ) -> bool {
100- self . underline
118+ self . options . contains ( LineSegmentOptions :: UNDERLINED )
101119 }
102120
103121 pub ( crate ) const fn is_reversed ( & self ) -> bool {
104- self . reverse
122+ self . options . contains ( LineSegmentOptions :: REVERSED )
105123 }
106124
107125 pub ( crate ) const fn get_length ( & self ) -> usize {
@@ -187,9 +205,7 @@ mod tests {
187205 let line_segment = LineSegment :: new_with_color_and_style (
188206 "Sævör grét áðan því úlpan var ónýt" ,
189207 DisplayColor :: IndicatorColor ,
190- true ,
191- true ,
192- true ,
208+ LineSegmentOptions :: all ( ) ,
193209 ) ;
194210
195211 assert_eq ! ( line_segment. get_color( ) , DisplayColor :: IndicatorColor ) ;
@@ -204,9 +220,7 @@ mod tests {
204220 let line_segment = LineSegment :: new_with_color_and_style (
205221 "? דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה" ,
206222 DisplayColor :: IndicatorColor ,
207- false ,
208- false ,
209- false ,
223+ LineSegmentOptions :: NONE ,
210224 ) ;
211225
212226 assert_eq ! ( line_segment. get_color( ) , DisplayColor :: IndicatorColor ) ;
@@ -218,8 +232,11 @@ mod tests {
218232
219233 #[ test]
220234 fn line_segment_case_new_with_color_and_style_all_styles_dimmed ( ) {
221- let line_segment =
222- LineSegment :: new_with_color_and_style ( "Test String" , DisplayColor :: IndicatorColor , true , false , false ) ;
235+ let line_segment = LineSegment :: new_with_color_and_style (
236+ "Test String" ,
237+ DisplayColor :: IndicatorColor ,
238+ LineSegmentOptions :: DIMMED ,
239+ ) ;
223240
224241 assert ! ( line_segment. is_dimmed( ) ) ;
225242 assert ! ( !line_segment. is_underlined( ) ) ;
@@ -228,8 +245,11 @@ mod tests {
228245
229246 #[ test]
230247 fn line_segment_case_new_with_color_and_style_all_styles_underlined ( ) {
231- let line_segment =
232- LineSegment :: new_with_color_and_style ( "Test String" , DisplayColor :: IndicatorColor , false , true , false ) ;
248+ let line_segment = LineSegment :: new_with_color_and_style (
249+ "Test String" ,
250+ DisplayColor :: IndicatorColor ,
251+ LineSegmentOptions :: UNDERLINED ,
252+ ) ;
233253
234254 assert ! ( !line_segment. is_dimmed( ) ) ;
235255 assert ! ( line_segment. is_underlined( ) ) ;
@@ -238,8 +258,11 @@ mod tests {
238258
239259 #[ test]
240260 fn line_segment_case_new_with_color_and_style_all_styles_reversed ( ) {
241- let line_segment =
242- LineSegment :: new_with_color_and_style ( "Test String" , DisplayColor :: IndicatorColor , false , false , true ) ;
261+ let line_segment = LineSegment :: new_with_color_and_style (
262+ "Test String" ,
263+ DisplayColor :: IndicatorColor ,
264+ LineSegmentOptions :: REVERSED ,
265+ ) ;
243266
244267 assert ! ( !line_segment. is_dimmed( ) ) ;
245268 assert ! ( !line_segment. is_underlined( ) ) ;
0 commit comments