systemSetup

system setup, configuration and dotfiles
git clone https://noulin.net/git/systemSetup.git
Log | Files | Refs | README | LICENSE

showMyHistory.c (3038B)


      1 #! /usr/bin/env sheepy
      2 #include "libsheepyObject.h"
      3 #define H "~/myhistory.txt"
      4 #define HE "~/myhistoryExit.txt"
      5 #define HP "~/myhistoryPath.txt"
      6 #define HB "~/myhistory.bin"
      7 
      8 #define maxPathLen 50
      9 
     10 int main(int ARGC, char** ARGV) {
     11   initLibsheepy(ARGV[0]);
     12 
     13   cleanCharP(hbp) = expandHomeG(HB);
     14 
     15   u8 *buf = null;
     16   size_t hlen = readFile(hbp, (void**)&buf);
     17 
     18   // count commands
     19   // search for longest path
     20   u8 *e = buf;
     21   cleanAllocateSmallJson(a);
     22   u64 commandCount = 0;
     23   u16 max = 0;
     24   do {
     25     u32 len = *(u32*)e;
     26     cleanAllocateSmallBytes(b);
     27     o(b,set, e+4, len-8);
     28     deserialG(a, b);
     29     // a = [path,exit code, date, command]
     30     max = maxV(max, lenG(getG(a, rtChar, 0)));
     31     inc commandCount;
     32     e += len;
     33   } while(e < buf + hlen);
     34 
     35   bool ellipsis = (max > maxPathLen) ? yes : no;
     36   if (ellipsis) max = maxPathLen;
     37 
     38   // show history
     39   e         = buf;
     40   u64 count = 0;
     41   // compute length of commandCount to align columns
     42   char counts[32] = init0Var;
     43   pErrorNULL(bIntToS(counts,commandCount));
     44   u32 countLen    = strlen(counts);
     45   cleanAllocateSmallBytes(b);
     46   char ellip[256];
     47   do {
     48     inc count;
     49     u32 len = *(u32*)e;
     50     o(b,set, e+4, len-8);
     51     deserialG(a, b);
     52     // a = [path,exit code, date, command]
     53     u32 exitCode = parseIntG(getG(a, rtChar, 1));
     54     char *res = bEllipsisStartS(ellip /*dest*/, getG(a, rtChar, 0)/*path string*/, max /*targetLen*/, "]"/*ellipsisString*/);
     55     char *path = ellipsis ? ellip : getG(a, rtChar, 0);
     56     if (exitCode) {
     57       printf("%*d "RED"%3d"RST" %s "YLW"%*s"RST" "RED"%s"RST"\n", countLen, (u32)count, exitCode, /*date*/getG(a, rtChar, 2), max, path, /*cmd*/getG(a, rtChar, 3));
     58     }
     59     else {
     60       printf("%*d "GRN"%3d"RST" %s "YLW"%*s"RST" %s\n", countLen, (u32)count, exitCode, /*date*/getG(a, rtChar, 2), max, path, /*cmd*/getG(a, rtChar, 3));
     61     }
     62     e += len;
     63   } while(e < buf + hlen);
     64 
     65   ret 0;
     66 }
     67 
     68 #if 0
     69 int main(int ARGC, char** ARGV) {
     70   initLibsheepy(ARGV[0]);
     71   cleanAllocateSmallArray(h);
     72   cleanAllocateSmallArray(he);
     73   cleanAllocateSmallArray(hP);
     74   cleanCharP(hp)  = expandHomeG(H);
     75   cleanCharP(hep) = expandHomeG(HE);
     76   cleanCharP(hpp) = expandHomeG(HP);
     77   readFileG(h, hp);
     78   readFileG(he, hep);
     79   readFileG(hP, hpp);
     80 
     81   u16 max = 0;
     82   iter(hP, E) {
     83     castS(e,E);
     84     max = maxV(max, lenG(e));
     85   }
     86   u32 count       = lenG(h);
     87   char counts[32] = init0Var;
     88   pErrorNULL(bIntToS(counts,count));
     89   count           = strlen(counts);
     90   iter(he, e) {
     91     char *s      = ssGet(e);
     92     if (not hasG(s, ' ')) continue;
     93     char *date   = findS(s, " ") +1;
     94     u32 exitCode = parseIntG(s);
     95     if (exitCode) {
     96       printf("%*d "RED"%3d"RST" %s "YLW"%*s"RST" "RED"%s"RST"\n", count, iterIndexG(he)+1, exitCode, date, max, getG(hP, rtChar, iterIndexG(he)), getG(h, rtChar, iterIndexG(he)));
     97     }
     98     else {
     99       printf("%*d "GRN"%3d"RST" %s "YLW"%*s"RST" %s\n", count, iterIndexG(he)+1, exitCode, date, max, getG(hP, rtChar, iterIndexG(he)), getG(h, rtChar, iterIndexG(he)));
    100     }
    101   }
    102   ret 0;
    103 }
    104 #endif
    105 // vim: set expandtab ts=2 sw=2: