@@ -6,7 +6,6 @@ use leetcode_api::{
66 glob_leetcode,
77 leetcode:: resps:: { checkin:: TotalPoints , pass_qs:: PassData , user_data:: UserStatus } ,
88} ;
9- use miette:: IntoDiagnostic ;
109use notify_rust:: Notification ;
1110use ratatui:: prelude:: * ;
1211use ratatui_image:: { Resize , picker:: Picker , protocol:: StatefulProtocol , thread:: ThreadProtocol } ;
@@ -104,57 +103,80 @@ impl App<'_> {
104103 } ) ;
105104 }
106105
107- pub fn get_status_done (
108- & mut self ,
109- info : ( UserStatus , TotalPoints , PassData , Option < PathBuf > ) ,
110- ) -> miette:: Result < ( ) > {
106+ #[ expect( clippy:: cognitive_complexity, reason = "todo refactor" ) ]
107+ pub fn get_status_done ( & mut self , info : ( UserStatus , TotalPoints , PassData , Option < PathBuf > ) ) {
111108 (
112109 self . info . user_status ,
113110 self . info . points ,
114111 self . info . pass_data ,
115112 self . info . avatar_path ,
116113 ) = info;
117114
118- if self . img_state . is_none ( ) && self . info . avatar_path . is_some ( ) {
119- let mut picker =
120- Picker :: from_query_stdio ( ) . or ( Err ( miette:: miette!( "Image Picker error" ) ) ) ?;
121-
122- picker. set_background_color ( [ 255 , 0 , 255 , 0 ] ) ;
123- let dyn_img = image:: ImageReader :: open (
124- self . info
125- . avatar_path
126- . as_ref ( )
127- . expect ( "No avatar file" ) ,
128- )
129- . into_diagnostic ( ) ?
130- . with_guessed_format ( )
131- . into_diagnostic ( ) ?
132- . decode ( )
133- . into_diagnostic ( ) ?
134- . resize_to_fill ( 150 , 150 , ratatui_image:: FilterType :: Triangle ) ;
135-
136- // Send a [ResizeProtocol] to resize and encode it in a separate thread.
137- let ( tx_worker, rec_worker) = mpsc:: channel :: < ( StatefulProtocol , Resize , Rect ) > ( ) ;
138-
139- // Resize and encode in background thread.
140- let tx_main_render = self . events . tx . clone ( ) ;
141- thread:: spawn ( move || {
142- loop {
143- if let Ok ( ( mut protocol, resize, area) ) = rec_worker. recv ( ) {
144- protocol. resize_encode ( & resize, Rgba ( [ 0 ; 4 ] ) , area) ;
145- if let Err ( e) = tx_main_render. send ( UserEvent :: RedrawImg ( protocol) ) {
146- tracing:: error!( "{e}" ) ;
115+ ' o: {
116+ if self . img_state . is_none ( )
117+ && self . info . avatar_path . is_some ( )
118+ && G_USER_CONFIG . config . show_avatar
119+ {
120+ let mut picker = match Picker :: from_query_stdio ( ) {
121+ Ok ( p) => p,
122+ Err ( e) => {
123+ tracing:: error!( "Image error: {}" , e) ;
124+ break ' o;
125+ } ,
126+ } ;
127+
128+ picker. set_background_color ( [ 255 , 0 , 255 , 0 ] ) ;
129+
130+ let dyn_img = match image:: ImageReader :: open ( unsafe {
131+ self . info
132+ . avatar_path
133+ . as_ref ( )
134+ . unwrap_unchecked ( )
135+ } ) {
136+ Ok ( img) => img,
137+ Err ( e) => {
138+ tracing:: error!( "Image error: {}" , e) ;
139+ break ' o;
140+ } ,
141+ } ;
142+ let dyn_img = match dyn_img. with_guessed_format ( ) {
143+ Ok ( i) => i,
144+ Err ( e) => {
145+ tracing:: error!( "Image error: {}" , e) ;
146+ break ' o;
147+ } ,
148+ } ;
149+ let dyn_img = match dyn_img. decode ( ) {
150+ Ok ( i) => i,
151+ Err ( e) => {
152+ tracing:: error!( "Image error: {}" , e) ;
153+ break ' o;
154+ } ,
155+ } ;
156+ let dyn_img = dyn_img. resize_to_fill ( 150 , 150 , ratatui_image:: FilterType :: Triangle ) ;
157+
158+ // Send a [ResizeProtocol] to resize and encode it in a separate thread.
159+ let ( tx_worker, rec_worker) = mpsc:: channel :: < ( StatefulProtocol , Resize , Rect ) > ( ) ;
160+
161+ // Resize and encode in background thread.
162+ let tx_main_render = self . events . tx . clone ( ) ;
163+ thread:: spawn ( move || {
164+ loop {
165+ if let Ok ( ( mut protocol, resize, area) ) = rec_worker. recv ( ) {
166+ protocol. resize_encode ( & resize, Rgba ( [ 0 ; 4 ] ) , area) ;
167+ if let Err ( e) = tx_main_render. send ( UserEvent :: RedrawImg ( protocol) ) {
168+ tracing:: error!( "{e}" ) ;
169+ }
147170 }
148171 }
149- }
150- } ) ;
172+ } ) ;
151173
152- let async_state = ThreadProtocol :: new ( tx_worker, picker. new_resize_protocol ( dyn_img) ) ;
153- self . img_state = Some ( async_state) ;
174+ let async_state =
175+ ThreadProtocol :: new ( tx_worker, picker. new_resize_protocol ( dyn_img) ) ;
176+ self . img_state = Some ( async_state) ;
177+ }
154178 }
155179
156180 self . render ( ) ;
157-
158- Ok ( ( ) )
159181 }
160182}
0 commit comments