From b8308147042b4428273a83206a10546a502d28b3 Mon Sep 17 00:00:00 2001 From: f01d-zero Date: Mon, 10 Aug 2026 11:07:03 +0200 Subject: [PATCH] feat: add intro1 exercise - compile gate: main lacks tail expression until TODO uncommented - drop .gitkeep files; rustlings dev check forbids extra files - in-loop TTY check lets solution exit clean under piped stdout --- Cargo.toml | 5 ++++- exercises/.gitkeep | 1 - exercises/00_intro/intro1.rs | 36 ++++++++++++++++++++++++++++++++++++ info.toml | 7 +++++-- solutions/.gitkeep | 1 - solutions/00_intro/intro1.rs | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 5 deletions(-) delete mode 100644 exercises/.gitkeep create mode 100644 exercises/00_intro/intro1.rs delete mode 100644 solutions/.gitkeep create mode 100644 solutions/00_intro/intro1.rs diff --git a/Cargo.toml b/Cargo.toml index ddb7887..cf174e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,9 @@ # Don't edit the `bin` list manually. Run `rustlings dev update` after changing # exercises or solutions. -bin = [] +bin = [ + { name = "intro1", path = "exercises/00_intro/intro1.rs" }, + { name = "intro1_sol", path = "solutions/00_intro/intro1.rs" }, +] [package] name = "ratlings" diff --git a/exercises/.gitkeep b/exercises/.gitkeep deleted file mode 100644 index 8b13789..0000000 --- a/exercises/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/exercises/00_intro/intro1.rs b/exercises/00_intro/intro1.rs new file mode 100644 index 0000000..b8c5581 --- /dev/null +++ b/exercises/00_intro/intro1.rs @@ -0,0 +1,36 @@ +use ratatui::{DefaultTerminal, Frame}; +use std::io::IsTerminal; + +// Do not modify this line +fn main() -> std::io::Result<()> { + // TODO: run the TUI app. + // ratatui::run(app) + todo!("run the TUI app.") +} + +fn app(terminal: &mut DefaultTerminal) -> std::io::Result<()> { + loop { + terminal.draw(render)?; + if !std::io::stdout().is_terminal() || ratatui::crossterm::event::read()?.is_key_press() { + break Ok(()); + } + } +} + +fn render(frame: &mut Frame) { + frame.render_widget("hello world", frame.area()); +} + +#[cfg(test)] +mod tests { + use super::*; + use ratatui::{Terminal, backend::TestBackend}; + + #[test] + fn renders_hello_world() { + let mut terminal = + Terminal::new(TestBackend::new(15, 1)).expect("test backend initialization"); + terminal.draw(render).expect("render frame"); + terminal.backend().assert_buffer_lines(["hello world "]); + } +} diff --git a/info.toml b/info.toml index 5fdea47..f18a9ad 100644 --- a/info.toml +++ b/info.toml @@ -11,8 +11,6 @@ final_message = """ You finished Ratlings! 🎉 """ -exercises = [] - # Add exercise metadata here. See CONTRIBUTING.md and the Rustlings community # exercises guide: https://rustlings.rust-lang.org/community-exercises/ # @@ -20,3 +18,8 @@ exercises = [] # name = "example" # dir = "section" # hint = "A helpful hint." + +[[exercises]] +name = "intro1" +dir = "00_intro" +hint = "Uncomment the line below the TODO-comment." diff --git a/solutions/.gitkeep b/solutions/.gitkeep deleted file mode 100644 index 8b13789..0000000 --- a/solutions/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/solutions/00_intro/intro1.rs b/solutions/00_intro/intro1.rs new file mode 100644 index 0000000..e45486d --- /dev/null +++ b/solutions/00_intro/intro1.rs @@ -0,0 +1,33 @@ +use ratatui::{DefaultTerminal, Frame}; +use std::io::IsTerminal; + +fn main() -> std::io::Result<()> { + ratatui::run(app) +} + +fn app(terminal: &mut DefaultTerminal) -> std::io::Result<()> { + loop { + terminal.draw(render)?; + if !std::io::stdout().is_terminal() || ratatui::crossterm::event::read()?.is_key_press() { + break Ok(()); + } + } +} + +fn render(frame: &mut Frame) { + frame.render_widget("hello world", frame.area()); +} + +#[cfg(test)] +mod tests { + use super::*; + use ratatui::{Terminal, backend::TestBackend}; + + #[test] + fn renders_hello_world() { + let mut terminal = + Terminal::new(TestBackend::new(15, 1)).expect("test backend initialization"); + terminal.draw(render).expect("render frame"); + terminal.backend().assert_buffer_lines(["hello world "]); + } +}