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