Skip to content

Commit

Permalink
refactor(lcode): rename some variables
Browse files Browse the repository at this point in the history
  • Loading branch information
saying121 committed May 25, 2024
1 parent ed48c9d commit c82db8e
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 85 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ jobs:
url="https://github.com/saying121/lcode/releases/download/%s/lcode-%s-%s-unknown-linux-gnu.sha256"
tag=$GITHUB_REF_NAME
# shellcheck disable=SC2059
x86_url=$(printf $url "$tag" "$tag" x86_64)
wget -O x86_64.txt "$x86_url"
Expand Down
37 changes: 17 additions & 20 deletions crates/lcode/src/app/edit/cmds/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::mytui::my_widget::botton::{ButtonState, ButtonStates};
#[derive(Debug)]
#[derive(Default)]
pub struct ButState {
pub button_state: ButtonStates,
pub select_button: usize,
pub show: bool,
pub submitting: bool,
pub show: bool,
pub state: ButtonStates,
pub selected: usize,
pub submitting: bool,
}

impl ButState {
pub fn active_but(&mut self) {
self.button_state.states[self.select_button] = ButtonState::Active;
self.state.states[self.selected] = ButtonState::Active;
}
pub fn done(&mut self) {
self.submitting = false;
Expand All @@ -22,11 +22,11 @@ impl ButState {
}
pub fn test_done(&mut self) {
self.done();
self.button_state.states[0] = ButtonState::Normal;
self.state.states[0] = ButtonState::Normal;
}
pub fn submit_done(&mut self) {
self.done();
self.button_state.states[1] = ButtonState::Normal;
self.state.states[1] = ButtonState::Normal;
}
}

Expand All @@ -42,26 +42,23 @@ impl ButState {
self.show = !self.show;
}
pub fn left(&mut self) {
if self.button_state.states[self.select_button] != ButtonState::Active {
self.button_state.states[self.select_button] = ButtonState::Normal;
if self.state.states[self.selected] != ButtonState::Active {
self.state.states[self.selected] = ButtonState::Normal;
}
self.select_button = self.select_button.saturating_sub(1);
if self.button_state.states[self.select_button] != ButtonState::Active {
self.button_state.states[self.select_button] = ButtonState::Selected;
self.selected = self.selected.saturating_sub(1);
if self.state.states[self.selected] != ButtonState::Active {
self.state.states[self.selected] = ButtonState::Selected;
}
}
pub fn right(&mut self) {
if self.button_state.states[self.select_button] != ButtonState::Active {
self.button_state.states[self.select_button] = ButtonState::Normal;
if self.state.states[self.selected] != ButtonState::Active {
self.state.states[self.selected] = ButtonState::Normal;
}

self.select_button = self
.select_button
.saturating_add(1)
.min(1);
self.selected = self.selected.saturating_add(1).min(1);

if self.button_state.states[self.select_button] != ButtonState::Active {
self.button_state.states[self.select_button] = ButtonState::Selected;
if self.state.states[self.selected] != ButtonState::Active {
self.state.states[self.selected] = ButtonState::Selected;
}
}
}
11 changes: 7 additions & 4 deletions crates/lcode/src/app/edit/cmds/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use ratatui::widgets::ScrollbarState;
#[derive(Debug)]
#[derive(Default)]
pub struct ContentState {
pub content_row_num: usize,
pub vert_scroll_state: ScrollbarState,
pub vert_scroll: usize,
pub column_len: usize,
pub content_row_num: usize,

pub vert_scroll_state: ScrollbarState,
pub vert_scroll: usize,

pub column_len: usize,

pub horizontal_scroll_state: ScrollbarState,
pub horizontal_scroll: usize,
}
Expand Down
9 changes: 6 additions & 3 deletions crates/lcode/src/app/edit/cmds/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use ratatui::widgets::ScrollbarState;
#[derive(Debug)]
#[derive(Default)]
pub struct SubmitState {
pub result: RunResult,
pub show: bool,
pub show: bool,
pub content: RunResult,

pub vert_scroll_state: ScrollbarState,
pub vert_scroll: usize,

pub hori_scroll_state: ScrollbarState,
pub hori_scroll: usize,
pub row_len: usize,

pub row_len: usize,
}

impl SubmitState {
Expand Down
9 changes: 6 additions & 3 deletions crates/lcode/src/app/edit/cmds/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use ratatui::widgets::ScrollbarState;
#[derive(Debug)]
#[derive(Default)]
pub struct TestState {
pub result: RunResult,
pub show: bool,
pub show: bool,
pub content: RunResult,

pub vert_scroll_state: ScrollbarState,
pub vert_scroll: usize,

pub hori_scroll_state: ScrollbarState,
pub hori_scroll: usize,
pub row_len: usize,

pub row_len: usize,
}

impl TestState {
Expand Down
6 changes: 3 additions & 3 deletions crates/lcode/src/app/impl_app/edit_qs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'app_lf> App<'app_lf> {
}
pub fn menu_button_trig(&mut self) -> bool {
self.edit.button.active_but();
match self.edit.button.select_button {
match self.edit.button.selected {
0 => self.test_code(),
1 => self.submit_code(),
_ => false,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl<'app_lf> App<'app_lf> {
false
}
pub fn test_done(&mut self, res: RunResult) {
self.edit.test.result = res;
self.edit.test.content = res;

self.edit.test.open();
self.edit.submit.close();
Expand All @@ -125,7 +125,7 @@ impl<'app_lf> App<'app_lf> {
self.render();
}
pub fn submit_done(&mut self, res: RunResult) {
self.edit.submit.result = res;
self.edit.submit.content = res;

self.edit.submit.open();
self.edit.test.close();
Expand Down
28 changes: 14 additions & 14 deletions crates/lcode/src/app/info/cmds/keymaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use ratatui::widgets::{ListItem, ListState};
#[derive(Debug)]
#[derive(Default)]
pub struct KeymapState<'key> {
pub keymaps_state: ListState,
pub keymaps_items: Vec<ListItem<'key>>,
pub items: Vec<ListItem<'key>>,
pub list_state: ListState,
}

impl<'key> KeymapState<'key> {
pub fn new(keymaps_items: Vec<ListItem<'key>>) -> Self {
Self {
keymaps_state: ListState::default(),
keymaps_items,
list_state: ListState::default(),
items: keymaps_items,
}
}
pub fn trigger(&self) -> bool {
let a = self
.keymaps_state
.list_state
.selected()
.unwrap_or_default();
if a == 0 {
Expand All @@ -27,28 +27,28 @@ impl<'key> KeymapState<'key> {
}

pub fn first(&mut self) -> bool {
self.keymaps_state.select(Some(0));
self.list_state.select(Some(0));
true
}
pub fn last(&mut self) -> bool {
self.keymaps_state
.select(Some(self.keymaps_items.len() - 1));
self.list_state
.select(Some(self.items.len() - 1));
true
}
pub fn prev(&mut self) -> bool {
let i = match self.keymaps_state.selected() {
Some(i) => (self.keymaps_items.len() + i - 1) % self.keymaps_items.len(),
let i = match self.list_state.selected() {
Some(i) => (self.items.len() + i - 1) % self.items.len(),
None => 0,
};
self.keymaps_state.select(Some(i));
self.list_state.select(Some(i));
true
}
pub fn next(&mut self) -> bool {
let i = match self.keymaps_state.selected() {
Some(i) => (i + 1) % self.keymaps_items.len(),
let i = match self.list_state.selected() {
Some(i) => (i + 1) % self.items.len(),
None => 0,
};
self.keymaps_state.select(Some(i));
self.list_state.select(Some(i));
true
}
}
8 changes: 4 additions & 4 deletions crates/lcode/src/app/select/cmds/sync_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#[derive(Debug)]
#[derive(Default)]
pub struct BarState {
pub show: bool,
pub cur_perc: f64,
pub show: bool,
pub percent: f64,
}

impl BarState {
pub fn update(&mut self, cur_perc: f64) {
self.cur_perc = cur_perc;
pub fn update(&mut self, perc: f64) {
self.percent = perc;
}
pub fn close(&mut self) {
self.show = false;
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode/src/app/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<'tab0> SelectQS<'tab0> {
..Default::default()
}
}
pub fn update_percent(&mut self, cur_perc: f64) {
self.sync_bar.update(cur_perc);
pub fn update_percent(&mut self, perc: f64) {
self.sync_bar.update(perc);
}
/// refresh `filtered_qs`
pub fn filter_by_input(&mut self) {
Expand Down
22 changes: 11 additions & 11 deletions crates/lcode/src/app/topic/cmds/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use ratatui::widgets::ListState;
#[derive(Debug)]
#[derive(Default)]
pub struct DiffState {
pub user_diff: String,
pub difficulties: Box<[String]>,
pub diff_list_state: ListState,
pub user_diff: String,
pub difficulties: Box<[String]>,
pub list_state: ListState,
}

impl DiffState {
pub fn new(difficulties: Box<[String]>) -> Self {
Self {
user_diff: String::new(),
difficulties,
diff_list_state: ListState::default(),
list_state: ListState::default(),
}
}
pub fn toggle_diff(&mut self) {
let index = self
.diff_list_state
.list_state
.selected()
.unwrap_or_default();
let diff = self
Expand All @@ -34,26 +34,26 @@ impl DiffState {
}
}
pub fn first(&mut self) {
self.diff_list_state.select(Some(0));
self.list_state.select(Some(0));
}
pub fn last(&mut self) {
self.diff_list_state
self.list_state
.select(Some(self.difficulties.len()));
}
pub fn prev(&mut self) {
let len = self.difficulties.len().max(1);
let i = self
.diff_list_state
.list_state
.selected()
.map_or(0, |i| (len + i - 1) % len);
self.diff_list_state.select(Some(i));
self.list_state.select(Some(i));
}
pub fn next(&mut self) {
let len = self.difficulties.len().max(1);
let i = self
.diff_list_state
.list_state
.selected()
.map_or(0, |i| (len + i + 1) % len);
self.diff_list_state.select(Some(i));
self.list_state.select(Some(i));
}
}
2 changes: 1 addition & 1 deletion crates/lcode/src/app/topic/cmds/intputline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::app::TuiMode;
#[derive(Debug)]
#[derive(Default)]
pub struct InputLine<'line> {
pub text_line: TextArea<'line>,
pub mode: TuiMode,
pub text_line: TextArea<'line>,
}

impl<'line> InputLine<'line> {
Expand Down
8 changes: 4 additions & 4 deletions crates/lcode/src/app/topic/cmds/sync_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#[derive(Debug)]
#[derive(Default)]
pub struct BarState {
pub show: bool,
pub cur_perc: f64,
pub show: bool,
pub percent: f64,
}

impl BarState {
pub fn close(&mut self) {
self.show = false;
}

pub fn update(&mut self, cur_perc: f64) {
self.cur_perc = cur_perc;
pub fn update(&mut self, percent: f64) {
self.percent = percent;
}
}
4 changes: 2 additions & 2 deletions crates/lcode/src/app/topic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ impl<'tab2> TopicTagsQS<'tab2> {
)
}

pub fn update_percent(&mut self, cur_perc: f64) {
self.sync_bar.update(cur_perc);
pub fn update_percent(&mut self, perc: f64) {
self.sync_bar.update(perc);
}
/// refresh `filtered_qs`
pub fn refresh_filter_by_input(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions crates/lcode/src/mytui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub async fn run() -> Result<()> {
},
UserEvent::TestDone(t_res) => app.test_done(*t_res),
UserEvent::GetQsDone(qs) => app.get_qs_done(*qs).await,
UserEvent::Syncing(cur_perc) => app.select.update_percent(cur_perc),
UserEvent::SyncingNew(cur_perc) => app.topic.update_percent(cur_perc),
UserEvent::Syncing(perc) => app.select.update_percent(perc),
UserEvent::SyncingNew(perc) => app.topic.update_percent(perc),
UserEvent::SyncDone => app.sync_done().await,
UserEvent::SyncDoneNew => app.sync_new_done().await,
UserEvent::Render => {
Expand Down
Loading

0 comments on commit c82db8e

Please sign in to comment.