main.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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::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.msg_map.lock().await.clone(),
  48. self.model.selectables.lock().await.clone(),
  49. //self.model.selectables2.lock().await.clone(),
  50. );
  51. //debug!(target: "dnetview::render_view()", "ID MENU: {:?}", self.view.id_menu.ids);
  52. //debug!(target: "dnetview::render_view()", "SELECTABLES ID LIST: {:?}", self.model.selectables.lock().await.keys());
  53. let mut err: Option<DnetViewError> = None;
  54. terminal.draw(|f| match self.view.render(f) {
  55. Ok(()) => {}
  56. Err(e) => {
  57. err = Some(e);
  58. }
  59. })?;
  60. match err {
  61. Some(e) => return Err(e),
  62. None => {}
  63. }
  64. self.view.msg_list.scroll()?;
  65. for k in asi.by_ref().keys() {
  66. match k.unwrap() {
  67. Key::Char('q') => {
  68. terminal.clear()?;
  69. return Ok(())
  70. }
  71. Key::Char('j') => {
  72. self.view.id_menu.next();
  73. }
  74. Key::Char('k') => {
  75. self.view.id_menu.previous();
  76. }
  77. Key::Char('u') => {
  78. // TODO
  79. //view.msg_list.next();
  80. }
  81. Key::Char('d') => {
  82. // TODO
  83. //view.msg_list.previous();
  84. }
  85. _ => (),
  86. }
  87. }
  88. util::sleep(100).await;
  89. }
  90. }
  91. }
  92. #[async_std::main]
  93. async fn main() -> DnetViewResult<()> {
  94. //debug!(target: "dnetview", "main() START");
  95. let options = ProgramOptions::load()?;
  96. let verbosity_level = options.app.occurrences_of("verbose");
  97. let log_level = get_log_level(verbosity_level);
  98. let log_config = get_log_config();
  99. let file = File::create(&*options.log_path).unwrap();
  100. WriteLogger::init(log_level, log_config, file)?;
  101. info!("Log level: {}", log_level);
  102. let config_path = join_config_path(&PathBuf::from("dnetview_config.toml"))?;
  103. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  104. let config = Config::<DnvConfig>::load(config_path)?;
  105. let stdout = io::stdout().into_raw_mode()?;
  106. let backend = TermionBackend::new(stdout);
  107. let mut terminal = Terminal::new(backend)?;
  108. terminal.clear()?;
  109. let model = Model::new();
  110. let view = View::new();
  111. let ex = Arc::new(Executor::new());
  112. let ex2 = ex.clone();
  113. let mut dnetview = DnetView::new(model.clone(), view);
  114. let parser = DataParser::new(model, config);
  115. let nthreads = num_cpus::get();
  116. let (signal, shutdown) = async_channel::unbounded::<()>();
  117. let (_, result) = Parallel::new()
  118. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  119. .finish(|| {
  120. smol::future::block_on(async move {
  121. parser.start_connect_slots(ex2).await?;
  122. dnetview.render_view(&mut terminal).await?;
  123. drop(signal);
  124. Ok(())
  125. })
  126. });
  127. result
  128. }