💾 Archived View for gmi.noulin.net › gitRepositories › clispinner › file › clispinner.c.gmi captured on 2024-07-09 at 00:12:08. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

clispinner

Log

Files

Refs

LICENSE

clispinner.c (5842B)

     1 
     2 #include "libsheepyObject.h"
     3 #include "clispinner.h"
     4 #include "clispinnerInternal.h"
     5 
     6 
     7 #include <stdlib.h>
     8 #include <string.h>
     9 #include <stdio.h>
    10 
    11 void initiateClispinner(clispinnert *self);
    12 void registerMethodsClispinner(clispinnerFunctionst *f);
    13 void initiateAllocateClispinner(clispinnert **self);
    14 void finalizeClispinner(void);
    15 clispinnert* allocClispinner(u64 interval, const char *title);
    16 internal void freeClispinner(clispinnert *self);
    17 internal void terminateClispinner(clispinnert **self);
    18 internal char* toStringClispinner(clispinnert *self);
    19 internal clispinnert* duplicateClispinner(clispinnert *self);
    20 internal void smashClispinner(clispinnert **self);
    21 internal void finishClispinner(clispinnert **self);
    22 internal void startClispinner(clispinnert *self);
    23 internal void stopClispinner(clispinnert *self);
    24 internal void createTheSignalTimerClispinner(clispinnert *self);
    25 internal void setSignalTimerClispinner(clispinnert *self, signalTimert * signalTimer);
    26 internal signalTimert* getSignalTimerClispinner(clispinnert *self);
    27 internal void pauseProcessClispinner(clispinnert *self);
    28 internal void setSpinStringClispinner(clispinnert *self, const char *spinString);
    29 internal void setIntervalClispinner(clispinnert *self, u64 interval);
    30 internal bool isSpinningClispinner(clispinnert *self);
    31 
    32 static signalTimert *theSignalTimerAddr  = NULL;
    33 static bool cliSpinnerAllocatedTheSignalTimer = false;
    34 
    35 internal void doSpin(void *arg);
    36 
    37 void initiateClispinner(clispinnert *self) {
    38 
    39   self->type = "clispinner";
    40   if (!clispinnerF) {
    41     clispinnerF            = malloc(sizeof(clispinnerFunctionst));
    42     registerMethodsClispinner(clispinnerF);
    43     pErrorNot0(atexit(finalizeClispinner));
    44   }
    45   self->f          = clispinnerF;
    46   self->isSpinning = false;
    47   self->interval   = 100;
    48   self->title[0]   = 0;
    49   strcpy(self->spinString, "|/-\\");
    50 }
    51 
    52 void registerMethodsClispinner(clispinnerFunctionst *f) {
    53 
    54   f->free                 = freeClispinner;
    55   f->terminate            = terminateClispinner;
    56   f->toString             = toStringClispinner;
    57   f->duplicate            = duplicateClispinner;
    58   f->smash                = smashClispinner;
    59   f->finish               = finishClispinner;
    60   f->start                = startClispinner;
    61   f->stop                 = stopClispinner;
    62   f->createTheSignalTimer = createTheSignalTimerClispinner;
    63   f->setSignalTimer       = setSignalTimerClispinner;
    64   f->getSignalTimer       = getSignalTimerClispinner;
    65   f->pauseProcess         = pauseProcessClispinner;
    66   f->setSpinString        = setSpinStringClispinner;
    67   f->setInterval          = setIntervalClispinner;
    68   f->isSpinning           = isSpinningClispinner;
    69 }
    70 
    71 void initiateAllocateClispinner(clispinnert **self) {
    72 
    73   if (self) {
    74     (*self) = malloc(sizeof(clispinnert));
    75     if (*self) {
    76       initiateClispinner(*self);
    77     }
    78   }
    79 }
    80 
    81 void finalizeClispinner(void) {
    82 
    83   if (clispinnerF) {
    84     free(clispinnerF);
    85     clispinnerF = NULL;
    86   }
    87 }
    88 
    89 clispinnert* allocClispinner(u64 interval, const char *title) {
    90   clispinnert *r = NULL;
    91 
    92   initiateAllocateClispinner(&r);
    93   createTheSignalTimerClispinner(r);
    94   r->interval = interval;
    95 
    96   if (title) {
    97     strLCpy(r->title, CLISPINNERTITLESIZE, title);
    98   }
    99   else r->title[0] = 0;
   100   return(r);
   101 }
   102 
   103 
   104 internal void freeClispinner(clispinnert *self) {
   105 
   106   /* no internal data to free */
   107   return;
   108 }
   109 
   110 internal void terminateClispinner(clispinnert **self) {
   111 
   112   // stop and terminate theSignalTimerAddr if clispinner allocated it
   113   if (theSignalTimerAddr and cliSpinnerAllocatedTheSignalTimer) {
   114     terminateG(theSignalTimerAddr);
   115     cliSpinnerAllocatedTheSignalTimer = false;
   116   }
   117 
   118   freeClispinner(*self);
   119   free(*self);
   120   *self = NULL;
   121 }
   122 
   123 
   124 internal char* toStringClispinner(clispinnert *self) {
   125 
   126   return(strdup("TODO - class template"));
   127 }
   128 
   129 internal clispinnert* duplicateClispinner(clispinnert *self) {
   130 
   131   createAllocateClispinner(dup);
   132   /* TODO COPY data */
   133   return(dup);
   134 }
   135 
   136 internal void smashClispinner(clispinnert **self) {
   137 
   138   finishClispinner(self);
   139 }
   140 
   141 internal void finishClispinner(clispinnert **self) {
   142 
   143   free(*self);
   144   *self = NULL;
   145 }
   146 
   147 
   148 internal void startClispinner(clispinnert *self) {
   149 
   150   if (!theSignalTimerAddr) return;
   151 
   152   indexerInit(self->ix, strlen(self->spinString));
   153   startTimerG(theSignalTimerAddr, self->interval, doSpin, self);
   154 
   155   printf(self->title);
   156   self->isSpinning = true;
   157   // set unbuffered stdout, to show the characters directly
   158   //setbuf(stdout, NULL);
   159 }
   160 
   161 internal void stopClispinner(clispinnert *self) {
   162   if (!theSignalTimerAddr) return;
   163   stopG(theSignalTimerAddr);
   164   self->isSpinning = false;
   165 }
   166 
   167 internal void createTheSignalTimerClispinner(clispinnert *self) {
   168   initiateAllocateSignalTimer(&theSignalTimerAddr);
   169   cliSpinnerAllocatedTheSignalTimer = true;
   170 }
   171 
   172 internal void setSignalTimerClispinner(clispinnert *self, signalTimert * signalTimer) {
   173   theSignalTimerAddr = signalTimer;
   174 }
   175 
   176 internal signalTimert* getSignalTimerClispinner(clispinnert *self) {
   177   return theSignalTimerAddr;
   178 }
   179 
   180 internal void pauseProcessClispinner(clispinnert *self) {
   181   if (!theSignalTimerAddr) return;
   182   pauseProcessG(theSignalTimerAddr);
   183 }
   184 
   185 internal void setSpinStringClispinner(clispinnert *self, const char *spinString) {
   186   if (spinString) {
   187     strLCpy(self->spinString, 32, spinString);
   188   }
   189 }
   190 
   191 internal void setIntervalClispinner(clispinnert *self, u64 interval) {
   192   self->interval = interval;
   193   if (!theSignalTimerAddr) return;
   194   setIntervalG(theSignalTimerAddr, interval);
   195 }
   196 
   197 internal bool isSpinningClispinner(clispinnert *self) {
   198   return self->isSpinning;
   199 }
   200 
   201 internal void doSpin(void *arg) {
   202   cast(clispinnert *, this, arg);
   203 
   204   indexerPush(this->ix);
   205   putchar(this->spinString[this->ix.last]);
   206   fflush(stdout);
   207   putchar('\b');
   208 }
   209 
   210 bool checkLibsheepyVersionClispinner(const char *currentLibsheepyVersion) {
   211   return eqG(currentLibsheepyVersion, LIBSHEEPY_VERSION);
   212 }
   213