-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextselect.c
More file actions
334 lines (277 loc) · 7.94 KB
/
textselect.c
File metadata and controls
334 lines (277 loc) · 7.94 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "arg.h"
#include <errno.h>
#include <fcntl.h>
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define READBUFFER 1024
#define BUFFERGROW 512
#define PREFIX 16
#define USAGE "Usage: %s [-hnsSv0] [-o output] <input> [command ...]\n"
#define NORETURN __attribute__((noreturn))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
struct line {
char *content;
int length;
bool selected;
};
static char *argv0 = NULL;
static bool selected_invert = false;
static bool keep_empty = false;
static int prefixlen = 0;
#include "config.h"
static void die(const char *message) {
fprintf(stderr, "error: %s: %s\n", message, strerror(errno));
exit(EXIT_FAILURE);
}
static void help(void) {
fprintf(stderr,
USAGE
"Interactively select lines from a text file and optionally execute a command with the selected lines.\n"
"\n"
"Options:\n"
" -h Display this help message and exit\n"
" -n Keep empty lines which are not selectable\n"
" -o output Specify an output file to save the selected lines\n"
" -s Characters prepend to a selected line\n"
" -S Characters prepend to a unselected line\n"
" -v Invert the selection of lines\n"
" -0 Print selected lines delimited by a NUL-character\n"
"\n"
"Navigation and selection keys:\n"
" UP, LEFT Move the cursor up\n"
" DOWN, RIGHT Move the cursor down\n"
" v Invert the selection of lines\n"
" SPACE Select or deselect the current line\n"
" ENTER, q Quit the selection interface\n"
"\n"
"Examples:\n"
" textselect -o output.txt input.txt\n"
" textselect input.txt sort\n",
argv0);
}
static void usage(int exitcode) {
fprintf(stderr, USAGE, argv0);
exit(exitcode);
}
static void drawscreen(int height, int current_line, int head_line, struct line *lines, int lines_count) {
int width = getmaxx(stdscr);
werase(stdscr);
for (int i = 0; i < height; i++) {
if (i >= lines_count - head_line - 1) {
mvwprintw(stdscr, i, 0, "~");
continue;
}
if (lines[head_line + i].selected != selected_invert) {
mvwprintw(stdscr, i, 0, "%s", selected);
wattron(stdscr, A_BOLD);
} else {
mvwprintw(stdscr, i, 0, "%s", unselected);
}
if ((head_line + i) == current_line)
wattron(stdscr, A_REVERSE);
if (lines[head_line + i].length > width) {
mvwprintw(stdscr, i, prefixlen, "%.*s...", width - 3, lines[head_line + i].content);
} else {
mvwprintw(stdscr, i, prefixlen, "%s", lines[head_line + i].content);
}
wattroff(stdscr, A_REVERSE | A_BOLD);
}
wrefresh(stdscr);
}
static void handlescreen(struct line *lines, int lines_count) {
bool quit = false;
int height = 0;
int current_line = 0;
int head_line = 0;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
height = getmaxy(stdscr);
drawscreen(height, current_line, head_line, lines, lines_count);
while (!quit) {
height = getmaxy(stdscr);
switch (getch()) {
case KEY_UP:
case KEY_LEFT:
if (current_line > 0) {
current_line--;
if (current_line < head_line)
head_line--;
}
break;
case KEY_DOWN:
case KEY_RIGHT:
if (current_line < lines_count - 2) {
current_line++;
if (current_line >= head_line + height)
head_line++;
}
break;
case 'v':
selected_invert = !selected_invert;
break;
case ' ':
lines[current_line].selected = !lines[current_line].selected;
break;
case '\n': // Use '\n' for ENTER key
case 'q':
quit = true;
}
drawscreen(height, current_line, head_line, lines, lines_count);
}
endwin();
}
static size_t loadfile(const char *filename, char **buffer, int *lines) {
static char readbuf[READBUFFER];
ssize_t nread;
size_t alloc = 0, size = 0;
int fd;
*buffer = NULL;
*lines = 1;
if ((fd = open(filename, O_RDONLY)) == -1)
die("unable to open input-file");
while ((nread = read(fd, readbuf, sizeof(readbuf))) > 0) {
for (ssize_t i = 0; i < nread; i++) {
if (size == alloc) {
if ((*buffer = realloc(*buffer, alloc += BUFFERGROW)) == NULL) {
die("unable to allocate buffer");
}
}
if (readbuf[i] == '\n') {
(*buffer)[size++] = '\0';
(*lines)++;
} else {
(*buffer)[size++] = readbuf[i];
}
}
}
(*buffer)[size++] = '\0';
(*lines)++;
close(fd);
return size;
}
static int splitbuffer(char *buffer, size_t size, int maxlines, struct line **lines) {
int count = 0, start = 0;
(*lines) = calloc(maxlines, sizeof(struct line));
if (*lines == NULL)
die("unable to allocate line-mapping");
(*lines)[count].content = buffer;
(*lines)[count++].selected = false;
for (size_t i = 0; i < size; i++) {
if (buffer[i] == '\0' && (keep_empty || buffer[i - 1] != '\0')) {
(*lines)[count - 1].length = i - start;
(*lines)[count].content = &buffer[i + 1];
(*lines)[count++].selected = false;
start = i + 1;
}
}
(*lines)[count - 1].length = size - start - 1;
return count;
}
static void printselected(int fd, bool print0, struct line *lines, int lines_count) {
for (int i = 0; i < lines_count; i++) {
if (lines[i].selected != selected_invert && *lines[i].content != '\0') { // is selected AND it's not empty
write(fd, lines[i].content, lines[i].length);
write(fd, print0 ? "" : "\n", 1);
}
}
}
static pid_t runcommand(char **argv, int *destfd) {
int pipefd[2];
pid_t pid;
if (pipe(pipefd) == -1)
die("unable to create pipe");
if ((pid = fork()) == -1)
die("unable to fork for child process");
if (pid == 0) { // Child process
close(pipefd[1]); // Close write end of the pipe
dup2(pipefd[0], STDIN_FILENO); // Redirect stdin to read end of the pipe
execvp(argv[0], argv);
die("unable to execute child"); // If execvp fails
}
close(pipefd[0]); // Close read end of the pipe
*destfd = pipefd[1];
return pid;
}
static void alignspace(char *text) {
for (int i = strlen(text); i < prefixlen; i++) {
text[i] = ' ';
}
text[prefixlen] = '\0';
}
int main(int argc, char *argv[]) {
char *buffer, *input, *output = NULL;
bool print0 = false;
int lines_count, cmdfd;
struct line *lines;
size_t buffer_size;
argv0 = argv[0];
ARGBEGIN
switch (OPT) {
case 'h':
help();
exit(0);
case 'v':
selected_invert = true;
break;
case 'n':
keep_empty = true;
break;
case 'o':
output = EARGF(usage(1));
break;
case '0': // null
print0 = true;
break;
case 's':
strncpy(selected, EARGF(usage(1)), sizeof(selected));
selected[sizeof(selected) - 1] = '\0';
break;
case 'S':
strncpy(unselected, EARGF(usage(1)), sizeof(unselected));
unselected[sizeof(unselected) - 1] = '\0';
break;
default:
fprintf(stderr, "error: unknown option '-%c'\n", OPT);
usage(1);
}
ARGEND;
if (argc == 0) {
fprintf(stderr, "error: missing input\n");
usage(1);
}
input = argv[0];
SHIFT;
if (*selected || *unselected) {
int sellen = strlen(selected), unsellen = strlen(unselected);
prefixlen = MAX(sellen, unsellen) + 1;
alignspace(selected);
alignspace(unselected);
}
buffer_size = loadfile(input, &buffer, &lines_count);
lines_count = splitbuffer(buffer, buffer_size, lines_count, &lines);
handlescreen(lines, lines_count);
if (output != NULL) {
int fd;
fd = open(output, O_WRONLY | O_TRUNC | O_CREAT, 0664);
if (fd == -1)
die("unable to open output-file");
printselected(fd, print0, lines, lines_count);
}
if (argc == 0) {
printselected(STDOUT_FILENO, print0, lines, lines_count);
} else {
pid_t pid = runcommand(argv, &cmdfd);
printselected(cmdfd, print0, lines, lines_count);
close(cmdfd);
waitpid(pid, NULL, 0);
}
free(buffer);
free(lines);
return 0;
}