1 /** 2 * Styles demo for Dcell. 3 * 4 * Copyright: Copyright 2025 Garrett D'Amore 5 * Authors: Garrett D'Amore 6 * License: 7 * Distributed under the Boost Software License, Version 1.0. 8 * (See accompanying file LICENSE or https://www.boost.org/LICENSE_1_0.txt) 9 * SPDX-License-Identifier: BSL-1.0 10 */ 11 module styles; 12 13 import std.string; 14 15 import dcell; 16 17 void centerStr(Screen s, int y, Style style, string str) 18 { 19 s.style = style; 20 s.position = Coord((s.size.x - cast(int)(str.length)) / 2, y); 21 s.write(str); 22 } 23 24 void displayStyles(Screen s) 25 { 26 27 s.style.attr = Attr.bold; 28 s.style.fg = Color.black; 29 s.style.bg = Color.white; 30 31 s.clear(); 32 33 s.style.fg = Color.blue; 34 s.style.bg = Color.silver; 35 36 int row = 2; 37 s.position = Coord(2, row); 38 s.write("Press ESC to Exit"); 39 row += 2; 40 41 s.style.fg = Color.black; 42 s.style.bg = Color.white; 43 44 s.position = Coord(2, row); 45 s.style.attr = Attr.none; 46 s.write("Note: Style support is dependent on your terminal."); 47 row += 2; 48 49 s.style.attr = Attr.none; 50 s.position = Coord(2, row++); 51 s.write("Plain"); 52 53 s.style.attr = Attr.blink; 54 s.position = Coord(2, row++); 55 s.write("Blink"); 56 57 s.style.attr = Attr.reverse; 58 s.position = Coord(2, row++); 59 s.write("Reverse"); 60 61 s.style.attr = Attr.dim; 62 s.position = Coord(2, row++); 63 s.write("Dim"); 64 65 s.style.attr = Attr.underline; 66 s.position = Coord(2, row++); 67 s.write("Underline"); 68 69 s.style.attr = Attr.italic; 70 s.position = Coord(2, row++); 71 s.write("Italic"); 72 73 s.style.attr = Attr.bold; 74 s.position = Coord(2, row++); 75 s.write("Bold"); 76 77 s.style.attr = Attr.bold | Attr.italic; 78 s.position = Coord(2, row++); 79 s.write("Bold Italic"); 80 81 s.style.attr = Attr.bold | Attr.italic | Attr.underline; 82 s.position = Coord(2, row++); 83 s.write("Bold Italic Underline"); 84 85 s.style.attr = Attr.strikethrough; 86 s.position = Coord(2, row++); 87 s.write("Strikethrough"); 88 89 s.style.attr = Attr.doubleUnderline; 90 s.position = Coord(2, row++); 91 s.write("Double Underline"); 92 93 s.style.attr = Attr.curlyUnderline; 94 s.position = Coord(2, row++); 95 s.write("Curly Underline"); 96 97 s.style.attr = Attr.dottedUnderline; 98 s.position = Coord(2, row++); 99 s.write("Dotted Underline"); 100 101 s.style.attr = Attr.dashedUnderline; 102 s.position = Coord(2, row++); 103 s.write("Dashed Underline"); 104 105 s.style.attr = Attr.underline; 106 s.style.ul = Color.blue; 107 s.position = Coord(2, row++); 108 s.write("Blue Underline"); 109 110 s.style.attr = Attr.curlyUnderline; 111 s.style.ul = fromHex(0xc58af9); 112 s.position = Coord(2, row++); 113 s.write("Lavender Curly Underline"); 114 115 s.style.attr = Attr.none; 116 s.style.ul = Color.invalid; 117 s.position = Coord(2, row++); 118 s.style.url = "https://github.com/gdamore/dcell"; 119 s.write("Hyperlink"); 120 s.style.url = ""; 121 122 s.style.attr = Attr.none; 123 s.style.fg = Color.red; 124 s.position = Coord(2, row++); 125 s.write("Red Foreground"); 126 127 s.style.attr = Attr.none; 128 s.style.bg = Color.red; 129 s.style.fg = Color.black; 130 s.position = Coord(2, row++); 131 s.write("Red Background"); 132 133 s.show(); 134 } 135 136 void handleEvent(Screen ts, Event ev) 137 { 138 import core.stdc.stdlib : exit; 139 140 switch (ev.type) 141 { 142 case EventType.key: 143 if (ev.key.key == Key.esc || ev.key.key == Key.f1) 144 { 145 ts.stop(); 146 exit(0); 147 } 148 break; 149 case EventType.resize: 150 ts.resize(); 151 displayStyles(ts); 152 ts.sync(); 153 break; 154 default: 155 break; 156 } 157 } 158 159 void main() 160 { 161 auto ts = newScreen(); 162 assert(ts !is null); 163 scope (exit) 164 { 165 ts.stop(); 166 } 167 168 ts.start(); 169 170 displayStyles(ts); 171 for (;;) 172 { 173 ts.waitForEvent(); 174 foreach (ev; ts.events()) 175 { 176 handleEvent(ts, ev); 177 } 178 } 179 }