mod.rs 5.1 KB

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