mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Start list without Ratatui
This commit is contained in:
parent
47976caa69
commit
570bc9f32d
14 changed files with 303 additions and 500 deletions
|
@ -2,6 +2,3 @@
|
||||||
extend-exclude = [
|
extend-exclude = [
|
||||||
"CHANGELOG.md",
|
"CHANGELOG.md",
|
||||||
]
|
]
|
||||||
|
|
||||||
[default.extend-words]
|
|
||||||
"ratatui" = "ratatui"
|
|
||||||
|
|
163
Cargo.lock
generated
163
Cargo.lock
generated
|
@ -14,12 +14,6 @@ dependencies = [
|
||||||
"zerocopy",
|
"zerocopy",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "allocator-api2"
|
|
||||||
version = "0.2.18"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "0.6.15"
|
version = "0.6.15"
|
||||||
|
@ -93,21 +87,6 @@ version = "2.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cassowary"
|
|
||||||
version = "0.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "castaway"
|
|
||||||
version = "0.2.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5"
|
|
||||||
dependencies = [
|
|
||||||
"rustversion",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
@ -160,20 +139,6 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "compact_str"
|
|
||||||
version = "0.8.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644"
|
|
||||||
dependencies = [
|
|
||||||
"castaway",
|
|
||||||
"cfg-if",
|
|
||||||
"itoa",
|
|
||||||
"rustversion",
|
|
||||||
"ryu",
|
|
||||||
"static_assertions",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-channel"
|
name = "crossbeam-channel"
|
||||||
version = "0.5.13"
|
version = "0.5.13"
|
||||||
|
@ -214,12 +179,6 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "either"
|
|
||||||
version = "1.13.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "equivalent"
|
name = "equivalent"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
@ -268,10 +227,6 @@ name = "hashbrown"
|
||||||
version = "0.14.5"
|
version = "0.14.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||||
dependencies = [
|
|
||||||
"ahash",
|
|
||||||
"allocator-api2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
|
@ -315,31 +270,12 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "instability"
|
|
||||||
version = "0.3.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b23a0c8dfe501baac4adf6ebbfa6eddf8f0c07f56b058cc1288017e32397846c"
|
|
||||||
dependencies = [
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "is_terminal_polyfill"
|
name = "is_terminal_polyfill"
|
||||||
version = "1.70.1"
|
version = "1.70.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "itertools"
|
|
||||||
version = "0.13.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
|
||||||
dependencies = [
|
|
||||||
"either",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.11"
|
version = "1.0.11"
|
||||||
|
@ -405,15 +341,6 @@ version = "0.4.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lru"
|
|
||||||
version = "0.12.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "37ee39891760e7d94734f6f63fedc29a2e4a152f836120753a72503f09fcf904"
|
|
||||||
dependencies = [
|
|
||||||
"hashbrown",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.7.4"
|
version = "2.7.4"
|
||||||
|
@ -513,12 +440,6 @@ dependencies = [
|
||||||
"windows-targets 0.52.6",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "paste"
|
|
||||||
version = "1.0.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.86"
|
version = "1.0.86"
|
||||||
|
@ -530,34 +451,13 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.36"
|
version = "1.0.37"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ratatui"
|
|
||||||
version = "0.28.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5ba6a365afbe5615999275bea2446b970b10a41102500e27ce7678d50d978303"
|
|
||||||
dependencies = [
|
|
||||||
"bitflags 2.6.0",
|
|
||||||
"cassowary",
|
|
||||||
"compact_str",
|
|
||||||
"crossterm",
|
|
||||||
"instability",
|
|
||||||
"itertools",
|
|
||||||
"lru",
|
|
||||||
"paste",
|
|
||||||
"strum",
|
|
||||||
"strum_macros",
|
|
||||||
"unicode-segmentation",
|
|
||||||
"unicode-truncate",
|
|
||||||
"unicode-width",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
|
@ -587,9 +487,9 @@ dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
"crossterm",
|
||||||
"notify-debouncer-mini",
|
"notify-debouncer-mini",
|
||||||
"os_pipe",
|
"os_pipe",
|
||||||
"ratatui",
|
|
||||||
"rustlings-macros",
|
"rustlings-macros",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -606,12 +506,6 @@ dependencies = [
|
||||||
"toml_edit",
|
"toml_edit",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustversion"
|
|
||||||
version = "1.0.17"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
@ -710,40 +604,12 @@ version = "1.13.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "static_assertions"
|
|
||||||
version = "1.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "strum"
|
|
||||||
version = "0.26.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
|
||||||
dependencies = [
|
|
||||||
"strum_macros",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "strum_macros"
|
|
||||||
version = "0.26.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
|
|
||||||
dependencies = [
|
|
||||||
"heck",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"rustversion",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.75"
|
version = "2.0.75"
|
||||||
|
@ -796,29 +662,6 @@ version = "1.0.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-segmentation"
|
|
||||||
version = "1.11.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-truncate"
|
|
||||||
version = "1.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf"
|
|
||||||
dependencies = [
|
|
||||||
"itertools",
|
|
||||||
"unicode-segmentation",
|
|
||||||
"unicode-width",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-width"
|
|
||||||
version = "0.1.13"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf8parse"
|
name = "utf8parse"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
|
|
@ -49,9 +49,9 @@ include = [
|
||||||
ahash = { version = "0.8.11", default-features = false }
|
ahash = { version = "0.8.11", default-features = false }
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
clap = { version = "4.5.16", features = ["derive"] }
|
clap = { version = "4.5.16", features = ["derive"] }
|
||||||
|
crossterm = { version = "0.28.1", default-features = false, features = ["windows", "events"] }
|
||||||
notify-debouncer-mini = { version = "0.4.1", default-features = false }
|
notify-debouncer-mini = { version = "0.4.1", default-features = false }
|
||||||
os_pipe = "1.2.1"
|
os_pipe = "1.2.1"
|
||||||
ratatui = { version = "0.28.0", default-features = false, features = ["crossterm"] }
|
|
||||||
rustlings-macros = { path = "rustlings-macros", version = "=6.2.0" }
|
rustlings-macros = { path = "rustlings-macros", version = "=6.2.0" }
|
||||||
serde_json = "1.0.125"
|
serde_json = "1.0.125"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
|
|
|
@ -16,7 +16,7 @@ include = [
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
quote = "1.0.36"
|
quote = "1.0.37"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
toml_edit.workspace = true
|
toml_edit.workspace = true
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ratatui::crossterm::style::{style, StyledContent, Stylize};
|
use crossterm::style::{style, StyledContent, Stylize};
|
||||||
use std::{
|
use std::{
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
io::Write,
|
io::Write,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use ratatui::crossterm::style::Stylize;
|
use crossterm::style::Stylize;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{
|
use std::{
|
||||||
env::set_current_dir,
|
env::set_current_dir,
|
||||||
|
|
106
src/list.rs
106
src/list.rs
|
@ -1,95 +1,101 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use ratatui::{
|
use crossterm::{
|
||||||
backend::CrosstermBackend,
|
cursor,
|
||||||
crossterm::{
|
event::{
|
||||||
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseEventKind,
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
||||||
QueueableCommand,
|
|
||||||
},
|
},
|
||||||
Terminal,
|
terminal::{
|
||||||
|
disable_raw_mode, enable_raw_mode, DisableLineWrap, EnableLineWrap, EnterAlternateScreen,
|
||||||
|
LeaveAlternateScreen,
|
||||||
|
},
|
||||||
|
QueueableCommand,
|
||||||
};
|
};
|
||||||
use std::io::{self, StdoutLock, Write};
|
use std::io::{self, StdoutLock, Write};
|
||||||
|
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
|
|
||||||
use self::state::{Filter, UiState};
|
use self::state::{Filter, ListState};
|
||||||
|
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
|
fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
|
||||||
let mut terminal = Terminal::new(CrosstermBackend::new(stdout))?;
|
let mut list_state = ListState::new(app_state, stdout)?;
|
||||||
terminal.clear()?;
|
|
||||||
|
|
||||||
let mut ui_state = UiState::new(app_state);
|
loop {
|
||||||
|
|
||||||
'outer: loop {
|
|
||||||
terminal.try_draw(|frame| ui_state.draw(frame).map_err(io::Error::other))?;
|
|
||||||
|
|
||||||
let key = loop {
|
|
||||||
match event::read().context("Failed to read terminal event")? {
|
match event::read().context("Failed to read terminal event")? {
|
||||||
Event::Key(key) => match key.kind {
|
Event::Key(key) => {
|
||||||
KeyEventKind::Press | KeyEventKind::Repeat => break key,
|
match key.kind {
|
||||||
KeyEventKind::Release => (),
|
KeyEventKind::Release => continue,
|
||||||
},
|
KeyEventKind::Press | KeyEventKind::Repeat => (),
|
||||||
// Redraw
|
|
||||||
Event::Resize(_, _) => continue 'outer,
|
|
||||||
// Ignore
|
|
||||||
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
ui_state.message.clear();
|
list_state.message.clear();
|
||||||
|
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => break,
|
KeyCode::Char('q') => return Ok(()),
|
||||||
KeyCode::Down | KeyCode::Char('j') => ui_state.select_next(),
|
KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
|
||||||
KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(),
|
KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
|
||||||
KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(),
|
KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
|
||||||
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
|
KeyCode::End | KeyCode::Char('G') => list_state.select_last(),
|
||||||
KeyCode::Char('d') => {
|
KeyCode::Char('d') => {
|
||||||
let message = if ui_state.filter == Filter::Done {
|
let message = if list_state.filter() == Filter::Done {
|
||||||
ui_state.filter = Filter::None;
|
list_state.set_filter(Filter::None);
|
||||||
"Disabled filter DONE"
|
"Disabled filter DONE"
|
||||||
} else {
|
} else {
|
||||||
ui_state.filter = Filter::Done;
|
list_state.set_filter(Filter::Done);
|
||||||
"Enabled filter DONE │ Press d again to disable the filter"
|
"Enabled filter DONE │ Press d again to disable the filter"
|
||||||
};
|
};
|
||||||
|
|
||||||
ui_state = ui_state.with_updated_rows();
|
list_state.message.push_str(message);
|
||||||
ui_state.message.push_str(message);
|
|
||||||
}
|
}
|
||||||
KeyCode::Char('p') => {
|
KeyCode::Char('p') => {
|
||||||
let message = if ui_state.filter == Filter::Pending {
|
let message = if list_state.filter() == Filter::Pending {
|
||||||
ui_state.filter = Filter::None;
|
list_state.set_filter(Filter::None);
|
||||||
"Disabled filter PENDING"
|
"Disabled filter PENDING"
|
||||||
} else {
|
} else {
|
||||||
ui_state.filter = Filter::Pending;
|
list_state.set_filter(Filter::Pending);
|
||||||
"Enabled filter PENDING │ Press p again to disable the filter"
|
"Enabled filter PENDING │ Press p again to disable the filter"
|
||||||
};
|
};
|
||||||
|
|
||||||
ui_state = ui_state.with_updated_rows();
|
list_state.message.push_str(message);
|
||||||
ui_state.message.push_str(message);
|
|
||||||
}
|
}
|
||||||
KeyCode::Char('r') => {
|
KeyCode::Char('r') => {
|
||||||
ui_state = ui_state.with_reset_selected()?;
|
list_state.reset_selected()?;
|
||||||
}
|
}
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
ui_state.selected_to_current_exercise()?;
|
return list_state.selected_to_current_exercise();
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
// Redraw to remove the message.
|
||||||
|
KeyCode::Esc => (),
|
||||||
|
_ => continue,
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
list_state.redraw(stdout)?;
|
||||||
|
}
|
||||||
|
Event::Mouse(event) => {
|
||||||
|
match event.kind {
|
||||||
|
MouseEventKind::ScrollDown => list_state.select_next(),
|
||||||
|
MouseEventKind::ScrollUp => list_state.select_previous(),
|
||||||
|
_ => continue,
|
||||||
|
}
|
||||||
|
|
||||||
|
list_state.redraw(stdout)?;
|
||||||
|
}
|
||||||
|
// Redraw
|
||||||
|
Event::Resize(_, _) => list_state.redraw(stdout)?,
|
||||||
|
// Ignore
|
||||||
|
Event::FocusGained | Event::FocusLost => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list(app_state: &mut AppState) -> Result<()> {
|
pub fn list(app_state: &mut AppState) -> Result<()> {
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
stdout
|
stdout
|
||||||
.queue(EnterAlternateScreen)?
|
.queue(EnterAlternateScreen)?
|
||||||
.queue(EnableMouseCapture)?
|
.queue(cursor::Hide)?
|
||||||
.flush()?;
|
.queue(DisableLineWrap)?
|
||||||
|
.queue(EnableMouseCapture)?;
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
|
|
||||||
let res = handle_list(app_state, &mut stdout);
|
let res = handle_list(app_state, &mut stdout);
|
||||||
|
@ -97,6 +103,8 @@ pub fn list(app_state: &mut AppState) -> Result<()> {
|
||||||
// Restore the terminal even if we got an error.
|
// Restore the terminal even if we got an error.
|
||||||
stdout
|
stdout
|
||||||
.queue(LeaveAlternateScreen)?
|
.queue(LeaveAlternateScreen)?
|
||||||
|
.queue(cursor::Show)?
|
||||||
|
.queue(EnableLineWrap)?
|
||||||
.queue(DisableMouseCapture)?
|
.queue(DisableMouseCapture)?
|
||||||
.flush()?;
|
.flush()?;
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use ratatui::{
|
use crossterm::{
|
||||||
layout::{Constraint, Rect},
|
cursor::{MoveDown, MoveTo},
|
||||||
style::{Style, Stylize},
|
style::{Color, ResetColor, SetForegroundColor},
|
||||||
text::{Span, Text},
|
terminal::{self, BeginSynchronizedUpdate, EndSynchronizedUpdate},
|
||||||
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
|
QueueableCommand,
|
||||||
Frame,
|
};
|
||||||
|
use std::{
|
||||||
|
fmt::Write as _,
|
||||||
|
io::{self, StdoutLock, Write as _},
|
||||||
};
|
};
|
||||||
use std::{fmt::Write, mem};
|
|
||||||
|
|
||||||
use crate::{app_state::AppState, progress_bar::progress_bar_ratatui};
|
use crate::{app_state::AppState, term::clear_terminal, MAX_EXERCISE_NAME_LEN};
|
||||||
|
|
||||||
|
// +1 for padding.
|
||||||
|
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub enum Filter {
|
pub enum Filter {
|
||||||
|
@ -17,230 +22,213 @@ pub enum Filter {
|
||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UiState<'a> {
|
pub struct ListState<'a> {
|
||||||
pub table: Table<'static>,
|
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub filter: Filter,
|
filter: Filter,
|
||||||
app_state: &'a mut AppState,
|
app_state: &'a mut AppState,
|
||||||
table_state: TableState,
|
n_rows_with_filter: usize,
|
||||||
n_rows: usize,
|
name_col_width: usize,
|
||||||
|
offset: usize,
|
||||||
|
selected: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> UiState<'a> {
|
impl<'a> ListState<'a> {
|
||||||
pub fn with_updated_rows(mut self) -> Self {
|
pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result<Self> {
|
||||||
let current_exercise_ind = self.app_state.current_exercise_ind();
|
let name_col_width = app_state
|
||||||
|
|
||||||
self.n_rows = 0;
|
|
||||||
let rows = self
|
|
||||||
.app_state
|
|
||||||
.exercises()
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter_map(|(ind, exercise)| {
|
|
||||||
let exercise_state = if exercise.done {
|
|
||||||
if self.filter == Filter::Pending {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
"DONE".green()
|
|
||||||
} else {
|
|
||||||
if self.filter == Filter::Done {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
"PENDING".yellow()
|
|
||||||
};
|
|
||||||
|
|
||||||
self.n_rows += 1;
|
|
||||||
|
|
||||||
let next = if ind == current_exercise_ind {
|
|
||||||
">>>>".bold().red()
|
|
||||||
} else {
|
|
||||||
Span::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(Row::new([
|
|
||||||
next,
|
|
||||||
exercise_state,
|
|
||||||
Span::raw(exercise.name),
|
|
||||||
Span::raw(exercise.path),
|
|
||||||
]))
|
|
||||||
});
|
|
||||||
|
|
||||||
self.table = self.table.rows(rows);
|
|
||||||
|
|
||||||
if self.n_rows == 0 {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(app_state: &'a mut AppState) -> Self {
|
|
||||||
let header = Row::new(["Next", "State", "Name", "Path"]);
|
|
||||||
|
|
||||||
let max_name_len = app_state
|
|
||||||
.exercises()
|
.exercises()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise| exercise.name.len())
|
.map(|exercise| exercise.name.len())
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or(4) as u16;
|
.map_or(4, |max| max.max(4));
|
||||||
|
|
||||||
let widths = [
|
clear_terminal(stdout)?;
|
||||||
Constraint::Length(4),
|
stdout.write_all(b" Current State Name ")?;
|
||||||
Constraint::Length(7),
|
stdout.write_all(&SPACE[..name_col_width - 4])?;
|
||||||
Constraint::Length(max_name_len),
|
stdout.write_all(b"Path\r\n")?;
|
||||||
Constraint::Fill(1),
|
|
||||||
];
|
|
||||||
|
|
||||||
let table = Table::default()
|
|
||||||
.widths(widths)
|
|
||||||
.header(header)
|
|
||||||
.column_spacing(2)
|
|
||||||
.highlight_spacing(HighlightSpacing::Always)
|
|
||||||
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
|
|
||||||
.highlight_symbol("🦀")
|
|
||||||
.block(Block::default().borders(Borders::BOTTOM));
|
|
||||||
|
|
||||||
let selected = app_state.current_exercise_ind();
|
let selected = app_state.current_exercise_ind();
|
||||||
let table_state = TableState::default()
|
let n_rows_with_filter = app_state.exercises().len();
|
||||||
.with_offset(selected.saturating_sub(10))
|
|
||||||
.with_selected(Some(selected));
|
|
||||||
|
|
||||||
let filter = Filter::None;
|
let mut slf = Self {
|
||||||
let n_rows = app_state.exercises().len();
|
|
||||||
|
|
||||||
let slf = Self {
|
|
||||||
table,
|
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
filter,
|
filter: Filter::None,
|
||||||
app_state,
|
app_state,
|
||||||
table_state,
|
n_rows_with_filter,
|
||||||
n_rows,
|
name_col_width,
|
||||||
|
offset: selected.saturating_sub(10),
|
||||||
|
selected: Some(selected),
|
||||||
};
|
};
|
||||||
|
|
||||||
slf.with_updated_rows()
|
slf.redraw(stdout)?;
|
||||||
|
|
||||||
|
Ok(slf)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn filter(&self) -> Filter {
|
||||||
|
self.filter
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_filter(&mut self, filter: Filter) {
|
||||||
|
self.filter = filter;
|
||||||
|
self.n_rows_with_filter = match filter {
|
||||||
|
Filter::Done => self
|
||||||
|
.app_state
|
||||||
|
.exercises()
|
||||||
|
.iter()
|
||||||
|
.filter(|exercise| !exercise.done)
|
||||||
|
.count(),
|
||||||
|
Filter::Pending => self
|
||||||
|
.app_state
|
||||||
|
.exercises()
|
||||||
|
.iter()
|
||||||
|
.filter(|exercise| exercise.done)
|
||||||
|
.count(),
|
||||||
|
Filter::None => self.app_state.exercises().len(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.n_rows_with_filter == 0 {
|
||||||
|
self.selected = None;
|
||||||
|
} else {
|
||||||
|
self.selected = Some(
|
||||||
|
self.selected
|
||||||
|
.map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_next(&mut self) {
|
pub fn select_next(&mut self) {
|
||||||
if self.n_rows > 0 {
|
if self.n_rows_with_filter > 0 {
|
||||||
let next = self
|
let next = self.selected.map_or(0, |selected| {
|
||||||
.table_state
|
(selected + 1).min(self.n_rows_with_filter - 1)
|
||||||
.selected()
|
});
|
||||||
.map_or(0, |selected| (selected + 1).min(self.n_rows - 1));
|
self.selected = Some(next);
|
||||||
self.table_state.select(Some(next));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_previous(&mut self) {
|
pub fn select_previous(&mut self) {
|
||||||
if self.n_rows > 0 {
|
if self.n_rows_with_filter > 0 {
|
||||||
let previous = self
|
let previous = self
|
||||||
.table_state
|
.selected
|
||||||
.selected()
|
|
||||||
.map_or(0, |selected| selected.saturating_sub(1));
|
.map_or(0, |selected| selected.saturating_sub(1));
|
||||||
self.table_state.select(Some(previous));
|
self.selected = Some(previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_first(&mut self) {
|
pub fn select_first(&mut self) {
|
||||||
if self.n_rows > 0 {
|
if self.n_rows_with_filter > 0 {
|
||||||
self.table_state.select(Some(0));
|
self.selected = Some(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_last(&mut self) {
|
pub fn select_last(&mut self) {
|
||||||
if self.n_rows > 0 {
|
if self.n_rows_with_filter > 0 {
|
||||||
self.table_state.select(Some(self.n_rows - 1));
|
self.selected = Some(self.n_rows_with_filter - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
|
pub fn redraw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
let area = frame.area();
|
stdout.queue(BeginSynchronizedUpdate)?;
|
||||||
let narrow = area.width < 95;
|
stdout.queue(MoveTo(0, 1))?;
|
||||||
|
let (width, height) = terminal::size()?;
|
||||||
|
let narrow = width < 95;
|
||||||
let narrow_u16 = u16::from(narrow);
|
let narrow_u16 = u16::from(narrow);
|
||||||
let table_height = area.height - 3 - narrow_u16;
|
let max_n_rows_to_display = height.saturating_sub(narrow_u16 + 4);
|
||||||
|
|
||||||
frame.render_stateful_widget(
|
let displayed_exercises = self
|
||||||
&self.table,
|
.app_state
|
||||||
Rect {
|
.exercises()
|
||||||
x: 0,
|
.iter()
|
||||||
y: 0,
|
.enumerate()
|
||||||
width: area.width,
|
.filter(|(_, exercise)| match self.filter {
|
||||||
height: table_height,
|
Filter::Done => exercise.done,
|
||||||
},
|
Filter::Pending => !exercise.done,
|
||||||
&mut self.table_state,
|
Filter::None => true,
|
||||||
);
|
})
|
||||||
|
.skip(self.offset)
|
||||||
|
.take(max_n_rows_to_display as usize);
|
||||||
|
|
||||||
frame.render_widget(
|
let mut n_displayed_rows: u16 = 0;
|
||||||
Paragraph::new(progress_bar_ratatui(
|
let current_exercise_ind = self.app_state.current_exercise_ind();
|
||||||
self.app_state.n_done(),
|
for (ind, exercise) in displayed_exercises {
|
||||||
self.app_state.exercises().len() as u16,
|
if self.selected == Some(n_displayed_rows as usize) {
|
||||||
area.width,
|
write!(stdout, "🦀")?;
|
||||||
)?)
|
|
||||||
.block(Block::default().borders(Borders::BOTTOM)),
|
|
||||||
Rect {
|
|
||||||
x: 0,
|
|
||||||
y: table_height,
|
|
||||||
width: area.width,
|
|
||||||
height: 2,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
let message = if self.message.is_empty() {
|
|
||||||
// Help footer.
|
|
||||||
let mut text = Text::default();
|
|
||||||
let mut spans = Vec::with_capacity(4);
|
|
||||||
spans.push(Span::raw(
|
|
||||||
"↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │",
|
|
||||||
));
|
|
||||||
|
|
||||||
if narrow {
|
|
||||||
text.push_line(mem::take(&mut spans));
|
|
||||||
spans.push(Span::raw("filter "));
|
|
||||||
} else {
|
} else {
|
||||||
spans.push(Span::raw(" filter "));
|
stdout.write_all(b" ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.filter {
|
if ind == current_exercise_ind {
|
||||||
Filter::Done => {
|
stdout.queue(SetForegroundColor(Color::Red))?;
|
||||||
spans.push("<d>one".underlined().magenta());
|
stdout.write_all(b">>>>>>> ")?;
|
||||||
spans.push(Span::raw("/<p>ending"));
|
|
||||||
}
|
|
||||||
Filter::Pending => {
|
|
||||||
spans.push(Span::raw("<d>one/"));
|
|
||||||
spans.push("<p>ending".underlined().magenta());
|
|
||||||
}
|
|
||||||
Filter::None => spans.push(Span::raw("<d>one/<p>ending")),
|
|
||||||
}
|
|
||||||
|
|
||||||
spans.push(Span::raw(" │ <q>uit list"));
|
|
||||||
text.push_line(spans);
|
|
||||||
text
|
|
||||||
} else {
|
} else {
|
||||||
Text::from(self.message.as_str().light_blue())
|
stdout.write_all(b" ")?;
|
||||||
};
|
}
|
||||||
frame.render_widget(
|
|
||||||
message,
|
if exercise.done {
|
||||||
Rect {
|
stdout.queue(SetForegroundColor(Color::Yellow))?;
|
||||||
x: 0,
|
stdout.write_all(b"DONE ")?;
|
||||||
y: table_height + 2,
|
} else {
|
||||||
width: area.width,
|
stdout.queue(SetForegroundColor(Color::Green))?;
|
||||||
height: 1 + narrow_u16,
|
stdout.write_all(b"PENDING ")?;
|
||||||
},
|
}
|
||||||
);
|
|
||||||
|
stdout.queue(ResetColor)?;
|
||||||
|
|
||||||
|
stdout.write_all(exercise.name.as_bytes())?;
|
||||||
|
stdout.write_all(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?;
|
||||||
|
|
||||||
|
stdout.write_all(exercise.path.as_bytes())?;
|
||||||
|
stdout.write_all(b"\r\n")?;
|
||||||
|
|
||||||
|
n_displayed_rows += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout.queue(MoveDown(max_n_rows_to_display - n_displayed_rows))?;
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// let message = if self.message.is_empty() {
|
||||||
|
// // Help footer.
|
||||||
|
// let mut text = Text::default();
|
||||||
|
// let mut spans = Vec::with_capacity(4);
|
||||||
|
// spans.push(Span::raw(
|
||||||
|
// "↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │",
|
||||||
|
// ));
|
||||||
|
|
||||||
|
// if narrow {
|
||||||
|
// text.push_line(mem::take(&mut spans));
|
||||||
|
// spans.push(Span::raw("filter "));
|
||||||
|
// } else {
|
||||||
|
// spans.push(Span::raw(" filter "));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// match self.filter {
|
||||||
|
// Filter::Done => {
|
||||||
|
// spans.push("<d>one".underlined().magenta());
|
||||||
|
// spans.push(Span::raw("/<p>ending"));
|
||||||
|
// }
|
||||||
|
// Filter::Pending => {
|
||||||
|
// spans.push(Span::raw("<d>one/"));
|
||||||
|
// spans.push("<p>ending".underlined().magenta());
|
||||||
|
// }
|
||||||
|
// Filter::None => spans.push(Span::raw("<d>one/<p>ending")),
|
||||||
|
// }
|
||||||
|
|
||||||
|
// spans.push(Span::raw(" │ <q>uit list"));
|
||||||
|
// text.push_line(spans);
|
||||||
|
// text
|
||||||
|
// } else {
|
||||||
|
// Text::from(self.message.as_str().light_blue())
|
||||||
|
// };
|
||||||
|
|
||||||
|
stdout.queue(EndSynchronizedUpdate)?;
|
||||||
|
stdout.flush()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_reset_selected(mut self) -> Result<Self> {
|
pub fn reset_selected(&mut self) -> Result<()> {
|
||||||
let Some(selected) = self.table_state.selected() else {
|
let Some(selected) = self.selected else {
|
||||||
return Ok(self);
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let ind = self
|
let ind = self
|
||||||
|
@ -259,11 +247,12 @@ impl<'a> UiState<'a> {
|
||||||
let exercise_path = self.app_state.reset_exercise_by_ind(ind)?;
|
let exercise_path = self.app_state.reset_exercise_by_ind(ind)?;
|
||||||
write!(self.message, "The exercise {exercise_path} has been reset")?;
|
write!(self.message, "The exercise {exercise_path} has been reset")?;
|
||||||
|
|
||||||
Ok(self.with_updated_rows())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
let Some(selected) = self.selected else {
|
||||||
|
// TODO: Don't exit list
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ mod terminal_link;
|
||||||
mod watch;
|
mod watch;
|
||||||
|
|
||||||
const CURRENT_FORMAT_VERSION: u8 = 1;
|
const CURRENT_FORMAT_VERSION: u8 = 1;
|
||||||
|
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
||||||
|
|
||||||
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
|
@ -1,100 +1,53 @@
|
||||||
use anyhow::{bail, Result};
|
use std::io::{self, StdoutLock, Write};
|
||||||
use ratatui::text::{Line, Span};
|
|
||||||
use std::fmt::Write;
|
|
||||||
|
|
||||||
const PREFIX: &str = "Progress: [";
|
use crossterm::{
|
||||||
|
style::{Color, ResetColor, SetForegroundColor},
|
||||||
|
QueueableCommand,
|
||||||
|
};
|
||||||
|
|
||||||
|
const PREFIX: &[u8] = b"Progress: [";
|
||||||
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
|
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
|
||||||
// Leaving the last char empty (_) for `total` > 99.
|
// Leaving the last char empty (_) for `total` > 99.
|
||||||
const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16;
|
const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16;
|
||||||
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
|
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
|
||||||
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
|
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;
|
||||||
|
|
||||||
const PROGRESS_EXCEEDS_MAX_ERR: &str =
|
|
||||||
"The progress of the progress bar is higher than the maximum";
|
|
||||||
|
|
||||||
/// Terminal progress bar to be used when not using Ratataui.
|
/// Terminal progress bar to be used when not using Ratataui.
|
||||||
pub fn progress_bar(progress: u16, total: u16, line_width: u16) -> Result<String> {
|
pub fn progress_bar(
|
||||||
use ratatui::crossterm::style::Stylize;
|
stdout: &mut StdoutLock,
|
||||||
|
progress: u16,
|
||||||
if progress > total {
|
total: u16,
|
||||||
bail!(PROGRESS_EXCEEDS_MAX_ERR);
|
line_width: u16,
|
||||||
}
|
) -> io::Result<()> {
|
||||||
|
debug_assert!(progress <= total);
|
||||||
|
|
||||||
if line_width < MIN_LINE_WIDTH {
|
if line_width < MIN_LINE_WIDTH {
|
||||||
return Ok(format!("Progress: {progress}/{total} exercises"));
|
return write!(stdout, "Progress: {progress}/{total} exercises");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut line = String::with_capacity(usize::from(line_width));
|
stdout.write_all(PREFIX)?;
|
||||||
line.push_str(PREFIX);
|
|
||||||
|
|
||||||
let width = line_width - WRAPPER_WIDTH;
|
let width = line_width - WRAPPER_WIDTH;
|
||||||
let filled = (width * progress) / total;
|
let filled = (width * progress) / total;
|
||||||
|
|
||||||
let mut green_part = String::with_capacity(usize::from(filled + 1));
|
stdout.queue(SetForegroundColor(Color::Green))?;
|
||||||
for _ in 0..filled {
|
for _ in 0..filled {
|
||||||
green_part.push('#');
|
stdout.write_all(b"#")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if filled < width {
|
if filled < width {
|
||||||
green_part.push('>');
|
stdout.write_all(b">")?;
|
||||||
}
|
}
|
||||||
write!(line, "{}", green_part.green()).unwrap();
|
|
||||||
|
|
||||||
let width_minus_filled = width - filled;
|
let width_minus_filled = width - filled;
|
||||||
if width_minus_filled > 1 {
|
if width_minus_filled > 1 {
|
||||||
let red_part_width = width_minus_filled - 1;
|
let red_part_width = width_minus_filled - 1;
|
||||||
let mut red_part = String::with_capacity(usize::from(red_part_width));
|
stdout.queue(SetForegroundColor(Color::Red))?;
|
||||||
for _ in 0..red_part_width {
|
for _ in 0..red_part_width {
|
||||||
red_part.push('-');
|
stdout.write_all(b"-")?;
|
||||||
}
|
}
|
||||||
write!(line, "{}", red_part.red()).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(line, "] {progress:>3}/{total} exercises").unwrap();
|
stdout.queue(ResetColor)?;
|
||||||
|
write!(stdout, "] {progress:>3}/{total} exercises")
|
||||||
Ok(line)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Progress bar to be used with Ratataui.
|
|
||||||
// Not using Ratatui's Gauge widget to keep the progress bar consistent.
|
|
||||||
pub fn progress_bar_ratatui(progress: u16, total: u16, line_width: u16) -> Result<Line<'static>> {
|
|
||||||
use ratatui::style::Stylize;
|
|
||||||
|
|
||||||
if progress > total {
|
|
||||||
bail!(PROGRESS_EXCEEDS_MAX_ERR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if line_width < MIN_LINE_WIDTH {
|
|
||||||
return Ok(Line::raw(format!("Progress: {progress}/{total} exercises")));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut spans = Vec::with_capacity(4);
|
|
||||||
spans.push(Span::raw(PREFIX));
|
|
||||||
|
|
||||||
let width = line_width - WRAPPER_WIDTH;
|
|
||||||
let filled = (width * progress) / total;
|
|
||||||
|
|
||||||
let mut green_part = String::with_capacity(usize::from(filled + 1));
|
|
||||||
for _ in 0..filled {
|
|
||||||
green_part.push('#');
|
|
||||||
}
|
|
||||||
|
|
||||||
if filled < width {
|
|
||||||
green_part.push('>');
|
|
||||||
}
|
|
||||||
spans.push(green_part.green());
|
|
||||||
|
|
||||||
let width_minus_filled = width - filled;
|
|
||||||
if width_minus_filled > 1 {
|
|
||||||
let red_part_width = width_minus_filled - 1;
|
|
||||||
let mut red_part = String::with_capacity(usize::from(red_part_width));
|
|
||||||
for _ in 0..red_part_width {
|
|
||||||
red_part.push('-');
|
|
||||||
}
|
|
||||||
spans.push(red_part.red());
|
|
||||||
}
|
|
||||||
|
|
||||||
spans.push(Span::raw(format!("] {progress:>3}/{total} exercises")));
|
|
||||||
|
|
||||||
Ok(Line::from(spans))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use ratatui::crossterm::style::{style, Stylize};
|
use crossterm::style::{style, Stylize};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
12
src/term.rs
12
src/term.rs
|
@ -1,7 +1,17 @@
|
||||||
use std::io::{self, BufRead, StdoutLock, Write};
|
use std::io::{self, BufRead, StdoutLock, Write};
|
||||||
|
|
||||||
|
use crossterm::{
|
||||||
|
cursor::MoveTo,
|
||||||
|
terminal::{Clear, ClearType},
|
||||||
|
QueueableCommand,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
stdout.write_all(b"\x1b[H\x1b[2J\x1b[3J")
|
stdout
|
||||||
|
.queue(MoveTo(0, 0))?
|
||||||
|
.queue(Clear(ClearType::All))?
|
||||||
|
.queue(Clear(ClearType::Purge))
|
||||||
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn press_enter_prompt(stdout: &mut StdoutLock) -> io::Result<()> {
|
pub fn press_enter_prompt(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ratatui::crossterm::{
|
use crossterm::{
|
||||||
style::{style, Stylize},
|
style::{style, Stylize},
|
||||||
terminal,
|
terminal,
|
||||||
};
|
};
|
||||||
|
@ -76,7 +76,8 @@ impl<'a> WatchState<'a> {
|
||||||
self.done_status = DoneStatus::Pending;
|
self.done_status = DoneStatus::Pending;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.render()
|
self.render()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_file_change(&mut self, exercise_ind: usize) -> Result<()> {
|
pub fn handle_file_change(&mut self, exercise_ind: usize) -> Result<()> {
|
||||||
|
@ -120,7 +121,7 @@ impl<'a> WatchState<'a> {
|
||||||
self.writer.flush()
|
self.writer.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) -> Result<()> {
|
pub fn render(&mut self) -> io::Result<()> {
|
||||||
// Prevent having the first line shifted if clearing wasn't successful.
|
// Prevent having the first line shifted if clearing wasn't successful.
|
||||||
self.writer.write_all(b"\n")?;
|
self.writer.write_all(b"\n")?;
|
||||||
clear_terminal(&mut self.writer)?;
|
clear_terminal(&mut self.writer)?;
|
||||||
|
@ -155,14 +156,15 @@ impl<'a> WatchState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let line_width = terminal::size()?.0;
|
let line_width = terminal::size()?.0;
|
||||||
let progress_bar = progress_bar(
|
progress_bar(
|
||||||
|
&mut self.writer,
|
||||||
self.app_state.n_done(),
|
self.app_state.n_done(),
|
||||||
self.app_state.exercises().len() as u16,
|
self.app_state.exercises().len() as u16,
|
||||||
line_width,
|
line_width,
|
||||||
)?;
|
)?;
|
||||||
writeln!(
|
writeln!(
|
||||||
self.writer,
|
self.writer,
|
||||||
"{progress_bar}Current exercise: {}",
|
"\nCurrent exercise: {}",
|
||||||
self.app_state.current_exercise().terminal_link(),
|
self.app_state.current_exercise().terminal_link(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -171,7 +173,7 @@ impl<'a> WatchState<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_hint(&mut self) -> Result<()> {
|
pub fn show_hint(&mut self) -> io::Result<()> {
|
||||||
self.show_hint = true;
|
self.show_hint = true;
|
||||||
self.render()
|
self.render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
use super::WatchEvent;
|
use super::WatchEvent;
|
||||||
|
@ -78,7 +78,7 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue,
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) => continue,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue