Skip to content

Commit 6af5bc5

Browse files
committed
Merge branch 'feat-progress'
2 parents 5a25589 + e638886 commit 6af5bc5

4 files changed

Lines changed: 30 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
/.vscode

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ftoc"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Ryan <smbserv@qq.com>"]
55
edition = "2018"
66

src/main.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ struct AppSendSetting {
3838
struct AppRecvSetting {
3939
timeout: u64,
4040
}
41+
42+
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
43+
4144
fn 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
}
133136
fn 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

Comments
 (0)