Skip to content

Commit e9cb967

Browse files
committed
added display for raw data to downloads command
1 parent ecc8f17 commit e9cb967

4 files changed

Lines changed: 71 additions & 84 deletions

File tree

info.json

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/commands/downloads.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{pretty_dates, request, write_json_to_file};
1+
use crate::utils::{bytes_to_best_size, pretty_dates, request, write_json_to_file};
22
use serde::{Deserialize, Deserializer, Serialize};
33
use serde_json::Value;
44
use std::{path::PathBuf, vec};
@@ -11,6 +11,7 @@ pub struct DownloadData {
1111
pub html_url: String,
1212
pub body: String,
1313
pub assets: Vec<Asset>,
14+
pub downloads: i64,
1415
}
1516

1617
impl DownloadData {
@@ -30,7 +31,24 @@ impl DownloadData {
3031
html_url,
3132
body,
3233
assets: Vec::new(),
34+
downloads: 0,
35+
}
36+
}
37+
38+
pub fn display(&self) {
39+
println!("{} - {:>10}", self.name.to_ascii_uppercase(), self.tag);
40+
if self.body != "" {
41+
println!(" {}", self.body);
42+
}
43+
println!(" {:<14}: {}", "HTML URL", self.html_url);
44+
println!(" {:<14}: {}", "Downloads", self.downloads);
45+
println!(" {:<14}: {}", "Created at", self.created_at);
46+
println!(" {:<14}: {}", "Published at", self.published_at);
47+
println!(" {:<14}: {}", "ASSETS", self.assets.len());
48+
for asset in &self.assets {
49+
asset.display();
3350
}
51+
println!();
3452
}
3553
}
3654

@@ -61,6 +79,19 @@ impl Asset {
6179
updated_at,
6280
}
6381
}
82+
83+
pub fn display(&self) {
84+
println!(
85+
" {:<14}: {}",
86+
self.name.to_uppercase(),
87+
bytes_to_best_size(self.size)
88+
);
89+
println!(" {:<14}: {}", "Download URL", self.download_url);
90+
println!(" {:<14}: {}", "Downloads", self.downloads);
91+
println!(" {:<14}: {}", "Created at", self.created_at);
92+
println!(" {:<14}: {}", "Updated at", self.updated_at);
93+
println!()
94+
}
6495
}
6596

6697
pub fn downloads_command(
@@ -86,13 +117,16 @@ pub fn downloads_command(
86117
std::process::exit(0)
87118
}
88119

89-
let simple_data = simplify_json_release_data(&json);
90-
println!("amount: {}", simple_data.len());
91-
92120
if all && !display {
93121
println!("{:#?}", json);
94122
} else if all && display {
95-
// show the json without all, but in pretty format
123+
let simple_data = simplify_json_release_data(&json);
124+
let mut download_count = 0;
125+
for release in simple_data {
126+
release.display();
127+
download_count += release.downloads;
128+
}
129+
println!("Total Downloads: {}", download_count);
96130
} else if !all && !display {
97131
// if individual, get latest download url, and sum all item count
98132
// {download_url: Vec<(String name, String link for each asset of latest)>, html_url: String, download_count: i32}
@@ -127,8 +161,8 @@ fn simplify_json_release_data(json: &Value) -> Vec<DownloadData> {
127161
let mut download = DownloadData::new(
128162
name.to_string(),
129163
tag.to_string(),
130-
published_at.to_string(),
131-
created_at.to_string(),
164+
pretty_dates(&published_at),
165+
pretty_dates(&created_at),
132166
html_url.to_string(),
133167
body.to_string(),
134168
);
@@ -146,13 +180,14 @@ fn simplify_json_release_data(json: &Value) -> Vec<DownloadData> {
146180

147181
let asset = Asset::new(
148182
download_url.to_string(),
149-
created_at.to_string(),
183+
pretty_dates(&created_at),
150184
downloads.to_owned(),
151185
name.to_string(),
152186
size.to_owned(),
153-
updated_at.to_string(),
187+
pretty_dates(&updated_at),
154188
);
155189

190+
download.downloads += downloads;
156191
download.assets.push(asset);
157192
}
158193

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ fn main() {
174174
let output = command.get_value_of("output").to_option();
175175
let all = command.has("all");
176176
let display = command.has("display");
177-
178177
}
179178
"help" => cli.help(),
180179
_ => cli.help(),

src/utils.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,30 @@ pub fn write_json_to_file(json: Value, mut path: PathBuf) -> Result<(), String>
6969

7070
Ok(())
7171
}
72+
73+
pub fn bytes_to_best_size(bytes: i64) -> String {
74+
let mut size = bytes as f64;
75+
let mut unit = "B";
76+
77+
if size > 1024.0 {
78+
size /= 1024.0;
79+
unit = "KB";
80+
}
81+
82+
if size > 1024.0 {
83+
size /= 1024.0;
84+
unit = "MB";
85+
}
86+
87+
if size > 1024.0 {
88+
size /= 1024.0;
89+
unit = "GB";
90+
}
91+
92+
if size > 1024.0 {
93+
size /= 1024.0;
94+
unit = "TB";
95+
}
96+
97+
format!("{:.2} {}", size, unit)
98+
}

0 commit comments

Comments
 (0)