Skip to content

Commit 7deec9b

Browse files
committed
Refactor Codebase
1 parent 89e33a5 commit 7deec9b

7 files changed

Lines changed: 114 additions & 188 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pronounced *turmeric*, Termrec is a fast & tiny terminal session recorder writte
66
- Extremely Fast & Small in Size (~52kb)
77
- 0 External Libraries (Except Posix standard stuff)
88
- UTF-8 Support
9-
- Asciinema V1 & V2 File Format Support
9+
- ONLY Asciinema V1 Format Support
1010
- Runs On Any Unix-Based System
1111

1212
---

src/main.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ int masterfd;
1313
pid_t child;
1414

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

2120
int main(int argc, char** argv) {
@@ -31,11 +30,11 @@ int main(int argc, char** argv) {
3130
if (strcmp(argv[i], "rec") == 0) {
3231
if (i == argc - 1) { printf("No session name specified!\n"); exit(EXIT_FAILURE); }
3332
oa.mode = TERMREC_RECORD;
34-
oa.fileName = argv[i + 1];
33+
oa.rec.filePath = argv[i + 1];
3534
} else if (strcmp(argv[i], "play") == 0) {
3635
if (i == argc - 1) { printf("No session name specified!\n"); exit(EXIT_FAILURE); }
3736
oa.mode = TERMREC_PLAY;
38-
oa.fileName = argv[i + 1];
37+
oa.rec.filePath = argv[i + 1];
3938
} else if (strcmp(argv[i], "help") == 0) {
4039
PrintUsage(argv[0]);
4140
return 0;
@@ -44,16 +43,6 @@ int main(int argc, char** argv) {
4443
exit(EXIT_FAILURE);
4544
}
4645

47-
for (i = 2; i < argc; i++) {
48-
if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--format") == 0) {
49-
if (i == argc - 1) { printf("No file-format name specified!\n"); exit(EXIT_FAILURE); }
50-
const char* format = argv[i + 1];
51-
if (strcmp(format, "asciinema_v1") == 0) oa.format = ASCIINEMA_V1;
52-
else if (strcmp(format, "asciinema_v2") == 0) oa.format = ASCIINEMA_V2;
53-
else { printf("Invalid file-format specified!\n"); exit(EXIT_FAILURE); }
54-
}
55-
}
56-
5746
if (oa.mode == TERMREC_RECORD) {
5847
RecordSession(oa);
5948
} else if (oa.mode == TERMREC_PLAY) {

src/record.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef RECORD_H
22
#define RECORD_H 1
33

4+
#include <stdint.h>
5+
46
enum control_command {
57
CMD_NONE,
68
CMD_CTRL_A,
@@ -12,26 +14,20 @@ typedef enum {
1214
TERMREC_RECORD
1315
} AppMode;
1416

15-
typedef enum {
16-
ASCIINEMA_V1 = 1,
17-
ASCIINEMA_V2 = 2
18-
} FileFormat;
17+
struct Recording {
18+
uint32_t width; // cols
19+
uint32_t height; // rows
20+
uint64_t timestamp;
21+
const char* env;
22+
const char* filePath;
23+
};
1924

2025
struct outargs {
2126
int start_paused;
2227
int controlfd;
2328
int masterfd;
24-
25-
// Terminal Rows/Columns
26-
int rows, cols;
27-
2829
AppMode mode;
29-
FileFormat format;
30-
31-
const char* cmd;
32-
const char* env;
33-
const char* title;
34-
const char* fileName;
30+
struct Recording rec;
3531
};
3632

3733
#endif

src/recorder.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,17 @@ void RecordSession(struct outargs oa) {
4545
char *exec_cmd;
4646
char cwd[PATH_MAX];
4747

48-
oa.env = SerializeEnv();
48+
oa.rec.env = SerializeEnv();
4949
exec_cmd = NULL;
5050

51-
if (!oa.format) oa.format = ASCIINEMA_V1;
52-
if (!oa.fileName) oa.fileName = "events.cast";
51+
if (!oa.rec.filePath) oa.rec.filePath = "events.cast";
5352

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

5756
TermGetWinSize(&rows, &cols);
58-
oa.rows = rows;
59-
oa.cols = cols;
57+
oa.rec.width = cols;
58+
oa.rec.height = rows;
6059

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

7372
owin = rwin = win;
74-
if (!oa.rows || oa.rows > win.ws_row) oa.rows = win.ws_row;
75-
if (!oa.cols || oa.cols > win.ws_col) oa.cols = win.ws_col;
73+
if (!oa.rec.width || oa.rec.width > win.ws_col) oa.rec.width = win.ws_col;
74+
if (!oa.rec.height || oa.rec.height > win.ws_row) oa.rec.height = win.ws_row;
7675

77-
win.ws_row = oa.rows;
78-
win.ws_col = oa.cols;
76+
win.ws_col = oa.rec.width;
77+
win.ws_row = oa.rec.height;
7978

8079
TermEnableRawMode();
8180

@@ -278,8 +277,7 @@ static void handle_command(enum control_command cmd) {
278277
}
279278

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

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

301299
dur += delta;
302300

303-
WriteStdoutStart((format == ASCIINEMA_V1 ? delta : dur) / 1000);
301+
Writer_OnBeforeStdoutData(writerFile, delta / 1000.0f);
304302

305303
uint32_t state, cp;
306304
state = 0;
@@ -313,29 +311,29 @@ static inline void handle_input(unsigned char *buf, size_t buflen, FileFormat fo
313311
uint32_t h, l;
314312
h = ((cp - 0x10000) >> 10) + 0xd800;
315313
l = ((cp - 0x10000) & 0x3ff) + 0xdc00;
316-
WriteStdout_fprintf("\\u%04" PRIx32 "\\u%04" PRIx32, h, l);
314+
Writer_OnStdoutData(writerFile, "\\u%04" PRIx32 "\\u%04" PRIx32, h, l);
317315
} else {
318-
WriteStdout_fprintf("\\u%04" PRIx32, cp);
316+
Writer_OnStdoutData(writerFile, "\\u%04" PRIx32, cp);
319317
}
320318
} else {
321-
WriteStdout_fputs("\\ud83d\\udca9");
319+
Writer_OnStdoutData(writerFile, "\\ud83d\\udca9");
322320
}
323321
} else {
324322
switch (buf[j]) {
325323
case '"':
326324
case '\\':
327-
WriteStdout_fputc('\\'); // output backslash for escaping
328-
WriteStdout_fputc(buf[j]); // print the character itself
325+
Writer_OnStdoutData(writerFile, "\\"); // output backslash for escaping
326+
Writer_OnStdoutData(writerFile, "%c", buf[j]); // print the character itself
329327
break;
330328
default:
331-
WriteStdout_fputc(buf[j]);
329+
Writer_OnStdoutData(writerFile, "%c", buf[j]);
332330
break;
333331
}
334332
}
335333
}
336334
}
337335

338-
WriteStdoutEnd();
336+
Writer_OnAfterStdoutData(writerFile);
339337
}
340338

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

