Skip to content

Commit

Permalink
Merge pull request #32 from wtjones/adjusted-byte-units
Browse files Browse the repository at this point in the history
Adjusted byte units
  • Loading branch information
mtkennerly authored Aug 1, 2020
2 parents 4c460aa + c5c2fcc commit d3de364
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"

[dependencies]
base64 = "0.12.3"
byte-unit = "4.0.8"
copypasta = "0.7.0"
dialoguer = "0.6.2"
dirs = "3.0.0"
Expand Down
10 changes: 5 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ mod tests {
Overall:
Games: 0
Size: 0.00 MiB
Size: 0 B
Location: {}/dev/null
"#,
&drive()
Expand Down Expand Up @@ -906,15 +906,15 @@ Overall:
);
assert_eq!(
r#"
foo [0.10 MiB]:
foo [100.00 KiB]:
- <drive>/file1
- [FAILED] <drive>/file2
- [FAILED] HKEY_CURRENT_USER/Key1
- HKEY_CURRENT_USER/Key2
Overall:
Games: 1 of 1
Size: 0.10 of 0.15 MiB
Size: 100.00 of 150.00 KiB
Location: <drive>/dev/null
"#
.trim()
Expand Down Expand Up @@ -952,13 +952,13 @@ Overall:
);
assert_eq!(
r#"
foo [0.15 MiB]:
foo [150.00 KiB]:
- <drive>/original/file1
- <drive>/original/file2
Overall:
Games: 1
Size: 0.15 MiB
Size: 150.00 KiB
Location: <drive>/dev/null
"#
.trim()
Expand Down
4 changes: 2 additions & 2 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl GameListEntry {
config.is_game_enabled_for_backup(&self.scan_info.game_name)
};
let name_for_checkbox = self.scan_info.game_name.clone();

println!("game: {}", self.scan_info.game_name);
Container::new(
Column::new()
.padding(5)
Expand Down Expand Up @@ -443,7 +443,7 @@ impl GameListEntry {
)
.push(
Container::new(Text::new(
translator.mib(self.scan_info.sum_bytes(&self.backup_info), false),
translator.adjusted_size(self.scan_info.sum_bytes(&self.backup_info)),
))
.width(Length::Units(115))
.center_x(),
Expand Down
44 changes: 22 additions & 22 deletions src/lang.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use byte_unit::Byte;

use crate::{
manifest::Store,
prelude::{Error, OperationStatus, OperationStepDecision, StrictPath},
Expand Down Expand Up @@ -102,11 +104,11 @@ impl Translator {
pub fn cli_game_header(&self, name: &str, bytes: u64, decision: &OperationStepDecision) -> String {
if *decision == OperationStepDecision::Processed {
match self.language {
Language::English => format!("{} [{}]:", name, self.mib(bytes, false)),
Language::English => format!("{} [{}]:", name, self.adjusted_size(bytes)),
}
} else {
match self.language {
Language::English => format!("{} [{}] {}:", name, self.mib(bytes, false), self.label_ignored()),
Language::English => format!("{} [{}] {}:", name, self.adjusted_size(bytes), self.label_ignored()),
}
}
}
Expand Down Expand Up @@ -135,7 +137,7 @@ impl Translator {
Language::English => format!(
"\nOverall:\n Games: {}\n Size: {}\n Location: {}",
status.total_games,
self.mib(status.total_bytes, true),
self.adjusted_size(status.total_bytes),
location.render()
),
}
Expand All @@ -145,8 +147,8 @@ impl Translator {
"\nOverall:\n Games: {} of {}\n Size: {} of {}\n Location: {}",
status.processed_games,
status.total_games,
self.mib_unlabelled(status.processed_bytes),
self.mib(status.total_bytes, true),
self.adjusted_size_unlabelled(status.processed_bytes),
self.adjusted_size(status.total_bytes),
location.render()
),
}
Expand Down Expand Up @@ -339,37 +341,35 @@ impl Translator {
.into()
}

pub fn mib(&self, bytes: u64, show_zero: bool) -> String {
let mib = self.mib_unlabelled(bytes);
if !show_zero && mib == "0.00" {
match self.language {
Language::English => "~ 0",
}
.into()
} else {
match self.language {
Language::English => format!("{} MiB", mib),
}
}
pub fn adjusted_size(&self, bytes: u64) -> String {
let byte = Byte::from_bytes(bytes.into());
let adjusted_byte = byte.get_appropriate_unit(true);
adjusted_byte.to_string()
}

pub fn mib_unlabelled(&self, bytes: u64) -> String {
format!("{:.2}", bytes as f64 / 1024.0 / 1024.0)
pub fn adjusted_size_unlabelled(&self, bytes: u64) -> String {
let byte = Byte::from_bytes(bytes.into());
let adjusted_byte = byte.get_appropriate_unit(true);
format!("{:.2}", adjusted_byte.get_value())
}

pub fn processed_games(&self, status: &OperationStatus) -> String {
if status.completed() {
match self.language {
Language::English => format!("{} games | {}", status.total_games, self.mib(status.total_bytes, true)),
Language::English => format!(
"{} games | {}",
status.total_games,
self.adjusted_size(status.total_bytes)
),
}
} else {
match self.language {
Language::English => format!(
"{} of {} games | {} of {}",
status.processed_games,
status.total_games,
self.mib_unlabelled(status.processed_bytes),
self.mib(status.total_bytes, true)
self.adjusted_size_unlabelled(status.processed_bytes),
self.adjusted_size(status.total_bytes)
),
}
}
Expand Down

0 comments on commit d3de364

Please sign in to comment.