Skip to content

Commit bdf7691

Browse files
Properly handle blank input by reading until newline
1 parent 48b9a06 commit bdf7691

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ fn print_help() {
134134

135135
fn prompt(text: &str) -> String {
136136
print!("{}", text);
137-
let data: String = read!();
137+
let mut data: String = read!("{}\n");
138+
139+
// on Windows systems, if we read to \n, a \r character can be appended
140+
// so we remove it - AFAIK this is a Windows-only isse
141+
if data.ends_with('\r') {
142+
data.pop();
143+
}
138144
data
139145
}
140146

@@ -171,19 +177,19 @@ fn main() {
171177
let mut active_portfolio: Portfolio = Portfolio { assets: vec![] };
172178
let mut input: String;
173179
loop {
174-
print!("» ");
175-
input = read!();
180+
input = prompt("» ");
181+
176182
match input.as_str() {
177-
// TODO: handle blank input properly somehow
178183
"assets" => print_assets(&active_portfolio.assets),
179-
"exit" => break,
180184
"new" => active_portfolio.assets.push(add_asset()),
181185
"help" => print_help(),
182186
"load" => match load_portfolio() {
183187
None => println!("An error occurred when loading portfolio. Portfolio not loaded."),
184188
Some(x) => active_portfolio = x,
185189
},
186190
"dump" => dump_portfolio(&active_portfolio),
191+
"exit" => break,
192+
"" => continue,
187193
_ => println!("Unknown command. Enter 'help' for a list of valid commands"),
188194
}
189195
}

0 commit comments

Comments
 (0)