added proper progress bar and final message

This commit is contained in:
PandaDEV 2024-10-20 22:12:25 +10:00
parent 872d2ff4e4
commit ac8adcde2f
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
3 changed files with 190 additions and 25 deletions

130
Cargo.lock generated
View file

@ -90,7 +90,7 @@ dependencies = [
"miniz_oxide",
"object",
"rustc-demangle",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -618,6 +618,27 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kdam"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "526586ea01a9a132b5f8d3a60f6d6b41b411550236f5ee057795f20b37316957"
dependencies = [
"kdam_derive",
"terminal_size",
"windows-sys 0.52.0",
]
[[package]]
name = "kdam_derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5e25f9b861a88faa9d272ca4376e1a13c9a37d36de623f013c7bbb0ae2baa1"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "libc"
version = "0.2.161"
@ -775,7 +796,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -1191,9 +1212,9 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "streamshare"
version = "2.0.0"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "723b8059b8580d19ea0bdd3202fc8ba38c01719ee5ad2d5b70e530e8f3c5e22e"
checksum = "d8a66009b98588531c068a6b1fc2345a05292dd279c73554d514fd47f926b2e7"
dependencies = [
"futures",
"reqwest",
@ -1269,6 +1290,16 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "terminal_size"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "thiserror"
version = "1.0.64"
@ -1309,6 +1340,7 @@ name = "to-streamshare"
version = "0.3.1"
dependencies = [
"clap",
"kdam",
"streamshare",
"tokio",
]
@ -1620,7 +1652,7 @@ checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0"
dependencies = [
"windows-result",
"windows-strings",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -1629,7 +1661,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
dependencies = [
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -1639,7 +1671,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [
"windows-result",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
@ -1648,7 +1689,7 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@ -1657,7 +1698,22 @@ version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
@ -1666,28 +1722,46 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
@ -1700,24 +1774,48 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"

View file

@ -12,7 +12,8 @@ keywords = ["streamshare","file-sharing","upload"]
[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
streamshare = "2.0.0"
kdam = { version = "0.5.2", features = ["rich", "spinner"] }
streamshare = "3.0.0"
tokio = { version = "1.40.0", features = ["full"] }
[[bin]]

View file

@ -1,5 +1,11 @@
use clap::{CommandFactory, Parser};
use kdam::{tqdm, BarExt, Column, RichProgress, Spinner, term::Colorizer};
use streamshare::{delete, upload};
use std::io::{stderr, IsTerminal};
use std::fs;
use std::time::Duration;
use std::sync::{Arc, Mutex};
use std::thread;
#[derive(Parser, Debug)]
#[command(name = "toss", version, about, long_about = None)]
@ -16,7 +22,7 @@ struct Args {
}
#[tokio::main]
async fn main() {
async fn main() -> std::io::Result<()> {
let args = Args::parse();
if let Some(delete_param) = args.delete {
@ -29,23 +35,83 @@ async fn main() {
eprintln!("Invalid format for --delete. Use 'file_identifier/deletion_token' (e.g., 'abc123/def456')");
}
} else if let Some(file_path) = args.file {
match upload(&file_path, true).await {
kdam::term::init(stderr().is_terminal());
kdam::term::hide_cursor()?;
let file_size = fs::metadata(&file_path)?.len();
let pb = RichProgress::new(
tqdm!(
total = file_size as usize,
unit_scale = true,
unit_divisor = 1024,
unit = "B",
mininterval = 0.01,
dynamic_ncols = true,
colour = "green"
),
vec![
Column::Spinner(Spinner::new(&["", "", "", "", "", "", "", "", "", ""], 80.0, 1.0)),
Column::Percentage(1),
Column::Text("".to_owned()),
Column::CountTotal,
Column::Text("".to_owned()),
Column::Animation,
Column::Text("".to_owned()),
Column::Rate,
Column::Text("".to_owned()),
Column::RemainingTime,
],
);
let pb_arc = Arc::new(Mutex::new(pb));
let current_progress = Arc::new(Mutex::new(0));
let pb_arc_clone = pb_arc.clone();
let current_progress_clone = current_progress.clone();
let update_thread = thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(50));
let progress = *current_progress_clone.lock().unwrap();
if progress >= file_size {
break;
}
pb_arc_clone.lock().unwrap().update_to(progress as usize).unwrap();
}
});
match upload(&file_path, move |current, _total| {
*current_progress.lock().unwrap() = current;
}).await {
Ok((file_identifier, deletion_token)) => {
let mut pb = pb_arc.lock().unwrap();
pb.update_to(file_size as usize).unwrap();
println!("\n{}", "".to_owned() + &"".repeat(79) + "");
println!("{:^90}", "Upload Complete!".colorize("bold green"));
println!("{}", "".repeat(79));
let download_url = format!(
"https://streamshare.wireway.ch/download/{}",
file_identifier
);
println!("{:<15} {:<31}", "URL:".colorize("bold yellow"), download_url);
println!("{:<15} {:<68}", "File ID:".colorize("bold yellow"), file_identifier);
println!("{:<15} {:<61}", "Deletion Token:".colorize("bold yellow"), deletion_token);
println!("File uploaded successfully");
println!("Download URL: {}", download_url);
println!("File Identifier: {}", file_identifier);
println!("Deletion Token: {}", deletion_token);
println!("{}", "".to_owned() + &"".repeat(79) + "");
}
Err(e) => eprintln!("Error: {}", e),
Err(e) => eprintln!("{}", format!("Error: {}", e).colorize("bold red")),
}
update_thread.join().unwrap();
kdam::term::show_cursor()?;
} else {
Args::command().print_help().unwrap();
}
Ok(())
}
fn parse_delete_param(param: &str) -> Option<(&str, &str)> {