feat: add loading animation example
Add a busy-wait sleep on a monotonic clock and re-export String, format!, and Duration from the library. The example animates a progress bar with a spinner and logs each step in a Vec<String>. Co-Authored-By: qwen (lmstudio/qwen3.8-27b@q3_k_xl): wrote example
This commit is contained in:
+47
-3
@@ -1,11 +1,55 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
fn print(message: &str) {
|
||||
let _ = rslibc::io::write_str(message);
|
||||
use core::time::Duration;
|
||||
|
||||
use rslibc::{format, print, println, vec, String, Vec};
|
||||
|
||||
const FRAMES: [&str; 4] = ["|", "/", "-", "\\"];
|
||||
const WIDTH: usize = 20;
|
||||
const STEPS: usize = 10;
|
||||
|
||||
fn bar(done: usize) -> String {
|
||||
let mut out = String::new();
|
||||
for i in 0..WIDTH {
|
||||
out.push(if i < done { '#' } else { '-' });
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn load(step: usize) -> rslibc::Result<u32, &'static str> {
|
||||
// Pretend to do work and return a checksum-like value.
|
||||
let mut total = 0u32;
|
||||
for value in vec![1, 2, 3, 4, 5] {
|
||||
total = total.wrapping_add(value * step as u32);
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
#[rslibc_macros::main]
|
||||
async fn main() {
|
||||
print("Hello\n");
|
||||
println!("rslibc loading example");
|
||||
let mut log: Vec<String> = Vec::new();
|
||||
|
||||
for step in 0..STEPS {
|
||||
match load(step) {
|
||||
Ok(value) => {
|
||||
let frame = FRAMES[step % FRAMES.len()];
|
||||
let percent = 100 * (step + 1) / STEPS;
|
||||
print!(
|
||||
"\r [{}] {:>3}% {}",
|
||||
bar((step + 1) * WIDTH / STEPS),
|
||||
percent,
|
||||
frame
|
||||
);
|
||||
log.push(format!("step {step} -> {value}"));
|
||||
}
|
||||
Err(message) => println!("error: {message}"),
|
||||
}
|
||||
|
||||
rslibc::runtime::sleep(Duration::from_millis(60));
|
||||
}
|
||||
|
||||
println!("\r [{}] 100% done", bar(WIDTH));
|
||||
println!("log = {log:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user