-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcharacter.rs
136 lines (118 loc) · 3.34 KB
/
character.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::fmt::{Debug, Display, Formatter};
use ratatui::style::Color;
pub(crate) const CHARACTER_NULL: char = '0';
pub(crate) const CHARACTER_WHITESPACE: char = '_';
pub(crate) const CHARACTER_CONTROL: char = '⍾';
pub(crate) const CHARACTER_FILL: char = '•';
pub(crate) const CHARACTER_UNKNOWN: char = '�';
const COLOR_NULL: Color = Color::DarkGray;
const COLOR_ASCII: Color = Color::Cyan;
const COLOR_UNICODE: Color = Color::LightCyan;
const COLOR_WHITESPACE: Color = Color::Green;
const COLOR_CONTROL: Color = Color::Magenta;
const COLOR_FILL: Color = Color::LightCyan;
const COLOR_UNKNOWN: Color = Color::Yellow;
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Type {
Ascii,
Unicode(usize),
Unknown,
}
impl Type {
pub(crate) fn size(&self) -> usize {
match self {
Type::Ascii | Type::Unknown => 1,
Type::Unicode(size) => *size,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Category {
Null,
Ascii,
Unicode,
Whitespace,
Control,
Fill,
Unknown,
}
impl From<&char> for Category {
fn from(character: &char) -> Self {
if character == &'\0' {
Category::Null
} else if character.is_whitespace() {
Category::Whitespace
} else if character.is_control() {
Category::Control
} else if character.is_ascii() {
Category::Ascii
} else {
Category::Unicode
}
}
}
impl Category {
pub(crate) fn escape(&self, character: char) -> char {
match self {
Category::Null => CHARACTER_NULL,
Category::Ascii | Category::Unicode => character,
Category::Whitespace if character == ' ' => ' ',
Category::Whitespace => CHARACTER_WHITESPACE,
Category::Control => CHARACTER_CONTROL,
Category::Fill => CHARACTER_FILL,
Category::Unknown => CHARACTER_UNKNOWN,
}
}
pub(crate) fn color(&self) -> &'static Color {
match self {
Category::Null => &COLOR_NULL,
Category::Ascii => &COLOR_ASCII,
Category::Unicode => &COLOR_UNICODE,
Category::Whitespace => &COLOR_WHITESPACE,
Category::Control => &COLOR_CONTROL,
Category::Fill => &COLOR_FILL,
Category::Unknown => &COLOR_UNKNOWN,
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct RichChar {
character: char,
category: Category,
}
impl RichChar {
pub(crate) fn new(character: char, category: Category) -> Self {
Self { character, category }
}
pub(crate) fn escape(&self) -> char {
self.category.escape(self.character)
}
pub(crate) fn color(&self) -> &'static Color {
self.category.color()
}
}
impl Display for RichChar {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.character, f)
}
}
impl From<RichChar> for char {
fn from(rich_char: RichChar) -> Self {
rich_char.character
}
}
impl From<&RichChar> for char {
fn from(rich_char: &RichChar) -> Self {
rich_char.character
}
}
impl From<RichChar> for String {
fn from(rich_char: RichChar) -> Self {
rich_char.character.to_string()
}
}
impl From<&RichChar> for String {
fn from(rich_char: &RichChar) -> Self {
rich_char.character.to_string()
}
}