]> git.xolatile.top Git - public-libhl.git/commitdiff
hell
authoranon <anon@anon.anon>
Wed, 16 Aug 2023 15:30:29 +0000 (17:30 +0200)
committeranon <anon@anon.anon>
Wed, 16 Aug 2023 15:30:29 +0000 (17:30 +0200)
Makefile
src2/Highlight.h [new file with mode: 0644]
src2/main.cpp [new file with mode: 0644]

index f9862184c5d56e9812c3578969982e2a38e2f9be..6bc4f365c7d7dc9f051178357e18fa100e7f7a5b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -10,3 +10,6 @@ OBJ:=$(subst .c,.o,${SRC})
 
 hl: ${OBJ}
        ${LINK.c} ${OBJ} -o $@
+
+new:
+       g++ src2/main.cpp -o hl -ggdb
diff --git a/src2/Highlight.h b/src2/Highlight.h
new file mode 100644 (file)
index 0000000..29cceb2
--- /dev/null
@@ -0,0 +1,61 @@
+#include <map>
+#include <vector>
+#include <stdio.h>
+
+typedef enum {
+       BOLD,
+       ITALICS,
+       UNDERLINE,
+       END
+} attr_t;
+
+typedef void (*attr_callback_t)(void);
+void bold_on(void){
+       fputs("\033[1m", stdout);
+}
+void bold_off(void){
+       fputs("\033[0m", stdout);
+}
+attr_callback_t attr_callbacks[END*2] = {
+       (attr_callback_t)bold_on,
+       (attr_callback_t)bold_off,
+};
+
+typedef int color;
+
+struct hl_t {
+       attr_t attrs;
+       color fg_color;
+       color bg_color;
+       //? font;
+};
+
+struct hl_group {
+       char* name;
+       std::map<char*, hl_t> o;
+       hl_group* link;
+};
+
+typedef enum {
+       KEYWORD,
+       MATCH,
+       REGION
+} token_t;
+
+struct token{
+       hl_group* hl;
+       token_t t;
+       char* syntax;
+};
+
+std::vector<token*> token_table;
+
+token* newtoken(char* syntax, token_t t, hl_group* g){
+       token* mt = new (token){
+               .hl = g,
+               .t = t,
+               .syntax = syntax
+       };
+       token_table.push_back(mt);
+       return mt;
+}
diff --git a/src2/main.cpp b/src2/main.cpp
new file mode 100644 (file)
index 0000000..627b816
--- /dev/null
@@ -0,0 +1,45 @@
+#include "Highlight.h"
+
+const char *const test_string = "this (test) is my test string which im with testing in this test";
+
+int fits(const char* const pattern, const char* const to){
+       if(pattern == NULL){ return true; }
+       for(int i = 0;; i++){
+               if(pattern[i] == '\00'){ return i; }
+               if(to[i] == '\00' or pattern[i] != to[i]){ return false; }
+       }
+}
+
+void render_string(const char* const string, char* mode){
+       for(const char* s = string; *s != '\00';){
+               for(auto &i : token_table){
+                       int f;
+                       f = fits(i->syntax, s);
+                       if(f){
+                               int pos = i->hl->o.find(mode)->second.attrs;
+                               attr_callbacks[pos]();
+                               for(int h = 0; h < f; h++){
+                                       putchar(*(s+h));
+                               }
+                               attr_callbacks[pos+1]();
+                               s += f;
+                       }else{
+                               putchar(*s);
+                               ++s;
+                       }
+               }
+       }
+}
+
+signed main(){
+       hl_group mygroup = (hl_group){
+               .name = "test",
+               .link = NULL
+       };
+       mygroup.o["cterm"] = (hl_t){
+               .attrs = BOLD
+       };
+       token* mytoken = newtoken("test", KEYWORD, &mygroup);
+       render_string(test_string, "cterm");
+       putchar('\n');
+}