@@ -170,34 +170,33 @@ void RichString_appendChr(RichString* this, int attrs, char c, int count) {
170170}
171171
172172void RichString_setAttrn_preserveWithStandout (RichString * this , int attrs , int start , int finish ) {
173- // Extract the foreground and background colors from the passed attrs
173+ finish = CLAMP (finish , 0 , this -> chlen - 1 );
174+
175+ // Extract the foreground and background color indexes from the passed attrs
174176 int passed_color_pair_number = PAIR_NUMBER (attrs );
175177 short passed_fg_color = -1 , passed_bg_color = -1 ;
176178 if (passed_color_pair_number != 0 ) {
177179 pair_content (passed_color_pair_number , & passed_fg_color , & passed_bg_color );
178180 }
179181
180- finish = CLAMP (finish , 0 , this -> chlen - 1 );
181- int length = finish - start + 1 ;
182- // Store the initial state of the attributes in a temporary array
183- cchar_t before [length ];
184- memcpy (before , this -> chptr + start , length * sizeof (cchar_t ));
185-
186182 cchar_t * ch = this -> chptr + start ;
187183 for (int i = start ; i <= finish ; i ++ ) {
188- // Extract foreground and background from 'before'
189- int pairNum = PAIR_NUMBER (ch -> attr );
184+ // Extract foreground and background color indexes from the current char
185+ int currentCharPairNum = PAIR_NUMBER (ch -> attr );
190186 short before_fg_color = -1 , before_bg_color = -1 ;
191- if (pairNum != 0 ) {
192- pair_content (pairNum , & before_fg_color , & before_bg_color );
187+ if (currentCharPairNum != 0 ) {
188+ pair_content (currentCharPairNum , & before_fg_color , & before_bg_color );
193189 }
194190
195- // When text color matches higlight, it is not obvious we are higlighting, we at least set italics TODO
191+ // When text color matches higlight, the resulting STANDOUT is the same as on default text, so we at least set italics TODO
196192 int attrToPass = A_STANDOUT ;
197193 if (before_fg_color == passed_bg_color ) {
198194 attrToPass = attrToPass | A_ITALIC ;
199195 }
200- ch -> attr = (ch -> attr & A_BOLD || pairNum != 0 ) ? (ch -> attr | attrToPass ) : (unsigned int )attrs ;
196+ // If current char has is BOLD or its ColorPair Index is not the default 0,
197+ // apply our own attrToPass with STANDOUT + optionally ITALICS,
198+ // instead of the passed attrs, which has the BG highlight color
199+ ch -> attr = (ch -> attr & A_BOLD || currentCharPairNum != 0 ) ? (ch -> attr | attrToPass ) : (unsigned int )attrs ;
201200 ch ++ ;
202201 }
203202}
0 commit comments