-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyank.cc
More file actions
56 lines (48 loc) · 1.38 KB
/
yank.cc
File metadata and controls
56 lines (48 loc) · 1.38 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
#include <fcntl.h>
#include <stdio.h> // includes
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include "common.h"
void put(std::string esc) {
char* tmux = getenv("TMUX");
if (tmux != nullptr) {
fprintf(stderr, "TMUX is set\n");
esc = "\ePtmux;\e" + esc + "\e\\";
}
ssize_t res = write(1, esc.data(), esc.size());
fprintf(stderr, "Written %ld bytes of %ld\n", res, esc.size());
fflush(stdout);
}
int main(int argc, char** argv) {
dup2(2, 100);
// Log to /dev/null by default
int er = open("/dev/null", O_CREAT | O_WRONLY | O_APPEND, 0777);
// dup2(er, 2);
for (int i = 0; i < argc; i++) {
fprintf(stderr, "%s\n", argv[i]);
}
int fd = open_tty();
fprintf(stderr, "tty %d\n", fd);
if (fd != -1) {
dup2(fd, 1);
} else {
// FIXME: Probably separate script should be used instead?
// From TMUX <c-a> y
// bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'yank
// >#{pane_tty}'
fprintf(stderr, "Writing to stdout\n");
}
std::string s(std::istreambuf_iterator<char>(std::cin), {});
// put("\e]52;c;!\a");
int SZ = 128001;
for (size_t i = 0; i < s.length(); i += SZ) {
const char* start = s.data() + i;
size_t end = std::min(i + SZ, s.length());
size_t len = end - i;
put("\e]52;c;" + base64_encode((unsigned char*)start, len) + "\a");
}
// usleep(100000);
exit(0);
}