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