🐝 I really like where this mua is(was?) headed, but it seems as though there has not been much activity recently.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.3 KiB

3 years ago
  1. /*
  2. * meli
  3. *
  4. * Copyright 2017-2018 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*! UI types used throughout meli.
  22. *
  23. * The `segment_tree` module performs maximum range queries. This is used in getting the maximum
  24. * element of a column within a specific range in e-mail lists. That way a very large value that
  25. * is not the in the currently displayed page does not cause the column to be rendered bigger
  26. * than it has to.
  27. *
  28. * `UIMode` describes the application's... mode. Same as in the modal editor `vi`.
  29. *
  30. * `UIEvent` is the type passed around `Component`s when something happens.
  31. */
  32. extern crate serde;
  33. #[macro_use]
  34. mod helpers;
  35. pub use self::helpers::*;
  36. use super::execute::Action;
  37. use super::terminal::*;
  38. use melib::backends::FolderHash;
  39. use melib::{EnvelopeHash, RefreshEvent};
  40. use nix::unistd::Pid;
  41. use std;
  42. use std::fmt;
  43. use std::thread;
  44. use uuid::Uuid;
  45. #[derive(Debug)]
  46. pub enum StatusEvent {
  47. DisplayMessage(String),
  48. BufClear,
  49. BufSet(String),
  50. UpdateStatus(String),
  51. }
  52. /// `ThreadEvent` encapsulates all of the possible values we need to transfer between our threads
  53. /// to the main process.
  54. #[derive(Debug)]
  55. pub enum ThreadEvent {
  56. NewThread(thread::ThreadId, String),
  57. /// User input.
  58. Input(Key),
  59. /// User input and input as raw bytes.
  60. InputRaw((Key, Vec<u8>)),
  61. /// A watched folder has been refreshed.
  62. RefreshMailbox(Box<RefreshEvent>),
  63. UIEvent(UIEvent),
  64. /// A thread has updated some of its information
  65. Pulse,
  66. //Decode { _ }, // For gpg2 signature check
  67. }
  68. impl From<RefreshEvent> for ThreadEvent {
  69. fn from(event: RefreshEvent) -> Self {
  70. ThreadEvent::RefreshMailbox(Box::new(event))
  71. }
  72. }
  73. #[derive(Debug)]
  74. pub enum ForkType {
  75. /// Already finished fork, we only want to restore input/output
  76. Finished,
  77. /// Embed pty
  78. Embed(Pid),
  79. Generic(std::process::Child),
  80. NewDraft(File, std::process::Child),
  81. }
  82. #[derive(Debug)]
  83. pub enum NotificationType {
  84. INFO,
  85. ERROR,
  86. NewMail,
  87. }
  88. #[derive(Debug)]
  89. pub enum UIEvent {
  90. Input(Key),
  91. ExInput(Key),
  92. InsertInput(Key),
  93. EmbedInput((Key, Vec<u8>)),
  94. //Quit?
  95. Resize,
  96. /// Force redraw.
  97. Fork(ForkType),
  98. ChangeMailbox(usize),
  99. ChangeMode(UIMode),
  100. Command(String),
  101. Notification(Option<String>, String, Option<NotificationType>),
  102. Action(Action),
  103. StatusEvent(StatusEvent),
  104. MailboxUpdate((usize, FolderHash)), // (account_idx, mailbox_idx)
  105. ComponentKill(Uuid),
  106. WorkerProgress(FolderHash),
  107. StartupCheck(FolderHash),
  108. RefreshEvent(Box<RefreshEvent>),
  109. EnvelopeUpdate(EnvelopeHash),
  110. EnvelopeRename(EnvelopeHash, EnvelopeHash), // old_hash, new_hash
  111. EnvelopeRemove(EnvelopeHash),
  112. Timer(u8),
  113. }
  114. impl From<RefreshEvent> for UIEvent {
  115. fn from(event: RefreshEvent) -> Self {
  116. UIEvent::RefreshEvent(Box::new(event))
  117. }
  118. }
  119. #[derive(Debug, PartialEq, Copy, Clone)]
  120. pub enum UIMode {
  121. Normal,
  122. Insert,
  123. /// Forward input to an embed pseudoterminal.
  124. Embed,
  125. Execute,
  126. Fork,
  127. }
  128. impl fmt::Display for UIMode {
  129. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  130. write!(
  131. f,
  132. "{}",
  133. match *self {
  134. UIMode::Normal => "NORMAL",
  135. UIMode::Insert => "INSERT",
  136. UIMode::Execute => "EX",
  137. UIMode::Fork => "FORK",
  138. UIMode::Embed => "EMBED",
  139. }
  140. )
  141. }
  142. }
  143. /// An event notification that is passed to Entities for handling.
  144. pub struct Notification {
  145. _title: String,
  146. _content: String,
  147. _timestamp: std::time::Instant,
  148. }
  149. pub mod segment_tree {
  150. /*! Simple segment tree implementation for maximum in range queries. This is useful if given an
  151. * array of numbers you want to get the maximum value inside an interval quickly.
  152. */
  153. use smallvec::SmallVec;
  154. use std::convert::TryFrom;
  155. use std::iter::FromIterator;
  156. #[derive(Default, Debug, Clone)]
  157. pub struct SegmentTree {
  158. array: SmallVec<[u8; 1024]>,
  159. tree: SmallVec<[u8; 1024]>,
  160. }
  161. impl From<SmallVec<[u8; 1024]>> for SegmentTree {
  162. fn from(val: SmallVec<[u8; 1024]>) -> SegmentTree {
  163. SegmentTree::new(val)
  164. }
  165. }
  166. impl SegmentTree {
  167. pub fn new(val: SmallVec<[u8; 1024]>) -> SegmentTree {
  168. if val.is_empty() {
  169. return SegmentTree {
  170. array: val.clone(),
  171. tree: val,
  172. };
  173. }
  174. let height = (f64::from(u32::try_from(val.len()).unwrap_or(0)))
  175. .log2()
  176. .ceil() as u32;
  177. let max_size = 2 * (2_usize.pow(height));
  178. let mut segment_tree: SmallVec<[u8; 1024]> =
  179. SmallVec::from_iter(core::iter::repeat(0).take(max_size));
  180. for i in 0..val.len() {
  181. segment_tree[val.len() + i] = val[i];
  182. }
  183. for i in (1..val.len()).rev() {
  184. segment_tree[i] = std::cmp::max(segment_tree[2 * i], segment_tree[2 * i + 1]);
  185. }
  186. SegmentTree {
  187. array: val,
  188. tree: segment_tree,
  189. }
  190. }
  191. /// (left, right) is inclusive
  192. pub fn get_max(&self, mut left: usize, mut right: usize) -> u8 {
  193. let len = self.array.len();
  194. debug_assert!(left <= right);
  195. if right >= len {
  196. right = len.saturating_sub(1);
  197. }
  198. left += len;
  199. right += len + 1;
  200. let mut max = 0;
  201. while left < right {
  202. if (left & 1) > 0 {
  203. max = std::cmp::max(max, self.tree[left]);
  204. left += 1;
  205. }
  206. if (right & 1) > 0 {
  207. right -= 1;
  208. max = std::cmp::max(max, self.tree[right]);
  209. }
  210. left /= 2;
  211. right /= 2;
  212. }
  213. max
  214. }
  215. }
  216. #[test]
  217. fn test_segment_tree() {
  218. let array: SmallVec<[u8; 1024]> = [9, 1, 17, 2, 3, 23, 4, 5, 6, 37]
  219. .into_iter()
  220. .cloned()
  221. .collect::<SmallVec<[u8; 1024]>>();
  222. let segment_tree = SegmentTree::from(array.clone());
  223. assert_eq!(segment_tree.get_max(0, 5), 23);
  224. assert_eq!(segment_tree.get_max(6, 9), 37);
  225. }
  226. }
  227. #[derive(Debug)]
  228. pub struct RateLimit {
  229. last_tick: std::time::Instant,
  230. pub timer: crate::timer::PosixTimer,
  231. rate: std::time::Duration,
  232. reqs: u64,
  233. millis: std::time::Duration,
  234. pub active: bool,
  235. }
  236. //FIXME: tests.
  237. impl RateLimit {
  238. pub fn new(reqs: u64, millis: u64) -> Self {
  239. RateLimit {
  240. last_tick: std::time::Instant::now(),
  241. timer: crate::timer::PosixTimer::new_with_signal(
  242. std::time::Duration::from_secs(0),
  243. std::time::Duration::from_secs(1),
  244. nix::sys::signal::Signal::SIGALRM,
  245. )
  246. .unwrap(),
  247. rate: std::time::Duration::from_millis(millis / reqs),
  248. reqs,
  249. millis: std::time::Duration::from_millis(millis),
  250. active: false,
  251. }
  252. }
  253. pub fn reset(&mut self) {
  254. self.last_tick = std::time::Instant::now();
  255. self.active = false;
  256. }
  257. pub fn tick(&mut self) -> bool {
  258. let now = std::time::Instant::now();
  259. self.last_tick += self.rate;
  260. if self.last_tick < now {
  261. self.last_tick = now + self.rate;
  262. } else if self.last_tick > now + self.millis {
  263. self.timer.rearm();
  264. self.active = true;
  265. return false;
  266. }
  267. self.active = false;
  268. true
  269. }
  270. #[inline(always)]
  271. pub fn id(&self) -> u8 {
  272. self.timer.si_value
  273. }
  274. }