Skip to content

Instantly share code, notes, and snippets.

@qti3e
Last active January 31, 2024 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qti3e/5b03f5838ad57b3e86c6d58fa5a9e1f3 to your computer and use it in GitHub Desktop.
Save qti3e/5b03f5838ad57b3e86c6d58fa5a9e1f3 to your computer and use it in GitHub Desktop.
|/|
0> vim
2> cd client && yarn start
3> cd server && yarn start
0
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
char *dev;
int tty_fd;
static inline void send_tty(char *ch) {
ioctl(tty_fd, TIOCSTI, ch);
}
void echo_tty(char *cmd) {
usleep(225000);
for (int i = 0; cmd[i]; ++i) {
send_tty(cmd + i);
}
}
void open_tty() {
tty_fd = open(dev, O_RDWR);
if(tty_fd == -1) {
perror("open DEVICE");
exit(1);
}
}
void start_tmux() {
echo_tty("tmux\n");
}
void process_command(char *code) {
int i, raw_mode = 0, is_comment = 0;
char *n = calloc(1, sizeof(char));
if(fork() != 0)
return;
open_tty();
start_tmux();
for (i = 0; code[i]; ++i) {
if (is_comment) {
if (code[i] == '\n') {
is_comment = 0;
}
continue;
}
if (raw_mode) {
if (code[i] == '\n' && code[i - 1] != '\\') {
raw_mode = 0;
}
send_tty(code + i);
continue;
}
switch (code[i]) {
case '|':
// Vertical Split
echo_tty("\x02%");
break;
case '/':
// Horizontal Split
echo_tty("\x02\"");
break;
case 't':
// Show a timer
echo_tty("\x02t");
break;
case 'l':
// Arrow Left
echo_tty("\x02\x1B\x5B\x44\x02");
usleep(300000);
break;
case 'r':
// Arrow right
echo_tty("\x02\x1B\x5B\x43\x02");
usleep(300000);
break;
case 'u':
// Arrow up
echo_tty("\x02\x1B\x5B\x41\x02");
usleep(300000);
break;
case 'd':
// Arrow down
echo_tty("\x02\x1B\x5B\x42\x02");
usleep(300000);
break;
case '>':
raw_mode = 1;
break;
case '#':
is_comment = 1;
break;
}
if ('0' <= code[i] && code[i] <= '9') {
n[0] = code[i];
echo_tty("\x02q");
echo_tty(n);
usleep(300000);
}
}
free(n);
close(tty_fd);
}
void process_file(char *file_name) {
FILE *fd = fopen(file_name, "rb");
if (!fd) {
perror("fopen");
exit(-1);
}
fseek(fd, 0, SEEK_END);
long fsize = ftell(fd);
fseek(fd, 0, SEEK_SET);
char *code = malloc(fsize + 1);
fread(code, fsize, 1, fd);
fclose(fd);
code[fsize] = 0;
process_command(code);
}
int main (int argc, char *argv[]) {
dev = getenv("TTY");
if (dev == NULL) {
dev = ttyname(STDIN_FILENO);
}
if (argc == 2) {
process_file(argv[1]);
} else if (argc == 3 && !strcmp(argv[1], "-e")) {
process_command(argv[2]);
} else if (access(".tmux-layout", F_OK) != -1) {
process_file(".tmux-layout");
} else {
puts("No .tmux-layout file found in this directory.");
puts("Use:");
puts("tmux-layout [FILE]");
puts("tmux-layout -e [CMD]");
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment