@@ -38,6 +38,9 @@ struct AppSendSetting {
3838struct AppRecvSetting {
3939 timeout : u64 ,
4040}
41+
42+ const VERSION : & ' static str = env ! ( "CARGO_PKG_VERSION" ) ;
43+
4144fn parse_args ( a : Args ) -> Result < AppSetting , String > {
4245 let mut state = ArgState :: Default ;
4346 let mut send_timeout = 2000 ;
@@ -131,6 +134,7 @@ fn parse_args(a: Args) -> Result<AppSetting, String> {
131134 }
132135}
133136fn main ( ) -> Result < ( ) , String > {
137+ println ! ( "ftoc ({})" , VERSION ) ;
134138 parse_args ( std:: env:: args ( ) ) . and_then ( |x| {
135139 if x. dry_run {
136140 dbg ! ( x) ;
@@ -159,6 +163,8 @@ fn recv_file(s: &AppRecvSetting) -> Result<(), io::Error> {
159163 let mut last_index = 0 ;
160164 let mut has_started = false ;
161165 let mut time_wait_ms = 0 ;
166+ let mut total_len = 0u64 ;
167+ let mut recved_len = 0u64 ;
162168 println ! ( "waiting for file" ) ;
163169 loop {
164170 match state {
@@ -175,6 +181,7 @@ fn recv_file(s: &AppRecvSetting) -> Result<(), io::Error> {
175181 println ! ( "start recv file: {}" , x[ 1 ] ) ;
176182 writer = Some ( BufWriter :: new ( f) ) ;
177183 state = RecvState :: Start ;
184+ total_len = x[ 2 ] . parse ( ) . expect ( "can't read total length of file" ) ;
178185 }
179186 Err ( e) => {
180187 dbg ! ( e) ;
@@ -193,20 +200,22 @@ fn recv_file(s: &AppRecvSetting) -> Result<(), io::Error> {
193200 if let Ok ( x) = get_clipboard_string ( ) {
194201 if x. starts_with ( "ftoc-end" ) {
195202 state = RecvState :: End ;
203+ } else if x. starts_with ( "ftoc-start" ) {
204+ sleep_ms ( 100 ) ;
205+ continue ;
196206 } else if x. starts_with ( "ftoc" ) {
197207 let x: Vec < & str > = x. split ( ":" ) . collect ( ) ;
198- if x. len ( ) < 3 {
199- sleep_ms ( 100 ) ;
200- continue ;
201- }
202208 let idx: i32 = x[ 1 ] . parse ( ) . expect ( "invalid index" ) ;
203209
204210 if last_index == idx - 1 {
205211 if let Ok ( v) = decode_config ( x[ 2 ] , base64:: URL_SAFE_NO_PAD ) {
206212 if let Some ( x) = & mut writer {
207213 time_wait_ms = 0 ;
208214 last_index = idx;
209- println ! ( "recv block {}" , idx) ;
215+ recved_len += v. len ( ) as u64 ;
216+
217+ let percentage: f32 = ( recved_len as f32 ) / ( total_len as f32 ) ;
218+ println ! ( "recv block {} ({:.2}%)" , idx, percentage * 100f32 ) ;
210219 let _ = x. write ( v. as_ref ( ) ) ;
211220 } else {
212221 println ! ( "warn: block {} write failed" , idx)
@@ -251,16 +260,19 @@ fn send_file(s: &AppSendSetting) -> Result<(), io::Error> {
251260 if s. skip != 0 {
252261 println ! ( "(resume mode)" ) ;
253262 }
254- p. file_name ( )
255- . and_then ( |x| x. to_str ( ) )
256- . and_then ( |x| {
257- println ! ( "sending file : {}" , x) ;
258- Some ( format ! ( "ftoc-start:{}" , x) )
259- } )
260- . and_then ( |x| {
261- let _ = set_clipboard_string ( x. as_str ( ) ) ;
262- Some ( ( ) )
263- } ) ;
263+ let filename = p
264+ . file_name ( )
265+ . expect ( "can't read file name" )
266+ . to_str ( )
267+ . expect ( "can't convert file name" ) ;
268+ reader. seek ( io:: SeekFrom :: End ( 0 ) ) ?;
269+ let len = reader. stream_position ( ) ?;
270+ reader. seek ( io:: SeekFrom :: Start ( 0 ) ) ?;
271+ let x = format ! ( "ftoc-start:{}:{}" , filename, len) ;
272+ println ! ( "sending file : {} with {} bytes long" , filename, len) ;
273+
274+ let _ = set_clipboard_string ( x. as_str ( ) ) ;
275+
264276 sleep_ms ( 2000 ) ;
265277
266278 let mut v = vec ! [ 0u8 ; s. size] ;
0 commit comments