GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
REPL.cc
Go to the documentation of this file.
1#include "REPL.hh"
2
3#include <chrono>
4#include <iostream>
5
6namespace GamR {
7namespace Utils {
8
9REPL::REPL(std::string prompt) : fInput(""), fPrompt(prompt), fContinue(true), fNextLine(true)
10{
11 Prompt();
12 fThread = std::thread([&]() {
13 std::string input;
14 char keypress;
15 do {
16 input = "";
17 while (fContinue.load()) {
18 while (std::cin.peek() == EOF) {
19 std::this_thread::yield();
20 }
21 keypress = std::cin.get();
22 if (keypress == '\n') {
23 break;
24 }
25 input += keypress;
26 }
27 if (!fContinue.load()) {
28 break;
29 }
30 while (fContinue.load() && !fNextLine.load()) {
31 std::this_thread::yield();
32 }
33 if (!fContinue.load()) {
34 break;
35 }
36 fInputLock.lock();
37 fInput = input;
38 fInputLock.unlock();
39 fNextLine.store(false);
40 Prompt();
41 } while (fContinue.load());
42 });
43 fThread.detach();
44}
45
47{
48 Halt();
49}
50
52{
53 std::cout << "\r" << fPrompt << std::flush;
54}
55
57{
58 return fContinue.load();
59}
60
62{
63 fContinue.store(false);
64 return true;
65}
66
67void REPL::ReadLine(std::string &input)
68{
69 if (fNextLine.load()) {
70 std::this_thread::sleep_for(std::chrono::milliseconds(1));
71 input.clear();
72 } else {
73 fInputLock.lock();
74 input = fInput;
75 fInputLock.unlock();
76 fNextLine.store(true);
77 }
78}
79
80} // namespace Utils
81} // namespace GamR
void ReadLine(std::string &input)
Definition REPL.cc:67
REPL(std::string prompt)
Definition REPL.cc:9
void Prompt()
Definition REPL.cc:51
bool Halt()
Definition REPL.cc:61
bool Loop()
Definition REPL.cc:56
Definition Gain.cc:19