main.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use std::{
  2. io,
  3. io::Read,
  4. time::{Duration, Instant},
  5. };
  6. use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
  7. use tui::{
  8. backend::{Backend, TermionBackend},
  9. Terminal,
  10. };
  11. pub mod app;
  12. pub mod info;
  13. pub mod list;
  14. pub mod types;
  15. pub mod ui;
  16. use crate::app::App;
  17. fn main() -> Result<(), io::Error> {
  18. // Set up terminal output
  19. let stdout = io::stdout().into_raw_mode()?;
  20. let backend = TermionBackend::new(stdout);
  21. let mut terminal = Terminal::new(backend)?;
  22. // create app and run it
  23. let tick_rate = Duration::from_millis(250);
  24. // here
  25. let app = App::new();
  26. let res = run_app(&mut terminal, app, tick_rate);
  27. terminal.clear()?;
  28. if let Err(err) = res {
  29. println!("{:?}", err)
  30. }
  31. Ok(())
  32. }
  33. fn run_app<B: Backend>(
  34. terminal: &mut Terminal<B>,
  35. mut app: App,
  36. tick_rate: Duration,
  37. ) -> io::Result<()> {
  38. let mut asi = async_stdin();
  39. terminal.clear()?;
  40. let mut last_tick = Instant::now();
  41. app.node_list.state.select(Some(0));
  42. loop {
  43. terminal.draw(|f| ui::ui(f, &mut app))?;
  44. for k in asi.by_ref().keys() {
  45. match k.unwrap() {
  46. Key::Char('q') => {
  47. terminal.clear()?;
  48. return Ok(())
  49. }
  50. Key::Char('j') => app.node_list.next(),
  51. Key::Char('k') => app.node_list.previous(),
  52. _ => (),
  53. }
  54. }
  55. if last_tick.elapsed() >= tick_rate {
  56. last_tick = Instant::now();
  57. }
  58. }
  59. }