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.
 
 
 
 
 
 

405 lines
10 KiB

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