353-
assert(oa->format >= ASCIINEMA_V1 && oa->format <= ASCIINEMA_V2);
354-
355351
start_paused = paused = oa->start_paused;
356352

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

361357
if (close(STDIN_FILENO) == -1) {
362358
die("close");
@@ -428,15 +424,17 @@ void StartOutputProcess(struct outargs *oa) {
428424
}
429425

430426
if (!paused) {
431-
handle_input(obuf, nread, oa->format);
427+
handle_input(writerFile, obuf, nread);
432428
}
433429
}
434430
}
435431
}
436432

437433
end:
438-
WriteDuration(dur / 1000);
439-
WriterClose();
434+
Writer_OnStdoutAllEnd(writerFile);
435+
Writer_WriteDuration(writerFile, dur / 1000.0f);
436+
Writer_Close(writerFile);
437+
440438
if (close(oa->masterfd) == -1) {
441439
die("close");
442440
}

src/terminal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#include "terminal.h"
33
#include "main.h"
44

5-
struct termios OriginalTermIOS;
5+
static struct termios InitialAttribs;
66

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

10-
struct termios raw = OriginalTermIOS;
10+
struct termios raw = InitialAttribs;
1111

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

2323
void TermDisableRawMode() {
24-
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &OriginalTermIOS) == -1)
24+
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &InitialAttribs) == -1)
2525
die("tcsetattr");
2626
}
2727

0 commit comments

Comments
 (0)