meli/src/bin.rs

219 lines
9.0 KiB
Rust
Raw Normal View History

2017-09-01 15:24:32 +03:00
/*
* meli - bin.rs
2017-09-01 15:24:32 +03:00
*
* Copyright 2017 Manos Pitsidianakis
*
2017-09-01 15:24:32 +03:00
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-11 18:58:57 +03:00
pub mod ui;
2018-07-11 17:07:51 +03:00
use ui::*;
extern crate melib;
2018-07-11 18:58:57 +03:00
extern crate nom;
2018-07-11 17:07:51 +03:00
extern crate termion;
2018-07-11 18:58:57 +03:00
pub use melib::*;
2017-09-01 15:24:32 +03:00
2018-07-11 17:07:51 +03:00
use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
2017-09-28 18:06:35 +03:00
use std::thread;
2018-07-11 17:07:51 +03:00
use std::io::{stdout, stdin, };
use std::collections::VecDeque;
use std::time::{Duration, Instant};
2017-09-28 18:06:35 +03:00
fn main() {
2018-07-11 17:07:51 +03:00
/* Lock all stdios */
let _stdout = stdout();
let mut _stdout = _stdout.lock();
let stdin = stdin();
let stdin = stdin;
/*
let _stderr = stderr();
let mut _stderr = _stderr.lock();
*/
2018-07-11 17:07:51 +03:00
2017-09-28 18:06:35 +03:00
2018-07-11 17:07:51 +03:00
let (sender, receiver): (SyncSender<ThreadEvent>, Receiver<ThreadEvent>) = sync_channel(::std::mem::size_of::<ThreadEvent>());
2017-09-28 18:06:35 +03:00
{
let mut cmd_queue = VecDeque::with_capacity(5);
2017-09-28 18:06:35 +03:00
let sender = sender.clone();
2018-07-11 17:07:51 +03:00
thread::Builder::new().name("input-thread".to_string()).spawn(move || {
get_events(stdin, move | k| {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: queue is {:?}", Instant::now(), cmd_queue);
let front: Option<(Instant, char)> = cmd_queue.front().map(|v: &(Instant, char)| { v.clone() });
let back: Option<(Instant, char)> = cmd_queue.back().map(|v: &(Instant, char)| { v.clone() });
let mut push: Option<(Instant, char)> = None;
if let Key::Char(v) = k {
if v == 'g' {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: got 'g' in thread",Instant::now());
push = Some((Instant::now(), v));
} else if v > '/' && v < ':' {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: got '{}' in thread", Instant::now(), v);
if let Some((_, 'g')) = front {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: 'g' is front", Instant::now());
match back {
Some((i, cmd)) if cmd != 'g' => {
let (i, cmd) = back.unwrap();
let n = cmd as u8;
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: check for num c={}, n={}", Instant::now(),cmd, n);
if n > 0x2f && n < 0x3a {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: got a num {}", Instant::now(), cmd);
let now = Instant::now();
if now - i < Duration::from_millis(300) {
push = Some((now,cmd));
let ten_millis = Duration::from_millis(10);
return;
}
}
},
Some((i, cmd)) => {
let n = v as u8;
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: check for num c={}, n={}", Instant::now(),v, n);
if n > 0x2f && n < 0x3a {
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: got a num {}", Instant::now(), v);
let now = Instant::now();
if now - i < Duration::from_millis(300) {
push = Some((now,v));
}
cmd_queue.pop_front();
let mut s = String::with_capacity(3);
for (_, c) in cmd_queue.iter() {
s.push(*c);
}
s.push(v);
let times = s.parse::<usize>();
2018-07-14 21:41:38 +03:00
//eprintln!("{:?}: parsed {:?}", Instant::now(), times);
if let Ok(g) = times {
sender.send(ThreadEvent::GoCmd(g)).unwrap();
return;
}
}
},
None => {},
}
}
}
if let Some(v) = push {
cmd_queue.push_back(v);
return;
}
}
if push.is_none() {sender.send(ThreadEvent::Input(k)).unwrap();}
})}).unwrap();
2017-09-28 18:06:35 +03:00
}
2018-07-14 21:41:38 +03:00
/*
let folder_length = set.accounts["test_account"].folders.len();
let mut account = Account::new("test_account".to_string(), set.accounts["test_account"].clone(), backends);
2018-07-14 21:41:38 +03:00
2017-09-28 18:06:35 +03:00
{
let sender = sender.clone();
account.watch(RefreshEventConsumer::new(Box::new(move |r| {
sender.send(ThreadEvent::from(r)).unwrap();
})));
}
2018-07-14 21:41:38 +03:00
*/
let mut state = State::new(_stdout);
2018-07-11 17:07:51 +03:00
2018-07-14 21:41:38 +03:00
let menu = Entity {component: Box::new(AccountMenu::new(&state.context.accounts)) };
2018-07-11 17:07:51 +03:00
let listing = MailListing::new(Mailbox::new_dummy());
let b = Entity { component: Box::new(listing) };
2018-07-14 15:04:42 +03:00
let window = Entity { component: Box::new(VSplit::new(menu,b,90)) };
let status_bar = Entity { component: Box::new(StatusBar::new(window)) };
state.register_entity(status_bar);
2018-07-14 21:41:38 +03:00
let mut idxa = 0;
let mut idxm = 0;
2018-07-14 22:40:44 +03:00
let account_length = state.context.accounts.len();
'main: loop {
2018-07-14 21:41:38 +03:00
state.refresh_mailbox(idxa,idxm);
let folder_length = state.context.accounts[idxa].len();
state.render();
'inner: loop {
2017-09-28 18:06:35 +03:00
match receiver.recv().unwrap() {
2018-07-11 17:07:51 +03:00
ThreadEvent::Input(k) => {
match k {
key @ Key::Char('j') | key @ Key::Char('k') => {
state.rcv_event(UIEvent { id: 0, event_type: UIEventType::Input(key)});
2018-07-14 15:04:42 +03:00
state.redraw();
2018-07-11 18:58:57 +03:00
},
2018-07-11 17:07:51 +03:00
key @ Key::Up | key @ Key::Down => {
state.rcv_event(UIEvent { id: 0, event_type: UIEventType::Input(key)});
2018-07-14 15:04:42 +03:00
state.redraw();
}
2018-07-11 17:07:51 +03:00
Key::Char('\n') => {
state.rcv_event(UIEvent { id: 0, event_type: UIEventType::Input(Key::Char('\n'))});
2018-07-14 15:04:42 +03:00
state.redraw();
2017-09-28 18:06:35 +03:00
}
2018-07-11 17:07:51 +03:00
Key::Char('i') | Key::Esc => {
state.rcv_event(UIEvent { id: 0, event_type: UIEventType::Input(Key::Esc)});
2018-07-14 15:04:42 +03:00
state.redraw();
2018-07-11 17:07:51 +03:00
}
Key::F(_) => {
},
Key::Char('q') | Key::Char('Q') => {
break 'main;
},
2018-07-14 21:41:38 +03:00
Key::Char('J') => if idxm + 1 < folder_length {
idxm += 1;
2017-09-28 18:06:35 +03:00
break 'inner;
2018-07-11 17:07:51 +03:00
},
2018-07-14 21:41:38 +03:00
Key::Char('K') => if idxm > 0 {
idxm -= 1;
2017-09-28 18:06:35 +03:00
break 'inner;
2018-07-11 17:07:51 +03:00
},
2018-07-14 22:40:44 +03:00
Key::Char('l') => if idxa + 1 < account_length {
idxa += 1;
idxm = 0;
break 'inner;
},
Key::Char('h') => if idxa > 0 {
idxa -= 1;
idxm = 0;
break 'inner;
},
Key::Char('r') => {
state.update_size();
state.render();
},
Key::Char(v) if v > '/' && v < ':' => {
},
2018-07-11 17:07:51 +03:00
_ => {}
2017-09-28 18:06:35 +03:00
}
2017-09-01 15:24:32 +03:00
},
2018-07-11 17:07:51 +03:00
ThreadEvent::RefreshMailbox { name : n } => {
2017-09-28 18:06:35 +03:00
eprintln!("Refresh mailbox {}", n);
2018-07-11 17:07:51 +03:00
},
ThreadEvent::UIEventType(e) => {
state.rcv_event(UIEvent { id: 0, event_type: e});
state.render();
},
ThreadEvent::GoCmd(v) => {
eprintln!("got go cmd with {:?}", v);
},
}
}
}
}