-
Notifications
You must be signed in to change notification settings - Fork 0
/
el.cpp
169 lines (136 loc) · 3.64 KB
/
el.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "el.h"
#include "el_term.h"
#include <assert.h>
#include <stdio.h>
#include <string>
#include <lua.hpp>
extern "C" {
#include <lauxlib.h>
}
class Lua {
lua_State *L;
public:
Lua() : L(luaL_newstate()) { luaL_openlibs(L); }
~Lua() { lua_close(L); }
Lua(const Lua &) = delete;
Lua &operator=(const Lua &) = delete;
void Dofile(std::string_view src) {
if (src.empty()) {
return;
}
std::string file;
if (src[0] == '~') {
// expand: ~
file += el::GetHome();
src = src.substr(1);
}
file += src;
// set global table el
auto top = lua_gettop(L);
lua_newtable(L);
// default: el.line_height=1
lua_pushinteger(L, 1);
lua_setfield(L, -2, "line_height");
lua_setglobal(L, "el");
assert(lua_gettop(L) == top);
luaL_dofile(L, file.c_str());
}
// set el[key] = value
void set(const char *key, int value) {
auto top = lua_gettop(L);
lua_getglobal(L, "el");
lua_pushinteger(L, value);
lua_setfield(L, -2, key);
lua_pop(L, 1);
assert(lua_gettop(L) == top);
}
int get_int(const char *key) {
auto top = lua_gettop(L);
lua_getglobal(L, "el");
lua_getfield(L, -1, key);
lua_remove(L, -2);
auto value = lua_tointeger(L, -1);
lua_pop(L, 1);
assert(lua_gettop(L) == top);
return value;
}
// call: el[func]()
void call(const char *func) {
auto top = lua_gettop(L);
lua_getglobal(L, "el");
lua_getfield(L, -1, func);
lua_remove(L, -2);
lua_call(L, 0, 0);
auto end = lua_gettop(L);
assert(end == top);
}
int call_timer() {
auto top = lua_gettop(L);
lua_getglobal(L, "el");
lua_getfield(L, -1, "timer");
lua_remove(L, -2);
lua_call(L, 0, 1);
auto value = lua_tointeger(L, -1);
lua_pop(L, 1);
auto end = lua_gettop(L);
assert(end == top);
return value;
}
const char *call(const char *func, const char *buf, size_t len) {
auto top = lua_gettop(L);
lua_getglobal(L, "el");
auto field_type = lua_getfield(L, -1, func);
lua_remove(L, -2);
lua_pushlstring(L, buf, len);
if (field_type == LUA_TFUNCTION) {
lua_call(L, 1, 1);
}
auto value = lua_tostring(L, -1);
lua_pop(L, 1);
assert(lua_gettop(L) == top);
return value;
}
};
namespace el {
struct EscapeLineImpl {
Lua m_lua;
std::vector<char> m_buf;
RowCol Initialize(int height, std::string_view lua_file) {
m_lua.Dofile(lua_file);
m_lua.set("line_height", height);
return Update();
}
RowCol Update() {
auto size = GetTermSize();
m_lua.set("rows", size.Row);
m_lua.set("cols", size.Col);
m_lua.call("update");
size.Row -= m_lua.get_int("line_height");
return size;
}
int Timer() { return m_lua.call_timer(); }
std::span<char> Input(const char *buf, size_t len) {
auto result = m_lua.call("input", buf, len);
m_buf.assign(result, result + strlen(result));
return m_buf;
}
std::span<char> Output(const char *buf, size_t len) {
auto result = m_lua.call("output", buf, len);
m_buf.assign(result, result + strlen(result));
return m_buf;
}
};
EscapeLine::EscapeLine() : m_impl(new EscapeLineImpl) {}
EscapeLine::~EscapeLine() { delete m_impl; }
RowCol EscapeLine::Initialize(int height, std::string_view lua_file) {
return m_impl->Initialize(height, lua_file);
}
RowCol EscapeLine::Update() { return m_impl->Update(); }
int EscapeLine::Timer() { return m_impl->Timer(); }
std::span<char> EscapeLine::Input(const char *buf, size_t len) {
return m_impl->Input(buf, len);
}
std::span<char> EscapeLine::Output(const char *buf, size_t len) {
return m_impl->Output(buf, len);
}
} // namespace el