-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
191 lines (162 loc) · 5.06 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "el.h"
#include "el_pty.h"
#include "el_term.h"
#include <cxxopts.hpp>
#include <iostream>
#include <uv.h>
std::shared_ptr<el::Pty> g_pty;
uv_tty_t g_tty_in;
uv_pipe_t g_ptyin_pipe;
uv_pipe_t g_ptyout_pipe;
uv_tty_t g_tty_out;
uv_signal_t g_signal_resize;
uv_timer_t g_timer;
el::EscapeLine g_el;
static void alloc_buffer(uv_handle_t *handle, size_t suggested_size,
uv_buf_t *buf) {
*buf = uv_buf_init((char *)malloc(suggested_size), suggested_size);
}
struct write_req_t {
uv_write_t req;
uv_buf_t buf;
};
void free_write_req(uv_write_t *req) {
write_req_t *wr = (write_req_t *)req;
free(wr->buf.base);
free(wr);
}
void write_data(uv_stream_t *dest, size_t size, const char *data,
uv_write_cb cb) {
write_req_t *req = (write_req_t *)malloc(sizeof(write_req_t));
req->buf = uv_buf_init((char *)malloc(size), size);
memcpy(req->buf.base, data, size);
uv_write((uv_write_t *)req, (uv_stream_t *)dest, &req->buf, 1, cb);
}
int Run(const cxxopts::ParseResult &result) {
el::SetupTerm();
std::string lua_file = "~/.config/escapeline/config.lua";
auto lua_arg = result["lua"].as<std::string>();
if (lua_arg.size() > 0) {
lua_file = lua_arg;
}
// pty
g_pty =
el::Pty::Create(g_el.Initialize(result["height"].as<int>(), lua_file));
if (!g_pty) {
return 1;
}
// spwawn
if (result.unmatched().size() > 0) {
if (!g_pty->Fork(result.unmatched())) {
return 2;
}
} else {
if (!g_pty->ForkDefault()) {
return 2;
}
}
uv_work_t work;
uv_queue_work(
uv_default_loop(), &work,
[](uv_work_t *req) {
// watch child process
g_pty->BlockProcess();
},
[](uv_work_t *req, int status) {
// stop loop
g_pty = nullptr;
uv_read_stop((uv_stream_t *)&g_ptyout_pipe);
uv_read_stop((uv_stream_t *)&g_tty_in);
uv_signal_stop(&g_signal_resize);
uv_timer_stop(&g_timer);
});
// stdin ==> ptyin,
uv_tty_init(uv_default_loop(), &g_tty_in, el::Stdin(), 1);
uv_tty_set_mode(&g_tty_in, UV_TTY_MODE_RAW);
uv_pipe_init(uv_default_loop(), &g_ptyin_pipe, 0);
uv_pipe_open(&g_ptyin_pipe, g_pty->WriteFile());
uv_read_start(
(uv_stream_t *)&g_tty_in, &alloc_buffer,
[](uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread == UV_EOF) {
// end of file
uv_close((uv_handle_t *)&g_tty_in, NULL);
}
} else if (nread > 0) {
// process key input
auto data = g_el.Input(buf->base, nread);
write_data((uv_stream_t *)&g_ptyin_pipe, data.size(), data.data(),
[](uv_write_t *req, int status) { free_write_req(req); });
}
// OK to free buffer as write_data copies it.
if (buf->base)
free(buf->base);
});
// stdout <== ptyout
uv_pipe_init(uv_default_loop(), &g_ptyout_pipe, 0);
uv_pipe_open(&g_ptyout_pipe, g_pty->ReadFile());
uv_tty_init(uv_default_loop(), &g_tty_out, el::Stdout(), 0);
uv_tty_set_mode(&g_tty_out, UV_TTY_MODE_NORMAL);
uv_read_start(
(uv_stream_t *)&g_ptyout_pipe, &alloc_buffer,
[](uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread == UV_EOF) {
// end of file
uv_close((uv_handle_t *)&g_ptyout_pipe, NULL);
}
} else if (nread > 0) {
auto data = g_el.Output(buf->base, nread);
write_data((uv_stream_t *)&g_tty_out, data.size(), data.data(),
[](uv_write_t *req, int status) { free_write_req(req); });
}
// OK to free buffer as write_data copies it.
if (buf->base)
free(buf->base);
});
uv_signal_init(uv_default_loop(), &g_signal_resize);
uv_signal_start(
&g_signal_resize,
[](uv_signal_t *handle, int signum) {
// update status line
auto size = g_el.Update();
g_pty->SetSize(size);
},
SIGWINCH);
uv_timer_init(uv_default_loop(), &g_timer);
uv_timer_start(
&g_timer,
[](uv_timer_t *handle) {
if (g_pty) {
auto interval = g_el.Timer();
uv_timer_set_repeat(handle, interval);
}
},
0, 500);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
return 0;
}
int main(int argc, char **argv) {
// escapeline --lua rc.lua -- cmd.exe ...
cxxopts::Options options("EscapeLine", "escape sequence status line");
options.add_options()
//
("h,height", "status line height",
cxxopts::value<int>()->default_value("1"))
//
("l,lua", "lua script", cxxopts::value<std::string>()->default_value(""))
// ( "v,verbose", "Verbose output",
// cxxopts::value<bool>()->default_value("false"))
;
options.allow_unrecognised_options();
try {
auto result = options.parse(argc, argv);
return Run(result);
} catch (const cxxopts::exceptions::exception &ex) {
std::cerr << ex.what() << std::endl;
std::cout << options.help() << std::endl;
return -1;
}
}