This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.cpp
More file actions
104 lines (102 loc) · 2.62 KB
/
Copy pathrender.cpp
File metadata and controls
104 lines (102 loc) · 2.62 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
96
97
98
99
100
101
102
103
104
#include "render.h"
#define TokenType Win32TokenType
#include <windows.h>
#undef TokenType
#include <cmath>
bool isNumber(const std::string& s) {
for (auto& c : s) {
if (!std::isdigit(c) && c != '.') return false;
}
return true;
}
/*
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 10 = 淡绿色
3 = 浅绿色 11 = 淡浅绿色
4 = 红色 12 = 淡红色
5 = 紫色 13 = 淡紫色
6 = 黄色 14 = 淡黄色
7 = 白色 15 = 亮白色
*/
template <class T>
void colorPrint(const T& c, int color) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
std::cout << c;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
template <class T>
void printTimes(const T& c, int n) {
for (int i = 0; i < n; ++i) std::cout << c;
}
void flushLine(int n) {
printTimes('\b', n);
printTimes(' ', n);
printTimes('\b', n);
}
void Render::printToken(const std::string& token) {
if (!token.empty())
if (env->checkSymbol(token) || SPECIAL_FORMS.contains(token))
colorPrint(token, 2);
else if (isNumber(token))
colorPrint(token, 1);
else if (token[0] == '"' && token[token.length() - 1] == '"' && token.length()>1)
colorPrint(token, 5);
else
std::cout << token;
}
void Render::render(const std::string& rInput, const int& rTailLen) {
//flushLine
printTimes(' ', tailLen);
flushLine(input.length());
//render
std::string token{};
bool hasQuotation{false};
for (auto& c : rInput) {
if (c == '"') {
token += c;
if (hasQuotation) {
printToken(token);
token = "";
}
hasQuotation = !hasQuotation;
} else if (!hasQuotation && (c == '(' || c == ')' || c == ' ')) {
printToken(token);
token = "";
if (c == ' ')
std::cout << c;
else
colorPrint(c, 4);
}
else {
token += c;
}
}
printToken(token);
printTimes('\b', rTailLen);
//update
input = rInput;
tailLen = rTailLen;
}
void Render::init() {
if (!input.empty()) {
history[history.size() - 1] = input;
history.push_back("");
historyPos = history.size() - 1;
}
input = "";
tailLen = 0;
}
std::string Render::getInput() const {
return input;
}
int Render::getTailLen() const {
return tailLen;
}
std::string Render::getHistory() const {
return history[historyPos];
}
int Render::getHistoryLen() const {
return history.size();
}