Print CLI prompt at top of loop instead of before each `continue`
authorMatt Corallo <git@bluematt.me>
Sun, 7 Nov 2021 18:08:52 +0000 (18:08 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 7 Nov 2021 20:46:59 +0000 (20:46 +0000)
src/cli.rs

index 54b2eab7c5f46fda4ba7f60c39ee85a09c3fc6fb..197c0ecf607a763f88873099d44da96982bd188f 100644 (file)
@@ -151,10 +151,14 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
        println!("LDK logs are available at <your-supplied-ldk-data-dir-path>/.ldk/logs");
        println!("Local Node ID is {}.", channel_manager.get_our_node_id());
        let stdin = io::stdin();
-       print!("> ");
-       io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
-       for line in stdin.lock().lines() {
-               let line = line.unwrap();
+       let mut line_reader = stdin.lock().lines();
+       loop {
+               print!("> ");
+               io::stdout().flush().unwrap(); // Without flushing, the `>` doesn't print
+               let line = match line_reader.next() {
+                       Some(l) => l.unwrap(),
+                       None => break,
+               };
                let mut words = line.split_whitespace();
                if let Some(word) = words.next() {
                        match word {
@@ -164,8 +168,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let channel_value_sat = words.next();
                                        if peer_pubkey_and_ip_addr.is_none() || channel_value_sat.is_none() {
                                                println!("ERROR: openchannel has 2 required arguments: `openchannel pubkey@host:port channel_amt_satoshis` [--public]");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
@@ -174,8 +176,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                        Ok(info) => info,
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
-                                                               print!("> ");
-                                                               io::stdout().flush().unwrap();
                                                                continue;
                                                        }
                                                };
@@ -183,8 +183,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
                                        if chan_amt_sat.is_err() {
                                                println!("ERROR: channel amount must be a number");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
 
@@ -192,8 +190,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                .await
                                                .is_err()
                                        {
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        };
 
@@ -202,8 +198,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                Some("--public=false") => false,
                                                Some(_) => {
                                                        println!("ERROR: invalid `--public` command format. Valid formats: `--public`, `--public=true` `--public=false`");
-                                                       print!("> ");
-                                                       io::stdout().flush().unwrap();
                                                        continue;
                                                }
                                                None => false,
@@ -228,8 +222,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let invoice_str = words.next();
                                        if invoice_str.is_none() {
                                                println!("ERROR: sendpayment requires an invoice: `sendpayment <invoice>`");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
 
@@ -237,8 +229,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                Ok(inv) => inv,
                                                Err(e) => {
                                                        println!("ERROR: invalid invoice: {:?}", e);
-                                                       print!("> ");
-                                                       io::stdout().flush().unwrap();
                                                        continue;
                                                }
                                        };
@@ -251,15 +241,11 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                        Some(pk) => pk,
                                                        None => {
                                                                println!("ERROR: couldn't parse destination pubkey");
-                                                               print!("> ");
-                                                               io::stdout().flush().unwrap();
                                                                continue;
                                                        }
                                                },
                                                None => {
                                                        println!("ERROR: keysend requires a destination pubkey: `keysend <dest_pubkey> <amt_msat>`");
-                                                       print!("> ");
-                                                       io::stdout().flush().unwrap();
                                                        continue;
                                                }
                                        };
@@ -268,8 +254,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                None => {
                                                        println!("ERROR: keysend requires an amount in millisatoshis: `keysend <dest_pubkey> <amt_msat>`");
 
-                                                       print!("> ");
-                                                       io::stdout().flush().unwrap();
                                                        continue;
                                                }
                                        };
@@ -277,8 +261,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                Ok(amt) => amt,
                                                Err(e) => {
                                                        println!("ERROR: couldn't parse amount_msat: {}", e);
-                                                       print!("> ");
-                                                       io::stdout().flush().unwrap();
                                                        continue;
                                                }
                                        };
@@ -296,16 +278,12 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let amt_str = words.next();
                                        if amt_str.is_none() {
                                                println!("ERROR: getinvoice requires an amount in millisatoshis");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
 
                                        let amt_msat: Result<u64, _> = amt_str.unwrap().parse();
                                        if amt_msat.is_err() {
                                                println!("ERROR: getinvoice provided payment amount was not a number");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        get_invoice(
@@ -320,8 +298,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let peer_pubkey_and_ip_addr = words.next();
                                        if peer_pubkey_and_ip_addr.is_none() {
                                                println!("ERROR: connectpeer requires peer connection info: `connectpeer pubkey@host:port`");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let (pubkey, peer_addr) =
@@ -329,8 +305,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                        Ok(info) => info,
                                                        Err(e) => {
                                                                println!("{:?}", e.into_inner().unwrap());
-                                                               print!("> ");
-                                                               io::stdout().flush().unwrap();
                                                                continue;
                                                        }
                                                };
@@ -349,15 +323,11 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let channel_id_str = words.next();
                                        if channel_id_str.is_none() {
                                                println!("ERROR: closechannel requires a channel ID: `closechannel <channel_id>`");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
                                        if channel_id_vec.is_none() {
                                                println!("ERROR: couldn't parse channel_id as hex");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let mut channel_id = [0; 32];
@@ -368,15 +338,11 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        let channel_id_str = words.next();
                                        if channel_id_str.is_none() {
                                                println!("ERROR: forceclosechannel requires a channel ID: `forceclosechannel <channel_id>`");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let channel_id_vec = hex_utils::to_vec(channel_id_str.unwrap());
                                        if channel_id_vec.is_none() {
                                                println!("ERROR: couldn't parse channel_id as hex");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        let mut channel_id = [0; 32];
@@ -389,8 +355,6 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                        const MSG_STARTPOS: usize = "signmsg".len() + 1;
                                        if line.as_bytes().len() <= MSG_STARTPOS {
                                                println!("ERROR: signmsg requires a message");
-                                               print!("> ");
-                                               io::stdout().flush().unwrap();
                                                continue;
                                        }
                                        println!(
@@ -400,14 +364,10 @@ pub(crate) async fn poll_for_user_input<E: EventHandler>(
                                                        &keys_manager.get_node_secret()
                                                )
                                        );
-                                       print!("> ");
-                                       io::stdout().flush().unwrap();
                                }
                                _ => println!("Unknown command. See `\"help\" for available commands."),
                        }
                }
-               print!("> ");
-               io::stdout().flush().unwrap();
        }
 }