main.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. use async_std::sync::Arc;
  2. use std::{fs::File, io, io::Read, path::PathBuf};
  3. use darkfi::util::{
  4. cli::{get_log_config, get_log_level, spawn_config, Config},
  5. join_config_path,
  6. };
  7. use easy_parallel::Parallel;
  8. use log::{debug, info};
  9. use simplelog::*;
  10. use smol::Executor;
  11. use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
  12. use tui::{
  13. backend::{Backend, TermionBackend},
  14. Terminal,
  15. };
  16. pub mod config;
  17. pub mod error;
  18. pub mod model;
  19. pub mod options;
  20. pub mod parser;
  21. pub mod rpc;
  22. pub mod util;
  23. pub mod view;
  24. use crate::{
  25. config::{DnvConfig, CONFIG_FILE_CONTENTS},
  26. error::{DnetViewError, DnetViewResult},
  27. model::Model,
  28. options::ProgramOptions,
  29. parser::DataParser,
  30. view::View,
  31. };
  32. struct DnetView {
  33. model: Arc<Model>,
  34. view: View,
  35. }
  36. impl DnetView {
  37. fn new(model: Arc<Model>, view: View) -> Self {
  38. Self { model, view }
  39. }
  40. async fn render_view<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> DnetViewResult<()> {
  41. let mut asi = async_stdin();
  42. terminal.clear()?;
  43. self.view.id_menu.state.select(Some(0));
  44. self.view.msg_list.state.select(Some(0));
  45. loop {
  46. self.view.update(
  47. self.model.id_vec.lock().await.clone(),
  48. self.model.msg_map.lock().await.clone(),
  49. self.model.selectables.lock().await.clone(),
  50. );
  51. debug!(target: "dnetview::render_view()", "ID LIST: {:?}", self.view.id_menu.ids);
  52. let mut err: Option<DnetViewError> = None;
  53. terminal.draw(|f| match self.view.render(f) {
  54. Ok(()) => {}
  55. Err(e) => {
  56. err = Some(e);
  57. }
  58. })?;
  59. match err {
  60. Some(e) => return Err(e),
  61. None => {}
  62. }
  63. self.view.msg_list.scroll()?;
  64. for k in asi.by_ref().keys() {
  65. match k.unwrap() {
  66. Key::Char('q') => {
  67. terminal.clear()?;
  68. return Ok(())
  69. }
  70. Key::Char('j') => {
  71. self.view.id_menu.next();
  72. }
  73. Key::Char('k') => {
  74. self.view.id_menu.previous();
  75. }
  76. Key::Char('u') => {
  77. // TODO
  78. //view.msg_list.next();
  79. }
  80. Key::Char('d') => {
  81. // TODO
  82. //view.msg_list.previous();
  83. }
  84. _ => (),
  85. }
  86. }
  87. util::sleep(100).await;
  88. }
  89. }
  90. }
  91. #[async_std::main]
  92. async fn main() -> DnetViewResult<()> {
  93. debug!(target: "dnetview", "main() START");
  94. let options = ProgramOptions::load()?;
  95. let verbosity_level = options.app.occurrences_of("verbose");
  96. let log_level = get_log_level(verbosity_level);
  97. let log_config = get_log_config();
  98. let file = File::create(&*options.log_path).unwrap();
  99. WriteLogger::init(log_level, log_config, file)?;
  100. info!("Log level: {}", log_level);
  101. let config_path = join_config_path(&PathBuf::from("dnetview_config.toml"))?;
  102. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  103. let config = Config::<DnvConfig>::load(config_path)?;
  104. let stdout = io::stdout().into_raw_mode()?;
  105. let backend = TermionBackend::new(stdout);
  106. let mut terminal = Terminal::new(backend)?;
  107. terminal.clear()?;
  108. let model = Model::new();
  109. let view = View::new();
  110. let ex = Arc::new(Executor::new());
  111. let ex2 = ex.clone();
  112. let mut dnetview = DnetView::new(model.clone(), view.clone());
  113. let parser = DataParser::new(model.clone(), config);
  114. let nthreads = num_cpus::get();
  115. let (signal, shutdown) = async_channel::unbounded::<()>();
  116. let (_, result) = Parallel::new()
  117. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  118. .finish(|| {
  119. smol::future::block_on(async move {
  120. parser.start_connect_slots(ex2).await?;
  121. dnetview.render_view(&mut terminal).await?;
  122. drop(signal);
  123. Ok(())
  124. })
  125. });
  126. result
  127. }