Skip to content

Commit

Permalink
Refactor Codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
pegvin committed Feb 24, 2024
1 parent 89e33a5 commit 7deec9b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 188 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pronounced *turmeric*, Termrec is a fast & tiny terminal session recorder writte
- Extremely Fast & Small in Size (~52kb)
- 0 External Libraries (Except Posix standard stuff)
- UTF-8 Support
- Asciinema V1 & V2 File Format Support
- ONLY Asciinema V1 Format Support
- Runs On Any Unix-Based System

---
Expand Down
19 changes: 4 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ int masterfd;
pid_t child;

void PrintUsage(const char* name) {
printf("Usage: %s [command] [session-path] [options]\n\n", name);
printf("[command]\n - play: play a session\n - rec: record a session\n - help: shows help message\n\n");
printf("[options]\n -f, --format: file-format of the session being played (default asciinema_v1)\n valid formats are: asciinema_v1, asciinema_v2\n");
printf("Usage: %s [command] [session-path]\n\n", name);
printf("[command]\n - play: play a session\n - rec: record a session\n - help: shows help message\n");
}

int main(int argc, char** argv) {
Expand All @@ -31,11 +30,11 @@ int main(int argc, char** argv) {
if (strcmp(argv[i], "rec") == 0) {
if (i == argc - 1) { printf("No session name specified!\n"); exit(EXIT_FAILURE); }
oa.mode = TERMREC_RECORD;
oa.fileName = argv[i + 1];
oa.rec.filePath = argv[i + 1];
} else if (strcmp(argv[i], "play") == 0) {
if (i == argc - 1) { printf("No session name specified!\n"); exit(EXIT_FAILURE); }
oa.mode = TERMREC_PLAY;
oa.fileName = argv[i + 1];
oa.rec.filePath = argv[i + 1];
} else if (strcmp(argv[i], "help") == 0) {
PrintUsage(argv[0]);
return 0;
Expand All @@ -44,16 +43,6 @@ int main(int argc, char** argv) {
exit(EXIT_FAILURE);
}

for (i = 2; i < argc; i++) {
if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--format") == 0) {
if (i == argc - 1) { printf("No file-format name specified!\n"); exit(EXIT_FAILURE); }
const char* format = argv[i + 1];
if (strcmp(format, "asciinema_v1") == 0) oa.format = ASCIINEMA_V1;
else if (strcmp(format, "asciinema_v2") == 0) oa.format = ASCIINEMA_V2;
else { printf("Invalid file-format specified!\n"); exit(EXIT_FAILURE); }
}
}

if (oa.mode == TERMREC_RECORD) {
RecordSession(oa);
} else if (oa.mode == TERMREC_PLAY) {
Expand Down
24 changes: 10 additions & 14 deletions src/record.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef RECORD_H
#define RECORD_H 1

#include <stdint.h>

enum control_command {
CMD_NONE,
CMD_CTRL_A,
Expand All @@ -12,26 +14,20 @@ typedef enum {
TERMREC_RECORD
} AppMode;

typedef enum {
ASCIINEMA_V1 = 1,
ASCIINEMA_V2 = 2
} FileFormat;
struct Recording {
uint32_t width; // cols
uint32_t height; // rows
uint64_t timestamp;
const char* env;
const char* filePath;
};

struct outargs {
int start_paused;
int controlfd;
int masterfd;

// Terminal Rows/Columns
int rows, cols;

AppMode mode;
FileFormat format;

const char* cmd;
const char* env;
const char* title;
const char* fileName;
struct Recording rec;
};

#endif
50 changes: 24 additions & 26 deletions src/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@ void RecordSession(struct outargs oa) {
char *exec_cmd;
char cwd[PATH_MAX];

oa.env = SerializeEnv();
oa.rec.env = SerializeEnv();
exec_cmd = NULL;

if (!oa.format) oa.format = ASCIINEMA_V1;
if (!oa.fileName) oa.fileName = "events.cast";
if (!oa.rec.filePath) oa.rec.filePath = "events.cast";

if (pipe(controlfd) != 0) die("pipe");
oa.controlfd = controlfd[0];

TermGetWinSize(&rows, &cols);
oa.rows = rows;
oa.cols = cols;
oa.rec.width = cols;
oa.rec.height = rows;

if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("CWD: %s\n", cwd);
Expand All @@ -71,11 +70,11 @@ void RecordSession(struct outargs oa) {
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) die("ioctl(TIOCGWINSZ)");

owin = rwin = win;
if (!oa.rows || oa.rows > win.ws_row) oa.rows = win.ws_row;
if (!oa.cols || oa.cols > win.ws_col) oa.cols = win.ws_col;
if (!oa.rec.width || oa.rec.width > win.ws_col) oa.rec.width = win.ws_col;
if (!oa.rec.height || oa.rec.height > win.ws_row) oa.rec.height = win.ws_row;

win.ws_row = oa.rows;
win.ws_col = oa.cols;
win.ws_col = oa.rec.width;
win.ws_row = oa.rec.height;

TermEnableRawMode();

Expand Down Expand Up @@ -278,8 +277,7 @@ static void handle_command(enum control_command cmd) {
}

// This Function Is Responsible For Writing The Data To The File
static inline void handle_input(unsigned char *buf, size_t buflen, FileFormat format) {
assert(format >= ASCIINEMA_V1 && format <= ASCIINEMA_V2);
static inline void handle_input(FILE* writerFile, unsigned char *buf, size_t buflen) {
static int first = 1;
double delta;

Expand All @@ -300,7 +298,7 @@ static inline void handle_input(unsigned char *buf, size_t buflen, FileFormat fo

dur += delta;

WriteStdoutStart((format == ASCIINEMA_V1 ? delta : dur) / 1000);
Writer_OnBeforeStdoutData(writerFile, delta / 1000.0f);

uint32_t state, cp;
state = 0;
Expand All @@ -313,29 +311,29 @@ static inline void handle_input(unsigned char *buf, size_t buflen, FileFormat fo
uint32_t h, l;
h = ((cp - 0x10000) >> 10) + 0xd800;
l = ((cp - 0x10000) & 0x3ff) + 0xdc00;
WriteStdout_fprintf("\\u%04" PRIx32 "\\u%04" PRIx32, h, l);
Writer_OnStdoutData(writerFile, "\\u%04" PRIx32 "\\u%04" PRIx32, h, l);
} else {
WriteStdout_fprintf("\\u%04" PRIx32, cp);
Writer_OnStdoutData(writerFile, "\\u%04" PRIx32, cp);
}
} else {
WriteStdout_fputs("\\ud83d\\udca9");
Writer_OnStdoutData(writerFile, "\\ud83d\\udca9");
}
} else {
switch (buf[j]) {
case '"':
case '\\':
WriteStdout_fputc('\\'); // output backslash for escaping
WriteStdout_fputc(buf[j]); // print the character itself
Writer_OnStdoutData(writerFile, "\\"); // output backslash for escaping
Writer_OnStdoutData(writerFile, "%c", buf[j]); // print the character itself
break;
default:
WriteStdout_fputc(buf[j]);
Writer_OnStdoutData(writerFile, "%c", buf[j]);
break;
}
}
}
}

WriteStdoutEnd();
Writer_OnAfterStdoutData(writerFile);
}

/*
Expand All @@ -350,13 +348,11 @@ void StartOutputProcess(struct outargs *oa) {
status = EXIT_SUCCESS;
master = oa->masterfd;

assert(oa->format >= ASCIINEMA_V1 && oa->format <= ASCIINEMA_V2);

start_paused = paused = oa->start_paused;

setbuf(stdout, NULL); // Set The Stream To Be Un-Buffered
WriterInit(oa->fileName);
WriteHeader(oa);
FILE* writerFile = Writer_Init(oa->rec.filePath);
Writer_WriteHeader(writerFile, &oa->rec);

if (close(STDIN_FILENO) == -1) {
die("close");
Expand Down Expand Up @@ -428,15 +424,17 @@ void StartOutputProcess(struct outargs *oa) {
}

if (!paused) {
handle_input(obuf, nread, oa->format);
handle_input(writerFile, obuf, nread);
}
}
}
}

end:
WriteDuration(dur / 1000);
WriterClose();
Writer_OnStdoutAllEnd(writerFile);
Writer_WriteDuration(writerFile, dur / 1000.0f);
Writer_Close(writerFile);

if (close(oa->masterfd) == -1) {
die("close");
}
Expand Down
8 changes: 4 additions & 4 deletions src/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "terminal.h"
#include "main.h"

struct termios OriginalTermIOS;
static struct termios InitialAttribs;

void TermEnableRawMode() {
if (tcgetattr(STDIN_FILENO, &OriginalTermIOS) == -1) die("tcgetattr");
if (tcgetattr(STDIN_FILENO, &InitialAttribs) == -1) die("tcgetattr");

struct termios raw = OriginalTermIOS;
struct termios raw = InitialAttribs;

raw.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON);
raw.c_oflag &= ~OPOST;
Expand All @@ -21,7 +21,7 @@ void TermEnableRawMode() {
}

void TermDisableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &OriginalTermIOS) == -1)
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &InitialAttribs) == -1)
die("tcsetattr");
}

Expand Down
Loading

0 comments on commit 7deec9b

Please sign in to comment.