1 /** 2 * Hello world demo for Dcell. 3 * 4 * Copyright: Copyright 2022 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 hello; 12 13 import std.stdio; 14 import std.string; 15 import std.concurrency; 16 17 import dcell; 18 19 void emitStr(Screen s, int x, int y, Style style, string str) 20 { 21 // NB: this naively assumes only ASCII 22 while (str != "") 23 { 24 s[x, y] = Cell(str[0], style); 25 str = str[1..$]; 26 x += 1; 27 } 28 } 29 30 void displayHelloWorld(Screen s) 31 { 32 auto size = s.size(); 33 Style def; 34 def.bg = Color.silver; 35 def.fg = Color.black; 36 s.setStyle(def); 37 s.clear(); 38 Style style = {fg: Color.red, bg: Color.papayaWhip}; 39 emitStr(s, size.x / 2 - 9, size.y / 2 - 1, style, " Hello, World! "); 40 emitStr(s, size.x / 2 - 11, size.y / 2 + 1, def, " Press ESC to exit. "); 41 42 // this demonstrates a different method. 43 // it places a red X in the center of the screen. 44 s[$/2, $/2].text = "X"; 45 s[$/2, $/2].style.fg = Color.white; 46 s[$/2, $/2].style.bg = Color.red; 47 s.show(); 48 } 49 50 void handleEvent(Screen ts, Event ev) 51 { 52 import core.stdc.stdlib : exit; 53 54 switch (ev.type) 55 { 56 case EventType.key: 57 if (ev.key.key == Key.esc || ev.key.key == Key.f1) 58 { 59 ts.stop(); 60 exit(0); 61 } 62 break; 63 case EventType.resize: 64 ts.resize(); 65 displayHelloWorld(ts); 66 ts.sync(); 67 break; 68 default: 69 break; 70 } 71 } 72 73 void main() 74 { 75 import std.stdio; 76 77 auto ts = newScreen(); 78 assert(ts !is null); 79 80 ts.start(thisTid()); 81 displayHelloWorld(ts); 82 for (;;) 83 { 84 receive( 85 (Event ev) { handleEvent(ts, ev); } 86 ); 87 } 88 }