vtv-player.c - vtv-tools - virtual terminal video tools
 (HTM) git clone git://bitreich.org/vtv-tools  git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/vtv-tools
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       vtv-player.c (1882B)
       ---
            1 // Copyright 2023 Troels Henriksen <athas@sigkill.dk>
            2 //
            3 // See LICENSE file for licensing information.
            4 
            5 #include <stdio.h>
            6 #include <stdlib.h>
            7 #include <unistd.h>
            8 #include <errno.h>
            9 #include <string.h>
           10 #include <signal.h>
           11 
           12 #include "vtv.h"
           13 
           14 void hide_cursor() { printf("\033[?25l"); }
           15 void show_cursor() { printf("\033[?25h"); }
           16 void move(int x, int y) { printf("\033[%d;%dH", y, x); }
           17 void home() { printf("\033[;H"); }
           18 void clear_screen() { printf("\033[2J"); }
           19 void clear_line() { printf("\033[2K"); }
           20 void def() { printf("\033[0m"); }
           21 void reset() { printf("\033c"); }
           22 
           23 void sigint(int unused) {
           24   (void)unused;
           25   reset();
           26   exit(0);
           27 }
           28 
           29 int main(int argc, char* argv[]) {
           30   int fps = 20;
           31   int frame_lines = 25;
           32   int times = -1;
           33   const char *vtv_file;
           34   struct vtv* vtv;
           35 
           36   while (1) {
           37     switch (getopt(argc, argv, "r:h:t:")) {
           38     case 'r':
           39       fps = atoi(optarg);
           40       break;
           41     case 'h':
           42       frame_lines = atoi(optarg);
           43       break;
           44     case 't':
           45       times = atoi(optarg);
           46       break;
           47     case -1:
           48       if (optind == argc-1) {
           49         vtv_file = argv[optind];
           50         goto done;
           51       }
           52       // fallthrough
           53     default:
           54       fprintf(stderr, "Usage: %s [-r fps] [-h frameheight] [-t times] FILE\n", argv[0]);
           55       exit(1);
           56     }
           57   }
           58  done:
           59   vtv = vtv_read_from_file(vtv_file);
           60   if (vtv == NULL) {
           61     fprintf(stderr, "%s: cannot read %s: %s\n",
           62             argv[0], vtv_file, strerror(errno));
           63     exit(1);
           64   }
           65 
           66   int num_frames = (vtv->num_lines + frame_lines - 1) / frame_lines;
           67 
           68   hide_cursor();
           69   clear_screen();
           70 
           71   int frame = 0;
           72   signal(SIGINT, sigint);
           73 
           74   for (int i = times; i != 0; i--) {
           75     useconds_t nap = 1000000.0 / fps;
           76     frame = (frame+1) % num_frames;
           77     home();
           78     vtv_show_frame(vtv, stdout, frame, frame_lines,
           79                    "\033[0m   MISSING LINE");
           80     usleep(nap);
           81     if (i < 0)
           82       i = -1;
           83   }
           84   def();
           85   reset();
           86 }
           87