mod.rs 4.8 KB

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