util.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use termcolor::{Color, ColorSpec};
  19. use fud::resource::{ResourceStatus, ResourceType};
  20. const UNITS: [&str; 7] = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
  21. pub fn status_to_colorspec(status: &ResourceStatus) -> ColorSpec {
  22. ColorSpec::new()
  23. .set_fg(match status {
  24. ResourceStatus::Downloading => Some(Color::Blue),
  25. ResourceStatus::Seeding => Some(Color::Green),
  26. ResourceStatus::Discovering => Some(Color::Magenta),
  27. ResourceStatus::Incomplete => Some(Color::Red),
  28. ResourceStatus::Verifying => Some(Color::Yellow),
  29. })
  30. .set_bold(true)
  31. .clone()
  32. }
  33. pub fn type_to_colorspec(rtype: &ResourceType) -> ColorSpec {
  34. ColorSpec::new()
  35. .set_fg(match rtype {
  36. ResourceType::File => Some(Color::Blue),
  37. ResourceType::Directory => Some(Color::Magenta),
  38. ResourceType::Unknown => None,
  39. })
  40. .set_bold(true)
  41. .clone()
  42. }
  43. pub fn format_bytes(bytes: u64) -> String {
  44. let mut size = bytes as f64;
  45. let mut unit_index = 0;
  46. while size >= 1024.0 && unit_index < UNITS.len() - 1 {
  47. size /= 1024.0;
  48. unit_index += 1;
  49. }
  50. format!("{size:.1} {}", UNITS[unit_index])
  51. }
  52. pub fn format_progress_bytes(current: u64, total: u64) -> String {
  53. let mut total = total as f64;
  54. let mut unit_index = 0;
  55. while total >= 1024.0 && unit_index < UNITS.len() - 1 {
  56. total /= 1024.0;
  57. unit_index += 1;
  58. }
  59. let current = (current as f64) / 1024_f64.powi(unit_index as i32);
  60. format!("{current:.1}/{total:.1} {}", UNITS[unit_index])
  61. }
  62. /// Returns a formated string from the duration.
  63. /// - 1 -> 1s
  64. /// - 60 -> 1m
  65. /// - 90 -> 1m30s
  66. pub fn format_duration(seconds: u64) -> String {
  67. if seconds == 0 {
  68. return "0s".to_string();
  69. }
  70. let units = [
  71. (86400, "d"), // days
  72. (3600, "h"), // hours
  73. (60, "m"), // minutes
  74. (1, "s"), // seconds
  75. ];
  76. for (i, (unit_seconds, unit_symbol)) in units.iter().enumerate() {
  77. if seconds >= *unit_seconds {
  78. let first = seconds / unit_seconds;
  79. let remaining = seconds % unit_seconds;
  80. if remaining > 0 && i < units.len() - 1 {
  81. let (next_unit_seconds, next_unit_symbol) = units[i + 1];
  82. let second = remaining / next_unit_seconds;
  83. return format!("{first}{unit_symbol}{second}{next_unit_symbol}");
  84. }
  85. return format!("{first}{unit_symbol}");
  86. }
  87. }
  88. "0s".to_string()
  89. }