96 lines
2.8 KiB
C
96 lines
2.8 KiB
C
#include "hl.h"
|
|
|
|
// Terminal manipulation
|
|
#define TERMINAL_RESET "\033[0m"
|
|
|
|
#define TERMINAL_COLOR_FG_BLACK "\033[30m"
|
|
#define TERMINAL_COLOR_FG_RED "\033[31m"
|
|
#define TERMINAL_COLOR_FG_GREEN "\033[32m"
|
|
#define TERMINAL_COLOR_FG_YELLOW "\033[33m"
|
|
#define TERMINAL_COLOR_FG_BLUE "\033[34m"
|
|
#define TERMINAL_COLOR_FG_MAGENTA "\033[35m"
|
|
#define TERMINAL_COLOR_FG_CYAN "\033[36m"
|
|
#define TERMINAL_COLOR_FG_WHITE "\033[37m"
|
|
|
|
#define TERMINAL_COLOR_BG_BLACK "\033[40m"
|
|
#define TERMINAL_COLOR_BG_RED "\033[41m"
|
|
#define TERMINAL_COLOR_BG_GREEN "\033[42m"
|
|
#define TERMINAL_COLOR_BG_YELLOW "\033[43m"
|
|
#define TERMINAL_COLOR_BG_BLUE "\033[44m"
|
|
#define TERMINAL_COLOR_BG_MAGENTA "\033[45m"
|
|
#define TERMINAL_COLOR_BG_CYAN "\033[46m"
|
|
#define TERMINAL_COLOR_BG_WHITE "\033[47m"
|
|
|
|
#define TERMINAL_STYLE_BOLD "\033[1m"
|
|
#define TERMINAL_STYLE_ITALICS "\033[3m"
|
|
#define TERMINAL_STYLE_REVERSE "\033[7m"
|
|
|
|
typedef struct {
|
|
const char * attribute;
|
|
const char * foreground_color;
|
|
const char * background_color;
|
|
} terminal_hl_t;
|
|
|
|
extern display_t * cterm;
|
|
|
|
extern void cterm_render_callback(const char * const string,
|
|
const int length,
|
|
void * const attributes);
|
|
|
|
extern int terminal_hl_init(void);
|
|
|
|
display_t * cterm = &(display_t) {
|
|
.key = "cterm",
|
|
.callback = cterm_render_callback
|
|
};
|
|
|
|
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;
|
|
if (term_hl->attribute) {
|
|
fputs(term_hl->attribute, stdout);
|
|
}
|
|
if (term_hl->foreground_color) {
|
|
fputs(term_hl->foreground_color, stdout);
|
|
}
|
|
for (int i = 0; i < length; i++) {
|
|
putchar(*(string+i));
|
|
}
|
|
fputs(TERMINAL_RESET, stdout);
|
|
}
|
|
|
|
|
|
void fun(const char * attribute,
|
|
const char * color,
|
|
hl_group_t * * group){
|
|
terminal_hl_t * t = (terminal_hl_t *) malloc(sizeof(terminal_hl_t));
|
|
t->attribute = attribute;
|
|
t->foreground_color = color;
|
|
t->background_color = NULL;
|
|
(*group) = (hl_group_t *)malloc(sizeof(hl_group_t));
|
|
(*group)->link = NULL;
|
|
(*group)->attributes = (void*)t;
|
|
}
|
|
|
|
int terminal_hl_init(void) {
|
|
hl_init();
|
|
new_display_mode(cterm);
|
|
//
|
|
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_CYAN, &special_hl);
|
|
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_YELLOW, &control_hl);
|
|
fun(NULL, TERMINAL_COLOR_FG_YELLOW, &operator_hl);
|
|
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_GREEN, &keyword_hl);
|
|
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_BLUE, &comment_hl);
|
|
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_RED, &string_literal_hl);
|
|
|
|
return 0;
|
|
}
|