oxedyne/fe2o3/fe2o3_tui/src/lib_tui/cfg/line.rs
3.2 KiB, 3 runs
created by r1870400018:1153, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | use std::ops::{ |
| 4 | Index, |
| 5 | IndexMut, |
| 6 | }; |
| 7 | |
| 8 | |
| 9 | #[derive(Clone, Copy, Debug, Default)] |
| 10 | pub struct LineParts { |
| 11 | pub top_left: char, |
| 12 | pub top_right: char, |
| 13 | pub bot_right: char, |
| 14 | pub bot_left: char, |
| 15 | pub horiz: char, |
| 16 | pub vert: char, |
| 17 | } |
| 18 | |
| 19 | impl LineParts { |
| 20 | pub fn top_left(&self) -> String { self.top_left.to_string() } |
| 21 | pub fn top_right(&self) -> String { self.top_right.to_string() } |
| 22 | pub fn bot_right(&self) -> String { self.bot_right.to_string() } |
| 23 | pub fn bot_left(&self) -> String { self.bot_left.to_string() } |
| 24 | pub fn horiz(&self) -> String { self.horiz.to_string() } |
| 25 | pub fn vert(&self) -> String { self.vert.to_string() } |
| 26 | } |
| 27 | |
| 28 | #[derive(Clone, Copy, Debug)] |
| 29 | #[repr(usize)] |
| 30 | pub enum LineType { |
| 31 | Blank, |
| 32 | SingleSharp, |
| 33 | DoubleSharp, |
| 34 | SingleRounded, |
| 35 | ThickSingleSharp, |
| 36 | } |
| 37 | |
| 38 | new_enum!(LineType; |
| 39 | Blank, |
| 40 | SingleSharp, |
| 41 | DoubleSharp, |
| 42 | SingleRounded, |
| 43 | ThickSingleSharp, |
| 44 | ); |
| 45 | |
| 46 | #[derive(Clone, Debug)] |
| 47 | pub struct LineLibrary { |
| 48 | pub lines: [LineParts; LineType::num_of_variants()], |
| 49 | } |
| 50 | |
| 51 | impl Default for LineLibrary { |
| 52 | fn default() -> Self { |
| 53 | let mut lines = [LineParts::default(); LineType::num_of_variants()]; |
| 54 | for (i, typ) in LineType::variants().iter().enumerate() { |
| 55 | lines[i] = match typ { |
| 56 | LineType::Blank => LineParts { |
| 57 | top_left: ' ', |
| 58 | top_right: ' ', |
| 59 | bot_right: ' ', |
| 60 | bot_left: ' ', |
| 61 | horiz: ' ', |
| 62 | vert: ' ', |
| 63 | }, |
| 64 | LineType::SingleSharp => LineParts { |
| 65 | top_left: '┌', |
| 66 | top_right: '┐', |
| 67 | bot_right: '┘', |
| 68 | bot_left: '└', |
| 69 | horiz: '─', |
| 70 | vert: '│', |
| 71 | }, |
| 72 | LineType::ThickSingleSharp => LineParts { |
| 73 | top_left: '┏', |
| 74 | top_right: '┓', |
| 75 | bot_right: '┛', |
| 76 | bot_left: '┗', |
| 77 | horiz: '━', |
| 78 | vert: '┃', |
| 79 | }, |
| 80 | LineType::DoubleSharp => LineParts { |
| 81 | top_left: '╔', |
| 82 | top_right: '╗', |
| 83 | bot_right: '╝', |
| 84 | bot_left: '╚', |
| 85 | horiz: '═', |
| 86 | vert: '║', |
| 87 | }, |
| 88 | LineType::SingleRounded => LineParts { |
| 89 | top_left: '╭', |
| 90 | top_right: '╮', |
| 91 | bot_right: '╯', |
| 92 | bot_left: '╰', |
| 93 | horiz: '─', |
| 94 | vert: '│', |
| 95 | }, |
| 96 | }; |
| 97 | } |
| 98 | Self { lines } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl Index<LineType> for LineLibrary { |
| 103 | type Output = LineParts; |
| 104 | |
| 105 | fn index(&self, typ: LineType) -> &Self::Output { |
| 106 | &self.lines[typ as usize] |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl IndexMut<LineType> for LineLibrary { |
| 111 | fn index_mut(&mut self, typ: LineType) -> &mut Self::Output { |
| 112 | &mut self.lines[typ as usize] |
| 113 | } |
| 114 | } |
| 115 |