@@ -2,6 +2,7 @@ use crate::backend::nc_request::Token;
22use crate :: backend:: { nc_room:: NCRoomInterface , nc_talk:: NCBackend } ;
33use crate :: config:: Config ;
44use chrono:: { DateTime , Local , Utc } ;
5+ use colorhash:: ColorHash ;
56use ratatui:: {
67 prelude:: * ,
78 widgets:: { Block , Cell , HighlightSpacing , Row , Table , TableState } ,
@@ -23,6 +24,7 @@ pub struct ChatBox<'a> {
2324 unread_message_style : Style ,
2425 table_header_style : Style ,
2526 date_format : String ,
27+ user_styles : ColorHash ,
2628}
2729
2830impl ChatBox < ' _ > {
@@ -40,6 +42,7 @@ impl ChatBox<'_> {
4042 default_highlight_style : config. theme . default_highlight_style ( ) ,
4143 table_header_style : config. theme . table_header_style ( ) ,
4244 date_format : config. data . ui . date_format . clone ( ) ,
45+ user_styles : ColorHash :: new ( ) . lightness ( 70.0 ) ,
4346 }
4447 }
4548
@@ -90,13 +93,24 @@ impl ChatBox<'_> {
9093 last_date = date_str;
9194 }
9295
96+ let colour = self . user_styles . rgb ( & message_data. get_actor_id ( ) ) ;
97+
98+ #[ allow( clippy:: cast_possible_truncation) ]
99+ #[ allow( clippy:: cast_sign_loss) ]
100+ let name_style = Style :: new ( ) . fg ( Color :: Rgb (
101+ colour. red ( ) as u8 ,
102+ colour. green ( ) as u8 ,
103+ colour. blue ( ) as u8 ,
104+ ) ) ;
105+
93106 let name = textwrap:: wrap (
94107 message_data. get_name ( ) . to_string ( ) . as_str ( ) ,
95108 Options :: new ( NAME_WIDTH . into ( ) ) . break_words ( true ) ,
96109 )
97110 . into_iter ( )
98111 . map ( std:: borrow:: Cow :: into_owned)
99112 . map ( Line :: from)
113+ . map ( |l| l. style ( name_style) )
100114 . collect_vec ( ) ;
101115
102116 let message_string = message_data
@@ -237,21 +251,25 @@ mod tests {
237251 let mut mock_nc_backend = MockNCTalk :: new ( ) ;
238252 let mut mock_room = MockNCRoomInterface :: new ( ) ;
239253 let timestamp_1 = DateTime :: < Utc > :: from_timestamp ( 2000 , 0 ) . unwrap ( ) ;
254+ let actor_id_1 = "abcd1234" . to_string ( ) ;
240255 let mock_message_1 = NCMessage :: from ( NCReqDataMessage {
241256 id : 0 ,
242257 message : "Butz" . to_string ( ) ,
243258 messageType : "comment" . to_string ( ) ,
244259 actorDisplayName : "Hundi" . to_string ( ) ,
245260 timestamp : timestamp_1. timestamp ( ) ,
261+ actorId : actor_id_1. clone ( ) ,
246262 ..Default :: default ( )
247263 } ) ;
248264 let timestamp_2 = DateTime :: < Utc > :: from_timestamp ( 200_000 , 0 ) . unwrap ( ) ;
265+ let actor_id_2 = "1234abcd" . to_string ( ) ;
249266 let mock_message_2 = NCMessage :: from ( NCReqDataMessage {
250267 id : 1 ,
251268 message : "Bert" . to_string ( ) ,
252269 messageType : "comment" . to_string ( ) ,
253270 actorDisplayName : "Stinko" . to_string ( ) ,
254271 timestamp : timestamp_2. timestamp ( ) ,
272+ actorId : actor_id_2. clone ( ) ,
255273 ..Default :: default ( )
256274 } ) ;
257275 let message_tree = BTreeMap :: from ( [ ( 1 , mock_message_1) , ( 2 , mock_message_2) ] ) ;
@@ -294,6 +312,9 @@ mod tests {
294312
295313 terminal. backend ( ) . assert_buffer ( & expected) ;
296314
315+ let user_style_1 = Style :: default ( ) . fg ( Color :: Rgb ( 196 , 205 , 151 ) ) ; // Hash for Hundi
316+ let user_style_2 = Style :: default ( ) . fg ( Color :: Rgb ( 151 , 205 , 156 ) ) ; // Hash for Stinko
317+
297318 chat_box. update_messages ( & mock_nc_backend, & "123" . to_string ( ) ) ;
298319
299320 terminal
@@ -318,13 +339,15 @@ mod tests {
318339 Rect :: new ( 0 , 1 , 40 , 1 ) ,
319340 config. theme . default_highlight_style ( ) ,
320341 ) ;
342+ expected. set_style ( Rect :: new ( 6 , 2 , 20 , 1 ) , user_style_1) ;
321343 expected. set_style (
322344 Rect :: new ( 27 , 1 , 13 , 1 ) ,
323345 config
324346 . theme
325347 . default_highlight_style ( )
326348 . add_modifier ( Modifier :: BOLD ) ,
327349 ) ;
350+ expected. set_style ( Rect :: new ( 6 , 4 , 20 , 1 ) , user_style_2) ;
328351 expected. set_style (
329352 Rect :: new ( 27 , 3 , 13 , 1 ) ,
330353 config
0 commit comments