hunter 1.0.1

Fast, lag-free terminal file browser
// use termion::event::{Event, Key};

// use std::sync::mpsc::{channel, Sender, Receiver, SendError};
// use std::io::Write;
// use std::ops::FnOnce;
// use std::marker::Send;

// use crate::widget::Widget;
// use crate::coordinates::Coordinates;

// pub enum WidgetProto2<W> {
//     SetCoordinates(Coordinates),
//     DrawHeader,
//     DrawFooter,
//     Refresh,
//     Draw,
//     Event(Event),
//     Key(Key),
//     GetWidget,
//     ReplaceWidget(Box<Fn() -> W + Send>)
// }

// unsafe impl<W> Send for AsyncWidget2<W> where W: Widget + Send {}
// struct AsyncWidget2<W>  where W: Widget + Send,
// {
//     pub widget: W,
//     pub coordinates: Coordinates,
//     pub rx_cmd: Receiver<WidgetProto2<W>>,
//     pub tx_widget: Sender<W>
// }

// pub struct AsyncPlug2<W>  {
//     pub coordinates: Coordinates,
//     tx_cmd: Sender<WidgetProto2<W>>,
//     rx_widget: Receiver<W>
// }

// impl<W: Widget + Send> PartialEq for AsyncWidget2<W> {
//     fn eq(&self, other: &AsyncWidget2<W>) -> bool {
//         if self.coordinates == other.coordinates {
//             true
//         } else {
//             false
//         }
//     }
// }

// impl<W> PartialEq for AsyncPlug2<W>  {
//     fn eq(&self, other: &AsyncPlug2<W>) -> bool {
//         if self.coordinates == other.coordinates {
//             true
//         } else {
//             false
//         }
//     }
// }

// impl<W: Widget + Send> AsyncWidget2<W> {
//     fn new_from_widget(widget: W,
//                        coordinates: Coordinates,
//                        rx_cmd: Receiver<WidgetProto2<W>>,
//                        tx_widget: Sender<W>) -> AsyncWidget2<W> {
//         AsyncWidget2::<W> {
//             widget: widget,
//             coordinates: coordinates,
//             rx_cmd: rx_cmd,
//             tx_widget: tx_widget
//         }
//     }

//     fn replace_widget(&mut self, closure: Box<Fn() -> W + Send>) {
//         self.widget = closure();
//     }

//     fn get_widget(&mut self) {
//         // let widget = self.widget;
//         // self.widget = None;
//         // self.tx_widget.send(widget).unwrap();
//     }

//     fn set_coordinates(&mut self, coordinates: Coordinates) {
//         if self.coordinates == coordinates {
//             return;
//         }
//         self.coordinates = coordinates.clone();
//         self.widget.set_coordinates(&coordinates);
//         self.refresh();
//     }

//     fn refresh(&mut self) {
//         self.widget.refresh();
//     }

//     fn draw(&self) {
//         let drawlist = self.widget.get_drawlist();
//         let mut bufout = std::io::BufWriter::new(std::io::stdout());
//         write!(bufout, "{}", drawlist).unwrap();
//     }

//     fn on_key(&mut self, key: Key) {
//         self.widget.on_key(key);
//     }

//     fn on_event(&mut self, event: Event) {
//         self.widget.on_event(event);
//     }

//     fn on_message(&mut self, msg: WidgetProto2<W>) {
//         match msg {
//             WidgetProto2::SetCoordinates(coordinates) =>
//                 self.set_coordinates(coordinates),
//             WidgetProto2::DrawHeader => {},
//             WidgetProto2::DrawFooter => {},
//             WidgetProto2::Refresh => { self.refresh(); },
//             WidgetProto2::Draw => { self.draw(); },
//             WidgetProto2::Event(event) => { self.on_event(event); },
//             WidgetProto2::Key(key) => { self.on_key(key); }
//             WidgetProto2::GetWidget => { self.get_widget(); }
//             WidgetProto2::ReplaceWidget(widget) => { self.replace_widget(widget); }
//         }
//     }

//     fn get_messages(&mut self) -> Result<Vec<WidgetProto2<W>>, Box<std::error::Error>> {
//         let msg = self.rx_cmd.recv()?;
//         let mut msgs = self.rx_cmd.try_iter().collect::<Vec<WidgetProto2<W>>>();
//         msgs.insert(0, msg);

//         let mut kill_pos = 0;

//         for (i, msg) in msgs.iter().enumerate() {
//             match msg {
//                 WidgetProto2::ReplaceWidget(_) => kill_pos = i,
//                 _ => {}
//             }
//         }

//         let msgs = msgs.split_off(kill_pos);
//         Ok(msgs)
//     }

