Skip to content

Commit 231211f

Browse files
Enhance features - add user input support
1 parent 3fd1516 commit 231211f

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
name = "portfolio-tracker"
33
version = "0.1.0"
44
edition = "2021"
5+
description = "A terminal-based portfolio tracker"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

89
[dependencies]
910
comfy-table = "6.1.1"
11+
text_io = "0.1.12"
12+
indoc = "1.0"
13+
14+
[profile.release]
15+
# make a small binary - I care much more about small size than speed here
16+
opt-level = "z"
17+
lto = true
18+
strip = true
19+
codegen-units = 1

src/main.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use comfy_table::presets::UTF8_FULL;
22
use comfy_table::Table;
33
use comfy_table::TableComponent::*;
4+
use indoc::indoc;
5+
use text_io::read;
46

57
struct Asset {
68
ticker: String,
@@ -32,7 +34,7 @@ fn format_money(cents: u32) -> String {
3234
format!("${:.2}", cents as f32 / 100.0)
3335
}
3436

35-
fn print_assets(assets: &[Asset]) {
37+
fn print_assets(assets: &Vec<Asset>) {
3638
let mut table = Table::new();
3739

3840
// I prefer a Unicode table with solid lines
@@ -80,13 +82,48 @@ fn print_assets(assets: &[Asset]) {
8082
},
8183
]);
8284
}
83-
84-
println!("Assets");
8585
println!("{table}");
8686
}
8787

88+
fn get_current_ticker_price(_ticker: String) -> u32 {
89+
// TODO: use Yahoo Finance for this
90+
200
91+
}
92+
93+
fn add_asset() -> Asset {
94+
print!("Enter ticker: ");
95+
let symbol: String = read!();
96+
97+
print!("Enter buy price in total cents: ");
98+
let buy_price: u32 = read!();
99+
100+
print!("Enter sell price if sold, otherwise enter 'held': ");
101+
let sell_price_raw: String = read!();
102+
103+
// if I access a string twice I have to make it owned for some reason - IDK
104+
// what that means or if there is a better way
105+
Asset {
106+
ticker: symbol.to_owned(),
107+
buy_price_cents: buy_price,
108+
current_price_cents: get_current_ticker_price(symbol),
109+
sell_price_cents: (if sell_price_raw.eq("held") {
110+
None
111+
} else {
112+
Some(sell_price_raw.parse().unwrap())
113+
}),
114+
}
115+
}
116+
117+
fn print_help() {
118+
let help_text = indoc! {"
119+
assets - prints all assets, both held and sold
120+
new - adds a new asset
121+
help - prints this help text"};
122+
println!("{}", help_text);
123+
}
124+
88125
fn main() {
89-
let assets: [Asset; 2] = [
126+
let mut assets: Vec<Asset> = vec![
90127
Asset {
91128
ticker: "MSFT".to_string(),
92129
buy_price_cents: 1000,
@@ -100,7 +137,17 @@ fn main() {
100137
sell_price_cents: Some(40000),
101138
},
102139
];
103-
104-
print_assets(&assets);
105-
println!();
140+
let mut input: String;
141+
loop {
142+
print!("» ");
143+
input = read!();
144+
match input.as_str() {
145+
// TODO: handle blank input properly somehow
146+
"assets" => print_assets(&assets),
147+
"exit" => break,
148+
"new" => assets.push(add_asset()),
149+
"help" => print_help(),
150+
_ => println!("Unknown command. Enter 'help' for a list of valid commands"),
151+
}
152+
}
106153
}

0 commit comments

Comments
 (0)