util.rs 3.5 KB

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