1 module sbylib.engine.project.modulestatus;
2 
3 import std;
4 import sbylib.engine.util : blue, yellow, cyan, green, red, magenda, gray;
5 
6 enum ModuleStatus {
7     Compiling,
8     WaitingForRun,
9     Running,
10     CompileError,
11     RuntimeError,
12     Stopping,
13 }
14 
15 class ModuleStatusList {
16     private ModuleStatus[string] status;
17     private string[string] msg;
18 
19     ModuleStatus[] values() {
20         return status.values;
21     }
22 
23     ModuleStatus opIndex(string key) const 
24         in (key in status, key ~ " is not registered")
25     {
26         return status[key];
27     }
28 
29     ModuleStatus opIndexAssign(ModuleStatus s, string key) 
30         in (key !in status || status[key] != s)
31     {
32         status[key] = s;
33         rewriteStatus();
34         return s;
35     }
36 
37     void opIndexAssign(Tuple!(ModuleStatus, string) t, string key)
38         in (key !in status || status[key] != t[0])
39     {
40         status[key] = t[0];
41         msg[key] = t[1];
42         rewriteStatus();
43     }
44 
45     private void rewriteStatus() {
46         clearScreen();
47         foreach (key, value; status.byKeyValue.array.sort!((a,b) => a.key < b.key).map!(p => tuple(p.key, p.value)).assocArray) {
48             writefln("%30s : %20s", key, colorize(value));
49             if (value == ModuleStatus.CompileError) {
50                 writeln(msg[key].red);
51             }
52             if (value == ModuleStatus.RuntimeError) {
53                 writeln(msg[key].red);
54             }
55         }
56     }
57 
58     private void clearScreen() const {
59         writefln("\033[%d;%dH" ,0,0); // move cursor
60         writeln("\033[2J"); // clear screen
61     }
62 
63     private static string colorize(in ModuleStatus status) {
64         final switch (status) {
65             case ModuleStatus.Compiling:
66                 return status.to!string.yellow;
67             case ModuleStatus.WaitingForRun:
68                 return status.to!string.cyan;
69             case ModuleStatus.Running:
70                 return status.to!string.green;
71             case ModuleStatus.CompileError:
72                 return status.to!string.red;
73             case ModuleStatus.RuntimeError:
74                 return status.to!string.red;
75             case ModuleStatus.Stopping:
76                 return status.to!string.gray;
77         }
78     }
79 }