From b5ca5a092982cc72021f32b7c05e616b4eddb823 Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sun, 20 Oct 2019 20:45:50 -0400 Subject: [PATCH] More factoring of tui-related code. --- src/main.rs | 110 ++++++++----------------------------------------- src/tui/app.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++ src/tui/ui.rs | 9 ++-- 3 files changed, 124 insertions(+), 99 deletions(-) diff --git a/src/main.rs b/src/main.rs index df4bfd5..fc4f6d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,8 +39,6 @@ mod skip_wizard; mod setup; mod tui; -use crate::tui::app::PrintWindow; -use crate::tui::event::{Events, Event, Config}; #[derive(StructOpt, Debug)] @@ -218,12 +216,7 @@ fn main() -> Result<(), Box> { if present_print_menu_tui { - let reports = tui::app::REPORTS; - - let events = Events::with_config(Config { - tick_rate: Duration::from_millis(250u64), - ..Config::default() - }); + use crate::tui::event::{Events, Event, Config}; let stdout = io::stdout().into_raw_mode()?; let stdout = MouseTerminal::from(stdout); @@ -232,11 +225,16 @@ fn main() -> Result<(), Box> { let mut terminal = Terminal::new(backend)?; terminal.hide_cursor()?; - let mut app = PrintWindow::new("Reports"); + let mut app = tui::app::PrintWindow::new("Reports"); + + let events = Events::with_config(Config { + tick_rate: Duration::from_millis(250u64), + ..Config::default() + }); loop { - tui::ui::draw(&mut terminal, &app, reports.len() as u16)?; + tui::ui::draw(&mut terminal, &app)?; match events.next()? { @@ -251,12 +249,6 @@ fn main() -> Result<(), Box> { Key::Down => { app.on_down(); } - Key::Left => { - // app.on_left(); - } - Key::Right => { - // app.on_right(); - } _ => {} }, _ => {} @@ -271,83 +263,15 @@ fn main() -> Result<(), Box> { std::mem::drop(terminal); std::thread::sleep(Duration::from_millis(10)); - for report in app.to_print { - println!("Exporting: {}", reports[report]); - match report + 1 { - 1 => { - csv_export::_1_account_sums_to_csv( - &settings, - &raw_acct_map, - &account_map - ); - } - 2 => { - csv_export::_2_account_sums_nonzero_to_csv( - &account_map, - &settings, - &raw_acct_map - ); - } - 3 => { - csv_export::_3_account_sums_to_csv_with_orig_basis( - &settings, - &raw_acct_map, - &account_map - ); - } - 4 => { - csv_export::_4_transaction_mvmt_detail_to_csv( - &settings, - &action_records_map, - &raw_acct_map, - &account_map, - &transactions_map - )?; - } - 5 => { - csv_export::_5_transaction_mvmt_summaries_to_csv( - &settings, - &action_records_map, - &raw_acct_map, - &account_map, - &transactions_map - )?; - } - 6 => { - csv_export::_6_transaction_mvmt_detail_to_csv_w_orig( - &settings, - &action_records_map, - &raw_acct_map, - &account_map, - &transactions_map - )?; - } - 7 => { - txt_export::_1_account_lot_detail_to_txt( - &settings, - &raw_acct_map, - &account_map, - &transactions_map, - &action_records_map - )?; - } - 8 => { - txt_export::_2_account_lot_summary_to_txt( - &settings, - &raw_acct_map, - &account_map, - )?; - } - 9 => { - txt_export::_3_account_lot_summary_non_zero_to_txt( - &settings, - &raw_acct_map, - &account_map, - )?; - } - _ => {} - } - } + tui::app::export( + &app, + &settings, + &action_records_map, + &raw_acct_map, + &account_map, + &transactions_map + )?; + } // use tests::test; diff --git a/src/tui/app.rs b/src/tui/app.rs index 4a1dfde..c40748b 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -1,6 +1,15 @@ // Copyright (c) 2017-2019, scoobybejesus // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt +use std::error::Error; +use std::collections::{HashMap}; + + +use crate::transaction::{Transaction, ActionRecord}; +use crate::account::{Account, RawAccount}; +use crate::core_functions::{ImportProcessParameters}; +use crate::csv_export; +use crate::txt_export; pub (crate) const REPORTS: [&'static str; 9] = [ "1. CSV: Account Sums", @@ -99,4 +108,99 @@ impl<'a> PrintWindow<'a> { } vec.dedup(); } +} + +pub fn export( + app: &PrintWindow, + settings: &ImportProcessParameters, + action_records_map: &HashMap, + raw_acct_map: &HashMap, + account_map: &HashMap, + transactions_map: &HashMap, +) -> Result<(), Box> { + + let reports = REPORTS.to_vec(); + + for report in app.to_print.iter() { + + println!("Exporting: {}", reports[*report]); + + match report + 1 { + + 1 => { + csv_export::_1_account_sums_to_csv( + &settings, + &raw_acct_map, + &account_map + ); + } + 2 => { + csv_export::_2_account_sums_nonzero_to_csv( + &account_map, + &settings, + &raw_acct_map + ); + } + 3 => { + csv_export::_3_account_sums_to_csv_with_orig_basis( + &settings, + &raw_acct_map, + &account_map + ); + } + 4 => { + csv_export::_4_transaction_mvmt_detail_to_csv( + &settings, + &action_records_map, + &raw_acct_map, + &account_map, + &transactions_map + )?; + } + 5 => { + csv_export::_5_transaction_mvmt_summaries_to_csv( + &settings, + &action_records_map, + &raw_acct_map, + &account_map, + &transactions_map + )?; + } + 6 => { + csv_export::_6_transaction_mvmt_detail_to_csv_w_orig( + &settings, + &action_records_map, + &raw_acct_map, + &account_map, + &transactions_map + )?; + } + 7 => { + txt_export::_1_account_lot_detail_to_txt( + &settings, + &raw_acct_map, + &account_map, + &transactions_map, + &action_records_map + )?; + } + 8 => { + txt_export::_2_account_lot_summary_to_txt( + &settings, + &raw_acct_map, + &account_map, + )?; + } + 9 => { + txt_export::_3_account_lot_summary_non_zero_to_txt( + &settings, + &raw_acct_map, + &account_map, + )?; + } + _ => {} + } + } + + Ok(()) } \ No newline at end of file diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 222a16c..3529b0d 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -10,13 +10,10 @@ use ::tui::layout::{Layout, Constraint, Direction}; use ::tui::backend::Backend; use crate::tui::app::PrintWindow; +use crate::tui; -pub fn draw( - terminal: &mut Terminal, - app: &PrintWindow, - reports_len: u16, -) -> Result<(), io::Error> { +pub fn draw(terminal: &mut Terminal, app: &PrintWindow) -> Result<(), io::Error> { terminal.draw(|mut f| { @@ -24,7 +21,7 @@ pub fn draw( .constraints([ Constraint::Length(1), Constraint::Length(8), - Constraint::Length(reports_len + 2), + Constraint::Length(tui::app::REPORTS.len() as u16 + 2), Constraint::Percentage(35) ].as_ref()) .split(f.size());