mirror of
https://github.com/Waradu/to-streamshare.git
synced 2025-04-22 04:14:06 +02:00
Implementation of ProgressBar (#1)
Co-authored-by: Waradu <waradu@outlook.com>
This commit is contained in:
parent
c7e859badf
commit
7a101fde14
4 changed files with 202 additions and 68 deletions
126
Cargo.lock
generated
126
Cargo.lock
generated
|
@ -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]]
|
||||
|
@ -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.4.0"
|
||||
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"
|
||||
|
|
|
@ -12,6 +12,7 @@ keywords = ["streamshare", "file-sharing", "upload"]
|
|||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.20", features = ["derive"] }
|
||||
kdam = { version = "0.5.2", features = ["rich", "spinner"] }
|
||||
streamshare = "3"
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
|
||||
|
|
18
README.md
18
README.md
|
@ -2,6 +2,20 @@
|
|||
|
||||
Upload files to [streamshare](https://streamshare.wireway.ch) with the terminal.
|
||||
|
||||
Run `cargo install to-streamshare` to install it and use it with `toss "filepath"`.
|
||||
### Install
|
||||
|
||||
Delete a file with `toss --delete file_identifier/deletion_token`
|
||||
```bash
|
||||
cargo install to-streamshare
|
||||
```
|
||||
|
||||
### Upload
|
||||
|
||||
```bash
|
||||
toss "filepath"
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
||||
```bash
|
||||
toss --delete file_identifier/deletion_token
|
||||
```
|
||||
|
|
119
src/main.rs
119
src/main.rs
|
@ -1,7 +1,11 @@
|
|||
use std::{io::Write, time::Instant};
|
||||
|
||||
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)]
|
||||
|
@ -18,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 {
|
||||
|
@ -31,50 +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 {
|
||||
let start_time = Instant::now();
|
||||
let mut file_size: u64 = 0;
|
||||
kdam::term::init(stderr().is_terminal());
|
||||
kdam::term::hide_cursor()?;
|
||||
|
||||
let show_progress = |uploaded_bytes, total_bytes| {
|
||||
let percentage = (uploaded_bytes as f64 / total_bytes as f64) * 100.0;
|
||||
let uploaded = readable(uploaded_bytes);
|
||||
let total = readable(total_bytes);
|
||||
let elapsed_secs = start_time.elapsed().as_secs_f64();
|
||||
let speed = readable((uploaded_bytes as f64 / elapsed_secs) as u64);
|
||||
file_size = total_bytes;
|
||||
let file_size = fs::metadata(&file_path)?.len();
|
||||
|
||||
print!(
|
||||
"\r\x1b[2K{:.2}% {}/{} ({}/s)",
|
||||
percentage, uploaded, total, speed
|
||||
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::Animation,
|
||||
Column::Text("•".to_owned()),
|
||||
Column::CountTotal,
|
||||
Column::Text("•".to_owned()),
|
||||
Column::Rate,
|
||||
Column::Text("•".to_owned()),
|
||||
Column::RemainingTime,
|
||||
],
|
||||
);
|
||||
std::io::stdout().flush().unwrap();
|
||||
};
|
||||
|
||||
match upload(&file_path, show_progress).await {
|
||||
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);
|
||||
|
||||
let elapsed_secs = start_time.elapsed().as_secs_f64();
|
||||
println!(
|
||||
"\r\x1b[2K100.00% {}/{} (Upload completed in {:.2}s)",
|
||||
readable(file_size),
|
||||
readable(file_size),
|
||||
elapsed_secs
|
||||
);
|
||||
println!();
|
||||
println!("{}", "└".to_owned() + &"─".repeat(79) + "┘");
|
||||
}
|
||||
Err(e) => eprintln!("{}", format!("Error: {}", e).colorize("bold red")),
|
||||
}
|
||||
|
||||
println!("File uploaded successfully");
|
||||
println!("Download URL: {}", download_url);
|
||||
println!("File Identifier: {}", file_identifier);
|
||||
println!("Deletion Token: {}", deletion_token);
|
||||
}
|
||||
Err(e) => eprintln!("Error: {}", e),
|
||||
}
|
||||
update_thread.join().unwrap();
|
||||
kdam::term::show_cursor()?;
|
||||
} else {
|
||||
Args::command().print_help().unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_delete_param(param: &str) -> Option<(&str, &str)> {
|
||||
|
@ -85,19 +122,3 @@ fn parse_delete_param(param: &str) -> Option<(&str, &str)> {
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn readable(bytes: u64) -> String {
|
||||
const KB: f64 = 1024.0;
|
||||
const MB: f64 = KB * 1024.0;
|
||||
const GB: f64 = MB * 1024.0;
|
||||
|
||||
if bytes as f64 >= GB {
|
||||
format!("{:.2}gb", bytes as f64 / GB)
|
||||
} else if bytes as f64 >= MB {
|
||||
format!("{:.2}mb", bytes as f64 / MB)
|
||||
} else if bytes as f64 >= KB {
|
||||
format!("{:.2}kb", bytes as f64 / KB)
|
||||
} else {
|
||||
format!("{}b", bytes)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue