This commit is contained in:
anon 2023-08-16 17:30:29 +02:00
parent 7d8e55eaef
commit 26a160f6b0
3 changed files with 109 additions and 0 deletions

View File

@ -10,3 +10,6 @@ OBJ:=$(subst .c,.o,${SRC})
hl: ${OBJ} hl: ${OBJ}
${LINK.c} ${OBJ} -o $@ ${LINK.c} ${OBJ} -o $@
new:
g++ src2/main.cpp -o hl -ggdb

61
src2/Highlight.h Normal file
View File

@ -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;
}

45
src2/main.cpp Normal file
View File

@ -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');
}