util.rs 3.0 KB

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