extern crate easycurses;
use easycurses::*;
use std::cmp::{max, min};
use std::iter::repeat;
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;
fn main() {
let mut easy = EasyCurses::initialize_system().unwrap();
easy.set_cursor_visibility(CursorVisibility::Invisible);
easy.set_echo(false);
easy.set_keypad_enabled(true);
easy.set_input_mode(InputMode::Character);
easy.set_input_timeout(TimeoutMode::Immediate);
easy.set_scrolling(true);
let (_, mut col_count) = easy.get_row_col_count();
let frame_target_duration = Duration::new(1, 0).checked_div(60).expect("failed when rhs!=0, what?");
let mut position = 5;
loop {
let top_of_loop = Instant::now();
while let Some(input) = easy.get_input() {
match input {
Input::KeyLeft => position = max(0, position - 1),
Input::KeyRight => position = min(col_count - 1, position + 1),
Input::KeyResize => {
col_count = easy.get_row_col_count().1;
position = min(col_count - 1, position);
}
other => println!("Unknown: {:?}", other),
}
}
let output = repeat('#').take(position as usize).collect::<String>();
let elapsed_this_frame = top_of_loop.elapsed();
if let Some(frame_remaining) = frame_target_duration.checked_sub(elapsed_this_frame) {
sleep(frame_remaining);
}
easy.print("\n");
easy.print(&output);
easy.refresh();
}
}