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.

components.rs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. fn is_visible(&self) -> bool {
  71. true
  72. }
  73. fn can_quit_cleanly(&mut self) -> bool {
  74. true
  75. }
  76. fn set_dirty(&mut self);
  77. fn kill(&mut self, _id: ComponentId, _context: &mut Context) {}
  78. fn set_id(&mut self, _id: ComponentId) {}
  79. fn id(&self) -> ComponentId;
  80. fn get_shortcuts(&self, _context: &Context) -> ShortcutMaps {
  81. Default::default()
  82. }
  83. fn get_status(&self, _context: &Context) -> Option<String> {
  84. None
  85. }
  86. }
  87. /*
  88. pub(crate) fn is_box_char(ch: char) -> bool {
  89. match ch {
  90. HORZ_BOUNDARY | VERT_BOUNDARY => true,
  91. _ => false,
  92. }
  93. }
  94. * pub(crate) fn is_box_char(ch: char) -> bool {
  95. * match ch {
  96. * '└' | '─' | '┘' | '┴' | '┌' | '│' | '├' | '┐' | '┬' | '┤' | '┼' | '╷' | '╵' | '╴' | '╶' => true,
  97. * _ => false,
  98. * }
  99. * }
  100. */
  101. fn bin_to_ch(b: u32) -> char {
  102. match b {
  103. 0b0001 => '╶',
  104. 0b0010 => '╵',
  105. 0b0011 => '└',
  106. 0b0100 => '╴',
  107. 0b0101 => '─',
  108. 0b0110 => '┘',
  109. 0b0111 => '┴',
  110. 0b1000 => '╷',
  111. 0b1001 => '┌',
  112. 0b1010 => '│',
  113. 0b1011 => '├',
  114. 0b1100 => '┐',
  115. 0b1101 => '┬',
  116. 0b1110 => '┤',
  117. 0b1111 => '┼',
  118. x => unreachable!(format!("unreachable bin_to_ch(x), x = {:b}", x)),
  119. }
  120. }
  121. fn ch_to_bin(ch: char) -> Option<u32> {
  122. match ch {
  123. '└' => Some(0b0011),
  124. '─' => Some(0b0101),
  125. '┘' => Some(0b0110),
  126. '┴' => Some(0b0111),
  127. '┌' => Some(0b1001),
  128. '│' => Some(0b1010),
  129. '├' => Some(0b1011),
  130. '┐' => Some(0b1100),
  131. '┬' => Some(0b1101),
  132. '┤' => Some(0b1110),
  133. '┼' => Some(0b1111),
  134. '╷' => Some(0b1000),
  135. '╵' => Some(0b0010),
  136. '╴' => Some(0b0100),
  137. '╶' => Some(0b0001),
  138. _ => None,
  139. }
  140. }
  141. #[allow(clippy::never_loop)]
  142. fn set_and_join_vert(grid: &mut CellBuffer, idx: Pos) -> u32 {
  143. let (x, y) = idx;
  144. let mut bin_set = 0b1010;
  145. /* Check left side
  146. *
  147. * 1
  148. * -> 2 │ 0
  149. * 3
  150. */
  151. loop {
  152. if x > 0 {
  153. if let Some(cell) = grid.get_mut(x - 1, y) {
  154. if let Some(adj) = ch_to_bin(cell.ch()) {
  155. if (adj & 0b0001) > 0 {
  156. bin_set |= 0b0100;
  157. break;
  158. } else if adj == 0b0100 {
  159. cell.set_ch(bin_to_ch(0b0101));
  160. cell.set_fg(Color::Byte(240));
  161. bin_set |= 0b0100;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. bin_set &= 0b1011;
  168. break;
  169. }
  170. /* Check right side
  171. *
  172. * 1
  173. * 2 │ 0 <-
  174. * 3
  175. */
  176. loop {
  177. if let Some(cell) = grid.get_mut(x + 1, y) {
  178. if let Some(adj) = ch_to_bin(cell.ch()) {
  179. if (adj & 0b0100) > 0 {
  180. bin_set |= 0b0001;
  181. break;
  182. }
  183. }
  184. }
  185. bin_set &= 0b1110;
  186. break;
  187. }
  188. /* Set upper side
  189. *
  190. * 1 <-
  191. * 2 │ 0
  192. * 3
  193. */
  194. loop {
  195. if y > 0 {
  196. if let Some(cell) = grid.get_mut(x, y - 1) {
  197. if let Some(adj) = ch_to_bin(cell.ch()) {
  198. cell.set_ch(bin_to_ch(adj | 0b1000));
  199. cell.set_fg(Color::Byte(240));
  200. } else {
  201. bin_set &= 0b1101;
  202. }
  203. }
  204. }
  205. break;
  206. }
  207. /* Set bottom side
  208. *
  209. * 1
  210. * 2 │ 0
  211. * 3 <-
  212. */
  213. loop {
  214. if let Some(cell) = grid.get_mut(x, y + 1) {
  215. if let Some(adj) = ch_to_bin(cell.ch()) {
  216. cell.set_ch(bin_to_ch(adj | 0b0010));
  217. cell.set_fg(Color::Byte(240));
  218. } else {
  219. bin_set &= 0b0111;
  220. }
  221. }
  222. break;
  223. }
  224. if bin_set == 0 {
  225. bin_set = 0b1010;
  226. }
  227. bin_set
  228. }
  229. #[allow(clippy::never_loop)]
  230. fn set_and_join_horz(grid: &mut CellBuffer, idx: Pos) -> u32 {
  231. let (x, y) = idx;
  232. let mut bin_set = 0b0101;
  233. /* Check 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. if (adj & 0b1000) > 0 {
  244. bin_set |= 0b0010;
  245. break;
  246. } else if adj == 0b0010 {
  247. bin_set |= 0b0010;
  248. cell.set_ch(bin_to_ch(0b1010));
  249. cell.set_fg(Color::Byte(240));
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. bin_set &= 0b1101;
  256. break;
  257. }
  258. /* Check bottom side
  259. *
  260. * 1
  261. * 2 ─ 0
  262. * 3 <-
  263. */
  264. loop {
  265. if let Some(cell) = grid.get_mut(x, y + 1) {
  266. if let Some(adj) = ch_to_bin(cell.ch()) {
  267. if (adj & 0b0010) > 0 {
  268. bin_set |= 0b1000;
  269. break;
  270. } else if adj == 0b1000 {
  271. bin_set |= 0b1000;
  272. cell.set_ch(bin_to_ch(0b1010));
  273. cell.set_fg(Color::Byte(240));
  274. break;
  275. }
  276. }
  277. }
  278. bin_set &= 0b0111;
  279. break;
  280. }
  281. /* Set left side
  282. *
  283. * 1
  284. * -> 2 ─ 0
  285. * 3
  286. */
  287. loop {
  288. if x > 0 {
  289. if let Some(cell) = grid.get_mut(x - 1, y) {
  290. if let Some(adj) = ch_to_bin(cell.ch()) {
  291. cell.set_ch(bin_to_ch(adj | 0b0001));
  292. cell.set_fg(Color::Byte(240));
  293. } else {
  294. bin_set &= 0b1011;
  295. }
  296. }
  297. }
  298. break;
  299. }
  300. /* Set right side
  301. *
  302. * 1
  303. * 2 ─ 0 <-
  304. * 3
  305. */
  306. loop {
  307. if let Some(cell) = grid.get_mut(x + 1, y) {
  308. if let Some(adj) = ch_to_bin(cell.ch()) {
  309. cell.set_ch(bin_to_ch(adj | 0b0100));
  310. cell.set_fg(Color::Byte(240));
  311. } else {
  312. bin_set &= 0b1110;
  313. }
  314. }
  315. break;
  316. }
  317. if bin_set == 0 {
  318. bin_set = 0b0101;
  319. }
  320. bin_set
  321. }
  322. pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) {
  323. /* Connected sides:
  324. *
  325. * 1
  326. * 2 c 0
  327. * 3
  328. *
  329. * #3210
  330. * 0b____
  331. */
  332. let bin_set = match ch {
  333. '│' => set_and_join_vert(grid, idx),
  334. '─' => set_and_join_horz(grid, idx),
  335. _ => unreachable!(),
  336. };
  337. grid[idx].set_ch(bin_to_ch(bin_set));
  338. grid[idx].set_fg(Color::Byte(240));
  339. }
  340. pub fn create_box(grid: &mut CellBuffer, area: Area) {
  341. if !is_valid_area!(area) {
  342. return;
  343. }
  344. let upper_left = upper_left!(area);
  345. let bottom_right = bottom_right!(area);
  346. for x in get_x(upper_left)..get_x(bottom_right) {
  347. grid[(x, get_y(upper_left))].set_ch(HORZ_BOUNDARY);
  348. grid[(x, get_y(bottom_right))].set_ch(HORZ_BOUNDARY);
  349. grid[(x, get_y(bottom_right))].set_fg(Color::Byte(240));
  350. }
  351. for y in get_y(upper_left)..get_y(bottom_right) {
  352. grid[(get_x(upper_left), y)].set_ch(VERT_BOUNDARY);
  353. grid[(get_x(bottom_right), y)].set_ch(VERT_BOUNDARY);
  354. grid[(get_x(bottom_right), y)].set_fg(Color::Byte(240));
  355. }
  356. set_and_join_box(grid, upper_left, HORZ_BOUNDARY);
  357. set_and_join_box(grid, set_x(upper_left, get_x(bottom_right)), HORZ_BOUNDARY);
  358. set_and_join_box(grid, set_y(upper_left, get_y(bottom_right)), VERT_BOUNDARY);
  359. set_and_join_box(grid, bottom_right, VERT_BOUNDARY);
  360. }