+mod keys;
mod map;
mod point;
mod selector;
+pub use keys::*;
pub use map::*;
pub use point::*;
pub use selector::*;
}
}
}
- stdout().flush()?;
Ok(())
}
draw_frame(map, offset)?;
+ stdout().flush()?;
+
thread::sleep(time);
}
}
for _ in 0..steps {
execute!(stdout(), terminal::Clear(terminal::ClearType::All))?;
draw_frame(&mut tmp, offset)?;
+ stdout().flush()?;
tmp.update();
thread::sleep(time);
}
--- /dev/null
+use crossterm::{
+ event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
+ execute, queue, terminal,
+};
+use eyre::Result;
+use std::time::Duration;
+
+pub fn action_loop(speed: usize) -> Result<()> {
+ loop {
+ if poll(Duration::from_millis(speed.try_into()?))? {
+ if let Event::Key(key) = read()? {
+ match key_event(key) {
+ Action::None => continue,
+ Action::Exit => crate::exit()?,
+ x => println!("{:?}", x),
+ }
+ }
+ }
+ }
+}
+
+#[derive(Debug)]
+pub enum Action {
+ None,
+ Move(Direction),
+ Resize(Direction),
+ Transport(Clock),
+ Channel(usize),
+ Select,
+ SelectArea,
+ Reload,
+ Randomize,
+ Exit,
+ Help,
+}
+
+#[derive(Debug)]
+pub enum Clock {
+ Stop,
+ Start,
+ Pause,
+ Faster(usize),
+ Slower(usize),
+}
+
+#[derive(Debug)]
+pub enum Direction {
+ Up,
+ Down,
+ Left,
+ Right,
+}
+
+pub fn key_event(event: KeyEvent) -> Action {
+ match event.modifiers {
+ KeyModifiers::NONE => match_normal_key(event.code),
+ KeyModifiers::CONTROL => match_ctl_key(event.code),
+ KeyModifiers::ALT => match_alt_key(event.code),
+ KeyModifiers::SHIFT => match_shift_key(event.code),
+ _ => Action::None,
+ }
+}
+
+pub fn match_normal_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Enter => Action::Transport(Clock::Pause),
+ KeyCode::Esc => Action::Exit,
+ KeyCode::Up => Action::Move(Direction::Up),
+ KeyCode::Down => Action::Move(Direction::Down),
+ KeyCode::Right => Action::Move(Direction::Right),
+ KeyCode::Left => Action::Move(Direction::Left),
+ KeyCode::Char(c) => match c {
+ '?' => Action::Help,
+ ' ' => Action::Select,
+ 'q' => Action::Exit,
+ 'j' => Action::Move(Direction::Down),
+ 'k' => Action::Move(Direction::Up),
+ 'h' => Action::Move(Direction::Left),
+ 'l' => Action::Move(Direction::Right),
+ 'r' => Action::Reload,
+ '[' => Action::Transport(Clock::Slower(1)),
+ ']' => Action::Transport(Clock::Faster(1)),
+ '{' => Action::Transport(Clock::Slower(5)),
+ '}' => Action::Transport(Clock::Faster(5)),
+ '0' => Action::Channel(0),
+ '1' => Action::Channel(1),
+ '2' => Action::Channel(2),
+ '3' => Action::Channel(3),
+ '4' => Action::Channel(4),
+ '5' => Action::Channel(5),
+ '6' => Action::Channel(6),
+ '7' => Action::Channel(7),
+ '8' => Action::Channel(8),
+ '9' => Action::Channel(9),
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_ctl_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Char(c) => match c {
+ 'c' => Action::Exit,
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_shift_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Up => Action::Resize(Direction::Up),
+ KeyCode::Down => Action::Resize(Direction::Down),
+ KeyCode::Right => Action::Resize(Direction::Right),
+ KeyCode::Left => Action::Resize(Direction::Left),
+ KeyCode::Char(c) => match c {
+ 'J' => Action::Resize(Direction::Down),
+ 'K' => Action::Resize(Direction::Up),
+ 'H' => Action::Resize(Direction::Left),
+ 'L' => Action::Resize(Direction::Right),
+ 'R' => Action::Randomize,
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_alt_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Char(c) => match c {
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
use crate::Point;
use crossterm::{
- cursor,
- event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
- execute, queue,
+ cursor, queue,
style::{self, Attribute, Color},
- terminal,
};
use eyre::Result;
use std::{
io::stdout,
ops::{Deref, DerefMut},
- time::Duration,
};
pub struct Cursor {
)?;
Ok(())
}
-
- pub fn update(&mut self, event: KeyEvent) -> Result<()> {
- let key = event.code;
- match event.modifiers {
- KeyModifiers::NONE => match key {
- KeyCode::Char(c) => match c {
- 'j' => self.y += 1,
- 'k' => {
- if self.y != 0 {
- self.y -= 1
- }
- }
- 'h' => {
- if self.x != 0 {
- self.x -= 1
- }
- }
- 'l' => self.x += 1,
- _ => {}
- },
- _ => {}
- },
- KeyModifiers::CONTROL => match key {
- KeyCode::Char(c) => match c {
- 'c' => std::process::exit(0),
- _ => {}
- },
- _ => {}
- },
- KeyModifiers::ALT => match key {
- _ => {}
- },
- KeyModifiers::SHIFT => match key {
- _ => {}
- },
- _ => {}
- };
- Ok(())
- }
-}
-
-pub fn move_cursor() -> Result<()> {
- let mut cursor = Cursor::new();
- loop {
- if poll(Duration::from_millis(10))? {
- execute!(stdout(), terminal::Clear(terminal::ClearType::All))?;
- match read()? {
- Event::Key(event) => {
- cursor.update(event)?;
- }
- _ => continue,
- }
- }
- cursor.render()?;
- }
}
--- /dev/null
+use crossterm::{
+ event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
+ execute, queue, terminal,
+};
+
+use super::*;
+
+pub enum Action {
+ None,
+ Move(Direction),
+ Resize(Direction),
+ Transport(Transport),
+ Select,
+ UnSelect,
+ Exit,
+}
+
+pub enum Transport {
+ Stop,
+ Start,
+ Pause,
+ Faster(usize),
+ Slower(usize),
+}
+
+pub enum Direction {
+ Up,
+ Down,
+ Left,
+ Right,
+}
+
+pub fn key_event(event: KeyEvent) -> Action {
+ match event.modifiers {
+ KeyModifiers::NONE => match_normal_key(event.code),
+ KeyModifiers::CONTROL => match_ctl_key(event.code),
+ KeyModifiers::ALT => match_alt_key(event.code),
+ KeyModifiers::SHIFT => match_shift_key(event.code),
+ _ => Action::None,
+ }
+}
+
+pub fn match_normal_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Enter => Action::Transport(Transport::Pause),
+ KeyCode::Esc => Action::Exit,
+ KeyCode::Up => Action::Move(Direction::Up),
+ KeyCode::Down => Action::Move(Direction::Down),
+ KeyCode::Right => Action::Move(Direction::Right),
+ KeyCode::Left => Action::Move(Direction::Left),
+ KeyCode::Char(c) => match c {
+ ' ' => Action::Select,
+ 'q' => Action::Exit,
+ 'j' => Action::Move(Direction::Down),
+ 'k' => Action::Move(Direction::Up),
+ 'h' => Action::Move(Direction::Left),
+ 'l' => Action::Move(Direction::Right),
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_ctl_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Char(c) => match c {
+ 'c' => Action::Exit,
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_shift_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Enter => Action::Transport(Transport::Stop),
+ KeyCode::Up => Action::Resize(Direction::Up),
+ KeyCode::Down => Action::Resize(Direction::Down),
+ KeyCode::Right => Action::Resize(Direction::Right),
+ KeyCode::Left => Action::Resize(Direction::Left),
+ KeyCode::Char(c) => match c {
+ ' ' => Action::UnSelect,
+ 'j' => Action::Resize(Direction::Down),
+ 'k' => Action::Resize(Direction::Up),
+ 'h' => Action::Resize(Direction::Left),
+ 'l' => Action::Resize(Direction::Right),
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
+
+pub fn match_alt_key(key: KeyCode) -> Action {
+ match key {
+ KeyCode::Char(c) => match c {
+ _ => Action::None,
+ },
+ _ => Action::None,
+ }
+}
pub use graphics::*;
pub use music::*;
+use eyre::Result;
use std::time::Duration;
+pub fn exit() -> Result<()> {
+ crossterm::terminal::disable_raw_mode()?;
+ std::process::exit(0);
+}
+
pub fn bpm_to_ms(bpm: usize) -> Duration {
let ms = 60000 / bpm;
Duration::from_millis(ms.try_into().unwrap())
use cellseq::*;
use eyre::Result;
+use crossterm::{
+ event::{poll, read, Event},
+ terminal,
+};
+
fn main() -> Result<()> {
- crossterm::terminal::enable_raw_mode()?;
+ terminal::enable_raw_mode()?;
+
+ action_loop(10)?;
+ // let mut mask: Mask<bool> = Mask::new(48, 24, false);
+ //
+ // let mut map = World::new(74, 32);
+ // map.randomize(0.75);
- let mut mask: Mask<bool> = Mask::new(48, 24, false);
- let mut map = World::new(74, 32);
- map.randomize(0.75);
+ // let time = bpm_to_ms(100);
- let time = bpm_to_ms(100);
+ // loop {
+ // if poll(std::time::Duration::from_millis(50))? {
+ // // It's guaranteed that the `read()` won't block when the `poll()`
+ // // function returns `true`
+ // match read()? {
+ // Event::Key(event) => println!("\n{:?}", event),
+ // _ => continue,
+ // }
+ // }
+ // }
- run_map(&mut map, Point::new(10, 2), time)?;
+ // run_map(&mut map, Point::new(10, 2), time)?;
// edit_mask(&mask, (10, 5))?;
// move_cursor()?;
Ok(())
}
}
}
-
-pub enum Transport {
- Pause,
- Play,
- FastForward(usize),
-}