Skip to content

Commit 192598d

Browse files
BioTomateDEspenserblack
andauthored
Clean up Colorize trait
Co-authored-by: Spenser Black <spenserblack01@gmail.com>
1 parent ec013ae commit 192598d

3 files changed

Lines changed: 63 additions & 186 deletions

File tree

src/color.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use Color::{
33
AnsiColor, Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta,
44
BrightRed, BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, TrueColor, White, Yellow,
55
};
6+
67
/// The 8 standard colors.
78
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89
#[allow(missing_docs)]
@@ -136,7 +137,7 @@ impl Color {
136137
}
137138
}
138139

139-
fn into_truecolor(self) -> Self {
140+
const fn into_truecolor(self) -> Self {
140141
match self {
141142
Black => TrueColor { r: 0, g: 0, b: 0 },
142143
Red => TrueColor { r: 205, g: 0, b: 0 },

src/lib.rs

Lines changed: 57 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,14 @@ pub use style::{Style, Styles};
124124
pub struct ColoredString {
125125
/// The plain text that will have color and style applied to it.
126126
pub input: String,
127+
127128
/// The color of the text as it will be printed.
128129
pub fgcolor: Option<Color>,
130+
129131
/// The background color (if any). None means that the text will be printed
130132
/// without a special background.
131133
pub bgcolor: Option<Color>,
134+
132135
/// Any special styling to be applied to the text (see Styles for a list of
133136
/// available options).
134137
pub style: style::Style,
@@ -139,127 +142,66 @@ pub struct ColoredString {
139142
/// You can use `colored` effectively simply by importing this trait
140143
/// and then using its methods on `String` and `&str`.
141144
#[allow(missing_docs)]
142-
pub trait Colorize {
145+
pub trait Colorize: Sized {
143146
// Font Colors
144-
fn black(self) -> ColoredString
145-
where
146-
Self: Sized,
147-
{
147+
fn black(self) -> ColoredString {
148148
self.color(Color::Black)
149149
}
150-
fn red(self) -> ColoredString
151-
where
152-
Self: Sized,
153-
{
150+
fn red(self) -> ColoredString {
154151
self.color(Color::Red)
155152
}
156-
fn green(self) -> ColoredString
157-
where
158-
Self: Sized,
159-
{
153+
fn green(self) -> ColoredString {
160154
self.color(Color::Green)
161155
}
162-
fn yellow(self) -> ColoredString
163-
where
164-
Self: Sized,
165-
{
156+
fn yellow(self) -> ColoredString {
166157
self.color(Color::Yellow)
167158
}
168-
fn blue(self) -> ColoredString
169-
where
170-
Self: Sized,
171-
{
159+
fn blue(self) -> ColoredString {
172160
self.color(Color::Blue)
173161
}
174-
fn magenta(self) -> ColoredString
175-
where
176-
Self: Sized,
177-
{
162+
fn magenta(self) -> ColoredString {
178163
self.color(Color::Magenta)
179164
}
180-
fn purple(self) -> ColoredString
181-
where
182-
Self: Sized,
183-
{
165+
fn purple(self) -> ColoredString {
184166
self.color(Color::Magenta)
185167
}
186-
fn cyan(self) -> ColoredString
187-
where
188-
Self: Sized,
189-
{
168+
fn cyan(self) -> ColoredString {
190169
self.color(Color::Cyan)
191170
}
192-
fn white(self) -> ColoredString
193-
where
194-
Self: Sized,
195-
{
171+
fn white(self) -> ColoredString {
196172
self.color(Color::White)
197173
}
198-
fn bright_black(self) -> ColoredString
199-
where
200-
Self: Sized,
201-
{
174+
fn bright_black(self) -> ColoredString {
202175
self.color(Color::BrightBlack)
203176
}
204-
fn bright_red(self) -> ColoredString
205-
where
206-
Self: Sized,
207-
{
177+
fn bright_red(self) -> ColoredString {
208178
self.color(Color::BrightRed)
209179
}
210-
fn bright_green(self) -> ColoredString
211-
where
212-
Self: Sized,
213-
{
180+
fn bright_green(self) -> ColoredString {
214181
self.color(Color::BrightGreen)
215182
}
216-
fn bright_yellow(self) -> ColoredString
217-
where
218-
Self: Sized,
219-
{
183+
fn bright_yellow(self) -> ColoredString {
220184
self.color(Color::BrightYellow)
221185
}
222-
fn bright_blue(self) -> ColoredString
223-
where
224-
Self: Sized,
225-
{
186+
fn bright_blue(self) -> ColoredString {
226187
self.color(Color::BrightBlue)
227188
}
228-
fn bright_magenta(self) -> ColoredString
229-
where
230-
Self: Sized,
231-
{
189+
fn bright_magenta(self) -> ColoredString {
232190
self.color(Color::BrightMagenta)
233191
}
234-
fn bright_purple(self) -> ColoredString
235-
where
236-
Self: Sized,
237-
{
192+
fn bright_purple(self) -> ColoredString {
238193
self.color(Color::BrightMagenta)
239194
}
240-
fn bright_cyan(self) -> ColoredString
241-
where
242-
Self: Sized,
243-
{
195+
fn bright_cyan(self) -> ColoredString {
244196
self.color(Color::BrightCyan)
245197
}
246-
fn bright_white(self) -> ColoredString
247-
where
248-
Self: Sized,
249-
{
198+
fn bright_white(self) -> ColoredString {
250199
self.color(Color::BrightWhite)
251200
}
252-
fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString
253-
where
254-
Self: Sized,
255-
{
201+
fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString {
256202
self.color(Color::TrueColor { r, g, b })
257203
}
258-
fn custom_color<T>(self, color: T) -> ColoredString
259-
where
260-
Self: Sized,
261-
T: Into<CustomColor>,
262-
{
204+
fn custom_color<C: Into<CustomColor>>(self, color: C) -> ColoredString {
263205
let color = color.into();
264206

265207
self.color(Color::TrueColor {
@@ -268,134 +210,70 @@ pub trait Colorize {
268210
b: color.b,
269211
})
270212
}
271-
fn ansi_color<T>(self, color: T) -> ColoredString
272-
where
273-
Self: Sized,
274-
T: Into<u8>,
275-
{
213+
fn ansi_color<T: Into<u8>>(self, color: T) -> ColoredString {
276214
self.color(Color::AnsiColor(color.into()))
277215
}
278-
fn color<S: Into<Color>>(self, color: S) -> ColoredString;
216+
fn color<C: Into<Color>>(self, color: C) -> ColoredString;
217+
279218
// Background Colors
280-
fn on_black(self) -> ColoredString
281-
where
282-
Self: Sized,
283-
{
219+
fn on_black(self) -> ColoredString {
284220
self.on_color(Color::Black)
285221
}
286-
fn on_red(self) -> ColoredString
287-
where
288-
Self: Sized,
289-
{
222+
fn on_red(self) -> ColoredString {
290223
self.on_color(Color::Red)
291224
}
292-
fn on_green(self) -> ColoredString
293-
where
294-
Self: Sized,
295-
{
225+
fn on_green(self) -> ColoredString {
296226
self.on_color(Color::Green)
297227
}
298-
fn on_yellow(self) -> ColoredString
299-
where
300-
Self: Sized,
301-
{
228+
fn on_yellow(self) -> ColoredString {
302229
self.on_color(Color::Yellow)
303230
}
304-
fn on_blue(self) -> ColoredString
305-
where
306-
Self: Sized,
307-
{
231+
fn on_blue(self) -> ColoredString {
308232
self.on_color(Color::Blue)
309233
}
310-
fn on_magenta(self) -> ColoredString
311-
where
312-
Self: Sized,
313-
{
234+
fn on_magenta(self) -> ColoredString {
314235
self.on_color(Color::Magenta)
315236
}
316-
fn on_purple(self) -> ColoredString
317-
where
318-
Self: Sized,
319-
{
237+
fn on_purple(self) -> ColoredString {
320238
self.on_color(Color::Magenta)
321239
}
322-
fn on_cyan(self) -> ColoredString
323-
where
324-
Self: Sized,
325-
{
240+
fn on_cyan(self) -> ColoredString {
326241
self.on_color(Color::Cyan)
327242
}
328-
fn on_white(self) -> ColoredString
329-
where
330-
Self: Sized,
331-
{
243+
fn on_white(self) -> ColoredString {
332244
self.on_color(Color::White)
333245
}
334-
fn on_bright_black(self) -> ColoredString
335-
where
336-
Self: Sized,
337-
{
246+
fn on_bright_black(self) -> ColoredString {
338247
self.on_color(Color::BrightBlack)
339248
}
340-
fn on_bright_red(self) -> ColoredString
341-
where
342-
Self: Sized,
343-
{
249+
fn on_bright_red(self) -> ColoredString {
344250
self.on_color(Color::BrightRed)
345251
}
346-
fn on_bright_green(self) -> ColoredString
347-
where
348-
Self: Sized,
349-
{
252+
fn on_bright_green(self) -> ColoredString {
350253
self.on_color(Color::BrightGreen)
351254
}
352-
fn on_bright_yellow(self) -> ColoredString
353-
where
354-
Self: Sized,
355-
{
255+
fn on_bright_yellow(self) -> ColoredString {
356256
self.on_color(Color::BrightYellow)
357257
}
358-
fn on_bright_blue(self) -> ColoredString
359-
where
360-
Self: Sized,
361-
{
258+
fn on_bright_blue(self) -> ColoredString {
362259
self.on_color(Color::BrightBlue)
363260
}
364-
fn on_bright_magenta(self) -> ColoredString
365-
where
366-
Self: Sized,
367-
{
261+
fn on_bright_magenta(self) -> ColoredString {
368262
self.on_color(Color::BrightMagenta)
369263
}
370-
fn on_bright_purple(self) -> ColoredString
371-
where
372-
Self: Sized,
373-
{
264+
fn on_bright_purple(self) -> ColoredString {
374265
self.on_color(Color::BrightMagenta)
375266
}
376-
fn on_bright_cyan(self) -> ColoredString
377-
where
378-
Self: Sized,
379-
{
267+
fn on_bright_cyan(self) -> ColoredString {
380268
self.on_color(Color::BrightCyan)
381269
}
382-
fn on_bright_white(self) -> ColoredString
383-
where
384-
Self: Sized,
385-
{
270+
fn on_bright_white(self) -> ColoredString {
386271
self.on_color(Color::BrightWhite)
387272
}
388-
fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString
389-
where
390-
Self: Sized,
391-
{
273+
fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString {
392274
self.on_color(Color::TrueColor { r, g, b })
393275
}
394-
fn on_custom_color<T>(self, color: T) -> ColoredString
395-
where
396-
Self: Sized,
397-
T: Into<CustomColor>,
398-
{
276+
fn on_custom_color<C: Into<CustomColor>>(self, color: C) -> ColoredString {
399277
let color = color.into();
400278

401279
self.on_color(Color::TrueColor {
@@ -404,14 +282,11 @@ pub trait Colorize {
404282
b: color.b,
405283
})
406284
}
407-
fn on_ansi_color<T>(self, color: T) -> ColoredString
408-
where
409-
Self: Sized,
410-
T: Into<u8>,
411-
{
285+
fn on_ansi_color<T: Into<u8>>(self, color: T) -> ColoredString {
412286
self.on_color(Color::AnsiColor(color.into()))
413287
}
414-
fn on_color<S: Into<Color>>(self, color: S) -> ColoredString;
288+
fn on_color<C: Into<Color>>(self, color: C) -> ColoredString;
289+
415290
// Styles
416291
fn clear(self) -> ColoredString;
417292
fn normal(self) -> ColoredString;
@@ -510,7 +385,7 @@ impl ColoredString {
510385
}
511386

512387
#[cfg(feature = "no-color")]
513-
fn has_colors() -> bool {
388+
const fn has_colors() -> bool {
514389
false
515390
}
516391

@@ -520,24 +395,24 @@ impl ColoredString {
520395
}
521396

522397
let mut res = String::from("\x1B[");
523-
let mut has_wrote = if self.style == style::CLEAR {
398+
let mut has_written = if self.style == style::CLEAR {
524399
false
525400
} else {
526401
res.push_str(&self.style.to_str());
527402
true
528403
};
529404

530-
if let Some(ref bgcolor) = self.bgcolor {
531-
if has_wrote {
405+
if let Some(bgcolor) = &self.bgcolor {
406+
if has_written {
532407
res.push(';');
533408
}
534409

535410
res.push_str(&bgcolor.to_bg_str());
536-
has_wrote = true;
411+
has_written = true;
537412
}
538413

539-
if let Some(ref fgcolor) = self.fgcolor {
540-
if has_wrote {
414+
if let Some(fgcolor) = &self.fgcolor {
415+
if has_written {
541416
res.push(';');
542417
}
543418

0 commit comments

Comments
 (0)