//     fn run(&mut self) {
//         loop {
//             std::thread::sleep_ms(20);
//             match self.get_messages() {
//                 Ok(msgs) => {
//                     for msg in msgs {
//                         self.on_message(msg);
//                     }
//                 }
//                 Err(err) => return
//             };
//         }
//     }
// }

// // impl AsyncPlug {
// //     pub fn new(widget: Box<Widget + Send>) -> AsyncPlug {
// //         let coordinates = widget.get_coordinates().clone();
// //         let (tx_cmd, rx_cmd) = channel();
// //         let mut async_widget = AsyncWidget::new_from_widget(widget,
// //                                                             coordinates.clone(),
// //                                                             rx_cmd);

// //         std::thread::spawn(move || async_widget.run() );

// //         AsyncPlug {
// //             coordinates: coordinates,
// //             tx_cmd: tx_cmd
// //         }
// //     }

// //     pub fn new_from_closure(closure: WidgetClosure) -> AsyncPlug
// //     {
// //         let (tx_cmd, rx_cmd) = channel();

// //         std::thread::spawn(move || {
// //             let widget = closure();
// //             let mut async_widget
// //                 = AsyncWidget::new_from_widget(widget,
// //                                                Coordinates::new(),
// //                                                rx_cmd);
// //             async_widget.run();
// //         });

// //         AsyncPlug {
// //             coordinates: Coordinates::new(),
// //             tx_cmd: tx_cmd
// //         }
// //     }

// //     pub fn replace_widget(&mut self, closure: WidgetClosure) {
// //         let tx = self.tx_cmd.clone();
// //         std::thread::spawn(move || {
// //             tx.send(WidgetProto::ReplaceWidget(closure)).unwrap();
// //         });
// //     }

// //     fn tx_cmd(&self, cmd: WidgetProto) -> Result<(),SendError<WidgetProto>> {
// //         self.tx_cmd.send(cmd)
// //     }
// // }

// impl<W: Widget + Send + 'static> AsyncPlug2<W>  {
//     pub fn new_from_closure(closure: Box<Fn() -> W + Send>) -> AsyncPlug2<W>
//     {
//         let (tx_cmd, rx_cmd) = channel();
//         let (tx_widget, rx_widget) = channel();

//         std::thread::spawn(move || {
//             let widget = closure();
//             let mut async_widget
//                 = AsyncWidget2::new_from_widget(widget,
//                                                Coordinates::new(),
//                                                 rx_cmd,
//                                                 tx_widget);
//             async_widget.run();
//         });

//         AsyncPlug2 {
//             coordinates: Coordinates::new(),
//             tx_cmd: tx_cmd,
//             rx_widget: rx_widget
//         }
//     }

//     pub fn replace_widget(&mut self, closure: Box<Fn() -> W + Send>) {
//         let tx = self.tx_cmd.clone();
//         std::thread::spawn(move || {
//             tx.send(WidgetProto2::ReplaceWidget(closure)).unwrap();
//         });
//     }

//     pub fn get_widget(&mut self) -> W {
//         self.tx_cmd(WidgetProto2::GetWidget).unwrap();
//         let widget = self.rx_widget.recv().unwrap();
//         widget
//     }

//     fn tx_cmd(&self, cmd: WidgetProto2<W>) -> Result<(),SendError<WidgetProto2<W>>> {
//         self.tx_cmd.send(cmd)
//     }
// }

// impl<W: Widget + Send + 'static> Widget for AsyncPlug2<W> {
//     fn render_header(&self) -> String {
//         self.tx_cmd(WidgetProto2::DrawHeader).unwrap();
//         "".to_string()
//     }

//     fn refresh(&mut self) {
//         self.tx_cmd(WidgetProto2::Refresh).unwrap();
//     }

//     fn get_drawlist(&self) -> String {
//         match self.tx_cmd(WidgetProto2::Draw) {
//             Ok(_) => {},
//             Err(err) => {dbg!(err.to_string());}
//         }
//         "".to_string()
//     }

//     fn get_coordinates(&self) -> &Coordinates {
//         &self.coordinates
//     }
//     fn set_coordinates(&mut self, coordinates: &Coordinates) {
//         if self.coordinates == *coordinates {
//             return;
//         }
//         self.coordinates = coordinates.clone();
//         self.tx_cmd(WidgetProto2::SetCoordinates(coordinates.clone())).unwrap();
//         self.refresh();
//     }
//     fn on_event(&mut self, event: Event) {
//         self.tx_cmd(WidgetProto2::Event(event)).unwrap();
//     }
//     fn on_key(&mut self, key: Key) {
//         self.tx_cmd(WidgetProto2::Key(key)).unwrap();
//     }
// }