1use {
2 owo_colors::*,
3 std::{fmt, io},
4};
5
6#[derive(Clone, Debug)]
14pub struct Theme {
15 pub symbol_style: Style,
17
18 pub number_style: Style,
20
21 pub string_style: Style,
23
24 pub name_style: Style,
26
27 pub meta_style: Style,
29
30 pub error_style: Style,
32
33 pub heading_style: Style,
35
36 pub delimiter_style: Style,
38}
39
40impl Theme {
41 pub fn plain() -> Self {
43 Self {
44 symbol_style: Style::new(),
45 number_style: Style::new(),
46 string_style: Style::new(),
47 name_style: Style::new(),
48 meta_style: Style::new(),
49 error_style: Style::new(),
50 heading_style: Style::new(),
51 delimiter_style: Style::new(),
52 }
53 }
54
55 pub fn symbol<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
57 self.symbol_style.style(thing)
58 }
59
60 pub fn number<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
62 self.number_style.style(thing)
63 }
64
65 pub fn string<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
67 self.string_style.style(thing)
68 }
69
70 pub fn name<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
72 self.name_style.style(thing)
73 }
74
75 pub fn meta<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
77 self.meta_style.style(thing)
78 }
79
80 pub fn error<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
82 self.error_style.style(thing)
83 }
84
85 pub fn heading<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
87 self.heading_style.style(thing)
88 }
89
90 pub fn delimiter<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
92 self.delimiter_style.style(thing)
93 }
94
95 pub fn write_symbol<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
97 where
98 WriteT: io::Write,
99 ThingT: fmt::Display,
100 {
101 write!(writer, "{}", self.symbol(thing))
102 }
103
104 pub fn write_number<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
106 where
107 WriteT: io::Write,
108 ThingT: fmt::Display,
109 {
110 write!(writer, "{}", self.number(thing))
111 }
112
113 pub fn write_string<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
115 where
116 WriteT: io::Write,
117 ThingT: fmt::Display,
118 {
119 write!(writer, "{}", self.string(thing))
120 }
121
122 pub fn write_name<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
124 where
125 WriteT: io::Write,
126 ThingT: fmt::Display,
127 {
128 write!(writer, "{}", self.name(thing))
129 }
130
131 pub fn write_meta<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
133 where
134 WriteT: io::Write,
135 ThingT: fmt::Display,
136 {
137 write!(writer, "{}", self.meta(thing))
138 }
139
140 pub fn write_error<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
142 where
143 WriteT: io::Write,
144 ThingT: fmt::Display,
145 {
146 write!(writer, "{}", self.error(thing))
147 }
148
149 pub fn write_heading<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
151 where
152 WriteT: io::Write,
153 ThingT: fmt::Display,
154 {
155 write!(writer, "{}", self.heading(thing))
156 }
157
158 pub fn write_delimiter<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
160 where
161 WriteT: io::Write,
162 ThingT: fmt::Display,
163 {
164 write!(writer, "{}", self.delimiter(thing))
165 }
166}
167
168impl Default for Theme {
169 fn default() -> Self {
170 Self {
171 symbol_style: Style::new().yellow(),
172 number_style: Style::new().magenta(),
173 string_style: Style::new().cyan(),
174 name_style: Style::new().green(),
175 meta_style: Style::new().blue().italic(),
176 error_style: Style::new().red().bold(),
177 heading_style: Style::new().green().bold().underline(),
178 delimiter_style: Style::new().dimmed(),
179 }
180 }
181}