commit c9ca55cff9785280b3d27ea8a45e9654d7d1402d
parent f2bbcb62bc09d9b6619136dc10aa7e9f49daa0e2
Author: Remy Noulin <loader2x@gmail.com>
Date: Sun, 14 Oct 2018 22:04:39 +0200
add emi, README and example file
README.md | 33 ++++++++++++
emi.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++
package.yml | 22 ++++++++
someEmojiNames.txt | 3 ++
4 files changed, 212 insertions(+)
Diffstat:
4 files changed, 212 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,33 @@
+# Sheepy
+This is a sheepy package for [sheepy](https://github.com/RemyNoulin/sheepy) and using [libsheepy](https://github.com/RemyNoulin/libsheepy)
+
+# emi
+
+emi converts emoji short names to UTF8 code points.
+
+Currently, the emoji list supported is [UNICODE v11](https://unicode.org/emoji/charts/full-emoji-list.html).
+
+This tool is inspired by [emojify](https://github.com/mrowa44/emojify).
+
+# Usage
+
+Install with spm: `spm -g install emi`
+
+```
+emi -h
+[+] emi: help
+emi "string :grinning_face_with_big_eyes:" To convert emoji short names to symbols
+emi string :grinning_face_with_big_eyes: Also to convert emoji names
+emi filename To convert a file
+emi -list List of emoji names, the source list is UNICODE v11 at https://unicode.org/emoji/charts/full-emoji-list.html
+emi -h This help
+🐑
+```
+
+For example:
+```
+emi string :grinning_face_with_big_eyes:
+```
+
+output:
+> string :grinning_face_with_big_eyes:
diff --git a/emi.c b/emi.c
@@ -0,0 +1,154 @@
+#! /usr/bin/env sheepy
+#include "libsheepyObject.h"
+#include "shpPackages/emoji/emoji.h"
+#include <unistd.h>
+
+/* enable/disable logging */
+/* #undef pLog */
+/* #define pLog(...) */
+
+int main(int ARGC, char** ARGV) {
+
+ initLibsheepy(ARGV[0]);
+ setLogMode(LOG_PROG);
+
+ if (eqG(ARGV[1], "-h")) {
+ logI("help\n"
+ "emi \"string :grinning_face_with_big_eyes:\" To convert emoji short names to symbols\n"
+ "emi string :grinning_face_with_big_eyes: Also to convert emoji names\n"
+ "emi filename To convert a file\n"
+ "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"
+ "emi -h This help\n"
+ EM_EWE);
+ XSUCCESS;
+ }
+ elif (eqG(ARGV[1], "-list")) {
+ allocEmList();
+
+ createSmallArray(a);
+
+ iter(emList, E) {
+ cast(smallArrayt*, e, E);
+ pushG(&a, getG(e, rtChar, 0));
+ }
+
+ sortG(&a);
+
+ logI(BLD GRN "Emoji names:" RST);
+
+ iter(&a, N) {
+ printf(":%s:\n", ssGet(N));
+ }
+
+ freeG(&a);
+
+ freeEmList();
+ XSUCCESS;
+ }
+
+ // input string
+ char *s = NULL;
+
+ // emojify string in arguments
+ if (ARGC > 1) {
+ s = join(&ARGV[1], " ");
+ //logVarG(s);
+ }
+ else {
+ // no arguments
+ // check stdin
+
+ // read all the data from stdin to s
+ fd_set readfds;
+ FD_ZERO(&readfds);
+ FD_SET(STDIN_FILENO, &readfds);
+
+ struct timeval timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+
+ int chr;
+
+ int sel_rv = select(1, &readfds, NULL, NULL, &timeout);
+ if (sel_rv > 0) {
+ while ((chr = getchar()) != EOF) appendG(&s,chr);
+ } else if (sel_rv == -1) {
+ perror("select failed");
+ }
+
+ //logVarG(s);
+ if (isEmptyG(s)) {
+ XSUCCESS;
+ }
+ }
+
+ if (hasG(s, ':')) {
+ // there can be emoji names
+
+ convertNamesToUTF8Emoji:
+ allocEmList();
+
+ // split : and check odd indexes (odd indexes are between :)
+ char **spl = splitG(s, ':');
+ //logVarG(spl);
+
+ if (lenG(spl) == 2) {
+ // only one :, no possible emoji
+ prependG(&spl[1], ":");
+ }
+ elif (lenG(spl) > 2) {
+
+ // lookup emojis
+ rangeFromStep(i, /*from=*/1, lenG(spl), /*step=*/2) {
+ //logI(getG(spl, unusedV, i));
+
+ // whether the emoji name is found in emList
+ bool found = false;
+ iter(emList, E) {
+ cast(smallArrayt*, e, E);
+ if (eqG(getG(e, rtChar, 0), getG(spl, unusedV, i))) {
+ setG(spl, i, strdup(getG(e, rtChar, 1)));
+ found = true;
+ break;
+ }
+ } // iter emList
+
+ if (!found) {
+ // restore : since the item is not an emoji name
+ prependG(&spl[i], ":");
+ if ((i+1) != lenG(spl)) {
+ // not last item in split, so add : to item end
+ appendG(&spl[i], ":");
+ }
+ }
+ }
+ }
+
+ char *emojified = join(spl, "");
+
+ freeG(spl);
+ freeEmList();
+
+ puts(emojified);
+ free(emojified);
+ }
+ // no emoji short name detected
+ else {
+ // check if it is a filename
+ //logVarG(ARGV[1]);
+ if (!fileExists(s))
+ puts(s);
+ else {
+ char *f = readFileG(rtChar, s);
+
+ if (hasG(f, ':')) {
+ free(s);
+ s = f;
+ goto convertNamesToUTF8Emoji;
+ }
+ }
+ }
+
+ free(s);
+}
+// vim: set expandtab ts=2 sw=2:
diff --git a/package.yml b/package.yml
@@ -0,0 +1,22 @@
+---
+ name: emi
+ version: 0.0.1
+ description: "emoji cli to convert short names to UTF8 code points"
+ bin: ./emi.c
+ #cflags: -DA -ggdb -std=gnu11 -fPIC -pipe
+ #lflags: -lpcre
+ repository:
+ type: git
+ url: git+https://github.com/RemyNoulin/emi.git
+ keywords:
+ - utility
+ - command
+ - emoji
+ author: remynoulin
+ license: MIT
+ bugs:
+ url: https://github.com/RemyNoulin/emi/issues
+ homepage: https://github.com/RemyNoulin/emi#readme
+ dependencies:
+ emoji:
+ private: false # true for private package
diff --git a/someEmojiNames.txt b/someEmojiNames.txt
@@ -0,0 +1,3 @@
+:notebook: :old_key: :musical_notes: :office_building: :owl:
+
+:bomb: