util.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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 colored::Colorize;
  19. pub fn is_whitespace(s: &str) -> bool {
  20. s.chars().all(char::is_whitespace)
  21. }
  22. pub fn min_f32(x: f32, y: f32) -> f32 {
  23. if x < y { x } else { y }
  24. }
  25. #[allow(dead_code)]
  26. pub fn ansi_texture(width: usize, height: usize, data: &Vec<u8>) -> String {
  27. let mut out = String::new();
  28. out.push('┌');
  29. for _ in 0..width {
  30. out.push('─');
  31. }
  32. out.push('┐');
  33. out.push('\n');
  34. for i in 0..height {
  35. out.push('│');
  36. for j in 0..width {
  37. let idx = 4 * (i * width + j);
  38. let r = data[idx];
  39. let g = data[idx + 1];
  40. let b = data[idx + 2];
  41. let a = data[idx + 3];
  42. #[cfg(target_os = "android")]
  43. {
  44. if a > 204 {
  45. out.push('█');
  46. } else if a > 153 {
  47. out.push('▓');
  48. } else if a > 102 {
  49. out.push('▒');
  50. } else if a > 51 {
  51. out.push('░');
  52. } else {
  53. out.push(' ');
  54. }
  55. }
  56. #[cfg(target_os = "linux")]
  57. {
  58. let r = ((a as f32 * r as f32) / 255.) as u8;
  59. let g = ((a as f32 * g as f32) / 255.) as u8;
  60. let b = ((a as f32 * b as f32) / 255.) as u8;
  61. let val = "█".truecolor(r, g, b).to_string();
  62. out.push_str(&val);
  63. }
  64. }
  65. out.push('│');
  66. out.push('\n');
  67. }
  68. out.push('└');
  69. for _ in 0..width {
  70. out.push('─');
  71. }
  72. out.push('┘');
  73. out.push('\n');
  74. out
  75. }
  76. pub struct TupleIterStruct3<I1, I2, I3> {
  77. idx: usize,
  78. i1: I1,
  79. i2: I2,
  80. i3: I3,
  81. }
  82. impl<I1, I2, I3> Iterator for TupleIterStruct3<I1, I2, I3>
  83. where
  84. I1: Iterator,
  85. I2: Iterator,
  86. I3: Iterator,
  87. {
  88. type Item = (usize, I1::Item, I2::Item, I3::Item);
  89. fn next(&mut self) -> Option<Self::Item> {
  90. let Some(x1) = self.i1.next() else { return None };
  91. let Some(x2) = self.i2.next() else { return None };
  92. let Some(x3) = self.i3.next() else { return None };
  93. let res = (self.idx, x1, x2, x3);
  94. self.idx += 1;
  95. Some(res)
  96. }
  97. }
  98. #[allow(dead_code)]
  99. pub fn zip3<X1, X2, X3, I1, I2, I3>(i1: I1, i2: I2, i3: I3) -> TupleIterStruct3<I1, I2, I3>
  100. where
  101. I1: Iterator<Item = X1>,
  102. I2: Iterator<Item = X2>,
  103. I3: Iterator<Item = X3>,
  104. {
  105. TupleIterStruct3 { idx: 0, i1, i2, i3 }
  106. }
  107. pub fn enumerate<X>(v: Vec<X>) -> impl Iterator<Item = (usize, X)> {
  108. v.into_iter().enumerate()
  109. }
  110. pub fn enumerate_ref<X>(v: &Vec<X>) -> impl Iterator<Item = (usize, &X)> {
  111. v.iter().enumerate()
  112. }
  113. pub fn enumerate_mut<X>(v: &mut Vec<X>) -> impl Iterator<Item = (usize, &mut X)> {
  114. v.iter_mut().enumerate()
  115. }