Skip to content

Instantly share code, notes, and snippets.

@Low-power
Last active December 9, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Low-power/81be6251d8c918cbc1db028de8798099 to your computer and use it in GitHub Desktop.
Save Low-power/81be6251d8c918cbc1db028de8798099 to your computer and use it in GitHub Desktop.
Convert terminal typescripts (by script(1) from util-linux) into asciicasts (for asciinema).
/* Copyright 2015-2021 Rivoreo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef DEFAULT_ASCIICAST_VERSION
#define DEFAULT_ASCIICAST_VERSION 1
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static void print_usage(const char *name) {
fprintf(stderr, "Usage: %s [-w <width>,<height>] [-V <asciicast-version>] [-en] <timing-file> [<script-file>]\n", name);
}
static int get_size(unsigned int *x, unsigned int *y, const char *s) {
char *end_p;
*x = strtoul(s, &end_p, 0);
if(*end_p != ',' && *end_p != ':' && *end_p != ' ' && *end_p != '*') return -1;
*y = strtoul(end_p + 1, &end_p, 0);
return *end_p ? -1 : 0;
}
static int fgetline(FILE *f, char *line, size_t len) {
size_t i = 0;
int c;
while((c = fgetc(f)) != '\n') {
if(c == EOF) {
if(!i) return -1;
break;
}
if(i >= len - 1) return -2;
line[i++] = c;
}
line[i] = 0;
return i;
}
static size_t escape_print(FILE *f, size_t len, int always_escape) {
size_t count = 0;
while(count < len) {
int c = fgetc(f);
switch(c) {
case EOF:
return count;
case 0 ... 7:
case 0xb:
case 0xe ... 0x1f:
always_escape:
printf("\\u00%02x", c);
break;
case '\b':
fputs("\\b", stdout);
break;
case ' ':
fputs("\\t", stdout);
break;
case '\n':
fputs("\\n", stdout);
break;
case '\f':
fputs("\\f", stdout);
break;
case '\r':
fputs("\\r", stdout);
break;
case '\"':
fputs("\\\"", stdout);
break;
case '\\':
fputs("\\\\", stdout);
break;
default:
if(always_escape) goto always_escape;
putchar(c);
break;
}
count++;
}
return count;
}
int main(int argc, char **argv) {
unsigned int width = 80;
unsigned int height = 24;
int asciicast_version = DEFAULT_ASCIICAST_VERSION;
int always_escape = 0;
int skip_first_ts = 1;
const char *timing_file_path = NULL;
const char *script_file_path = "typescript";
while(1) {
int c = getopt(argc, argv, "hw:V:en");
if(c == -1) break;
switch(c) {
case 'h':
print_usage(argv[0]);
return 0;
case 'w':
if(get_size(&width, &height, optarg) < 0) {
fprintf(stderr, "%s: Invalid size '%s'\n", argv[0], optarg);
return -1;
}
break;
case 'V':
asciicast_version = atoi(optarg);
break;
case 'e':
always_escape = 1;
break;
case 'n':
skip_first_ts = 0;
break;
case '?':
return -1;
}
}
if(argc <= optind || argc > optind + 2) {
print_usage(argv[0]);
return -1;
}
if(asciicast_version != 1 && asciicast_version != 2) {
fprintf(stderr, "%s: Invalid asciicast version '%d', must be 1 or 2\n",
argv[0], asciicast_version);
return -1;
}
timing_file_path = argv[optind];
if(argc > optind + 1) script_file_path = argv[optind + 1];
FILE *tf = fopen(timing_file_path, "r");
if(!tf) {
perror(timing_file_path);
return 1;
}
FILE *sf = fopen(script_file_path, "rb");
if(!sf) {
perror(script_file_path);
return 1;
}
int c;
while((c = fgetc(sf)) != EOF && c != '\n');
if(asciicast_version == 1) {
printf("{\n"
"\"version\":1,\n"
"\"width\":%u,\n\"height\":%u,\n"
"\"command\":null,\n\"title\":null,\n"
"\"stdout\":[\n",
width, height);
} else {
unsigned long long int timestamp;
struct stat sta;
if((stat(script_file_path, &sta) == 0 && S_ISREG(sta.st_mode)) ||
(stat(timing_file_path, &sta) == 0 && S_ISREG(sta.st_mode))) {
timestamp = sta.st_mtime;
} else {
timestamp = time(NULL);
}
printf("{ \"version\":2, \"width\":%u, \"height\":%u, \"timestamp\":%llu }\n",
width, height, timestamp);
}
char buffer[32];
unsigned int nlines = 1;
double delay, duration = 0;
size_t chunk_size = 0, last_chunk_size = 0, read_size;
size_t *chunk_size_p = skip_first_ts ? &last_chunk_size : &chunk_size;
char *end_p;
do {
switch(fgetline(tf, buffer, sizeof buffer)) {
case -1:
if(skip_first_ts && last_chunk_size) {
if(asciicast_version == 1) {
fputs(",\n[ 0, \"", stdout);
} else {
printf("[ %g, \"o\", \"", duration);
}
read_size = escape_print(sf, last_chunk_size, always_escape);
puts("\" ]");
}
goto end;
case -2:
fprintf(stderr, "%s: %s:%d: Line too long\n", argv[0], timing_file_path, nlines);
return 1;
}
delay = strtod(buffer, &end_p);
if(*end_p != ' ' || !end_p[1]) {
fprintf(stderr, "%s: %s:%d: Invalid syntax\n", argv[0], timing_file_path, nlines);
return 1;
}
*end_p = 0;
duration += delay;
if(skip_first_ts) last_chunk_size = chunk_size;
chunk_size = strtoull(end_p + 1, &end_p, 10);
if(*end_p) {
fprintf(stderr, "%s: %s:%d: Invalid syntax\n", argv[0], timing_file_path, nlines);
return 1;
}
if(asciicast_version == 1) {
if(nlines++ > 1) puts(",");
printf("[ %s, \"", buffer);
} else {
printf("[ %g, \"o\", \"", duration);
}
read_size = escape_print(sf, *chunk_size_p, always_escape);
puts("\" ]");
} while(read_size == *chunk_size_p);
end:
if(asciicast_version == 1) printf("],\n\"duration\":%g\n}\n", duration);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment