mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 14:57:32 -05:00
Fix selected when there are no rows
This commit is contained in:
parent
686143100f
commit
470dc65956
2 changed files with 53 additions and 26 deletions
|
@ -72,7 +72,9 @@ pub fn list(app_state: &mut AppState) -> Result<()> {
|
||||||
ui_state.message.push_str(message);
|
ui_state.message.push_str(message);
|
||||||
}
|
}
|
||||||
KeyCode::Char('r') => {
|
KeyCode::Char('r') => {
|
||||||
let exercise = ui_state.reset_selected()?;
|
let Some(exercise) = ui_state.reset_selected()? else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
ui_state = ui_state.with_updated_rows();
|
ui_state = ui_state.with_updated_rows();
|
||||||
ui_state
|
ui_state
|
||||||
|
|
|
@ -22,15 +22,14 @@ pub struct UiState<'a> {
|
||||||
pub filter: Filter,
|
pub filter: Filter,
|
||||||
app_state: &'a mut AppState,
|
app_state: &'a mut AppState,
|
||||||
table_state: TableState,
|
table_state: TableState,
|
||||||
selected: usize,
|
n_rows: usize,
|
||||||
last_ind: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> UiState<'a> {
|
impl<'a> UiState<'a> {
|
||||||
pub fn with_updated_rows(mut self) -> Self {
|
pub fn with_updated_rows(mut self) -> Self {
|
||||||
let current_exercise_ind = self.app_state.current_exercise_ind();
|
let current_exercise_ind = self.app_state.current_exercise_ind();
|
||||||
|
|
||||||
let mut rows_counter: usize = 0;
|
self.n_rows = 0;
|
||||||
let rows = self
|
let rows = self
|
||||||
.app_state
|
.app_state
|
||||||
.exercises()
|
.exercises()
|
||||||
|
@ -52,7 +51,7 @@ impl<'a> UiState<'a> {
|
||||||
"PENDING".yellow()
|
"PENDING".yellow()
|
||||||
};
|
};
|
||||||
|
|
||||||
rows_counter += 1;
|
self.n_rows += 1;
|
||||||
|
|
||||||
let next = if ind == current_exercise_ind {
|
let next = if ind == current_exercise_ind {
|
||||||
">>>>".bold().red()
|
">>>>".bold().red()
|
||||||
|
@ -70,8 +69,15 @@ impl<'a> UiState<'a> {
|
||||||
|
|
||||||
self.table = self.table.rows(rows);
|
self.table = self.table.rows(rows);
|
||||||
|
|
||||||
self.last_ind = rows_counter.saturating_sub(1);
|
if self.n_rows == 0 {
|
||||||
self.select(self.selected.min(self.last_ind));
|
self.table_state.select(None);
|
||||||
|
} else {
|
||||||
|
self.table_state.select(Some(
|
||||||
|
self.table_state
|
||||||
|
.selected()
|
||||||
|
.map_or(0, |selected| selected.min(self.n_rows - 1)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -107,42 +113,53 @@ impl<'a> UiState<'a> {
|
||||||
.with_offset(selected.saturating_sub(10))
|
.with_offset(selected.saturating_sub(10))
|
||||||
.with_selected(Some(selected));
|
.with_selected(Some(selected));
|
||||||
|
|
||||||
|
let filter = Filter::None;
|
||||||
|
let n_rows = app_state.exercises().len();
|
||||||
|
|
||||||
let slf = Self {
|
let slf = Self {
|
||||||
table,
|
table,
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
filter: Filter::None,
|
filter,
|
||||||
app_state,
|
app_state,
|
||||||
table_state,
|
table_state,
|
||||||
selected,
|
n_rows,
|
||||||
last_ind: 0,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
slf.with_updated_rows()
|
slf.with_updated_rows()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select(&mut self, ind: usize) {
|
|
||||||
self.selected = ind;
|
|
||||||
self.table_state.select(Some(ind));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn select_next(&mut self) {
|
pub fn select_next(&mut self) {
|
||||||
let next = (self.selected + 1).min(self.last_ind);
|
if self.n_rows > 0 {
|
||||||
self.select(next);
|
let next = self
|
||||||
|
.table_state
|
||||||
|
.selected()
|
||||||
|
.map_or(0, |selected| (selected + 1).min(self.n_rows - 1));
|
||||||
|
self.table_state.select(Some(next));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_previous(&mut self) {
|
pub fn select_previous(&mut self) {
|
||||||
let previous = self.selected.saturating_sub(1);
|
if self.n_rows > 0 {
|
||||||
self.select(previous);
|
let previous = self
|
||||||
|
.table_state
|
||||||
|
.selected()
|
||||||
|
.map_or(0, |selected| selected.saturating_sub(1));
|
||||||
|
self.table_state.select(Some(previous));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn select_first(&mut self) {
|
pub fn select_first(&mut self) {
|
||||||
self.select(0);
|
if self.n_rows > 0 {
|
||||||
|
self.table_state.select(Some(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn select_last(&mut self) {
|
pub fn select_last(&mut self) {
|
||||||
self.select(self.last_ind);
|
if self.n_rows > 0 {
|
||||||
|
self.table_state.select(Some(self.n_rows - 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
|
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
|
||||||
|
@ -195,18 +212,26 @@ impl<'a> UiState<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_selected(&mut self) -> Result<&'static Exercise> {
|
pub fn reset_selected(&mut self) -> Result<Option<&'static Exercise>> {
|
||||||
self.app_state.set_pending(self.selected)?;
|
let Some(selected) = self.table_state.selected() else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.app_state.set_pending(selected)?;
|
||||||
// TODO: Take care of filters!
|
// TODO: Take care of filters!
|
||||||
let exercise = &self.app_state.exercises()[self.selected];
|
let exercise = &self.app_state.exercises()[selected];
|
||||||
exercise.reset()?;
|
exercise.reset()?;
|
||||||
|
|
||||||
Ok(exercise)
|
Ok(Some(exercise))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn selected_to_current_exercise(&mut self) -> Result<()> {
|
pub fn selected_to_current_exercise(&mut self) -> Result<()> {
|
||||||
|
let Some(selected) = self.table_state.selected() else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: Take care of filters!
|
// TODO: Take care of filters!
|
||||||
self.app_state.set_current_exercise_ind(self.selected)
|
self.app_state.set_current_exercise_ind(selected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue