1use {
2 owo_colors::*,
3 std::{fmt, io},
4};
5
6pub struct Theme {
14 pub bare_style: Style,
16
17 pub number_style: Style,
19
20 pub string_style: Style,
22
23 pub name_style: Style,
25
26 pub meta_style: Style,
28
29 pub error_style: Style,
31
32 pub heading_style: Style,
34
35 pub delimiter_style: Style,
37}
38
39impl Theme {
40 pub fn plain() -> Self {
42 Self {
43 bare_style: Style::new(),
44 number_style: Style::new(),
45 string_style: Style::new(),
46 name_style: Style::new(),
47 meta_style: Style::new(),
48 error_style: Style::new(),
49 heading_style: Style::new(),
50 delimiter_style: Style::new(),
51 }
52 }
53
54 pub fn bare<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
56 self.bare_style.style(thing)
57 }
58
59 pub fn number<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
61 self.number_style.style(thing)
62 }
63
64 pub fn string<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
66 self.string_style.style(thing)
67 }
68
69 pub fn name<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
71 self.name_style.style(thing)
72 }
73
74 pub fn meta<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
76 self.meta_style.style(thing)
77 }
78
79 pub fn error<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
81 self.error_style.style(thing)
82 }
83
84 pub fn heading<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
86 self.heading_style.style(thing)
87 }
88
89 pub fn delimiter<ThingT>(&self, thing: ThingT) -> Styled<ThingT> {
91 self.delimiter_style.style(thing)
92 }
93
94 pub fn write_bare<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
96 where
97 WriteT: io::Write,
98 ThingT: fmt::Display,
99 {
100 write!(writer, "{}", self.bare(thing))
101 }
102
103 pub fn write_number<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
105 where
106 WriteT: io::Write,
107 ThingT: fmt::Display,
108 {
109 write!(writer, "{}", self.number(thing))
110 }
111
112 pub fn write_string<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
114 where
115 WriteT: io::Write,
116 ThingT: fmt::Display,
117 {
118 write!(writer, "{}", self.string(thing))
119 }
120
121 pub fn write_name<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
123 where
124 WriteT: io::Write,
125 ThingT: fmt::Display,
126 {
127 write!(writer, "{}", self.name(thing))
128 }
129
130 pub fn write_meta<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
132 where
133 WriteT: io::Write,
134 ThingT: fmt::Display,
135 {
136 write!(writer, "{}", self.meta(thing))
137 }
138
139 pub fn write_error<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
141 where
142 WriteT: io::Write,
143 ThingT: fmt::Display,
144 {
145 write!(writer, "{}", self.error(thing))
146 }
147
148 pub fn write_heading<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
150 where
151 WriteT: io::Write,
152 ThingT: fmt::Display,
153 {
154 write!(writer, "{}", self.heading(thing))
155 }
156
157 pub fn write_delimiter<WriteT, ThingT>(&self, writer: &mut WriteT, thing: ThingT) -> io::Result<()>
159 where
160 WriteT: io::Write,
161 ThingT: fmt::Display,
162 {
163 write!(writer, "{}", self.delimiter(thing))
164 }
165}
166
167impl Default for Theme {
168 fn default() -> Self {
169 Self {
170 bare_style: Style::new().yellow(),
171 number_style: Style::new().magenta(),
172 string_style: Style::new().cyan(),
173 name_style: Style::new().green(),
174 meta_style: Style::new().blue().italic(),
175 error_style: Style::new().red().bold(),
176 heading_style: Style::new().green().bold().underline(),
177 delimiter_style: Style::new().dimmed(),
178 }
179 }
180}