mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Hide input and disable its line buffering
This commit is contained in:
parent
17877366b7
commit
2d0860fe1b
4 changed files with 54 additions and 14 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -490,6 +490,7 @@ dependencies = [
|
||||||
"crossterm",
|
"crossterm",
|
||||||
"notify-debouncer-mini",
|
"notify-debouncer-mini",
|
||||||
"os_pipe",
|
"os_pipe",
|
||||||
|
"rustix",
|
||||||
"rustlings-macros",
|
"rustlings-macros",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -57,6 +57,9 @@ serde_json = "1.0.128"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
toml_edit.workspace = true
|
toml_edit.workspace = true
|
||||||
|
|
||||||
|
[target.'cfg(not(windows))'.dependencies]
|
||||||
|
rustix = { version = "0.38.35", default-features = false, features = ["std", "stdio", "termios"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.12.0"
|
tempfile = "3.12.0"
|
||||||
|
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -8,7 +8,7 @@ use std::{
|
||||||
};
|
};
|
||||||
use term::{clear_terminal, press_enter_prompt};
|
use term::{clear_terminal, press_enter_prompt};
|
||||||
|
|
||||||
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::WatchExit};
|
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile};
|
||||||
|
|
||||||
mod app_state;
|
mod app_state;
|
||||||
mod cargo_toml;
|
mod cargo_toml;
|
||||||
|
@ -130,15 +130,7 @@ fn main() -> Result<()> {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
watch::watch(&mut app_state, notify_exercise_names)?;
|
||||||
match watch::watch(&mut app_state, notify_exercise_names)? {
|
|
||||||
WatchExit::Shutdown => break,
|
|
||||||
// It is much easier to exit the watch mode, launch the list mode and then restart
|
|
||||||
// the watch mode instead of trying to pause the watch threads and correct the
|
|
||||||
// watch state.
|
|
||||||
WatchExit::List => list::list(&mut app_state)?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(Subcommands::Run { name }) => {
|
Some(Subcommands::Run { name }) => {
|
||||||
if let Some(name) = name {
|
if let Some(name) = name {
|
||||||
|
|
52
src/watch.rs
52
src/watch.rs
|
@ -11,7 +11,10 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::app_state::{AppState, ExercisesProgress};
|
use crate::{
|
||||||
|
app_state::{AppState, ExercisesProgress},
|
||||||
|
list,
|
||||||
|
};
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
notify_event::NotifyEventHandler,
|
notify_event::NotifyEventHandler,
|
||||||
|
@ -33,15 +36,14 @@ enum WatchEvent {
|
||||||
|
|
||||||
/// Returned by the watch mode to indicate what to do afterwards.
|
/// Returned by the watch mode to indicate what to do afterwards.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub enum WatchExit {
|
enum WatchExit {
|
||||||
/// Exit the program.
|
/// Exit the program.
|
||||||
Shutdown,
|
Shutdown,
|
||||||
/// Enter the list mode and restart the watch mode afterwards.
|
/// Enter the list mode and restart the watch mode afterwards.
|
||||||
List,
|
List,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `notify_exercise_names` as None activates the manual run mode.
|
fn run_watch(
|
||||||
pub fn watch(
|
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
||||||
) -> Result<WatchExit> {
|
) -> Result<WatchExit> {
|
||||||
|
@ -110,6 +112,48 @@ pub fn watch(
|
||||||
Ok(WatchExit::Shutdown)
|
Ok(WatchExit::Shutdown)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn watch_list_loop(
|
||||||
|
app_state: &mut AppState,
|
||||||
|
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
||||||
|
) -> Result<()> {
|
||||||
|
loop {
|
||||||
|
match run_watch(app_state, notify_exercise_names)? {
|
||||||
|
WatchExit::Shutdown => break Ok(()),
|
||||||
|
// It is much easier to exit the watch mode, launch the list mode and then restart
|
||||||
|
// the watch mode instead of trying to pause the watch threads and correct the
|
||||||
|
// watch state.
|
||||||
|
WatchExit::List => list::list(app_state)?,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `notify_exercise_names` as None activates the manual run mode.
|
||||||
|
pub fn watch(
|
||||||
|
app_state: &mut AppState,
|
||||||
|
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
||||||
|
) -> Result<()> {
|
||||||
|
#[cfg(not(windows))]
|
||||||
|
{
|
||||||
|
let stdin_fd = rustix::stdio::stdin();
|
||||||
|
let mut termios = rustix::termios::tcgetattr(stdin_fd)?;
|
||||||
|
let original_local_modes = termios.local_modes;
|
||||||
|
// Disable stdin line buffering and hide input.
|
||||||
|
termios.local_modes -=
|
||||||
|
rustix::termios::LocalModes::ICANON | rustix::termios::LocalModes::ECHO;
|
||||||
|
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
|
||||||
|
|
||||||
|
let res = watch_list_loop(app_state, notify_exercise_names);
|
||||||
|
|
||||||
|
termios.local_modes = original_local_modes;
|
||||||
|
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
watch_list_loop(app_state, notify_exercise_names)
|
||||||
|
}
|
||||||
|
|
||||||
const QUIT_MSG: &[u8] = b"
|
const QUIT_MSG: &[u8] = b"
|
||||||
We hope you're enjoying learning Rust!
|
We hope you're enjoying learning Rust!
|
||||||
If you want to continue working on the exercises at a later point, you can simply run `rustlings` again.
|
If you want to continue working on the exercises at a later point, you can simply run `rustlings` again.
|
||||||
|
|
Loading…
Reference in a new issue