1 module ColourfulMoon;
2 
3 /**
4 * Simple text styling library
5 */
6 
7 /**
8 * Text style
9 * Params:
10 *	s = input text
11 * Returns: Styled text.
12 */
13 private T Style(T, S...)(S anotherS) {
14 	import std.array : appender;
15 	auto a = appender!T();
16 	foreach(i; anotherS)
17 		a.put(i);
18 	return a.data;
19 }
20 /// ditto
21 T Reset(T)(T s = "") {
22 	version(Posix) {
23 		return Style!T(s, "\033[0m");
24 	} else {
25 		return s;
26 	}
27 }
28 /// ditto
29 T Bold(T)(T s = "") {
30 	version(Posix) {
31 		return Style!T("\033[1m", s);
32 	} else {
33 		return s;
34 	}
35 }
36 /// ditto
37 T Underline(T)(T s = "") {
38 	version(Posix) {
39 		return Style!T("\033[4m", s);
40 	} else {
41 		return s;
42 	}
43 }
44 /// ditto
45 T Blink(T)(T s = "") {
46 	version(Posix) {
47 		return Style!T("\033[5m", s);
48 	} else {
49 		return s;
50 	}
51 }
52 /// ditto
53 T Reverse(T)(T s = "") {
54 	version(Posix) {
55 		return Style!T("\033[7m", s);
56 	} else {
57 		return s;
58 	}
59 }
60 
61 struct Colour {
62 	ubyte R, G, B;
63 
64 	string toConsole() {
65 		import std.math : floor;
66 		import std.conv : to;
67 
68 		if(R == G && R == B) {
69 			return to!string((R > 239) ? 15 : floor(cast(real) R / 10) + 232);
70 		} else {
71 			return to!string(16 + 36 * floor(cast(real) R / 51) + 6 * floor(cast(real) G / 51) + floor(cast(real) B / 51));
72 		}
73 	}
74 }
75 
76 /**
77 * Text colour
78 * Params:
79 *	s = input text
80 *	c = colour
81 * Returns: Coloured text.
82 */
83 T Foreground(T)(T s = "", Colour c = Colour()) {
84 	version(Posix) {
85 		return Style!T("\033[38;05;", c.toConsole(), "m", s);
86 	} else {
87 		return s;
88 	}
89 }
90 /// ditto
91 T Background(T)(T s = "", Colour c = Colour()) {
92 	version(Posix) {
93 		return Style!T("\033[48;05;", c.toConsole(), "m", s);
94 	} else {
95 		return s;
96 	}
97 }