Caveat: I don't know what I'm doing; I'm just tinkering, and have no real programming experience
To scratch my own itch, I'm trying to rewrite the Debian joke-app "sl" (draws an animated ASCII train going across the screen when the user mistypes "ls" as "sl") in Rust (just to see if I can do it).
I've gotten far enough to know how to create an alternate screen, print some text at a specified location, and then quit the program, restoring the original (non-alternate) screen. Because I'll be printing multiple frames of the train, I figure the cusor-positioning and printing of text should be in a function, but I can't figure out how to pass the AlternateScreen item to the function. Here's my basic code, followed by an example error I get at compile time (I've tried other permutations, and gotten other errors, but the point is that I've figured out multiple ways of not doing it, but I can't figure out how to do it):
extern crate termion;
use termion::{clear, screen::*};
use termion::raw::IntoRawMode;
use std::io::{Write, stdout};
use std::{time, thread};
fn print_object(&mut screen: termion::screen::AlternateScreen, &obj: &str, x: u16, y: u16) {
// Jump to the specified coords
writeln!(screen, "{}", termion::cursor::Goto(x, y));
// Print the object
write!(screen, "{}", &obj).unwrap();
screen.flush().unwrap(); // Force the previous write! to display (which otherwise doesn't without a /n).
} // end of print_object()
fn main() -> Result<(), std::io::Error> {
// Define useful variables
let obj: &str;
// Enter raw mode, using a different screen than the stdout default (so we can restore the screen later).
let mut screen = AlternateScreen::from(stdout().into_raw_mode().expect("Error entering raw mode."));
// Hide the cursor (so you don't have a white rectangle bouncing around the screen).
writeln!(screen, "{}", termion::cursor::Hide).expect("Error hiding the cursor.");
// Clear the screen.
writeln!(screen, "{}", clear::All).expect("Error clearing the screen.");
// Test print a message, wait 3 seconds, and then end the program, restoring the screen.
let x: u16 = 12;
let y: u16 = 22;
obj = "This is after clearing the screen. And now we're waiting three seconds.";
print_object(&screen, &obj, x, y);
// writeln!(screen, "{}{}", termion::cursor::Goto(x, y), &obj).unwrap();
thread::sleep(time::Duration::from_millis(3000)); // Or use 'from_secs(3)'.
Ok(())
} // end of main()
And the compiler error:
westk@westkent:~/PROGRAMMING/RUST/LEARNING/sl_rust$ cargo run
Compiling sl_rust v0.1.0 (/home/westk/PROGRAMMING/RUST/LEARNING/sl_rust)
error[E0107]: wrong number of type arguments: expected 1, found 0
--> src/main.rs:25:30
|
25 | fn print_object(&mut screen: termion::screen::AlternateScreen, &obj: &str, x: u16, y: u16) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 type argument
error: aborting due to previous error
For more information about this error, try `rustc --explain E0107`.
error: could not compile `sl_rust`
To learn more, run the command again with --verbose.
Any help in figuring this out would be appreciated. Thanks!