This repository was archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalcInputLoop.hpp
More file actions
60 lines (49 loc) · 1.42 KB
/
CalcInputLoop.hpp
File metadata and controls
60 lines (49 loc) · 1.42 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
#ifndef CALCULATOR_CALCINPUTLOOP_HPP
#define CALCULATOR_CALCINPUTLOOP_HPP
#include <string>
#include "boilerplate/ld_boilerplate.hpp"
#include "PrettifyException.hpp"
#define RETURN_IF(x) if (x) return last_result
template <class Calc>
std::wstring input_loop(Calc calc) {
/**
* Simply a variable re-used multiple times to store what the user inputs
*/
std::wstring input;
/**
* To return when the user requests to quit
*/
std::wstring last_result;
while (true) {
/**
* Quit if the user wants to quit, but else store user input in the
* [[input]] variable
*
* `input == "exit"` is only evaluated after [[LD::get_input]] has
* finished and if it didn't already return `true`.
*/
RETURN_IF(LD::get_input(L"> ", input, true) || input == L":exit");
#ifdef LD_USE_LINENOISE
/**
* Add this to the history so you can use the up arrow to recall past
* commands
*
* Only supported if LD_USE_LINENOISE is specified (i.e. not on Repl.it)
*/
linenoise::AddHistory(LD::w2str(input).c_str());
#endif
try {
std::vector<std::wstring> results = calc
.execute_multiple(input);
if (!results.empty()) {
last_result = results.back();
if (!last_result.empty()) {
LD::log(last_result + L"\n");
}
}
} catch (CalcASTException & exception) {
LD::log(prettify_exception(input, exception));
}
}
}
#endif //CALCULATOR_CALCINPUTLOOP_HPP