emi

an emoji cli
git clone https://noulin.net/git/emi.git
Log | Files | Refs | README | LICENSE

emi.c (3440B)


      1 #! /usr/bin/env sheepy
      2 #include "libsheepyObject.h"
      3 #include "shpPackages/emoji/emoji.h"
      4 #include <unistd.h>
      5 
      6 /* enable/disable logging */
      7 /* #undef pLog */
      8 /* #define pLog(...) */
      9 
     10 int main(int ARGC, char** ARGV) {
     11 
     12   initLibsheepy(ARGV[0]);
     13   setLogMode(LOG_PROG);
     14 
     15   if (eqG(ARGV[1], "-h")) {
     16     logI("help\n"
     17         "emi \"string :grinning_face_with_big_eyes:\" To convert emoji short names to symbols\n"
     18         "emi string :grinning_face_with_big_eyes:   Also to convert emoji names\n"
     19         "emi filename                               To convert a file\n"
     20         "emi -list                                  List of emoji names, the source list is UNICODE v11 at " UDL "https://unicode.org/emoji/charts/full-emoji-list.html"RST"\n"
     21         "emi -h                                     This help\n"
     22          EM_EWE);
     23     XSUCCESS;
     24   }
     25   elif (eqG(ARGV[1], "-list")) {
     26     allocEmList();
     27 
     28     createSmallArray(a);
     29 
     30     iter(emList, E) {
     31       cast(smallArrayt*, e, E);
     32       pushG(&a, getG(e, rtChar, 0));
     33     }
     34 
     35     sortG(&a);
     36 
     37     logI(BLD GRN "Emoji names:" RST);
     38 
     39     iter(&a, N) {
     40       printf(":%s:\n", ssGet(N));
     41     }
     42 
     43     freeG(&a);
     44 
     45     freeEmList();
     46     XSUCCESS;
     47   }
     48 
     49   // input string
     50   char *s = NULL;
     51 
     52   // emojify string in arguments
     53   if (ARGC > 1) {
     54     s = join(&ARGV[1], " ");
     55     //logVarG(s);
     56   }
     57   else {
     58     // no arguments
     59     // check stdin
     60 
     61     // read all the data from stdin to s
     62     fd_set readfds;
     63     FD_ZERO(&readfds);
     64     FD_SET(STDIN_FILENO, &readfds);
     65 
     66     struct timeval timeout;
     67     timeout.tv_sec = 0;
     68     timeout.tv_usec = 0;
     69 
     70     int chr;
     71 
     72     int sel_rv = select(1, &readfds, NULL, NULL, &timeout);
     73     if (sel_rv > 0) {
     74       while ((chr = getchar()) != EOF) appendG(&s,chr);
     75     } else if (sel_rv == -1) {
     76       perror("select failed");
     77     }
     78 
     79     //logVarG(s);
     80     if (isEmptyG(s)) {
     81       XSUCCESS;
     82     }
     83   }
     84 
     85   if (hasG(s, ':')) {
     86     // there can be emoji names
     87 
     88     convertNamesToUTF8Emoji:
     89     allocEmList();
     90 
     91     // split : and check odd indexes (odd indexes are between :)
     92     char **spl = splitG(s, ':');
     93     //logVarG(spl);
     94 
     95     if (lenG(spl) == 2) {
     96       // only one :, no possible emoji
     97       prependG(&spl[1], ":");
     98     }
     99     elif (lenG(spl) > 2) {
    100 
    101       // lookup emojis
    102       rangeFromStep(i, /*from=*/1, lenG(spl), /*step=*/2) {
    103         //logI(getG(spl, unusedV, i));
    104 
    105         // whether the emoji name is found in emList
    106         bool found = false;
    107         iter(emList, E) {
    108           cast(smallArrayt*, e, E);
    109           if (eqG(getG(e, rtChar, 0), getG(spl, unusedV, i))) {
    110             setG(spl, (u64)i, strdup(getG(e, rtChar, 1)));
    111             found = true;
    112             break;
    113           }
    114         } // iter emList
    115 
    116         if (!found) {
    117           // restore : since the item is not an emoji name
    118           prependG(&spl[i], ":");
    119           if ((i+1) != lenG(spl)) {
    120             // not last item in split, so add : to item end
    121             appendG(&spl[i], ":");
    122           }
    123         }
    124       }
    125     }
    126 
    127     char *emojified = join(spl, "");
    128 
    129     freeG(spl);
    130     freeEmList();
    131 
    132     puts(emojified);
    133     free(emojified);
    134   }
    135   // no emoji short name detected
    136   else {
    137     // check if it is a filename
    138     //logVarG(ARGV[1]);
    139     if (!fileExists(s))
    140       puts(s);
    141     else {
    142       char *f = readFileG(rtChar, s);
    143 
    144       if (hasG(f, ':')) {
    145         free(s);
    146         s = f;
    147         goto convertNamesToUTF8Emoji;
    148       }
    149     }
    150   }
    151 
    152   free(s);
    153 }
    154 // vim: set expandtab ts=2 sw=2: