mirror of
https://git.lain.church/emil/libhl.git
synced 2025-05-12 22:56:45 +00:00
roast me
This commit is contained in:
parent
e82948f35d
commit
e41ff28910
11
source/hl.h
11
source/hl.h
@ -16,6 +16,10 @@ typedef enum {
|
|||||||
REGION
|
REGION
|
||||||
} token_type_t;
|
} token_type_t;
|
||||||
|
|
||||||
|
typedef void (*attribute_callback_t) (const char * const string,
|
||||||
|
const int length,
|
||||||
|
void * const attributes);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char * key;
|
char * key;
|
||||||
attribute_callback_t callback;
|
attribute_callback_t callback;
|
||||||
@ -33,10 +37,6 @@ typedef struct {
|
|||||||
char * syntax;
|
char * syntax;
|
||||||
} token_t;
|
} token_t;
|
||||||
|
|
||||||
typedef void (*attribute_callback_t) (const char * const string,
|
|
||||||
const int length,
|
|
||||||
void * const attributes);
|
|
||||||
|
|
||||||
// GLOBALS
|
// GLOBALS
|
||||||
|
|
||||||
token_t * token_table[1000];
|
token_t * token_table[1000];
|
||||||
@ -225,6 +225,9 @@ void render_string(const char * const string,
|
|||||||
// -------------------------
|
// -------------------------
|
||||||
// ### Library Mangement ###
|
// ### Library Mangement ###
|
||||||
// -------------------------
|
// -------------------------
|
||||||
|
hl_group_t * keyword_hl = NULL;
|
||||||
|
hl_group_t * preprocessor_hl = NULL;
|
||||||
|
hl_group_t * symbol_hl = NULL;
|
||||||
|
|
||||||
int hl_init(void) {
|
int hl_init(void) {
|
||||||
return 0;
|
return 0;
|
||||||
|
17
source/hl_c.inc
Normal file
17
source/hl_c.inc
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const char * c_keywords[] = {
|
||||||
|
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
|
||||||
|
"do", "while", "for", "continue", "switch", "case", "default", "break",
|
||||||
|
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
|
||||||
|
"char", "short", "int", "long", "signed", "unsigned", "float", "double",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
const char * preprocessor_keywords[] = {
|
||||||
|
"#include", "#pragma", "#define", "#undef", "#ifdef", "#ifndef", "#elifdef", "#elifndef",
|
||||||
|
"#if", "#elif", "#else", "#endif", "#embed", "#line", "#error", "#warning",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
new_char_tokens("&|()[]{}*,", symbol_hl);
|
||||||
|
new_keyword_tokens(c_keywords, keyword_hl);
|
||||||
|
new_keyword_tokens(preprocessor_keywords, preprocessor_hl);
|
@ -10,31 +10,6 @@
|
|||||||
static char * buffer = NULL;
|
static char * buffer = NULL;
|
||||||
static size_t buffer_size = 0;
|
static size_t buffer_size = 0;
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char * attribute;
|
|
||||||
char * foreground_color;
|
|
||||||
char * background_color;
|
|
||||||
} terminal_hl_t;
|
|
||||||
|
|
||||||
void cterm_render_callback(const char * const string,
|
|
||||||
const int length,
|
|
||||||
void * const attributes) {
|
|
||||||
if(!length){
|
|
||||||
fputs(TERMINAL_STYLE_BOLD, stdout);
|
|
||||||
putchar(*string);
|
|
||||||
fputs(TERMINAL_RESET, stdout);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
|
|
||||||
fputs(term_hl->attribute, stdout);
|
|
||||||
fputs(term_hl->foreground_color, stdout);
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
putchar(*(string+i));
|
|
||||||
}
|
|
||||||
fputs(TERMINAL_RESET, stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc,
|
int main(int argc,
|
||||||
char * * argv) {
|
char * * argv) {
|
||||||
UNUSED(argc);
|
UNUSED(argc);
|
||||||
@ -59,62 +34,9 @@ int main(int argc,
|
|||||||
buffer[buffer_size - 1] = '\0';
|
buffer[buffer_size - 1] = '\0';
|
||||||
|
|
||||||
// Highlight init
|
// Highlight init
|
||||||
hl_init();
|
terminal_hl_init();
|
||||||
const char * c_keywords[] = {
|
|
||||||
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
|
|
||||||
"do", "while", "for", "continue", "switch", "case", "default", "break",
|
|
||||||
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
|
|
||||||
"char", "short", "int", "long", "signed", "unsigned", "float", "double",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
const char * preprocessor_keywords[] = {
|
|
||||||
"#include", "#pragma", "#define", "#undef", "#ifdef", "#ifndef", "#elifdef", "#elifndef",
|
|
||||||
"#if", "#elif", "#else", "#endif", "#embed", "#line", "#error", "#warning",
|
|
||||||
NULL
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
display_t * cterm = &(display_t) {
|
#include "hl_c.inc";
|
||||||
.key = "cterm",
|
|
||||||
.callback = cterm_render_callback
|
|
||||||
};
|
|
||||||
//
|
|
||||||
terminal_hl_t terminal_keyword_hl = (terminal_hl_t) {
|
|
||||||
.attribute = TERMINAL_STYLE_BOLD,
|
|
||||||
.foreground_color = TERMINAL_COLOR_FG_GREEN,
|
|
||||||
.background_color = NULL
|
|
||||||
};
|
|
||||||
hl_group_t keyword_hl = (hl_group_t) {
|
|
||||||
.link = NULL,
|
|
||||||
.attributes = (void*)&terminal_keyword_hl
|
|
||||||
};
|
|
||||||
//
|
|
||||||
terminal_hl_t terminal_preprocessor_hl = (terminal_hl_t) {
|
|
||||||
.attribute = TERMINAL_STYLE_BOLD,
|
|
||||||
.foreground_color = TERMINAL_COLOR_FG_BLUE,
|
|
||||||
.background_color = NULL
|
|
||||||
};
|
|
||||||
hl_group_t preprocessor_hl = (hl_group_t) {
|
|
||||||
.link = NULL,
|
|
||||||
.attributes = (void*)&terminal_preprocessor_hl
|
|
||||||
};
|
|
||||||
//
|
|
||||||
terminal_hl_t terminal_symbol_hl = (terminal_hl_t) {
|
|
||||||
.attribute = TERMINAL_STYLE_BOLD,
|
|
||||||
.foreground_color = TERMINAL_COLOR_FG_YELLOW,
|
|
||||||
.background_color = NULL
|
|
||||||
};
|
|
||||||
hl_group_t symbol_hl = (hl_group_t) {
|
|
||||||
.link = NULL,
|
|
||||||
.attributes = (void*)&terminal_symbol_hl
|
|
||||||
};
|
|
||||||
//
|
|
||||||
new_display_mode(cterm);
|
|
||||||
new_char_tokens("&|()[]{}*,", &symbol_hl);
|
|
||||||
new_keyword_tokens(c_keywords, &keyword_hl);
|
|
||||||
new_keyword_tokens(preprocessor_keywords, &preprocessor_hl);
|
|
||||||
//
|
//
|
||||||
render_string(buffer, "cterm");
|
render_string(buffer, "cterm");
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
67
source/terminal_hl.h
Normal file
67
source/terminal_hl.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
typedef struct {
|
||||||
|
char * attribute;
|
||||||
|
char * foreground_color;
|
||||||
|
char * background_color;
|
||||||
|
} terminal_hl_t;
|
||||||
|
|
||||||
|
void cterm_render_callback(const char * const string,
|
||||||
|
const int length,
|
||||||
|
void * const attributes) {
|
||||||
|
if(!length){
|
||||||
|
fputs(TERMINAL_STYLE_BOLD, stdout);
|
||||||
|
putchar(*string);
|
||||||
|
fputs(TERMINAL_RESET, stdout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
|
||||||
|
fputs(term_hl->attribute, stdout);
|
||||||
|
fputs(term_hl->foreground_color, stdout);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
putchar(*(string+i));
|
||||||
|
}
|
||||||
|
fputs(TERMINAL_RESET, stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
display_t * cterm = &(display_t) {
|
||||||
|
.key = "cterm",
|
||||||
|
.callback = cterm_render_callback
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int terminal_hl_init(void){
|
||||||
|
hl_init();
|
||||||
|
new_display_mode(cterm);
|
||||||
|
//
|
||||||
|
terminal_hl_t terminal_keyword_hl = (terminal_hl_t) {
|
||||||
|
.attribute = TERMINAL_STYLE_BOLD,
|
||||||
|
.foreground_color = TERMINAL_COLOR_FG_GREEN,
|
||||||
|
.background_color = NULL
|
||||||
|
};
|
||||||
|
keyword_hl = (hl_group_t) {
|
||||||
|
.link = NULL,
|
||||||
|
.attributes = (void*)&terminal_keyword_hl
|
||||||
|
};
|
||||||
|
//
|
||||||
|
terminal_hl_t terminal_preprocessor_hl = (terminal_hl_t) {
|
||||||
|
.attribute = TERMINAL_STYLE_BOLD,
|
||||||
|
.foreground_color = TERMINAL_COLOR_FG_BLUE,
|
||||||
|
.background_color = NULL
|
||||||
|
};
|
||||||
|
preprocessor_hl = (hl_group_t) {
|
||||||
|
.link = NULL,
|
||||||
|
.attributes = (void*)&terminal_preprocessor_hl
|
||||||
|
};
|
||||||
|
//
|
||||||
|
terminal_hl_t terminal_symbol_hl = (terminal_hl_t) {
|
||||||
|
.attribute = TERMINAL_STYLE_BOLD,
|
||||||
|
.foreground_color = TERMINAL_COLOR_FG_YELLOW,
|
||||||
|
.background_color = NULL
|
||||||
|
};
|
||||||
|
symbol_hl = (hl_group_t) {
|
||||||
|
.link = NULL,
|
||||||
|
.attributes = (void*)&terminal_symbol_hl
|
||||||
|
};
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user