Skip to content

Commit 33940e6

Browse files
authored
color supported (#56)
1 parent 17e5d78 commit 33940e6

File tree

12 files changed

+384
-58
lines changed

12 files changed

+384
-58
lines changed

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ coverage:
1818
ignore:
1919
- "repl"
2020
- "googletest"
21+
- "include/color.h"
22+
- "src/color.cc"
2123

2224
parsers:
2325
gcov:

docs/images/example2.png

-977 KB
Loading

docs/images/example3.png

25.6 KB
Loading

include/color.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,134 @@
11
#pragma once
2+
#include <ostream>
3+
#include <string_view>
24

35
namespace autumn {
46
namespace color {
7+
8+
class Color {
9+
public:
10+
Color(const std::string_view& c);
11+
friend std::string operator+(const Color& c, const std::string& rhs);
12+
friend std::string operator+(const std::string& rhs, const Color& c);
13+
friend std::ostream& operator<<(std::ostream&, const Color&);
14+
15+
private:
16+
const std::string_view _color;
17+
bool _color_env = true;
18+
};
19+
20+
// Reset
21+
extern const Color off; // Text Reset
22+
23+
// Regular Colors
24+
extern const Color black; // Black
25+
extern const Color red; // Red
26+
extern const Color green; // Green
27+
extern const Color yellow; // Yellow
28+
extern const Color blue; // Blue
29+
extern const Color purple; // Purple
30+
extern const Color cyan; // Cyan
31+
extern const Color white; // White
32+
33+
// Light
34+
namespace light {
35+
extern const Color light; // light
36+
extern const Color black; // Black
37+
extern const Color red; // Red
38+
extern const Color green; // Green
39+
extern const Color yellow; // Yellow
40+
extern const Color blue; // Blue
41+
extern const Color purple; // Purple
42+
extern const Color cyan; // Cyan
43+
extern const Color white; // White
44+
}
45+
46+
// Light
47+
namespace dark {
48+
49+
extern const Color dark; // Dark
50+
extern const Color black; // Black
51+
extern const Color red; // Red
52+
extern const Color green; // Green
53+
extern const Color yellow; // Yellow
54+
extern const Color blue; // Blue
55+
extern const Color purple; // Purple
56+
extern const Color cyan; // Cyan
57+
extern const Color white; // White
58+
59+
} // namespace dark
60+
61+
// Underline
62+
namespace underline {
63+
64+
extern const Color black; // Black
65+
extern const Color red; // Red
66+
extern const Color green; // Green
67+
extern const Color yellow; // Yellow
68+
extern const Color blue; // Blue
69+
extern const Color purple; // Purple
70+
extern const Color cyan; // Cyan
71+
extern const Color white; // White
72+
73+
}
74+
75+
// Background
76+
namespace background {
77+
extern const Color black; // Black
78+
extern const Color red; // Red
79+
extern const Color green; // Green
80+
extern const Color yellow; // Yellow
81+
extern const Color blue; // Blue
82+
extern const Color purple; // Purple
83+
extern const Color cyan; // Cyan
84+
extern const Color white; // White
85+
}
86+
87+
// High Intensity
88+
namespace intensity {
89+
90+
extern const Color black; // Black
91+
extern const Color red; // Red
92+
extern const Color green; // Green
93+
extern const Color yellow; // Yellow
94+
extern const Color blue; // Blue
95+
extern const Color purple; // Purple
96+
extern const Color cyan; // Cyan
97+
extern const Color white; // White
98+
99+
}
100+
101+
// Bold High IntensIty
102+
namespace bold {
103+
namespace intensity {
104+
105+
extern const Color black; // Black
106+
extern const Color red; // Red
107+
extern const Color green; // Green
108+
extern const Color yellow; // Yellow
109+
extern const Color blue; // Blue
110+
extern const Color purple; // Purple
111+
extern const Color cyan; // Cyan
112+
extern const Color white; // White
113+
114+
}
115+
}
116+
117+
// High Intensity bAckgrounds
118+
namespace background {
119+
namespace intensity {
120+
121+
extern const Color black; // Black
122+
extern const Color red; // Red
123+
extern const Color green; // Green
124+
extern const Color yellow; // Yellow
125+
extern const Color blue; // Blue
126+
extern const Color purple; // Purple
127+
extern const Color cyan; // Cyan
128+
extern const Color white; // White
129+
130+
}
131+
}
132+
5133
} // namespace color
6134
} // namespace autumn

include/object.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <unordered_map>
77

8+
#include "color.h"
89
#include "program.h"
910
#include "format.h"
1011

@@ -79,7 +80,8 @@ class Integer : public Object {
7980
}
8081

8182
std::string inspect() const override {
82-
return std::to_string(_value);
83+
std::string ret = std::to_string(_value);
84+
return color::light::yellow + ret + color::off;
8385
}
8486

8587
int value() const {
@@ -97,7 +99,8 @@ class Boolean : public Object {
9799
}
98100

99101
std::string inspect() const override {
100-
return _value ? "true" : "false";
102+
std::string ret = _value ? "true" : "false";
103+
return color::light::yellow + ret + color::off;
101104
}
102105

103106
bool value() const {
@@ -115,7 +118,10 @@ class String: public Object {
115118
}
116119

117120
std::string inspect() const override {
118-
return format(R"("{}")", _value) ;
121+
return format(R"("{}{}{}")",
122+
color::green,
123+
_value,
124+
color::off) ;
119125
}
120126

121127
const std::string& value() const {
@@ -131,7 +137,7 @@ class Null : public Object {
131137
}
132138

133139
std::string inspect() const override {
134-
return "null";
140+
return color::light::light + "null" + color::off;
135141
}
136142
};
137143

@@ -161,7 +167,10 @@ class Error : public Object {
161167
}
162168

163169
std::string inspect() const override {
164-
return "\x1b[1;31merror: \x1b[0m" + _message;
170+
return format("{}error:{}",
171+
color::light::red,
172+
color::off,
173+
_message);
165174
}
166175

167176
const std::string& message() const {
@@ -210,7 +219,7 @@ class Function : public Object {
210219
ret.append(_body->to_string());
211220
ret.append(" }");
212221

213-
return ret;
222+
return color::cyan + ret + color::off;
214223
}
215224

216225
std::shared_ptr<Environment>& env() const {
@@ -232,7 +241,7 @@ class Builtin : public Object {
232241
}
233242

234243
std::string inspect() const override {
235-
return "builtin function";
244+
return color::cyan + "builtin function" + color::off;
236245
}
237246

238247
std::shared_ptr<object::Object> run(const std::vector<std::shared_ptr<object::Object>>& args) const {

include/tracer.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <functional>
66
#include <string>
77

8+
#include "color.h"
9+
810
namespace autumn {
911

10-
// Tracer 和颜色耦合了?
11-
// TODO:
1212
class Tracer {
1313
public:
1414
Tracer() {
@@ -18,14 +18,26 @@ class Tracer {
1818
}
1919
}
2020

21-
std::function<void()> trace(const std::string& message) {
21+
std::function<void()> trace(const std::string& message, const std::string& token_literal) {
2222
++_level;
23-
print(format("\x1b[2m{}\x1b[0m {}", "BEGIN", message));
24-
return std::bind(&Tracer::untrace, this, message);
23+
print(format("{}BEGIN{} {}: {}{}{}",
24+
color::dark::dark,
25+
color::off,
26+
message,
27+
color::dark::yellow,
28+
token_literal,
29+
color::off));
30+
return std::bind(&Tracer::untrace, this, message, token_literal);
2531
}
2632

27-
void untrace(const std::string& message) {
28-
print(format("\x1b[2m{}\x1b[0m {}", "END", message));
33+
void untrace(const std::string& message, const std::string& token_literal) {
34+
print(format("{}END{} {}: {}{}{}",
35+
color::dark::dark,
36+
color::off,
37+
message,
38+
color::dark::yellow,
39+
token_literal,
40+
color::off));
2941
--_level;
3042
}
3143

repl/autumn.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <map>
44
#include <stdio.h>
55

6+
#include "color.h"
67
#include "lexer.h"
78
#include "parser.h"
89
#include "evaluator.h"
@@ -66,7 +67,8 @@ void parser_repl(const std::string& line) {
6667
auto program = parser.parse(line);
6768
if (!parser.errors().empty()) {
6869
for (auto& error : parser.errors()) {
69-
std::cerr << "\x1b[1;31merror: \x1b[0m"
70+
std::cerr << autumn::color::light::red
71+
<< "error: " << autumn::color::off
7072
<< error << std::endl;
7173
}
7274
return;
@@ -80,15 +82,8 @@ void eval_repl(const std::string& line) {
8082
return;
8183
}
8284

83-
if (typeid(*obj) == typeid(autumn::object::String)) {
84-
std::cout << "\x1b[32m";
85-
} else if (typeid(*obj) == typeid(autumn::object::Function)) {
86-
std::cout << "\x1b[36m";
87-
} else {
88-
std::cout << "\x1b[1;33m";
89-
}
9085
std::cout << obj->inspect();
91-
std::cout << "\x1b[0m" << std::endl;
86+
std::cout << autumn::color::off << std::endl;
9287
}
9388

9489
void do_nothing(const std::string& line) {

0 commit comments

Comments
 (0)