Skip to content

Commit 79f4dbc

Browse files
authored
Merge pull request #28 from saying121/disable-avatar
Add `show_avatar` config field
2 parents 35ac2f3 + fd3aae4 commit 79f4dbc

8 files changed

Lines changed: 96 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file.
77
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

10+
## [Unreleased]
11+
12+
### Added
13+
14+
- Add `show_avatar` config field.
15+
1016
## [0.9.8]
1117

1218
### Fixed

README-CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ cargo_integr = true
255255

256256
# 是否用 fronted id 创建代码目录
257257
dir_with_frontend_id = true
258+
259+
# 是否展示头像,在kitty上工作的很好,在其他终端有可能出现问题
260+
show_avatar = false
258261
```
259262

260263
## 用户信息

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,29 @@ page_size = 25
254254
# - helix
255255
#
256256
# You can add additional parameters at the end.
257-
# like `editor = ["vim", "--noplugin"]`
257+
# Like `editor = ["vim", "--noplugin"]`
258258
editor = ["vim"]
259259

260260
# Set your selected programming language.
261261
lang = "rust"
262262

263263
# Set the location for storing code and test cases.
264264
# You can also starting with `~`
265-
# like `code_dir = "~/.local/share/lcode"`
265+
# Like `code_dir = "~/.local/share/lcode"`.
266266
code_dir = "/home/user/.local/share/lcode"
267267

268268
# Checkout the [Cookies (Important)](#cookies-important) section above.
269269
browser = ""
270270

271-
# For better rust coding. It will add a `Cargo.toml` file
271+
# For better rust coding. It will add a `Cargo.toml` file.
272272
cargo_integr = true
273273

274-
# use frontend id create code dir or not
274+
# Use frontend id create code dir or not.
275275
dir_with_frontend_id = true
276+
277+
# Show you lc avatar or not.
278+
# Works fine in kitty, may have problems in other terminals.
279+
show_avatar = false
276280
```
277281

278282
You can checkout the info/tab3 in tui for ensure cookies is valid.

crates/lcode-config/src/config/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ pub struct Config {
5353
pub code_dir: PathBuf,
5454
#[serde(default)]
5555
pub browser: String,
56-
#[serde(default = "cargo_default")]
56+
#[serde(default = "default_true")]
5757
pub cargo_integr: bool,
58-
#[serde(default = "default_ser_bool")]
59-
pub dir_with_frontend_id: bool, // create qs dir use frontend id
58+
#[serde(default)]
59+
/// create qs dir use frontend id
60+
pub dir_with_frontend_id: bool,
61+
#[serde(default)]
62+
pub show_avatar: bool,
6063
}
6164

6265
impl Config {
@@ -86,6 +89,7 @@ impl Default for Config {
8689
browser: String::new(),
8790
cargo_integr: true,
8891
dir_with_frontend_id: false,
92+
show_avatar: false,
8993
}
9094
}
9195
}

crates/lcode-config/src/config/user_serializes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@ where
2727
serializer.serialize_str(res)
2828
}
2929

30-
pub(super) const fn cargo_default() -> bool {
30+
pub(super) const fn default_true() -> bool {
3131
true
3232
}
33-
/// return false
34-
pub(super) const fn default_ser_bool() -> bool {
35-
false
36-
}
3733
pub(super) fn lang_default() -> String {
3834
"rust".to_owned()
3935
}

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

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
109
use notify_rust::Notification;
1110
use ratatui::prelude::*;
1211
use 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
}

crates/lcode/src/mytui/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub async fn run() -> Result<()> {
2929
Term::stop()?;
3030
break;
3131
},
32-
UserEvent::UserInfo(info) => app.get_status_done(*info)?,
32+
UserEvent::UserInfo(info) => app.get_status_done(*info),
3333
UserEvent::SubmitDone(s_res) => {
3434
// update info
3535
if s_res.total_correct == s_res.total_testcases {

crates/lcode/src/mytui/ui/info.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lcode_config::global::G_THEME;
1+
use lcode_config::global::{G_THEME, G_USER_CONFIG};
22
use ratatui::{prelude::*, widgets::*};
33
use ratatui_image::{Resize, thread::ThreadImage};
44

@@ -77,11 +77,13 @@ pub fn draw_info(f: &mut Frame, app: &mut App, area: Rect) {
7777
.split(chunks[0]);
7878
assert!(chunks1.len() >= 2);
7979
f.render_widget(user_info_list, chunks1[0]);
80-
draw_avatar(
81-
f,
82-
app,
83-
helper::top_right_rect(14, 9, chunks1[0].inner(Margin::new(1, 1))),
84-
);
80+
if G_USER_CONFIG.config.show_avatar {
81+
draw_avatar(
82+
f,
83+
app,
84+
helper::top_right_rect(14, 9, chunks1[0].inner(Margin::new(1, 1))),
85+
);
86+
}
8587
f.render_widget(pass_info_list, chunks1[1]);
8688
f.render_stateful_widget(keymap_list, chunks[1], &mut app.info.keymap.list_state);
8789
}

0 commit comments

Comments
 (0)