Skip to content

Commit

Permalink
feat(lcode): tui press a can add last test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
saying121 committed May 28, 2024
1 parent caf3d95 commit e7d99cf
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 151 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- leetcode-api: add api `add_last_test_case`.
- lcode: when submit not pass test case can add last test case.

### Perf

Expand Down
2 changes: 1 addition & 1 deletion crates/lcode-config/src/config/read_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn get_user_conf() -> Result<User> {
let key: TuiKeyMap = toml::from_str(&key)
.into_diagnostic()
.context("get keymap failed")?;
user.keymap.add_keymap(key.keymap);
user.keymap.add_keymap(key.map_set);

Ok(user)
}
16 changes: 10 additions & 6 deletions crates/lcode-config/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ actions!(

(SYNC_INDEX, "sync_index");

(ESCAPE, "escape")
(ESCAPE, "escape");

(ADD_TEST_CASE, "add_test_case")
);

#[derive(Clone)]
Expand Down Expand Up @@ -93,16 +95,16 @@ impl Hash for KeyMap {
#[derive(Debug)]
pub struct TuiKeyMap {
#[serde(default)]
pub keymap: HashSet<KeyMap>,
pub map_set: HashSet<KeyMap>,
}

impl TuiKeyMap {
/// Add extra keymap
pub fn add_keymap(&mut self, add: HashSet<KeyMap>) {
for ele in &add {
self.keymap.remove(ele);
self.map_set.remove(ele);
}
self.keymap.extend(add);
self.map_set.extend(add);
}
}

Expand Down Expand Up @@ -165,11 +167,13 @@ impl Default for TuiKeyMap {
(vec![Key::new(CTRL, kc('p'))], TOGGLE_MENU, "Show or hide menu(only edit)");
(vec![Key::new(CTRL, kc('t'))], TOGGLE_TEST_RES, "Show or hide test result (only tab1/edit)");
(vec![Key::new(CTRL, kc('s'))], TOGGLE_SUBMIT_RES, "Show or hide submit result (only tab1/edit)");
(vec![Key::new(NO_CONTROL, enter)], TOGGLE_CURSOR, "Trigger cursor item, in edit pop menu will active button (add or rm topic_tags, or goto tab1/edit)")
(vec![Key::new(NO_CONTROL, enter)], TOGGLE_CURSOR, "Trigger cursor item, in edit pop menu will active button (add or rm topic_tags, or goto tab1/edit)");

(vec![Key::new(NO_CONTROL, kc('a'))], ADD_TEST_CASE, "When submit error can add test case. (tab1/edit)")

);
let keymap = HashSet::from(mps);

Self { keymap }
Self { map_set: keymap }
}
}
2 changes: 1 addition & 1 deletion crates/lcode-config/tests/config_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ fn macos_path() {
#[test]
fn get_conf_work() {
// _ = &USER_CONFIG.config;
dbg!(&G_USER_CONFIG.keymap.keymap);
dbg!(&G_USER_CONFIG.keymap.map_set);
}
6 changes: 5 additions & 1 deletion crates/lcode/src/app/dispatch/handle_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'app_lf> App<'app_lf> {
}
return;
}
for KeyMap { keys, action: r#do, .. } in &G_USER_CONFIG.keymap.keymap {
for KeyMap { keys, action: r#do, .. } in &G_USER_CONFIG.keymap.map_set {
if keys.is_empty() || keys[0] != keyevent.into() {
continue;
}
Expand Down Expand Up @@ -91,6 +91,10 @@ impl<'app_lf> App<'app_lf> {
},
TuiIndex::Edit if matches!(self.edit.code_block.mode, TuiMode::OutEdit) => match action
{
ADD_TEST_CASE if self.edit.add_test_case() && self.edit.submit.show => {
self.add_test_case()
},

UP => self.edit.vertical_scroll_k(),
DOWN => self.edit.vertical_scroll_j(),
LEFT => self.edit.horizontal_scroll_h(),
Expand Down
2 changes: 1 addition & 1 deletion crates/lcode/src/app/dispatch/next_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl NextKey {
self.times = 1;
self.keymaps = G_USER_CONFIG
.keymap
.keymap
.map_set
.iter()
.filter(|v| v.keys.len() > 1 && v.keys[0] == keyevent.into())
.collect();
Expand Down
9 changes: 9 additions & 0 deletions crates/lcode/src/app/edit/cmds/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ pub struct SubmitState {
pub hori_scroll: usize,

pub row_len: usize,

pub need_add_test_case: bool,
}

impl SubmitState {
pub fn not_need_add(&mut self) {
self.need_add_test_case = false;
}
pub const fn need_add(&self) -> bool {
self.need_add_test_case
}

pub fn toggle(&mut self) {
self.show = !self.show;
}
Expand Down
5 changes: 5 additions & 0 deletions crates/lcode/src/app/edit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ impl<'tab1> EditCode<'tab1> {
pub fn start_edit_tui(&mut self) -> bool {
self.code_block.start_edit_tui()
}

/// when true, mean can add a new test case
pub const fn add_test_case(&self) ->bool{
self.submit.need_add()
}
}

// Show only one pop view every time.
Expand Down
1 change: 1 addition & 0 deletions crates/lcode/src/app/impl_app/edit_qs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl<'app_lf> App<'app_lf> {
self.render();
}
pub fn submit_done(&mut self, res: RunResult) {
self.edit.submit.need_add_test_case = !res.last_testcase.is_empty();
self.edit.submit.content = res;

self.edit.submit.open();
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode/src/app/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub struct Info<'tab3> {
// keymaps
impl<'tab3> Info<'tab3> {
pub fn new() -> Self {
let mut pat = Vec::with_capacity(G_USER_CONFIG.keymap.keymap.len() + 1);
let mut pat = Vec::with_capacity(G_USER_CONFIG.keymap.map_set.len() + 1);
pat.push(ListItem::new(
"⭐ Give the project a star, cursor here Press Enter",
));

let a = G_USER_CONFIG
.keymap
.keymap
.map_set
.iter()
.map(|v| ListItem::new(v.to_string()));
pat.extend(a);
Expand Down
32 changes: 32 additions & 0 deletions crates/lcode/src/app/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ pub struct App<'app> {
pub events: EventsHandler,
}

impl<'app_lf> App<'app_lf> {
pub fn add_test_case(&mut self) -> bool {
let id = self
.edit
.submit
.content
.question_id
.parse()
.expect("submit res question id parse error");

// SAFETY: `last_testcase` field can live on whole app
let case: &'static str = unsafe {
std::mem::transmute(
self.edit
.submit
.content
.last_testcase
.as_str(),
)
};
tokio::spawn(async move {
glob_leetcode()
.await
.add_test_case(id, case)
.await
.ok();
});
self.edit.submit.not_need_add();

true
}
}
impl<'app_lf> App<'app_lf> {
/// edit cursor qs with outer editor, for select tab
pub async fn select_edit_cur_qs(&mut self) -> Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion crates/lcode/src/mytui/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ pub fn title_block<'a, T>(title: T) -> Block<'a>
where
T: Into<Line<'a>>,
{
let title = Title::from(title).alignment(Alignment::Center);
let title = Title::from(title);
Block::default()
.title(title)
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
}
18 changes: 7 additions & 11 deletions crates/lcode/src/mytui/ui/edit_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ pub fn draw_pop_submit(f: &mut Frame, app: &mut App, area: Rect) {
let area = centered_rect_percent(60, 60, area);
f.render_widget(Clear, area);

let block = title_block(Line::from(vec![
"<esc> exit, j/k up/down ".into(),
"Submit 🌊".set_style(G_THEME.edit.submit_title),
]))
.border_style(G_THEME.edit.submit_border);
let mut title = vec!["Submit 🌊".set_style(G_THEME.edit.submit_title)];
if app.edit.submit.need_add() {
title.push("Can add last test case 🧪".red());
}
let block = title_block(Line::from(title));
f.render_widget(block, area);

let layout = helper::nested_rect(area, 1, 1, 1, 1);
Expand Down Expand Up @@ -232,12 +232,8 @@ pub fn draw_pop_test(f: &mut Frame, app: &mut App, area: Rect) {
pub fn draw_save_state(f: &mut Frame, _app: &App, area: Rect) {
let area = centered_rect_percent(30, 20, area);

let para = Paragraph::new("save code done").block(
Block::default()
.borders(Borders::ALL)
.title("default press `esc` close")
.title_alignment(Alignment::Center),
);
let para =
Paragraph::new("save code done").block(helper::title_block("default press `esc` close"));

f.render_widget(Clear, area);
f.render_widget(para, area);
Expand Down
71 changes: 22 additions & 49 deletions crates/lcode/src/mytui/ui/filter_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,20 @@ pub fn draw_difficults(f: &mut Frame, app: &mut App, area: Rect) {

let list = List::new(items)
.block(
Block::default()
.border_style(style)
.borders(Borders::ALL)
.title(
if app
.topic
.difficulty
.user_diff
.is_empty()
{
"Difficulty"
}
else {
&app.topic.difficulty.user_diff
},
)
.title_alignment(Alignment::Center),
helper::title_block(
if app
.topic
.difficulty
.user_diff
.is_empty()
{
"Difficulty"
}
else {
&app.topic.difficulty.user_diff
},
)
.border_style(style),
)
.highlight_style(G_THEME.topic.list_highlight);
f.render_stateful_widget(list, area, &mut app.topic.difficulty.list_state);
Expand Down Expand Up @@ -121,13 +118,7 @@ pub fn draw_all_topic_tags(f: &mut Frame, app: &mut App, area: Rect) {
G_THEME.topic.inactive_border
};
let list = List::new(items)
.block(
Block::default()
.border_style(style)
.borders(Borders::ALL)
.title("All Topic Tag")
.title_alignment(Alignment::Center),
)
.block(helper::title_block("All Topic Tag").border_style(style))
.highlight_style(G_THEME.topic.list_highlight);
// .highlight_symbol(">>");
f.render_stateful_widget(list, area, &mut app.topic.topic.topic_tags_state);
Expand Down Expand Up @@ -160,13 +151,7 @@ pub fn draw_user_topic(f: &mut Frame, app: &mut App, area: Rect) {
G_THEME.topic.inactive_border
};
let list = List::new(items)
.block(
Block::default()
.border_style(style)
.borders(Borders::ALL)
.title("User Topic Tag")
.title_alignment(Alignment::Center),
)
.block(helper::title_block("User Topic Tag").border_style(style))
.highlight_style(G_THEME.topic.list_highlight);
// .highlight_symbol(">>");
f.render_stateful_widget(list, area, &mut app.topic.topic.user_topic_tags_state);
Expand All @@ -189,13 +174,7 @@ pub fn draw_filtered_qs(f: &mut Frame, app: &mut App, area: Rect) {
};
let count = items.len();
let list = List::new(items)
.block(
Block::default()
.title(format!("Questions count: {}", count))
.title_alignment(Alignment::Center)
.border_style(style)
.borders(Borders::ALL),
)
.block(helper::title_block(format!("Questions count: {}", count)).border_style(style))
.highlight_style(G_THEME.topic.list_highlight);
// .highlight_symbol(">>");
f.render_stateful_widget(
Expand All @@ -215,11 +194,7 @@ pub fn draw_sync_progress_new(f: &mut Frame, app: &App, area: Rect) {
G_THEME.topic.label,
);
let gauge = Gauge::default()
.block(
Block::default()
.title("waiting sync ……")
.borders(Borders::ALL),
)
.block(helper::title_block("waiting sync ……"))
.gauge_style(G_THEME.topic.gauge)
.label(label)
.ratio(app.topic.sync_bar.percent);
Expand Down Expand Up @@ -247,11 +222,9 @@ pub fn draw_input_line(f: &mut Frame, app: &mut App, area: Rect) {
G_THEME.topic.text_line_outedit,
),
};
app.topic.inputline.text_line.set_block(
Block::default()
.borders(Borders::ALL)
.set_style(sty)
.title(title),
);
app.topic
.inputline
.text_line
.set_block(helper::title_block(title).set_style(sty));
f.render_widget(app.topic.inputline.text_line.widget(), area);
}
24 changes: 5 additions & 19 deletions crates/lcode/src/mytui/ui/info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use lcode_config::global::G_THEME;
use ratatui::{prelude::*, widgets::*};

use crate::app::inner::App;
use crate::{app::inner::App, mytui::helper};

pub fn draw_info(f: &mut Frame, app: &mut App, area: Rect) {
let chunks = Layout::default()
Expand Down Expand Up @@ -60,27 +60,13 @@ pub fn draw_info(f: &mut Frame, app: &mut App, area: Rect) {
let pass_data = vec![ListItem::new("🐾 Pass Info")]
.into_iter()
.chain(pass_data);
let pass_info_list = List::new(pass_data).block(
Block::default()
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
.title("Pass Info"),
);
let pass_info_list = List::new(pass_data).block(helper::title_block("Pass Info"));

let user_info_list = List::new(items.into_iter().map(ListItem::new)).block(
Block::default()
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
.title("User Info"),
);
let user_info_list =
List::new(items.into_iter().map(ListItem::new)).block(helper::title_block("User Info"));

let keymap_list = List::new(app.info.keymap.items.clone())
.block(
Block::default()
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
.title("Keymaps"),
)
.block(helper::title_block("Keymaps"))
.highlight_style(G_THEME.info.list_highlight)
.highlight_symbol(">>");

Expand Down
Loading

0 comments on commit e7d99cf

Please sign in to comment.