-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy paththeme.rs
805 lines (734 loc) · 25.7 KB
/
theme.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
use config::{Config, File as ConfigFile, FileFormat};
use lazy_static::lazy_static;
use log;
use tracing;
use tracing::instrument;
use palette::named;
use serde::{Deserialize, Serialize};
use serde_json;
use std::collections::HashMap;
use std::error;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use strum_macros;
static DEFAULT_MAX_DEPTH: u8 = 10;
// Collection of settable "meanings" that can have colors set.
// NOTE: You can add a new meaning here without breaking backwards compatibility but please:
// - update the atuin/docs repository, which has a list of available meanings
// - add a fallback in the MEANING_FALLBACKS below, so that themes which do not have it
// get a sensible fallback (see Title as an example)
#[derive(
Serialize, Deserialize, Copy, Clone, Hash, Debug, Eq, PartialEq, strum_macros::Display,
)]
#[strum(serialize_all = "camel_case")]
pub enum Meaning {
AlertInfo,
AlertWarn,
AlertError,
Annotation,
Base,
Guidance,
Important,
Title,
Muted,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ThemeConfig {
// Definition of the theme
pub theme: ThemeDefinitionConfigBlock,
// Colors
pub colors: HashMap<Meaning, String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ThemeDefinitionConfigBlock {
/// Name of theme ("default" for base)
pub name: String,
/// Whether any theme should be treated as a parent _if available_
pub parent: Option<String>,
}
use crossterm::style::{Color, ContentStyle};
// For now, a theme is loaded as a mapping of meanings to colors, but it may be desirable to
// expand that in the future to general styles, so we populate a Meaning->ContentStyle hashmap.
#[derive(Debug)]
pub struct Theme {
pub name: String,
pub parent: Option<String>,
pub styles: HashMap<Meaning, ContentStyle>,
}
// Themes have a number of convenience functions for the most commonly used meanings.
// The general purpose `as_style` routine gives back a style, but for ease-of-use and to keep
// theme-related boilerplate minimal, the convenience functions give a color.
impl Theme {
// This is the base "default" color, for general text
pub fn get_base(&self) -> ContentStyle {
self.styles[&Meaning::Base]
}
pub fn get_info(&self) -> ContentStyle {
self.get_alert(log::Level::Info)
}
pub fn get_warning(&self) -> ContentStyle {
self.get_alert(log::Level::Warn)
}
pub fn get_error(&self) -> ContentStyle {
self.get_alert(log::Level::Error)
}
// The alert meanings may be chosen by the Level enum, rather than the methods above
// or the full Meaning enum, to simplify programmatic selection of a log-level.
pub fn get_alert(&self, severity: log::Level) -> ContentStyle {
self.styles[ALERT_TYPES.get(&severity).unwrap()]
}
pub fn new(
name: String,
parent: Option<String>,
styles: HashMap<Meaning, ContentStyle>,
) -> Theme {
Theme {
name,
parent,
styles,
}
}
pub fn closest_meaning<'a>(&self, meaning: &'a Meaning) -> &'a Meaning {
if self.styles.contains_key(meaning) {
meaning
} else if MEANING_FALLBACKS.contains_key(meaning) {
self.closest_meaning(&MEANING_FALLBACKS[meaning])
} else {
&Meaning::Base
}
}
// General access - if you have a meaning, this will give you a (crossterm) style
pub fn as_style(&self, meaning: Meaning) -> ContentStyle {
self.styles[self.closest_meaning(&meaning)]
}
// Turns a map of meanings to colornames into a theme
// If theme-debug is on, then we will print any colornames that we cannot load,
// but we do not have this on in general, as it could print unfiltered text to the terminal
// from a theme TOML file. However, it will always return a theme, falling back to
// defaults on error, so that a TOML file does not break loading
#[instrument(skip(debug))]
pub fn from_foreground_colors(
name: String,
parent: Option<&Theme>,
foreground_colors: HashMap<Meaning, String>,
debug: bool,
) -> Theme {
let styles: HashMap<Meaning, ContentStyle> = foreground_colors
.iter()
.map(|(name, color)| {
(
*name,
StyleFactory::from_fg_string(color).unwrap_or_else(|err| {
if debug {
tracing::warn!(
"Tried to load string as a color unsuccessfully: ({}={}) {}",
name,
color,
err
);
}
ContentStyle::default()
}),
)
})
.collect();
Theme::from_map(name, parent, &styles)
}
// Boil down a meaning-color hashmap into a theme, by taking the defaults
// for any unknown colors
fn from_map(
name: String,
parent: Option<&Theme>,
overrides: &HashMap<Meaning, ContentStyle>,
) -> Theme {
let styles = match parent {
Some(theme) => Box::new(theme.styles.clone()),
None => Box::new(DEFAULT_THEME.styles.clone()),
}
.iter()
.map(|(name, color)| match overrides.get(name) {
Some(value) => (*name, *value),
None => (*name, *color),
})
.collect();
Theme::new(name, parent.map(|p| p.name.clone()), styles)
}
}
// Use palette to get a color from a string name, if possible
fn from_string(name: &str) -> Result<Color, String> {
if name.is_empty() {
return Err("Empty string".into());
}
let first_char = name.chars().next().unwrap();
match first_char {
'#' => {
let hexcode = &name[1..];
let vec: Vec<u8> = hexcode
.chars()
.collect::<Vec<char>>()
.chunks(2)
.map(|pair| u8::from_str_radix(pair.iter().collect::<String>().as_str(), 16))
.filter_map(|n| n.ok())
.collect();
if vec.len() != 3 {
return Err("Could not parse 3 hex values from string".into());
}
Ok(Color::Rgb {
r: vec[0],
g: vec[1],
b: vec[2],
})
}
'@' => {
// For full flexibility, we need to use serde_json, given
// crossterm's approach.
serde_json::from_str::<Color>(format!("\"{}\"", &name[1..]).as_str())
.map_err(|_| format!("Could not convert color name {} to Crossterm color", name))
}
_ => {
let srgb = named::from_str(name).ok_or("No such color in palette")?;
Ok(Color::Rgb {
r: srgb.red,
g: srgb.green,
b: srgb.blue,
})
}
}
}
pub struct StyleFactory {}
impl StyleFactory {
fn from_fg_string(name: &str) -> Result<ContentStyle, String> {
match from_string(name) {
Ok(color) => Ok(Self::from_fg_color(color)),
Err(err) => Err(err),
}
}
// For succinctness, if we are confident that the name will be known,
// this routine is available to keep the code readable
fn known_fg_string(name: &str) -> ContentStyle {
Self::from_fg_string(name).unwrap()
}
fn from_fg_color(color: Color) -> ContentStyle {
ContentStyle {
foreground_color: Some(color),
..ContentStyle::default()
}
}
}
// Built-in themes. Rather than having extra files added before any theming
// is available, this gives a couple of basic options, demonstrating the use
// of themes: autumn and marine
lazy_static! {
static ref ALERT_TYPES: HashMap<log::Level, Meaning> = {
HashMap::from([
(log::Level::Info, Meaning::AlertInfo),
(log::Level::Warn, Meaning::AlertWarn),
(log::Level::Error, Meaning::AlertError),
])
};
static ref MEANING_FALLBACKS: HashMap<Meaning, Meaning> = {
HashMap::from([
(Meaning::Guidance, Meaning::AlertInfo),
(Meaning::Annotation, Meaning::AlertInfo),
(Meaning::Title, Meaning::Important),
])
};
static ref DEFAULT_THEME: Theme = {
Theme::new(
"default".to_string(),
None,
HashMap::from([
(
Meaning::AlertError,
StyleFactory::from_fg_color(Color::DarkRed),
),
(
Meaning::AlertWarn,
StyleFactory::from_fg_color(Color::DarkYellow),
),
(
Meaning::AlertInfo,
StyleFactory::from_fg_color(Color::DarkGreen),
),
(
Meaning::Annotation,
StyleFactory::from_fg_color(Color::DarkGrey),
),
(
Meaning::Guidance,
StyleFactory::from_fg_color(Color::DarkBlue),
),
(
Meaning::Important,
StyleFactory::from_fg_color(Color::White),
),
(Meaning::Muted, StyleFactory::from_fg_color(Color::Grey)),
(Meaning::Base, ContentStyle::default()),
]),
)
};
static ref BUILTIN_THEMES: HashMap<&'static str, Theme> = {
HashMap::from([
("default", HashMap::new()),
(
"autumn",
HashMap::from([
(
Meaning::AlertError,
StyleFactory::known_fg_string("saddlebrown"),
),
(
Meaning::AlertWarn,
StyleFactory::known_fg_string("darkorange"),
),
(Meaning::AlertInfo, StyleFactory::known_fg_string("gold")),
(
Meaning::Annotation,
StyleFactory::from_fg_color(Color::DarkGrey),
),
(Meaning::Guidance, StyleFactory::known_fg_string("brown")),
]),
),
(
"marine",
HashMap::from([
(
Meaning::AlertError,
StyleFactory::known_fg_string("yellowgreen"),
),
(Meaning::AlertWarn, StyleFactory::known_fg_string("cyan")),
(
Meaning::AlertInfo,
StyleFactory::known_fg_string("turquoise"),
),
(
Meaning::Annotation,
StyleFactory::known_fg_string("steelblue"),
),
(
Meaning::Base,
StyleFactory::known_fg_string("lightsteelblue"),
),
(Meaning::Guidance, StyleFactory::known_fg_string("teal")),
]),
),
])
.iter()
.map(|(name, theme)| (*name, Theme::from_map(name.to_string(), None, theme)))
.collect()
};
}
// To avoid themes being repeatedly loaded, we store them in a theme manager
#[derive(Debug)]
pub struct ThemeManager {
loaded_themes: HashMap<String, Theme>,
debug: bool,
override_theme_dir: Option<String>,
}
// Theme-loading logic
impl ThemeManager {
pub fn new(debug: Option<bool>, theme_dir: Option<String>) -> Self {
Self {
loaded_themes: HashMap::new(),
debug: debug.unwrap_or(false),
override_theme_dir: match theme_dir {
Some(theme_dir) => Some(theme_dir),
None => std::env::var("ATUIN_THEME_DIR").ok(),
},
}
}
// Try to load a theme from a `{name}.toml` file in the theme directory. If an override is set
// for the theme dir (via ATUIN_THEME_DIR env) we should load the theme from there
pub fn load_theme_from_file(
&mut self,
name: &str,
max_depth: u8,
) -> Result<&Theme, Box<dyn error::Error>> {
let mut theme_file = if let Some(p) = &self.override_theme_dir {
if p.is_empty() {
return Err(Box::new(Error::new(
ErrorKind::NotFound,
"Empty theme directory override and could not find theme elsewhere",
)));
}
PathBuf::from(p)
} else {
let config_dir = atuin_common::utils::config_dir();
let mut theme_file = PathBuf::new();
theme_file.push(config_dir);
theme_file.push("themes");
theme_file
};
let theme_toml = format!["{}.toml", name];
theme_file.push(theme_toml);
let mut config_builder = Config::builder();
config_builder = config_builder.add_source(ConfigFile::new(
theme_file.to_str().unwrap(),
FileFormat::Toml,
));
let config = config_builder.build()?;
self.load_theme_from_config(name, config, max_depth)
}
pub fn load_theme_from_config(
&mut self,
name: &str,
config: Config,
max_depth: u8,
) -> Result<&Theme, Box<dyn error::Error>> {
let debug = self.debug;
let theme_config: ThemeConfig = match config.try_deserialize() {
Ok(tc) => tc,
Err(e) => {
return Err(Box::new(Error::new(
ErrorKind::InvalidInput,
format!(
"Failed to deserialize theme: {}",
if debug {
e.to_string()
} else {
"set theme debug on for more info".to_string()
}
),
)))
}
};
let colors: HashMap<Meaning, String> = theme_config.colors;
let parent: Option<&Theme> = match theme_config.theme.parent {
Some(parent_name) => {
if max_depth == 0 {
return Err(Box::new(Error::new(
ErrorKind::InvalidInput,
"Parent requested but we hit the recursion limit",
)));
}
Some(self.load_theme(parent_name.as_str(), Some(max_depth - 1)))
}
None => None,
};
if debug && name != theme_config.theme.name {
tracing::warn!(
"Your theme config name is not the name of your loaded theme {} != {}",
name,
theme_config.theme.name
);
}
let theme = Theme::from_foreground_colors(theme_config.theme.name, parent, colors, debug);
let name = name.to_string();
self.loaded_themes.insert(name.clone(), theme);
let theme = self.loaded_themes.get(&name).unwrap();
Ok(theme)
}
// Check if the requested theme is loaded and, if not, then attempt to get it
// from the builtins or, if not there, from file
#[instrument(skip(self))]
pub fn load_theme(&mut self, name: &str, max_depth: Option<u8>) -> &Theme {
if self.loaded_themes.contains_key(name) {
return self.loaded_themes.get(name).unwrap();
}
let built_ins = &BUILTIN_THEMES;
match built_ins.get(name) {
Some(theme) => theme,
None => match self.load_theme_from_file(name, max_depth.unwrap_or(DEFAULT_MAX_DEPTH)) {
Ok(theme) => theme,
Err(err) => {
tracing::warn!("Could not load theme {}: {}", name, err);
built_ins.get("default").unwrap()
}
},
}
}
}
#[cfg(test)]
mod theme_tests {
use super::*;
use tracing_test::traced_test;
#[test]
fn test_can_load_builtin_theme() {
let mut manager = ThemeManager::new(Some(false), Some("".to_string()));
let theme = manager.load_theme("autumn", None);
assert_eq!(
theme.as_style(Meaning::Guidance).foreground_color,
from_string("brown").ok()
);
}
#[test]
fn test_can_create_theme() {
let mut manager = ThemeManager::new(Some(false), Some("".to_string()));
let mytheme = Theme::new(
"mytheme".to_string(),
None,
HashMap::from([(
Meaning::AlertError,
StyleFactory::known_fg_string("yellowgreen"),
)]),
);
manager.loaded_themes.insert("mytheme".to_string(), mytheme);
let theme = manager.load_theme("mytheme", None);
assert_eq!(
theme.as_style(Meaning::AlertError).foreground_color,
from_string("yellowgreen").ok()
);
}
#[test]
fn test_can_fallback_when_meaning_missing() {
let mut manager = ThemeManager::new(Some(false), Some("".to_string()));
// We use title as an example of a meaning that is not defined
// even in the base theme.
assert!(!DEFAULT_THEME.styles.contains_key(&Meaning::Title));
let config = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"title_theme\"
[colors]
Guidance = \"white\"
AlertInfo = \"zomp\"
",
FileFormat::Toml,
))
.build()
.unwrap();
let theme = manager
.load_theme_from_config("config_theme", config, 1)
.unwrap();
// Correctly picks overridden color.
assert_eq!(
theme.as_style(Meaning::Guidance).foreground_color,
from_string("white").ok()
);
// Does not fall back to any color.
assert_eq!(theme.as_style(Meaning::AlertInfo).foreground_color, None);
// Even for the base.
assert_eq!(theme.as_style(Meaning::Base).foreground_color, None);
// Falls back to red as meaning missing from theme, so picks base default.
assert_eq!(
theme.as_style(Meaning::AlertError).foreground_color,
Some(Color::DarkRed)
);
// Falls back to Important as Title not available.
assert_eq!(
theme.as_style(Meaning::Title).foreground_color,
theme.as_style(Meaning::Important).foreground_color,
);
let title_config = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"title_theme\"
[colors]
Title = \"white\"
AlertInfo = \"zomp\"
",
FileFormat::Toml,
))
.build()
.unwrap();
let title_theme = manager
.load_theme_from_config("title_theme", title_config, 1)
.unwrap();
assert_eq!(
title_theme.as_style(Meaning::Title).foreground_color,
Some(Color::White)
);
}
#[test]
fn test_no_fallbacks_are_circular() {
let mytheme = Theme::new("mytheme".to_string(), None, HashMap::from([]));
MEANING_FALLBACKS
.iter()
.for_each(|pair| assert_eq!(mytheme.closest_meaning(pair.0), &Meaning::Base))
}
#[test]
fn test_can_get_colors_via_convenience_functions() {
let mut manager = ThemeManager::new(Some(true), Some("".to_string()));
let theme = manager.load_theme("default", None);
assert_eq!(theme.get_error().foreground_color.unwrap(), Color::DarkRed);
assert_eq!(
theme.get_warning().foreground_color.unwrap(),
Color::DarkYellow
);
assert_eq!(theme.get_info().foreground_color.unwrap(), Color::DarkGreen);
assert_eq!(theme.get_base().foreground_color, None);
assert_eq!(
theme.get_alert(log::Level::Error).foreground_color.unwrap(),
Color::DarkRed
)
}
#[test]
#[traced_test]
fn test_can_use_parent_theme_for_fallbacks() {
let mut manager = ThemeManager::new(Some(false), Some("".to_string()));
// First, we introduce a base theme
let solarized = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"solarized\"
[colors]
Guidance = \"white\"
AlertInfo = \"pink\"
",
FileFormat::Toml,
))
.build()
.unwrap();
let solarized_theme = manager
.load_theme_from_config("solarized", solarized, 1)
.unwrap();
assert_eq!(
solarized_theme
.as_style(Meaning::AlertInfo)
.foreground_color,
from_string("pink").ok()
);
// Then we introduce a derived theme
let unsolarized = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"unsolarized\"
parent = \"solarized\"
[colors]
AlertInfo = \"red\"
",
FileFormat::Toml,
))
.build()
.unwrap();
let unsolarized_theme = manager
.load_theme_from_config("unsolarized", unsolarized, 1)
.unwrap();
// It will take its own values
assert_eq!(
unsolarized_theme
.as_style(Meaning::AlertInfo)
.foreground_color,
from_string("red").ok()
);
// ...or fall back to the parent
assert_eq!(
unsolarized_theme
.as_style(Meaning::Guidance)
.foreground_color,
from_string("white").ok()
);
// If the parent is not found, we end up with the base theme colors
let nunsolarized = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"nunsolarized\"
parent = \"nonsolarized\"
[colors]
AlertInfo = \"red\"
",
FileFormat::Toml,
))
.build()
.unwrap();
let nunsolarized_theme = manager
.load_theme_from_config("nunsolarized", nunsolarized, 1)
.unwrap();
assert_eq!(
nunsolarized_theme
.as_style(Meaning::Guidance)
.foreground_color,
Some(Color::DarkBlue)
);
logs_assert(|captured_logs: &[&str]| {
// More context lines with tracing than just the logline itself.
assert_eq!(captured_logs.len(), 1);
assert!(captured_logs[0].contains(
"Could not load theme nonsolarized: Empty theme directory override and could not find theme elsewhere"
));
assert!(captured_logs[0].contains("WARN"));
Ok(())
});
}
#[test]
#[traced_test]
fn test_can_debug_theme() {
let debug = true;
let mut manager = ThemeManager::new(Some(debug), Some("".to_string()));
let config = Config::builder()
.add_source(ConfigFile::from_str(
"
[theme]
name = \"mytheme\"
[colors]
Guidance = \"white\"
AlertInfo = \"xinetic\"
",
FileFormat::Toml,
))
.build()
.unwrap();
manager
.load_theme_from_config("config_theme", config, 1)
.unwrap();
logs_assert(|captured_logs: &[&str]| {
// More context lines with tracing than just the logline itself.
assert_eq!(captured_logs.len(), 2);
assert!(captured_logs[0].contains(
"Your theme config name is not the name of your loaded theme config_theme != mytheme"
));
assert!(captured_logs[1].contains(
"Tried to load string as a color unsuccessfully: (AlertInfo=xinetic) No such color in palette"
));
assert!(captured_logs.into_iter().all(|line| line.contains("WARN")));
Ok(())
});
}
#[test]
fn test_can_parse_color_strings_correctly() {
assert_eq!(
from_string("brown").unwrap(),
Color::Rgb {
r: 165,
g: 42,
b: 42
}
);
assert_eq!(from_string(""), Err("Empty string".into()));
["manatee", "caput mortuum", "123456"]
.iter()
.for_each(|inp| {
assert_eq!(from_string(inp), Err("No such color in palette".into()));
});
assert_eq!(
from_string("#ff1122").unwrap(),
Color::Rgb {
r: 255,
g: 17,
b: 34
}
);
["#1122", "#ffaa112", "#brown"].iter().for_each(|inp| {
assert_eq!(
from_string(inp),
Err("Could not parse 3 hex values from string".into())
);
});
assert_eq!(from_string("@dark_grey").unwrap(), Color::DarkGrey);
assert_eq!(
from_string("@rgb_(255,255,255)").unwrap(),
Color::Rgb {
r: 255,
g: 255,
b: 255
}
);
assert_eq!(from_string("@ansi_(255)").unwrap(), Color::AnsiValue(255));
["@", "@DarkGray", "@Dark 4ay", "@ansi(256)"]
.iter()
.for_each(|inp| {
assert_eq!(
from_string(inp),
Err(format!(
"Could not convert color name {} to Crossterm color",
inp
))
);
});
}
}