This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathterm.js
137 lines (103 loc) · 3.97 KB
/
term.js
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
// ----------------------------------------------------------------------------------------------------------------- //
// Nano Core 2 - An adblocker
// Copyright (C) 2018-2019 Nano Core 2 contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------------------------------------------- //
// Terminal utilities
// ----------------------------------------------------------------------------------------------------------------- //
"use strict";
// ----------------------------------------------------------------------------------------------------------------- //
const child_process = require("child_process");
// ----------------------------------------------------------------------------------------------------------------- //
// Only one terminal manager can be active at any given time
// Unless otherwise mentioned, all methods return the keyword this
// Constructor takes in the event listener, which will receive trimmed command strings from user
module.exports = class {
constructor(func) {
if (func)
this.set_listener(func);
this._paused = false;
this._handler = (line) => {
if (this._paused)
return;
if (this._func)
this._func(line.trim());
else
this.write_line("ERROR: No command handler.");
};
process.stdin.setEncoding("utf8");
process.stdin.on("data", this._handler);
process.stdin.resume();
}
destructor() {
process.stdin.pause();
process.stdin.removeListener("data", this._handler);
}
title(title) {
return this.write("\x1B]0;" + title + "\x07");
}
// String or buffer
write(data) {
process.stdout.write(data);
return this;
}
// String only
write_line(line) {
return this.write(line + "\n");
}
// Overwrites current listener
set_listener(func) {
this._func = func;
return this;
}
ready() {
return this.write("> ");
}
// Options:
// cwd - Current working directory
// on_data - Listener for stdout
//
// Resolves the exit code
exec(cmd, opt, ...args) {
return new Promise((resolve, reject) => {
const child = child_process.spawn(cmd, args, {
cwd: opt.cwd,
});
child.on("error", reject);
child.stdout.setEncoding("utf8");
child.stdout.on("data", (data) => {
this.write(data);
if (opt.on_data)
opt.on_data(data);
});
child.stderr.setEncoding("utf8");
child.stderr.on("data", (data) => {
this.write(data);
});
this._paused = true;
const on_user_input = (data) => {
child.stdin.write(data);
};
process.stdin.on("data", on_user_input);
child.on("close", (code) => {
process.stdin.removeListener("data", on_user_input);
this._paused = false;
this.write_line("Command exited with code " + code.toString());
resolve(code);
});
});
}
};
// ----------------------------------------------------------------------------------------------------------------- //