diff --git a/Cargo.toml b/Cargo.toml index 367708c..b188d33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,17 @@ [package] name = "cryptools" -version = "0.6.0" +version = "0.7.0" authors = ["scoobybejesus "] edition = "2018" -description = "This is a command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'." +description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'." + +[lib] +name = "crptls" +path = "src/lib.rs" + +[[bin]] +name = "main" +path = "src/main.rs" [dependencies] csv = "1.0.0" @@ -19,5 +27,4 @@ tui = "0.5" termion = "1.5" [profile.release] -lto = true - +lto = true \ No newline at end of file diff --git a/README.md b/README.md index 65e9496..bc727fb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ### Accounting library for cryptocurrency transaction activity. -###### (Currently runs as a binary) +###### (The package produces a binary that makes use of the library) This software calculates income, expenses, realized gains, and realized losses from cryptocurrency activity and denominates the results in the user's home currency. @@ -49,7 +49,7 @@ when appreciated cryptocurrency was used to make a tax-deductible charitable con 2. `cd cryptools` 3. `cargo build` (include `--release` for a non-debug build) -This will build `./target/debug/cryptools` (or `./target/rls/cryptools` for a non-debug build). +This will build `./target/debug/cryptools` (or `./target/release/cryptools` for a non-debug build). ## Usage diff --git a/examples/examples.md b/examples/examples.md index 8b58378..c86fb3b 100644 --- a/examples/examples.md +++ b/examples/examples.md @@ -3,7 +3,9 @@ The sample input files and the resulting reports are in the `/examples/resources/` directory. (Note: new reports have been added since the write-up below was written. -Nevertheless, evaluating the reports should mostly be self-explanatory.) +Nevertheless, evaluating the reports should mostly be self-explanatory. +Pass a -p flag from the command line to see the full list of available +reports - and select from them - once the import has taken place.) ## 1. Using the wizard @@ -39,7 +41,7 @@ Then tab-complete your way through `/Users//Documents`*, for exam ##### Now the program has ended, and you should have reports in the directory you provided. -The reports should match those in the `examples/resources` directory. +The reports should generally match those in the `examples/resources` directory. (Additional reports are created too, but you can generally match the existing reports by title.) @@ -77,6 +79,8 @@ Or maybe you may want to apply like-kind exchange treatment through a particular These parameters can all be set via command line options. See the `--help` screen for all the options. +As mentioned above, pass the -p flag to be presented with a list of available reports. + ## Notes on the reports There are two style of reports: Accounts and Transactions. diff --git a/src/cli_user_choices.rs b/src/cli_user_choices.rs index 32539ca..653050a 100644 --- a/src/cli_user_choices.rs +++ b/src/cli_user_choices.rs @@ -15,8 +15,8 @@ use rustyline::hint::{Hinter}; use rustyline::error::ReadlineError; use rustyline::highlight::{Highlighter}; -use crate::core_functions::InventoryCostingMethod; -use crate::string_utils; +use crptls::costing_method::InventoryCostingMethod; +use crptls::string_utils; pub fn choose_file_for_import(flag_to_accept_cli_args: bool) -> Result> { diff --git a/src/account.rs b/src/crptls_lib/account.rs similarity index 99% rename from src/account.rs rename to src/crptls_lib/account.rs index 03871a3..8c56f96 100644 --- a/src/account.rs +++ b/src/crptls_lib/account.rs @@ -12,7 +12,7 @@ use chrono_tz::US::Eastern; use decimal::d128; use serde_derive::{Serialize, Deserialize}; -use crate::transaction::{Transaction, ActionRecord, Polarity, TxType}; +use crate::crptls_lib::transaction::{Transaction, ActionRecord, Polarity, TxType}; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct RawAccount { diff --git a/src/core_functions.rs b/src/crptls_lib/core_functions.rs similarity index 77% rename from src/core_functions.rs rename to src/crptls_lib/core_functions.rs index a54f2f4..1a67f87 100644 --- a/src/core_functions.rs +++ b/src/crptls_lib/core_functions.rs @@ -5,42 +5,16 @@ use std::path::PathBuf; use std::error::Error; use std::fs::File; use std::collections::{HashMap}; -use std::fmt; use chrono::NaiveDate; -use structopt::StructOpt; -use crate::account::{Account, RawAccount, Lot}; -use crate::transaction::{Transaction, ActionRecord}; -use crate::csv_import_accts_txns; -use crate::import_cost_proceeds_etc; -use crate::create_lots_mvmts; +use crate::crptls_lib::account::{Account, RawAccount, Lot}; +use crate::crptls_lib::transaction::{Transaction, ActionRecord}; +use crate::crptls_lib::csv_import_accts_txns; +use crate::crptls_lib::import_cost_proceeds_etc; +use crate::crptls_lib::create_lots_mvmts; +use crate::crptls_lib::costing_method::InventoryCostingMethod; -/// An `InventoryMethod` determines the order in which a `Lot` is chosen when posting -/// `ActionRecord` amounts as individual `Movement`s. -#[derive(Clone, Debug, PartialEq, StructOpt)] -pub enum InventoryCostingMethod { - /// 1. LIFO according to the order the lot was created. - LIFObyLotCreationDate, - /// 2. LIFO according to the basis date of the lot. - LIFObyLotBasisDate, - /// 3. FIFO according to the order the lot was created. - FIFObyLotCreationDate, - /// 4. FIFO according to the basis date of the lot. - FIFObyLotBasisDate, -} - -impl fmt::Display for InventoryCostingMethod { - - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - InventoryCostingMethod::LIFObyLotCreationDate => write!(f, "LIFO by lot creation date"), - InventoryCostingMethod::LIFObyLotBasisDate => write!(f, "LIFO by lot basis date"), - InventoryCostingMethod::FIFObyLotCreationDate => write!(f, "FIFO by lot creation date"), - InventoryCostingMethod::FIFObyLotBasisDate => write!(f, "FIFO by lot basis date"), - } - } -} /// `ImportProcessParameters` are determined from command-line args and/or wizard input from the user. /// They are the settings that allow the software to carry out the importing-from-csv of @@ -60,7 +34,7 @@ pub struct ImportProcessParameters { pub print_menu: bool, } -pub(crate) fn import_and_process_final( +pub fn import_and_process_final( input_file_path: PathBuf, settings: &ImportProcessParameters, ) -> Result<( diff --git a/src/crptls_lib/costing_method.rs b/src/crptls_lib/costing_method.rs new file mode 100644 index 0000000..cb43ee6 --- /dev/null +++ b/src/crptls_lib/costing_method.rs @@ -0,0 +1,32 @@ +// Copyright (c) 2017-2019, scoobybejesus +// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt + +use std::fmt; + +use structopt::StructOpt; + +/// An `InventoryMethod` determines the order in which a `Lot` is chosen when posting +/// `ActionRecord` amounts as individual `Movement`s. +#[derive(Clone, Debug, PartialEq, StructOpt)] +pub enum InventoryCostingMethod { + /// 1. LIFO according to the order the lot was created. + LIFObyLotCreationDate, + /// 2. LIFO according to the basis date of the lot. + LIFObyLotBasisDate, + /// 3. FIFO according to the order the lot was created. + FIFObyLotCreationDate, + /// 4. FIFO according to the basis date of the lot. + FIFObyLotBasisDate, +} + +impl fmt::Display for InventoryCostingMethod { + + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + InventoryCostingMethod::LIFObyLotCreationDate => write!(f, "LIFO by lot creation date"), + InventoryCostingMethod::LIFObyLotBasisDate => write!(f, "LIFO by lot basis date"), + InventoryCostingMethod::FIFObyLotCreationDate => write!(f, "FIFO by lot creation date"), + InventoryCostingMethod::FIFObyLotBasisDate => write!(f, "FIFO by lot basis date"), + } + } +} \ No newline at end of file diff --git a/src/create_lots_mvmts.rs b/src/crptls_lib/create_lots_mvmts.rs similarity index 99% rename from src/create_lots_mvmts.rs rename to src/crptls_lib/create_lots_mvmts.rs index 42f9b90..7ec9d25 100644 --- a/src/create_lots_mvmts.rs +++ b/src/crptls_lib/create_lots_mvmts.rs @@ -9,10 +9,10 @@ use std::error::Error; use decimal::d128; use chrono::NaiveDate; -use crate::transaction::{Transaction, ActionRecord, TxType, Polarity, TxHasMargin}; -use crate::account::{Account, RawAccount, Lot, Movement}; -use crate::core_functions::{InventoryCostingMethod}; -use crate::decimal_utils::{round_d128_1e8}; +use crate::crptls_lib::transaction::{Transaction, ActionRecord, TxType, Polarity, TxHasMargin}; +use crate::crptls_lib::account::{Account, RawAccount, Lot, Movement}; +use crate::crptls_lib::costing_method::{InventoryCostingMethod}; +use crate::crptls_lib::decimal_utils::{round_d128_1e8}; pub(crate) fn create_lots_and_movements( txns_map: HashMap, diff --git a/src/csv_import_accts_txns.rs b/src/crptls_lib/csv_import_accts_txns.rs similarity index 98% rename from src/csv_import_accts_txns.rs rename to src/crptls_lib/csv_import_accts_txns.rs index d80ee82..bf33c67 100644 --- a/src/csv_import_accts_txns.rs +++ b/src/crptls_lib/csv_import_accts_txns.rs @@ -10,9 +10,9 @@ use std::collections::{HashMap}; use chrono::NaiveDate; use decimal::d128; -use crate::transaction::{Transaction, ActionRecord}; -use crate::account::{Account, RawAccount}; -use crate::decimal_utils::{round_d128_1e8}; +use crate::crptls_lib::transaction::{Transaction, ActionRecord}; +use crate::crptls_lib::account::{Account, RawAccount}; +use crate::crptls_lib::decimal_utils::{round_d128_1e8}; pub(crate) fn import_accounts( diff --git a/src/decimal_utils.rs b/src/crptls_lib/decimal_utils.rs similarity index 100% rename from src/decimal_utils.rs rename to src/crptls_lib/decimal_utils.rs diff --git a/src/import_cost_proceeds_etc.rs b/src/crptls_lib/import_cost_proceeds_etc.rs similarity index 98% rename from src/import_cost_proceeds_etc.rs rename to src/crptls_lib/import_cost_proceeds_etc.rs index 1ff6f14..516cd37 100644 --- a/src/import_cost_proceeds_etc.rs +++ b/src/crptls_lib/import_cost_proceeds_etc.rs @@ -6,10 +6,10 @@ use std::error::Error; use decimal::d128; -use crate::transaction::{Transaction, TxType, ActionRecord, Polarity}; -use crate::account::{Account, RawAccount}; -use crate::decimal_utils::{round_d128_1e2}; -use crate::core_functions::{ImportProcessParameters}; +use crate::crptls_lib::transaction::{Transaction, TxType, ActionRecord, Polarity}; +use crate::crptls_lib::account::{Account, RawAccount}; +use crate::crptls_lib::decimal_utils::{round_d128_1e2}; +use crate::crptls_lib::core_functions::{ImportProcessParameters}; pub(crate) fn add_cost_basis_to_movements( settings: &ImportProcessParameters, diff --git a/src/crptls_lib/mod.rs b/src/crptls_lib/mod.rs new file mode 100644 index 0000000..d0d123b --- /dev/null +++ b/src/crptls_lib/mod.rs @@ -0,0 +1,17 @@ +// Copyright (c) 2017-2019, scoobybejesus +// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt + +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_assignments)] + +pub mod account; +pub mod transaction; +pub mod core_functions; +pub mod string_utils; +pub mod decimal_utils; +pub mod costing_method; + +mod csv_import_accts_txns; +mod create_lots_mvmts; +mod import_cost_proceeds_etc; \ No newline at end of file diff --git a/src/string_utils.rs b/src/crptls_lib/string_utils.rs similarity index 100% rename from src/string_utils.rs rename to src/crptls_lib/string_utils.rs diff --git a/src/transaction.rs b/src/crptls_lib/transaction.rs similarity index 99% rename from src/transaction.rs rename to src/crptls_lib/transaction.rs index 0a672d0..c612a12 100644 --- a/src/transaction.rs +++ b/src/crptls_lib/transaction.rs @@ -12,7 +12,7 @@ use decimal::d128; use chrono::NaiveDate; use serde_derive::{Serialize, Deserialize}; -use crate::account::{Account, Movement, RawAccount}; +use crate::crptls_lib::account::{Account, Movement, RawAccount}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Transaction { diff --git a/src/export_all.rs b/src/export_all.rs index 6754d58..fe003a4 100644 --- a/src/export_all.rs +++ b/src/export_all.rs @@ -4,11 +4,11 @@ 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; +use crptls::transaction::{Transaction, ActionRecord}; +use crptls::account::{Account, RawAccount}; +use crptls::core_functions::{ImportProcessParameters}; +use crate::export_csv; +use crate::export_txt; pub fn export( @@ -21,25 +21,25 @@ pub fn export( println!("Creating all reports now."); - csv_export::_1_account_sums_to_csv( + export_csv::_1_account_sums_to_csv( &settings, &raw_acct_map, &account_map ); - csv_export::_2_account_sums_nonzero_to_csv( + export_csv::_2_account_sums_nonzero_to_csv( &account_map, &settings, &raw_acct_map ); - csv_export::_3_account_sums_to_csv_with_orig_basis( + export_csv::_3_account_sums_to_csv_with_orig_basis( &settings, &raw_acct_map, &account_map ); - csv_export::_4_transaction_mvmt_detail_to_csv( + export_csv::_4_transaction_mvmt_detail_to_csv( &settings, &action_records_map, &raw_acct_map, @@ -47,7 +47,7 @@ pub fn export( &transactions_map )?; - csv_export::_5_transaction_mvmt_summaries_to_csv( + export_csv::_5_transaction_mvmt_summaries_to_csv( &settings, &action_records_map, &raw_acct_map, @@ -55,7 +55,7 @@ pub fn export( &transactions_map )?; - csv_export::_6_transaction_mvmt_detail_to_csv_w_orig( + export_csv::_6_transaction_mvmt_detail_to_csv_w_orig( &settings, &action_records_map, &raw_acct_map, @@ -63,7 +63,7 @@ pub fn export( &transactions_map )?; - txt_export::_1_account_lot_detail_to_txt( + export_txt::_1_account_lot_detail_to_txt( &settings, &raw_acct_map, &account_map, @@ -71,13 +71,13 @@ pub fn export( &action_records_map )?; - txt_export::_2_account_lot_summary_to_txt( + export_txt::_2_account_lot_summary_to_txt( &settings, &raw_acct_map, &account_map, )?; - txt_export::_3_account_lot_summary_non_zero_to_txt( + export_txt::_3_account_lot_summary_non_zero_to_txt( &settings, &raw_acct_map, &account_map, diff --git a/src/csv_export.rs b/src/export_csv.rs similarity index 99% rename from src/csv_export.rs rename to src/export_csv.rs index 1369393..d326985 100644 --- a/src/csv_export.rs +++ b/src/export_csv.rs @@ -8,9 +8,9 @@ use std::error::Error; use decimal::d128; -use crate::transaction::{Transaction, ActionRecord, Polarity, TxType}; -use crate::account::{Account, RawAccount, Term}; -use crate::core_functions::{ImportProcessParameters}; +use crptls::transaction::{Transaction, ActionRecord, Polarity, TxType}; +use crptls::account::{Account, RawAccount, Term}; +use crptls::core_functions::{ImportProcessParameters}; pub fn _1_account_sums_to_csv( diff --git a/src/txt_export.rs b/src/export_txt.rs similarity index 98% rename from src/txt_export.rs rename to src/export_txt.rs index 82a657a..3fe0218 100644 --- a/src/txt_export.rs +++ b/src/export_txt.rs @@ -9,9 +9,9 @@ use std::io::prelude::Write; use decimal::d128; -use crate::transaction::{Transaction, ActionRecord}; -use crate::account::{Account, RawAccount}; -use crate::core_functions::{ImportProcessParameters}; +use crptls::transaction::{Transaction, ActionRecord}; +use crptls::account::{Account, RawAccount}; +use crptls::core_functions::{ImportProcessParameters}; pub fn _1_account_lot_detail_to_txt( diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d990990 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +// Copyright (c) 2017-2019, scoobybejesus +// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt + +pub use self::crptls_lib::account; +pub use self::crptls_lib::transaction; +pub use self::crptls_lib::core_functions; +pub use self::crptls_lib::string_utils; +pub use self::crptls_lib::decimal_utils; +pub use self::crptls_lib::costing_method; + +pub mod crptls_lib; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4bca91e..846eb49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,40 +11,23 @@ use std::ffi::OsString; use std::path::PathBuf; use std::error::Error; -use std::io; -use std::time::Duration; -use ::tui::Terminal; -use ::tui::backend::TermionBackend; -use termion::raw::IntoRawMode; -use termion::screen::AlternateScreen; -use termion::input::MouseTerminal; -use termion::event::Key; use structopt::StructOpt; -mod account; -mod transaction; -mod core_functions; -mod csv_import_accts_txns; -mod create_lots_mvmts; -mod import_cost_proceeds_etc; +mod setup; mod cli_user_choices; -mod csv_export; -mod txt_export; -mod string_utils; -mod decimal_utils; -mod tests; mod wizard; mod skip_wizard; -mod setup; -mod tui; +mod mytui; +mod export_csv; +mod export_txt; mod export_all; - +mod tests; #[derive(StructOpt, Debug)] #[structopt(name = "cryptools")] -pub(crate) struct Cli { +pub struct Cli { #[structopt(flatten)] flags: Flags, @@ -58,7 +41,7 @@ pub(crate) struct Cli { } #[derive(StructOpt, Debug)] -pub(crate) struct Flags { +pub struct Flags { /// User is instructing the program to skip the data entry wizard. /// When set, program will error without required command-line args. @@ -84,7 +67,7 @@ pub(crate) struct Flags { } #[derive(StructOpt, Debug)] -pub(crate) struct Options { +pub struct Options { /// Choose "h", "s", or "p" for hyphen, slash, or period (i.e., "-", "/", or ".") to indicate the separator /// character used in the file_to_import txDate column (i.e. 2017/12/31, 2017-12-31, or 2017.12.31). @@ -117,7 +100,6 @@ pub(crate) struct Options { } - fn main() -> Result<(), Box> { let args = Cli::from_args(); @@ -140,7 +122,7 @@ fn main() -> Result<(), Box> { raw_acct_map, action_records_map, transactions_map, - ) = core_functions::import_and_process_final(input_file_path, &settings)?; + ) = crptls::core_functions::import_and_process_final(input_file_path, &settings)?; let mut should_export_all = settings.should_export; let present_print_menu_tui = settings.print_menu; @@ -160,62 +142,13 @@ fn main() -> Result<(), Box> { if present_print_menu_tui { - use crate::tui::event::{Events, Event, Config}; - - let stdout = io::stdout().into_raw_mode()?; - let stdout = MouseTerminal::from(stdout); - let stdout = AlternateScreen::from(stdout); - let backend = TermionBackend::new(stdout); - let mut terminal = Terminal::new(backend)?; - terminal.hide_cursor()?; - - 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)?; - - match events.next()? { - - Event::Input(key) => match key { - - Key::Char(c) => { - app.on_key(c); - } - Key::Up => { - app.on_up(); - } - Key::Down => { - app.on_down(); - } - _ => {} - }, - _ => {} - } - - if app.should_quit { - break; - } - } - - // Seem to need both of these for the native terminal to be available for println!()'s below - std::mem::drop(terminal); - std::thread::sleep(Duration::from_millis(10)); - - tui::app::export( - &app, + mytui::print_menu_tui::print_menu_tui( &settings, &action_records_map, &raw_acct_map, &account_map, &transactions_map )?; - } // use tests::test; diff --git a/src/tui/app.rs b/src/mytui/app.rs similarity index 86% rename from src/tui/app.rs rename to src/mytui/app.rs index c40748b..6f19fa5 100644 --- a/src/tui/app.rs +++ b/src/mytui/app.rs @@ -4,12 +4,13 @@ use std::error::Error; use std::collections::{HashMap}; +use crptls::transaction::{Transaction, ActionRecord}; +use crptls::account::{Account, RawAccount}; +use crptls::core_functions::{ImportProcessParameters}; + +use crate::export_csv; +use crate::export_txt; -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", @@ -128,28 +129,28 @@ pub fn export( match report + 1 { 1 => { - csv_export::_1_account_sums_to_csv( + export_csv::_1_account_sums_to_csv( &settings, &raw_acct_map, &account_map ); } 2 => { - csv_export::_2_account_sums_nonzero_to_csv( + export_csv::_2_account_sums_nonzero_to_csv( &account_map, &settings, &raw_acct_map ); } 3 => { - csv_export::_3_account_sums_to_csv_with_orig_basis( + export_csv::_3_account_sums_to_csv_with_orig_basis( &settings, &raw_acct_map, &account_map ); } 4 => { - csv_export::_4_transaction_mvmt_detail_to_csv( + export_csv::_4_transaction_mvmt_detail_to_csv( &settings, &action_records_map, &raw_acct_map, @@ -158,7 +159,7 @@ pub fn export( )?; } 5 => { - csv_export::_5_transaction_mvmt_summaries_to_csv( + export_csv::_5_transaction_mvmt_summaries_to_csv( &settings, &action_records_map, &raw_acct_map, @@ -167,7 +168,7 @@ pub fn export( )?; } 6 => { - csv_export::_6_transaction_mvmt_detail_to_csv_w_orig( + export_csv::_6_transaction_mvmt_detail_to_csv_w_orig( &settings, &action_records_map, &raw_acct_map, @@ -176,7 +177,7 @@ pub fn export( )?; } 7 => { - txt_export::_1_account_lot_detail_to_txt( + export_txt::_1_account_lot_detail_to_txt( &settings, &raw_acct_map, &account_map, @@ -185,14 +186,14 @@ pub fn export( )?; } 8 => { - txt_export::_2_account_lot_summary_to_txt( + export_txt::_2_account_lot_summary_to_txt( &settings, &raw_acct_map, &account_map, )?; } 9 => { - txt_export::_3_account_lot_summary_non_zero_to_txt( + export_txt::_3_account_lot_summary_non_zero_to_txt( &settings, &raw_acct_map, &account_map, diff --git a/src/tui/event.rs b/src/mytui/event.rs similarity index 100% rename from src/tui/event.rs rename to src/mytui/event.rs diff --git a/src/tui/mod.rs b/src/mytui/mod.rs similarity index 73% rename from src/tui/mod.rs rename to src/mytui/mod.rs index 8028922..813589e 100644 --- a/src/tui/mod.rs +++ b/src/mytui/mod.rs @@ -1,8 +1,7 @@ // Copyright (c) 2017-2019, scoobybejesus // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt -pub use main; - -pub mod app; -pub mod ui; -pub mod event; \ No newline at end of file +pub mod print_menu_tui; +mod app; +mod ui; +mod event; \ No newline at end of file diff --git a/src/mytui/print_menu_tui.rs b/src/mytui/print_menu_tui.rs new file mode 100644 index 0000000..c0f4786 --- /dev/null +++ b/src/mytui/print_menu_tui.rs @@ -0,0 +1,88 @@ +// Copyright (c) 2017-2019, scoobybejesus +// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt + +use std::io; +use std::time::Duration; +use std::collections::{HashMap}; +use std::error::Error; + +use tui::Terminal; +use tui::backend::TermionBackend; +use termion::raw::IntoRawMode; +use termion::screen::AlternateScreen; +use termion::input::MouseTerminal; +use termion::event::Key; + +use crptls::transaction::{Transaction, ActionRecord}; +use crptls::account::{Account, RawAccount}; +use crptls::core_functions::{ImportProcessParameters}; + +use crate::mytui::event::{Events, Event, Config}; +use crate::mytui::ui as ui; +use crate::mytui::app as app; + + +pub (crate) fn print_menu_tui( + settings: &ImportProcessParameters, + action_records_map: &HashMap, + raw_acct_map: &HashMap, + account_map: &HashMap, + transactions_map: &HashMap, +) -> Result<(), Box> { + + let stdout = io::stdout().into_raw_mode()?; + let stdout = MouseTerminal::from(stdout); + let stdout = AlternateScreen::from(stdout); + let backend = TermionBackend::new(stdout); + let mut terminal = Terminal::new(backend)?; + terminal.hide_cursor()?; + + let mut app = app::PrintWindow::new("Reports"); + + let events = Events::with_config(Config { + tick_rate: Duration::from_millis(250u64), + ..Config::default() + }); + + loop { + + ui::draw(&mut terminal, &app)?; + + match events.next()? { + + Event::Input(key) => match key { + + Key::Char(c) => { + app.on_key(c); + } + Key::Up => { + app.on_up(); + } + Key::Down => { + app.on_down(); + } + _ => {} + }, + _ => {} + } + + if app.should_quit { + break; + } + } + + // Seem to need both of these for the native terminal to be available for println!()'s below + std::mem::drop(terminal); + std::thread::sleep(Duration::from_millis(10)); + + app::export( + &app, + &settings, + &action_records_map, + &raw_acct_map, + &account_map, + &transactions_map + )?; + + Ok(()) +} \ No newline at end of file diff --git a/src/tui/ui.rs b/src/mytui/ui.rs similarity index 94% rename from src/tui/ui.rs rename to src/mytui/ui.rs index 3529b0d..c956437 100644 --- a/src/tui/ui.rs +++ b/src/mytui/ui.rs @@ -9,8 +9,7 @@ use ::tui::widgets::{Widget, Block, Borders, SelectableList, Text, Paragraph}; use ::tui::layout::{Layout, Constraint, Direction}; use ::tui::backend::Backend; -use crate::tui::app::PrintWindow; -use crate::tui; +use crate::mytui::app::{PrintWindow, REPORTS}; pub fn draw(terminal: &mut Terminal, app: &PrintWindow) -> Result<(), io::Error> { @@ -21,7 +20,7 @@ pub fn draw(terminal: &mut Terminal, app: &PrintWindow) -> Result .constraints([ Constraint::Length(1), Constraint::Length(8), - Constraint::Length(tui::app::REPORTS.len() as u16 + 2), + Constraint::Length(REPORTS.len() as u16 + 2), Constraint::Percentage(35) ].as_ref()) .split(f.size()); diff --git a/src/setup.rs b/src/setup.rs index c6291d1..778a9d0 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -8,8 +8,10 @@ use std::process; use chrono::NaiveDate; +use crptls::core_functions::ImportProcessParameters; +use crptls::costing_method::InventoryCostingMethod; + use crate::cli_user_choices; -use crate::core_functions::{ImportProcessParameters, InventoryCostingMethod}; use crate::skip_wizard; use crate::wizard; @@ -77,52 +79,52 @@ pub (crate) fn run_setup(args: super::Cli) -> Result<(PathBuf, ImportProcessPara } fn wizard_or_not(accept_args: bool, args: ArgsForImportVarsTBD) -> Result<( - InventoryCostingMethod, - bool, - String, - bool, - PathBuf, - ), Box> { + InventoryCostingMethod, + bool, + String, + bool, + PathBuf, +), Box> { - let costing_method_choice; - let like_kind_election; - let like_kind_cutoff_date_string; - let should_export; - let output_dir_path; + let costing_method_choice; + let like_kind_election; + let like_kind_cutoff_date_string; + let should_export; + let output_dir_path; - if !accept_args { + if !accept_args { - let ( - costing_method_choice1, - like_kind_election1, - like_kind_cutoff_date_string1, - should_export1, - output_dir_path1, - ) = wizard::wizard(args)?; + let ( + costing_method_choice1, + like_kind_election1, + like_kind_cutoff_date_string1, + should_export1, + output_dir_path1, + ) = wizard::wizard(args)?; - costing_method_choice = costing_method_choice1; - like_kind_election = like_kind_election1; - like_kind_cutoff_date_string = like_kind_cutoff_date_string1; - should_export = should_export1; - output_dir_path = output_dir_path1; + costing_method_choice = costing_method_choice1; + like_kind_election = like_kind_election1; + like_kind_cutoff_date_string = like_kind_cutoff_date_string1; + should_export = should_export1; + output_dir_path = output_dir_path1; - } else { + } else { - let ( - costing_method_choice1, - like_kind_election1, - like_kind_cutoff_date_string1, - should_export1, - output_dir_path1, - ) = skip_wizard::skip_wizard(args)?; + let ( + costing_method_choice1, + like_kind_election1, + like_kind_cutoff_date_string1, + should_export1, + output_dir_path1, + ) = skip_wizard::skip_wizard(args)?; - costing_method_choice = costing_method_choice1; - like_kind_election = like_kind_election1; - like_kind_cutoff_date_string = like_kind_cutoff_date_string1; - should_export = should_export1; - output_dir_path = output_dir_path1; + costing_method_choice = costing_method_choice1; + like_kind_election = like_kind_election1; + like_kind_cutoff_date_string = like_kind_cutoff_date_string1; + should_export = should_export1; + output_dir_path = output_dir_path1; - } + } - Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, output_dir_path)) - } \ No newline at end of file + Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, output_dir_path)) +} \ No newline at end of file diff --git a/src/skip_wizard.rs b/src/skip_wizard.rs index 0dc9c7b..7eeb08f 100644 --- a/src/skip_wizard.rs +++ b/src/skip_wizard.rs @@ -4,10 +4,12 @@ use std::path::PathBuf; use std::error::Error; +use crptls::costing_method::InventoryCostingMethod; + use crate::cli_user_choices; -use crate::core_functions::{InventoryCostingMethod}; use crate::setup::{ArgsForImportVarsTBD}; + pub(crate) fn skip_wizard(args: ArgsForImportVarsTBD) -> Result<( InventoryCostingMethod, bool, diff --git a/src/tests/test.rs b/src/tests/test.rs index d466cc8..bb1819b 100644 --- a/src/tests/test.rs +++ b/src/tests/test.rs @@ -6,9 +6,9 @@ use std::collections::{HashMap}; use decimal::d128; -use crate::account::{Account}; -use crate::transaction::{Transaction, ActionRecord}; -use crate::decimal_utils::*; +use crptls::account::{Account}; +use crptls::transaction::{Transaction, ActionRecord}; +use crptls::decimal_utils::*; pub fn run_tests( transactions_map: &HashMap, diff --git a/src/wizard.rs b/src/wizard.rs index e422c41..d65c258 100644 --- a/src/wizard.rs +++ b/src/wizard.rs @@ -6,10 +6,12 @@ use std::process; use std::io::{self, BufRead}; use std::path::PathBuf; +use crptls::costing_method::InventoryCostingMethod; + use crate::cli_user_choices; -use crate::core_functions::{InventoryCostingMethod}; use crate::setup::{ArgsForImportVarsTBD}; + pub(crate) fn wizard(args: ArgsForImportVarsTBD) -> Result<( InventoryCostingMethod, bool,