mod.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 colored::Colorize;
  19. use std::time::{SystemTime, UNIX_EPOCH};
  20. mod clip;
  21. pub use clip::Clipboard;
  22. pub fn is_whitespace(s: &str) -> bool {
  23. s.chars().all(char::is_whitespace)
  24. }
  25. pub fn min_f32(x: f32, y: f32) -> f32 {
  26. if x < y {
  27. x
  28. } else {
  29. y
  30. }
  31. }
  32. /// Use src/util/time.rs Timestamp instead of this.
  33. pub fn unixtime() -> u64 {
  34. let timest = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
  35. assert!(timest < std::u64::MAX as u128);
  36. timest as u64
  37. }
  38. #[allow(dead_code)]
  39. pub fn ansi_texture(width: usize, height: usize, data: &Vec<u8>) -> String {
  40. let mut out = String::new();
  41. out.push('┌');
  42. for _ in 0..width {
  43. out.push('─');
  44. }
  45. out.push('┐');
  46. out.push('\n');
  47. for i in 0..height {
  48. out.push('│');
  49. for j in 0..width {
  50. let idx = 4 * (i * width + j);
  51. let r = data[idx];
  52. let g = data[idx + 1];
  53. let b = data[idx + 2];
  54. let a = data[idx + 3];
  55. #[cfg(target_os = "android")]
  56. {
  57. if a > 204 {
  58. out.push('█');
  59. } else if a > 153 {
  60. out.push('▓');
  61. } else if a > 102 {
  62. out.push('▒');
  63. } else if a > 51 {
  64. out.push('░');
  65. } else {
  66. out.push(' ');
  67. }
  68. }
  69. #[cfg(target_os = "linux")]
  70. {
  71. let r = ((a as f32 * r as f32) / 255.) as u8;
  72. let g = ((a as f32 * g as f32) / 255.) as u8;
  73. let b = ((a as f32 * b as f32) / 255.) as u8;
  74. let val = "█".truecolor(r, g, b).to_string();
  75. out.push_str(&val);
  76. }
  77. }
  78. out.push('│');
  79. out.push('\n');
  80. }
  81. out.push('└');
  82. for _ in 0..width {
  83. out.push('─');
  84. }
  85. out.push('┘');
  86. out.push('\n');
  87. out
  88. }
  89. pub struct TupleIterStruct3<I1, I2, I3> {
  90. idx: usize,
  91. i1: I1,
  92. i2: I2,
  93. i3: I3,
  94. }
  95. impl<I1, I2, I3> Iterator for TupleIterStruct3<I1, I2, I3>
  96. where
  97. I1: Iterator,
  98. I2: Iterator,
  99. I3: Iterator,
  100. {
  101. type Item = (usize, I1::Item, I2::Item, I3::Item);
  102. fn next(&mut self) -> Option<Self::Item> {
  103. let Some(x1) = self.i1.next() else { return None };
  104. let Some(x2) = self.i2.next() else { return None };
  105. let Some(x3) = self.i3.next() else { return None };
  106. let res = (self.idx, x1, x2, x3);
  107. self.idx += 1;
  108. Some(res)
  109. }
  110. }
  111. pub fn zip3<X1, X2, X3, I1, I2, I3>(i1: I1, i2: I2, i3: I3) -> TupleIterStruct3<I1, I2, I3>
  112. where
  113. I1: Iterator<Item = X1>,
  114. I2: Iterator<Item = X2>,
  115. I3: Iterator<Item = X3>,
  116. {
  117. TupleIterStruct3 { idx: 0, i1, i2, i3 }
  118. }
  119. pub struct TupleIterStruct4<I1, I2, I3, I4> {
  120. idx: usize,
  121. i1: I1,
  122. i2: I2,
  123. i3: I3,
  124. i4: I4,
  125. }
  126. impl<I1, I2, I3, I4> Iterator for TupleIterStruct4<I1, I2, I3, I4>
  127. where
  128. I1: Iterator,
  129. I2: Iterator,
  130. I3: Iterator,
  131. I4: Iterator,
  132. {
  133. type Item = (usize, I1::Item, I2::Item, I3::Item, I4::Item);
  134. fn next(&mut self) -> Option<Self::Item> {
  135. let Some(x1) = self.i1.next() else { return None };
  136. let Some(x2) = self.i2.next() else { return None };
  137. let Some(x3) = self.i3.next() else { return None };
  138. let Some(x4) = self.i4.next() else { return None };
  139. let res = (self.idx, x1, x2, x3, x4);
  140. self.idx += 1;
  141. Some(res)
  142. }
  143. }
  144. #[allow(dead_code)]
  145. pub fn zip4<X1, X2, X3, X4, I1, I2, I3, I4>(
  146. i1: I1,
  147. i2: I2,
  148. i3: I3,
  149. i4: I4,
  150. ) -> TupleIterStruct4<I1, I2, I3, I4>
  151. where
  152. I1: Iterator<Item = X1>,
  153. I2: Iterator<Item = X2>,
  154. I3: Iterator<Item = X3>,
  155. I4: Iterator<Item = X4>,
  156. {
  157. TupleIterStruct4 { idx: 0, i1, i2, i3, i4 }
  158. }
  159. pub fn enumerate<X>(v: Vec<X>) -> impl Iterator<Item = (usize, X)> {
  160. v.into_iter().enumerate()
  161. }
  162. pub fn enumerate_ref<X>(v: &Vec<X>) -> impl Iterator<Item = (usize, &X)> {
  163. v.iter().enumerate()
  164. }
  165. pub fn enumerate_mut<X>(v: &mut Vec<X>) -> impl Iterator<Item = (usize, &mut X)> {
  166. v.iter_mut().enumerate()
  167. }