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.

452 lines
11KB

  1. /*
  2. * meli - ui crate.
  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. /*!
  22. Components are ways to handle application data. They can draw on the terminal and receive events, but also do other stuff as well. (For example, see the `notifications` module.)
  23. See the `Component` Trait for more details.
  24. */
  25. use super::*;
  26. pub mod mail;
  27. pub use mail::*;
  28. pub mod notifications;
  29. pub mod indexer;
  30. pub use self::indexer::*;
  31. pub mod utilities;
  32. pub use self::utilities::*;
  33. pub mod contacts;
  34. pub use contacts::*;
  35. use std::fmt;
  36. use std::fmt::{Debug, Display};
  37. use std::ops::{Deref, DerefMut};
  38. use fnv::FnvHashMap;
  39. use uuid::Uuid;
  40. use super::{Key, StatusEvent, UIEvent, UIEventType};
  41. /// The upper and lower boundary char.
  42. const HORZ_BOUNDARY: char = '─';
  43. /// The left and right boundary char.
  44. const VERT_BOUNDARY: char = '│';
  45. /// The top-left corner
  46. const _TOP_LEFT_CORNER: char = '┌';
  47. /// The top-right corner
  48. const _TOP_RIGHT_CORNER: char = '┐';
  49. /// The bottom-left corner
  50. const _BOTTOM_LEFT_CORNER: char = '└';
  51. /// The bottom-right corner
  52. const _BOTTOM_RIGHT_CORNER: char = '┘';
  53. const LIGHT_VERTICAL_AND_RIGHT: char = '├';
  54. const _LIGHT_VERTICAL_AND_LEFT: char = '┤';
  55. const LIGHT_DOWN_AND_HORIZONTAL: char = '┬';
  56. const LIGHT_UP_AND_HORIZONTAL: char = '┴';
  57. const _DOUBLE_DOWN_AND_RIGHT: char = '╔';
  58. const _DOUBLE_DOWN_AND_LEFT: char = '╗';
  59. const _DOUBLE_UP_AND_LEFT: char = '╝';
  60. const _DOUBLE_UP_AND_RIGHT: char = '╚';
  61. type EntityId = Uuid;
  62. /// `Entity` is a container for Components.
  63. #[derive(Debug)]
  64. pub struct Entity {
  65. id: EntityId,
  66. pub component: Box<Component>, // more than one?
  67. }
  68. impl From<Box<Component>> for Entity {
  69. fn from(mut kind: Box<Component>) -> Entity {
  70. let id = Uuid::new_v4();
  71. kind.set_id(id);
  72. Entity {
  73. id,
  74. component: kind,
  75. }
  76. }
  77. }
  78. impl<C: 'static> From<Box<C>> for Entity
  79. where
  80. C: Component,
  81. {
  82. fn from(mut kind: Box<C>) -> Entity {
  83. let id = Uuid::new_v4();
  84. kind.set_id(id);
  85. Entity {
  86. id,
  87. component: kind,
  88. }
  89. }
  90. }
  91. impl Display for Entity {
  92. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  93. Display::fmt(&self.component, f)
  94. }
  95. }
  96. impl DerefMut for Entity {
  97. fn deref_mut(&mut self) -> &mut Box<Component> {
  98. &mut self.component
  99. }
  100. }
  101. impl Deref for Entity {
  102. type Target = Box<Component>;
  103. fn deref(&self) -> &Box<Component> {
  104. &self.component
  105. }
  106. }
  107. impl Entity {
  108. pub fn id(&self) -> &EntityId {
  109. &self.id
  110. }
  111. /// Pass events to child component.
  112. pub fn rcv_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
  113. self.component.process_event(event, context)
  114. }
  115. }
  116. pub type ShortcutMap = FnvHashMap<&'static str, Key>;
  117. /// Types implementing this Trait can draw on the terminal and receive events.
  118. /// If a type wants to skip drawing if it has not changed anything, it can hold some flag in its
  119. /// fields (eg self.dirty = false) and act upon that in their `draw` implementation.
  120. pub trait Component: Display + Debug + Send {
  121. fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context);
  122. fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool;
  123. fn is_dirty(&self) -> bool {
  124. true
  125. }
  126. fn set_dirty(&mut self);
  127. fn kill(&mut self, _id: EntityId) {}
  128. fn set_id(&mut self, _id: EntityId) {}
  129. fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
  130. Default::default()
  131. }
  132. }
  133. /*
  134. pub(crate) fn is_box_char(ch: char) -> bool {
  135. match ch {
  136. HORZ_BOUNDARY | VERT_BOUNDARY => true,
  137. _ => false,
  138. }
  139. }
  140. * pub(crate) fn is_box_char(ch: char) -> bool {
  141. * match ch {
  142. * '└' | '─' | '┘' | '┴' | '┌' | '│' | '├' | '┐' | '┬' | '┤' | '┼' | '╷' | '╵' | '╴' | '╶' => true,
  143. * _ => false,
  144. * }
  145. * }
  146. */
  147. fn bin_to_ch(b: u32) -> char {
  148. match b {
  149. 0b0001 => '╶',
  150. 0b0010 => '╵',
  151. 0b0011 => '└',
  152. 0b0100 => '╴',
  153. 0b0101 => '─',
  154. 0b0110 => '┘',
  155. 0b0111 => '┴',
  156. 0b1000 => '╷',
  157. 0b1001 => '┌',
  158. 0b1010 => '│',
  159. 0b1011 => '├',
  160. 0b1100 => '┐',
  161. 0b1101 => '┬',
  162. 0b1110 => '┤',
  163. 0b1111 => '┼',
  164. x => unreachable!(format!("unreachable bin_to_ch(x), x = {:b}", x)),
  165. }
  166. }
  167. fn ch_to_bin(ch: char) -> Option<u32> {
  168. match ch {
  169. '└' => Some(0b0011),
  170. '─' => Some(0b0101),
  171. '┘' => Some(0b0110),
  172. '┴' => Some(0b0111),
  173. '┌' => Some(0b1001),
  174. '│' => Some(0b1010),
  175. '├' => Some(0b1011),
  176. '┐' => Some(0b1100),
  177. '┬' => Some(0b1101),
  178. '┤' => Some(0b1110),
  179. '┼' => Some(0b1111),
  180. '╷' => Some(0b1000),
  181. '╵' => Some(0b0010),
  182. '╴' => Some(0b0100),
  183. '╶' => Some(0b0001),
  184. _ => None,
  185. }
  186. }
  187. #[allow(clippy::never_loop)]
  188. fn set_and_join_vert(grid: &mut CellBuffer, idx: Pos) -> u32 {
  189. let (x, y) = idx;
  190. let mut bin_set = 0b1010;
  191. /* Check left side
  192. *
  193. * 1
  194. * -> 2 │ 0
  195. * 3
  196. */
  197. loop {
  198. if x > 0 {
  199. if let Some(cell) = grid.get_mut(x - 1, y) {
  200. if let Some(adj) = ch_to_bin(cell.ch()) {
  201. if (adj & 0b0001) > 0 {
  202. bin_set |= 0b0100;
  203. break;
  204. } else if adj == 0b0100 {
  205. cell.set_ch(bin_to_ch(0b0101));
  206. bin_set |= 0b0100;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. bin_set &= 0b1011;
  213. break;
  214. }
  215. /* Check right side
  216. *
  217. * 1
  218. * 2 │ 0 <-
  219. * 3
  220. */
  221. loop {
  222. if let Some(cell) = grid.get_mut(x + 1, y) {
  223. if let Some(adj) = ch_to_bin(cell.ch()) {
  224. if (adj & 0b0100) > 0 {
  225. bin_set |= 0b0001;
  226. break;
  227. }
  228. }
  229. }
  230. bin_set &= 0b1110;
  231. break;
  232. }
  233. /* Set upper side
  234. *
  235. * 1 <-
  236. * 2 │ 0
  237. * 3
  238. */
  239. loop {
  240. if y > 0 {
  241. if let Some(cell) = grid.get_mut(x, y - 1) {
  242. if let Some(adj) = ch_to_bin(cell.ch()) {
  243. cell.set_ch(bin_to_ch(adj | 0b1000));
  244. break;
  245. }
  246. }
  247. }
  248. bin_set &= 0b1101;
  249. break;
  250. }
  251. /* Set bottom side
  252. *
  253. * 1
  254. * 2 │ 0
  255. * 3 <-
  256. */
  257. loop {
  258. if let Some(cell) = grid.get_mut(x, y + 1) {
  259. if let Some(adj) = ch_to_bin(cell.ch()) {
  260. cell.set_ch(bin_to_ch(adj | 0b0010));
  261. break;
  262. }
  263. }
  264. bin_set &= 0b0111;
  265. break;
  266. }
  267. if bin_set == 0 {
  268. bin_set = 0b1010;
  269. }
  270. bin_set
  271. }
  272. #[allow(clippy::never_loop)]
  273. fn set_and_join_horz(grid: &mut CellBuffer, idx: Pos) -> u32 {
  274. let (x, y) = idx;
  275. let mut bin_set = 0b0101;
  276. /* Check upper side
  277. *
  278. * 1 <-
  279. * 2 ─ 0
  280. * 3
  281. */
  282. loop {
  283. if y > 0 {
  284. if let Some(cell) = grid.get_mut(x, y - 1) {
  285. if let Some(adj) = ch_to_bin(cell.ch()) {
  286. if (adj & 0b1000) > 0 {
  287. bin_set |= 0b0010;
  288. break;
  289. } else if adj == 0b0010 {
  290. bin_set |= 0b0010;
  291. cell.set_ch(bin_to_ch(0b1010));
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. bin_set &= 0b1101;
  298. break;
  299. }
  300. /* Check bottom side
  301. *
  302. * 1
  303. * 2 ─ 0
  304. * 3 <-
  305. */
  306. loop {
  307. if let Some(cell) = grid.get_mut(x, y + 1) {
  308. if let Some(adj) = ch_to_bin(cell.ch()) {
  309. if (adj & 0b0010) > 0 {
  310. bin_set |= 0b1000;
  311. break;
  312. } else if adj == 0b1000 {
  313. cell.set_ch(bin_to_ch(0b1010));
  314. break;
  315. }
  316. }
  317. }
  318. bin_set &= 0b0111;
  319. break;
  320. }
  321. /* Set left side
  322. *
  323. * 1
  324. * -> 2 ─ 0
  325. * 3
  326. */
  327. loop {
  328. if x > 0 {
  329. if let Some(cell) = grid.get_mut(x - 1, y) {
  330. if let Some(adj) = ch_to_bin(cell.ch()) {
  331. cell.set_ch(bin_to_ch(adj | 0b0001));
  332. break;
  333. }
  334. }
  335. }
  336. bin_set &= 0b1011;
  337. break;
  338. }
  339. /* Set right side
  340. *
  341. * 1
  342. * 2 ─ 0 <-
  343. * 3
  344. */
  345. loop {
  346. if let Some(cell) = grid.get_mut(x + 1, y) {
  347. if let Some(adj) = ch_to_bin(cell.ch()) {
  348. cell.set_ch(bin_to_ch(adj | 0b0100));
  349. break;
  350. }
  351. }
  352. bin_set &= 0b1110;
  353. break;
  354. }
  355. if bin_set == 0 {
  356. bin_set = 0b0101;
  357. }
  358. bin_set
  359. }
  360. pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) {
  361. /* Connected sides:
  362. *
  363. * 1
  364. * 2 c 0
  365. * 3
  366. *
  367. * #3210
  368. * 0b____
  369. */
  370. let bin_set = match ch {
  371. '│' => set_and_join_vert(grid, idx),
  372. '─' => set_and_join_horz(grid, idx),
  373. _ => unreachable!(),
  374. };
  375. grid[idx].set_ch(bin_to_ch(bin_set));
  376. }
  377. pub fn create_box(grid: &mut CellBuffer, area: Area) {
  378. if !is_valid_area!(area) {
  379. return;
  380. }
  381. let upper_left = upper_left!(area);
  382. let bottom_right = bottom_right!(area);
  383. for x in get_x(upper_left)..get_x(bottom_right) {
  384. grid[(x, get_y(upper_left))].set_ch(HORZ_BOUNDARY);
  385. grid[(x, get_y(bottom_right))].set_ch(HORZ_BOUNDARY);
  386. }
  387. for y in get_y(upper_left)..get_y(bottom_right) {
  388. grid[(get_x(upper_left), y)].set_ch(VERT_BOUNDARY);
  389. grid[(get_x(bottom_right), y)].set_ch(VERT_BOUNDARY);
  390. }
  391. set_and_join_box(grid, upper_left, HORZ_BOUNDARY);
  392. set_and_join_box(grid, set_x(upper_left, get_x(bottom_right)), HORZ_BOUNDARY);
  393. set_and_join_box(grid, set_y(upper_left, get_y(bottom_right)), VERT_BOUNDARY);
  394. set_and_join_box(grid, bottom_right, VERT_BOUNDARY);
  395. }