main.rs 4.4 KB

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