From 26a160f6b0777bf4400d56205071c7c153c56901 Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 16 Aug 2023 17:30:29 +0200 Subject: [PATCH] hell --- Makefile | 3 +++ src2/Highlight.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src2/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src2/Highlight.h create mode 100644 src2/main.cpp diff --git a/Makefile b/Makefile index f986218..6bc4f36 100644 --- 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 index 0000000..29cceb2 --- /dev/null +++ b/src2/Highlight.h @@ -0,0 +1,61 @@ +#include +#include +#include + +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 o; + hl_group* link; +}; + +typedef enum { + KEYWORD, + MATCH, + REGION +} token_t; + +struct token{ + hl_group* hl; + token_t t; + char* syntax; +}; + +std::vector 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 index 0000000..627b816 --- /dev/null +++ b/src2/main.cpp @@ -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'); +}