Initial commit

This commit is contained in:
EdJoPaTo
2020-10-30 16:10:26 +01:00
commit b539806734
8 changed files with 724 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
use std::io;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use termion::event::Key;
use termion::input::TermRead;
pub enum Event<I> {
Input(I),
Tick,
}
/// A small event handler that wrap termion input and tick events. Each event
/// type is handled in its own thread and returned to a common `Receiver`
#[allow(dead_code)]
pub struct Events {
rx: mpsc::Receiver<Event<Key>>,
}
#[derive(Debug, Clone, Copy)]
pub struct Config {
pub exit_key: Key,
pub tick_rate: Duration,
}
impl Default for Config {
fn default() -> Config {
Config {
exit_key: Key::Char('q'),
tick_rate: Duration::from_millis(250),
}
}
}
impl Events {
pub fn new() -> Events {
Events::with_config(Config::default())
}
pub fn with_config(config: Config) -> Events {
let (tx, rx) = mpsc::channel();
{
let tx = tx.clone();
thread::spawn(move || {
let stdin = io::stdin();
for evt in stdin.keys() {
if let Ok(key) = evt {
if let Err(err) = tx.send(Event::Input(key)) {
eprintln!("{}", err);
return;
}
if key == config.exit_key {
return;
}
}
}
})
};
thread::spawn(move || loop {
if tx.send(Event::Tick).is_err() {
break;
}
thread::sleep(config.tick_rate);
});
Events { rx }
}
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv()
}
}
+63
View File
@@ -0,0 +1,63 @@
pub mod event;
use tui_tree_widget::{flatten, identifier, TreeItem, TreeState};
pub struct StatefulTree<'a> {
pub state: TreeState,
pub items: Vec<TreeItem<'a>>,
}
impl<'a> StatefulTree<'a> {
#[allow(dead_code)]
pub fn new() -> StatefulTree<'a> {
StatefulTree {
state: TreeState::default(),
items: Vec::new(),
}
}
pub fn with_items(items: Vec<TreeItem<'a>>) -> StatefulTree<'a> {
StatefulTree {
state: TreeState::default(),
items,
}
}
fn move_up_down(&mut self, down: bool) {
let visible = flatten(&self.state.opened, &self.items);
let current_identifier = self.state.selected();
let current_index = visible
.iter()
.position(|o| o.identifier == current_identifier);
let new_index = current_index.map_or(0, |current_index| {
if down {
current_index.saturating_add(1)
} else {
current_index.saturating_sub(1)
}
.min(visible.len() - 1)
});
let new_identifier = visible.get(new_index).unwrap().identifier.to_owned();
self.state.select(new_identifier);
}
pub fn next(&mut self) {
self.move_up_down(true);
}
pub fn previous(&mut self) {
self.move_up_down(false);
}
pub fn close(&mut self) {
let selected = self.state.selected();
if !self.state.close(&selected) {
let (head, _) = identifier::get_without_leaf(&selected);
self.state.select(head);
}
}
pub fn open(&mut self) {
self.state.open(self.state.selected());
}
}