1 module scone.output.text_style;
2 
3 import scone.output.types.cell : Cell;
4 import scone.output.types.color : Color;
5 import std.conv : to;
6 
7 struct TextStyle
8 {
9     typeof(this) fg(Color color)
10     {
11         this.foreground = color;
12 
13         return this;
14     }
15 
16     typeof(this) bg(Color color)
17     {
18         this.background = color;
19 
20         return this;
21     }
22 
23     Color foreground = Color.same;
24     Color background = Color.same;
25 }
26 
27 unittest
28 {
29     auto style1 = TextStyle();
30     assert(style1.foreground == Color.same);
31     assert(style1.background == Color.same);
32 
33     auto style2 = TextStyle().bg(Color.green);
34     assert(style2.foreground == Color.same);
35     assert(style2.background == Color.green);
36 
37     auto style3 = TextStyle(Color.red);
38     assert(style3.foreground == Color.red);
39     assert(style3.background == Color.same);
40 }
41 
42 unittest
43 {
44     auto style1 = TextStyle().fg(Color.red).bg(Color.green);
45     assert(style1.foreground == Color.red);
46     assert(style1.background == Color.green);
47 
48     auto style2 = TextStyle();
49     style2.foreground = Color.red;
50     style2.background = Color.green;
51 
52     assert(style1 == style2);
53 }
54 
55 struct StyledText
56 {
57     this(string text, TextStyle style = TextStyle())
58     {
59         Cell[] ret = new Cell[](text.length);
60 
61         foreach (i, c; to!dstring(text))
62         {
63             ret[i] = Cell(c, style);
64         }
65 
66         this.cells = ret;
67     }
68 
69     Cell[] cells;
70 }
71 
72 unittest
73 {
74     //dfmt off
75     auto st1 = StyledText("foo");
76     assert
77     (
78         st1.cells == [
79             Cell('f', TextStyle(Color.same, Color.same)),
80             Cell('o', TextStyle(Color.same, Color.same)),
81             Cell('o', TextStyle(Color.same, Color.same))
82         ]
83     );
84 
85     auto st2 = StyledText("bar", TextStyle().bg(Color.red));
86     assert
87     (
88         st2.cells == [
89             Cell('b', TextStyle(Color.same, Color.red)),
90             Cell('a', TextStyle(Color.same, Color.red)),
91             Cell('r', TextStyle(Color.same, Color.red)),
92         ]
93     );
94     ///dfmt on
95 }