/
tag.cpp
290 lines (236 loc) · 8.34 KB
/
tag.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <array>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <optional>
#include <regex>
#include <sstream>
#include <unistd.h>
#include <utility>
#include <vector>
#define let const auto
static const std::string QF_FILE("/tmp/tag_qf");
static const std::regex FILE_REGEX("^(?:(?:\x1b\\[[^m]*m)+)?([^\x1b]+)");
static const std::regex
LINE_NUMBER_MATCH("^(?:(?:\x1b\\[[^m]*m)+)?(\\d+)(?:(?:\x1b\\[0?m(?:\x1b\\["
"K)?)+)?:(?:\x1b\\[0?m)?(\\d+)(?:\x1b\\[0?m)?:(.*)");
const std::regex ANSI_REGEX("\x1b\\[[0-9;]*[mG]");
class Command {
const std::string cmd_id_;
const std::string cmd_;
const std::string extraArgs_ = "";
const bool includeFiles_ = false;
[[nodiscard]] std::string command(const std::string &args) const {
return cmd_ + " " + extraArgs_ + " " + args;
}
[[nodiscard]] std::string
commandWithoutDefaultArgs(const std::string &args) const {
return cmd_ + " " + args;
}
public:
Command(std::string cmd, const char *extraArgs)
: cmd_id_(cmd), cmd_(cmd), extraArgs_(extraArgs) {}
Command(std::string cmd, bool includeFiles)
: cmd_id_(cmd), cmd_(cmd), includeFiles_(includeFiles) {}
Command(std::string cmd_id, std::string cmd, bool includeFiles)
: cmd_id_(cmd_id), cmd_(cmd), includeFiles_(includeFiles) {}
std::string cmd_id() const { return cmd_id_; }
bool includeFiles() const { return includeFiles_; }
[[nodiscard]] int
run(const std::string &args,
std::function<void(const std::string &)> callback) const {
FILE *pipe = popen(this->command(args).c_str(), "r");
if (!pipe) {
std::cerr << "error: failed to launch process" << std::endl;
return 1;
}
char *line = nullptr;
size_t _len = 0;
while (getline(&line, &_len, pipe) != -1)
callback(line);
free(line);
int status = pclose(pipe);
if (WIFEXITED(status))
return WEXITSTATUS(status);
if (WIFSIGNALED(status))
return WTERMSIG(status);
return status;
}
[[nodiscard]] int runWithoutDefaultArgs(const std::string &args) const {
int status = std::system(this->commandWithoutDefaultArgs(args).c_str());
if (WIFEXITED(status))
return WEXITSTATUS(status);
return status;
}
};
static const std::array COMMANDS{
Command("rg", "--heading --color always --column"),
Command("ag", "--group --color --column"),
Command("fd", true),
Command("fdfind", true),
Command("find", true),
Command("git-status", "git -c color.status=always status -sb", true),
};
[[nodiscard]] static bool isZSH() {
if (let *shell = std::getenv("SHELL"))
if (std::string(shell).find("zsh") != std::string::npos)
return true;
return false;
}
static inline void rstrip(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { return !std::isspace(ch); })
.base(),
s.end());
}
// FIXME: cleanup somehow
[[nodiscard]] static std::string
vimEditCommand(const std::string &path,
std::optional<std::pair<int, int>> location,
const std::string &vimArgs = "") {
const std::string prefix =
"eval '$EDITOR " + vimArgs + "\\\"" + path + "\\\"";
if (location) {
let line = std::to_string(std::get<0>(*location));
let col = std::to_string(std::get<1>(*location));
return prefix + " \\\"+call cursor(" + line + ", " + col + ")\\\"'";
}
return prefix + "'";
}
[[nodiscard]] static std::string aliasForCommand(const std::string index,
const std::string &command) {
return "alias e" + index + "=\"" + command + "\"";
}
[[nodiscard]] static std::string aliasForCommand(int index,
const std::string &command) {
return aliasForCommand(std::to_string(index), command);
}
[[nodiscard]] static std::string
globalAliasForCommand(int index, const std::string &command) {
return "alias -g f" + std::to_string(index) + "=\"" + command + "\"";
}
static void printAliasedLine(int index, std::string line) {
std::cout << "[\x1b[0;31m" << index << "\x1b[0m] " << line;
}
[[nodiscard]] static std::string stripAnsi(const std::string line) {
std::stringstream result;
std::regex_replace(std::ostream_iterator<char>(result), line.begin(),
line.end(), ANSI_REGEX, "");
return result.str();
}
[[nodiscard]] static std::string formatQFLine(const std::string &path, int lnum,
int col,
const std::string line = "") {
return path + ":" + std::to_string(lnum) + ":" + std::to_string(col) + ":" +
line;
}
[[nodiscard]] static int
runAndWriteFile(const Command cmd, std::string outputFile, std::string args) {
std::string currentFile;
int aliasIndex = 1;
std::ofstream os(outputFile);
std::ofstream qfOS(QF_FILE);
os << aliasForCommand("all", vimEditCommand(QF_FILE, std::nullopt, "-q "))
<< std::endl;
return cmd.run(args, [&](const std::string &line) {
std::smatch match;
if (std::regex_search(line, match, LINE_NUMBER_MATCH)) {
// TODO: https://github.com/keith/tag/issues/24
if (!currentFile.size()) {
std::cout << line;
return;
}
let lnum = std::stoi(match.str(1));
let col = std::stoi(match.str(2));
let leftover = stripAnsi(match.str(3));
let editCmd = vimEditCommand(currentFile, std::make_pair(lnum, col));
os << aliasForCommand(aliasIndex, editCmd) << std::endl;
if (isZSH()) {
os << globalAliasForCommand(aliasIndex, currentFile) << std::endl;
}
qfOS << formatQFLine(currentFile, lnum, col, leftover) << std::endl;
printAliasedLine(aliasIndex, line);
++aliasIndex;
} else if (cmd.cmd_id() == "git-status") {
if (line.starts_with("##")) {
std::cout << line;
return;
}
// TODO: Doesn't handle renames or moves
std::string path = stripAnsi(line).substr(3);
rstrip(path);
qfOS << formatQFLine(path, 1, 1, "") << std::endl;
printAliasedLine(aliasIndex, line);
let editCmd = vimEditCommand(path, std::nullopt);
os << aliasForCommand(aliasIndex, editCmd) << std::endl;
if (isZSH()) {
os << globalAliasForCommand(aliasIndex, path) << std::endl;
}
++aliasIndex;
} else if (std::regex_search(line, match, FILE_REGEX)) {
currentFile = match.str(1);
rstrip(currentFile);
if (cmd.includeFiles()) {
qfOS << formatQFLine(currentFile, 1, 1, "") << std::endl;
printAliasedLine(aliasIndex, line);
let editCmd = vimEditCommand(currentFile, std::nullopt);
os << aliasForCommand(aliasIndex, editCmd) << std::endl;
if (isZSH()) {
os << globalAliasForCommand(aliasIndex, currentFile) << std::endl;
}
++aliasIndex;
} else {
std::cout << line;
}
} else {
std::cout << line;
}
// Force write in case of ctrl-c
os.flush();
});
}
[[noreturn]] static void helpAndExit() {
std::stringstream cmdNames;
cmdNames << "[";
for (auto cmd : COMMANDS)
cmdNames << cmd.cmd_id() << "|";
cmdNames.seekp(-1, cmdNames.cur);
cmdNames << ']';
std::cerr << "Usage: tag [--alias-file FILE] " << cmdNames.str() << " ARGS"
<< std::endl;
std::exit(127);
}
[[nodiscard]] static std::string popFirst(std::deque<std::string> &queue) {
if (queue.empty())
helpAndExit();
auto element = queue.front();
queue.pop_front();
return element;
}
int main(int argc, char *argv[]) {
std::deque<std::string> arguments(argv + 1, argv + argc);
if (arguments.empty())
helpAndExit();
std::string argFile("/tmp/tag_aliases");
if (arguments.front() == "--alias-file") {
arguments.pop_front();
argFile = popFirst(arguments);
}
let cmdString = popFirst(arguments);
let *command =
std::find_if(COMMANDS.begin(), COMMANDS.end(), [&cmdString](auto &cmd) {
return cmdString == cmd.cmd_id();
});
if (command == COMMANDS.end())
helpAndExit();
std::stringstream joinedArgs;
for (auto arg : arguments) {
joinedArgs << std::quoted(arg) << " ";
}
let args = joinedArgs.str();
let isPiped = !isatty(fileno(stdout));
if (isPiped && std::getenv("SKIP_PIPE_FILTERING") == nullptr)
return command->runWithoutDefaultArgs(args);
return runAndWriteFile(*command, argFile, args);
}