Skip to content

Commit b49cb3b

Browse files
Add feature to generate summary of portfolio
1 parent d58af10 commit b49cb3b

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/main.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,45 @@ fn format_money(cents: u32) -> String {
4545
format!("${:.2}", cents as f32 / 100.0)
4646
}
4747

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
5250
table.load_preset(UTF8_FULL);
5351
table.set_style(VerticalLines, '│');
5452
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);
5587

5688
table.set_header(vec![
5789
"Ticker",
@@ -138,6 +170,7 @@ fn add_asset(connector: &yf::YahooConnector) -> Option<Asset> {
138170
fn print_help() {
139171
let help_text = indoc! {"
140172
assets - prints all assets, both held and sold
173+
summary - prints a summary of the loaded portfolio
141174
new - adds a new asset
142175
help - prints this help text
143176
load - loads assets from a file
@@ -196,6 +229,7 @@ fn main() {
196229

197230
match input.as_str() {
198231
"assets" => print_assets(&active_portfolio.assets),
232+
"summary" => print_summary(&active_portfolio.assets),
199233
"new" => {
200234
// FIXME: after adding an asset, the prompt is printed twice
201235
let new_asset: Option<Asset> = add_asset(&connector);

0 commit comments

Comments
 (0)