@@ -45,13 +45,45 @@ fn format_money(cents: u32) -> String {
45
45
format ! ( "${:.2}" , cents as f32 / 100.0 )
46
46
}
47
47
48
- fn print_assets ( assets : & Vec < Asset > ) {
49
- let mut table = Table :: new ( ) ;
50
-
51
- // I prefer a Unicode table with solid lines
48
+ fn apply_table_display_settings ( table : & mut Table ) {
49
+ // this is my preferred style for a table
52
50
table. load_preset ( UTF8_FULL ) ;
53
51
table. set_style ( VerticalLines , '│' ) ;
54
52
table. set_style ( HorizontalLines , '─' ) ;
53
+ }
54
+
55
+ fn print_summary ( assets : & Vec < Asset > ) {
56
+ let mut table = Table :: new ( ) ;
57
+ // TODO: add support for sold assets in a seperate table
58
+ apply_table_display_settings ( & mut table) ;
59
+ table. set_header ( vec ! [
60
+ "Net Buy Price" ,
61
+ "Market Value" ,
62
+ "Unrealized Gains/Losses" ,
63
+ ] ) ;
64
+
65
+ let mut net_buy_price: u32 = 0 ;
66
+ let mut market_value: u32 = 0 ;
67
+ for asset in assets {
68
+ if asset. sell_price_cents . is_some ( ) {
69
+ continue ;
70
+ }
71
+ net_buy_price += asset. buy_price_cents * asset. quantity ;
72
+ market_value += asset. current_price_cents * asset. quantity ;
73
+ }
74
+ let unrealized_gains_losses: u32 = net_buy_price - market_value;
75
+ table. add_row ( vec ! [
76
+ format_money( net_buy_price) ,
77
+ format_money( market_value) ,
78
+ format_money( unrealized_gains_losses) ,
79
+ ] ) ;
80
+ println ! ( "{table}" ) ;
81
+ }
82
+
83
+ fn print_assets ( assets : & Vec < Asset > ) {
84
+ let mut table = Table :: new ( ) ;
85
+
86
+ apply_table_display_settings ( & mut table) ;
55
87
56
88
table. set_header ( vec ! [
57
89
"Ticker" ,
@@ -138,6 +170,7 @@ fn add_asset(connector: &yf::YahooConnector) -> Option<Asset> {
138
170
fn print_help ( ) {
139
171
let help_text = indoc ! { "
140
172
assets - prints all assets, both held and sold
173
+ summary - prints a summary of the loaded portfolio
141
174
new - adds a new asset
142
175
help - prints this help text
143
176
load - loads assets from a file
@@ -196,6 +229,7 @@ fn main() {
196
229
197
230
match input. as_str ( ) {
198
231
"assets" => print_assets ( & active_portfolio. assets ) ,
232
+ "summary" => print_summary ( & active_portfolio. assets ) ,
199
233
"new" => {
200
234
// FIXME: after adding an asset, the prompt is printed twice
201
235
let new_asset: Option < Asset > = add_asset ( & connector) ;
0 commit comments