#![allow(unused_must_use, dead_code)]
extern crate termion;
mod mindmap;
use termion::*;
use termion::raw::IntoRawMode;
use termion::input::TermRead;
use termion::event::Key;
use std::io::{Write, stdout, stdin};
use std::collections::HashMap;
use mindmap::Bubble;
fn main() {
// Enter alternate screen
{
let stdout = stdout();
let stdout = stdout.lock().into_raw_mode().unwrap();
let mut screen = screen::AlternateScreen::from(stdout);
let stdin = stdin();
let stdin = stdin.lock();
let state = State::new(&screen);
screen.flush().unwrap();
let mut events = stdin.events();
let mut cmdline = false;
let mut command = String::with_capacity(30);
loop {
let event = events.next().unwrap().unwrap();
if cmdline {
match event {
event::Event::Key(key) => match key {
// Escape
Key::Esc => {
cmdline = false;
write!(screen, "{}{}{}",
clear::CurrentLine,
cursor::Goto(20, 10),
command)
},
// Newline
Key::Char('\n') => {
cmdline = false;
write!(screen, "{}{}{}",
clear::CurrentLine,
cursor::Goto(20, 10),
command)
},
Key::Backspace => {
// Don't go back when no characters have been removed
match command.pop() {
Some(_) => write!(screen, "{0} {0}", cursor::Left(1)),
None => Ok(()),
}
},
// Append to command
Key::Char(a) => Ok({
let a = a as char;
write!(screen, "{}", a);
command.push(a);
}),
_ => Ok(()),
},
_ => Ok(()),
}
.unwrap();
} else {
match event {
event::Event::Key(key) => match key {
// Quit
Key::Char('q') => break,
// Clear the screen
Key::Char('c') => write!(screen, "{}", clear::All),
// Enter command line
Key::Char(':') => Ok({
let term_size = terminal_size().unwrap();
cmdline = true;
command.clear();
write!(screen, "{}:", termion::cursor::Goto(0, term_size.1));
}),
_ => Ok(()),
},
_ => Ok(()),
}
.unwrap();
}
screen.flush().unwrap();
}
}
// Exit alternate screen
}
struct State<'a> {
mindmap: Bubble,
cmdline: bool,
command: String,
command_map: HashMap<String, fn(State, String)>,
keybind_map: HashMap<event::Key, String>,
screen: &'a dyn Write,
}
impl<'a> State<'a> {
fn new(screen: &'a dyn Write) -> Self {
Self {
mindmap: Bubble::new(""),
cmdline: false,
command: String::new(),
command_map: HashMap::new(),
keybind_map: HashMap::new(),
screen: screen,
}
}
}