Skip to content

Commit d096526

Browse files
committed
fix(avatar): when avatar empty don't panic
1 parent 727a327 commit d096526

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

crates/lcode/src/app/impl_app/get_info.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ impl<'app> App<'app> {
3939
.await;
4040

4141
let avatar_path = avatar_path
42-
.as_os_str()
43-
.to_str()
42+
.as_ref()
43+
.map(|v| {
44+
v.as_os_str()
45+
.to_str()
46+
.unwrap_or_default()
47+
})
4448
.unwrap_or_default();
4549
if res_cn.checkin_ok() {
4650
Notification::new()
@@ -102,7 +106,7 @@ impl<'app> App<'app> {
102106

103107
pub fn get_status_done(
104108
&mut self,
105-
info: (UserStatus, TotalPoints, PassData, PathBuf),
109+
info: (UserStatus, TotalPoints, PassData, Option<PathBuf>),
106110
) -> miette::Result<()> {
107111
(
108112
self.info.user_status,
@@ -111,7 +115,7 @@ impl<'app> App<'app> {
111115
self.info.avatar_path,
112116
) = info;
113117

114-
if self.img_state.is_none() {
118+
if self.img_state.is_none() && self.info.avatar_path.is_some() {
115119
#[cfg(not(target_os = "windows"))]
116120
let mut picker =
117121
Picker::from_termios().or(Err(miette::miette!("Image Picker error")))?;
@@ -120,13 +124,18 @@ impl<'app> App<'app> {
120124

121125
picker.guess_protocol();
122126
picker.background_color = Some(Rgb::<u8>([255, 0, 255]));
123-
let dyn_img = image::ImageReader::open(&self.info.avatar_path)
124-
.into_diagnostic()?
125-
.with_guessed_format()
126-
.into_diagnostic()?
127-
.decode()
128-
.into_diagnostic()?
129-
.resize_to_fill(150, 150, ratatui_image::FilterType::Triangle);
127+
let dyn_img = image::ImageReader::open(
128+
self.info
129+
.avatar_path
130+
.as_ref()
131+
.expect("No avatar file"),
132+
)
133+
.into_diagnostic()?
134+
.with_guessed_format()
135+
.into_diagnostic()?
136+
.decode()
137+
.into_diagnostic()?
138+
.resize_to_fill(150, 150, ratatui_image::FilterType::Triangle);
130139

131140
// Send a [ResizeProtocol] to resize and encode it in a separate thread.
132141
let (tx_worker, rec_worker) =

crates/lcode/src/app/info/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Info<'tab3> {
1717

1818
pub points: TotalPoints,
1919
pub pass_data: PassData,
20-
pub avatar_path: PathBuf,
20+
pub avatar_path: Option<PathBuf>,
2121
}
2222

2323
// keymaps

crates/lcode/src/mytui/myevent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum UserEvent {
2828
SubmitDone(Box<RunResult>),
2929
TestDone(Box<RunResult>),
3030

31-
UserInfo(Box<(UserStatus, TotalPoints, PassData, PathBuf)>),
31+
UserInfo(Box<(UserStatus, TotalPoints, PassData, Option<PathBuf>)>),
3232

3333
Quit,
3434

crates/leetcode-api/src/leetcode/impl_lc/user_info.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ use crate::{
2626
// some info
2727
impl LeetCode {
2828
/// download user avatar image
29-
pub async fn dow_user_avator(&self, status: &UserStatus) -> PathBuf {
30-
let avatar_url = status
31-
.avatar
32-
.as_deref()
33-
.unwrap_or_default();
29+
pub async fn dow_user_avator(&self, status: &UserStatus) -> Option<PathBuf> {
30+
let avatar_url = status.avatar.as_deref()?;
3431
let mut avatar_path = G_CACHE_DIR.clone();
3532
if let Ok(url) = Url::parse(avatar_url) {
3633
if let Some(url_path) = url.path_segments() {
@@ -58,7 +55,7 @@ impl LeetCode {
5855
}
5956
}
6057
}
61-
avatar_path
58+
Some(avatar_path)
6259
}
6360
pub async fn pass_qs_status(&self, user_slug: &str) -> Result<PassData> {
6461
let json = GraphqlQuery::pass_status(user_slug);

0 commit comments

Comments
 (0)