-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (83 loc) · 2.49 KB
/
Copy pathmain.cpp
File metadata and controls
95 lines (83 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// simulator - a console sim where many flows share one pump and one logical
// clock. Type commands to drive time itself:
// pause freeze logical time (runners stop, dashboard stays live)
// resume resume logical time
// speed <n> scale logical time (n > 0; 0.5 = half, 4 = 4x)
// quit stop and exit
//
// FEATURE FOCUS: VirtualClock (scale / freeze) + single-thread cooperation.
// The pump, the five runners, and the renderer all live on one thread; this
// main thread does nothing but read stdin and poke the clock (thread-safe).
#include "app.h"
#include "console.h"
#include "snapshot.h"
#include <iostream>
#include <sstream>
#include <string>
namespace
{
// Draw the input prompt on its reserved row. The renderer never touches this
// row, so what the user types is not overwritten by frame redraws.
void DrawPrompt()
{
std::cout << console::At(sim::kPromptRow, 1) << console::kClearLine << " "
<< console::kBold << "Type a command and press Enter"
<< console::kReset << console::kDim
<< " (pause | resume | speed <n> | quit): " << console::kReset
<< std::flush;
}
void RunInputLoop(uniflow::Runtime& rt)
{
std::string line;
while (true)
{
DrawPrompt();
if (!std::getline(std::cin, line))
{
break; // EOF (e.g. piped input ended) - treat as quit
}
std::istringstream in(line);
std::string cmd;
in >> cmd;
if (cmd.empty())
{
continue;
}
if (cmd == "quit" || cmd == "exit" || cmd == "q")
{
break;
}
if (cmd == "pause")
{
rt.clock().Freeze(); // stop logical time for ALL runners at once
}
else if (cmd == "resume")
{
rt.clock().Resume();
}
else if (cmd == "speed")
{
double n = 0.0;
if (in >> n && n > 0.0)
{
rt.clock().SetScale(n); // one call rescales every flow's pace
}
}
// Unknown commands are ignored; the dashboard keeps running.
}
}
} // namespace
int main()
{
console::EnableAnsi();
console::HideCursor();
console::Clear();
App& app = App::inst();
app.Start();
RunInputLoop(app.rt);
app.Shutdown();
console::ShowCursor();
std::cout << console::At(sim::kPromptRow + 1, 1) << console::kClearLine
<< " simulator stopped.\n";
return 0;
}