summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-04-08 01:33:11 +0200
committermo8it <mo8it@proton.me>2024-04-08 01:33:11 +0200
commitbd5503a0d363384fb551f3e303d0376a08d50831 (patch)
tree34483c229593da41310994bd0219e179fa584468 /src
parent25e855a009c47d30bfa4da93a93d8390df20fe45 (diff)
Show message on reset
Diffstat (limited to 'src')
-rw-r--r--src/list.rs11
-rw-r--r--src/list/state.rs18
-rw-r--r--src/main.rs2
3 files changed, 21 insertions, 10 deletions
diff --git a/src/list.rs b/src/list.rs
index e2af21d..3d91b8a 100644
--- a/src/list.rs
+++ b/src/list.rs
@@ -5,7 +5,7 @@ use crossterm::{
ExecutableCommand,
};
use ratatui::{backend::CrosstermBackend, Terminal};
-use std::io;
+use std::{fmt::Write, io};
mod state;
@@ -42,6 +42,8 @@ pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
}
};
+ ui_state.message.clear();
+
match key.code {
KeyCode::Char('q') => break,
KeyCode::Down | KeyCode::Char('j') => ui_state.select_next(),
@@ -50,9 +52,14 @@ pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
KeyCode::Char('r') => {
let selected = ui_state.selected();
- exercises[selected].reset()?;
+ let exercise = &exercises[selected];
+ exercise.reset()?;
state_file.reset(selected)?;
+
ui_state.table = ui_state.table.rows(UiState::rows(state_file, exercises));
+ ui_state
+ .message
+ .write_fmt(format_args!("The exercise {exercise} has been reset!"))?;
}
KeyCode::Char('c') => {
state_file.set_next_exercise_ind(ui_state.selected())?;
diff --git a/src/list/state.rs b/src/list/state.rs
index 3d2f0a6..534b535 100644
--- a/src/list/state.rs
+++ b/src/list/state.rs
@@ -10,6 +10,7 @@ use crate::{exercise::Exercise, state_file::StateFile};
pub struct UiState<'a> {
pub table: Table<'a>,
+ pub message: String,
selected: usize,
table_state: TableState,
last_ind: usize,
@@ -77,14 +78,13 @@ impl<'a> UiState<'a> {
.block(Block::default().borders(Borders::BOTTOM));
let selected = 0;
- let table_state = TableState::default().with_selected(Some(selected));
- let last_ind = exercises.len() - 1;
Self {
table,
selected,
- table_state,
- last_ind,
+ table_state: TableState::default().with_selected(Some(selected)),
+ last_ind: exercises.len() - 1,
+ message: String::with_capacity(128),
}
}
@@ -130,10 +130,14 @@ impl<'a> UiState<'a> {
&mut self.table_state,
);
- let help_footer =
- "↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit";
+ let message = if self.message.is_empty() {
+ // Help footer.
+ "↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit"
+ } else {
+ &self.message
+ };
frame.render_widget(
- Span::raw(help_footer),
+ Span::raw(message),
Rect {
x: 0,
y: area.height - 1,
diff --git a/src/main.rs b/src/main.rs
index cba525a..f6c4c20 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -124,7 +124,7 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
let (ind, exercise) = find_exercise(&name, &exercises)?;
exercise.reset()?;
state_file.reset(ind)?;
- println!("The file {} has been reset!", exercise.path.display());
+ println!("The exercise {exercise} has been reset!");
}
Some(Subcommands::Hint { name }) => {
let (_, exercise) = find_exercise(&name, &exercises)?;