diff options
Diffstat (limited to 'src/tftpc.rs')
| -rw-r--r-- | src/tftpc.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/tftpc.rs b/src/tftpc.rs index c6b4c0f..689d055 100644 --- a/src/tftpc.rs +++ b/src/tftpc.rs @@ -5,7 +5,7 @@ use std::env; use std::fs::File; -use std::io; +use std::io::{self, Write}; use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; use std::path::{Path, PathBuf}; use std::time::Duration; @@ -32,6 +32,26 @@ struct Tftpc { conf: Configuration, } +fn update_progress(current: u64, total: u64, last: u64) -> u64 { + if total == 0 { + /* unknown; remote does not support tsize */ + return 0; + } + let onepercent = total / 100; + if current < total && current < last + onepercent { + /* not enough progress to warrant an update */ + return last; + } + + let percent = 100 * current / total; + print!("\r {}% ", percent); + io::stdout().flush().expect("flushing stdout failed"); + if current == total { + print!("\r"); + } + current +} + impl Tftpc { pub fn new(conf: Configuration) -> Tftpc { Tftpc { @@ -207,6 +227,7 @@ impl Tftpc { } pub fn start(&mut self) { + self.tftp.set_progress_callback(update_progress); let socket = UdpSocket::bind("[::]:0").expect("binding failed"); socket.set_read_timeout(Some(Duration::from_secs(5))).expect("setting socket timeout failed"); |
