Skip to content

Commit

Permalink
fix invalid highlighting, new: customize colors with wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
ikozyris committed Jun 22, 2024
1 parent b08b818 commit 7617f6a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
1 change: 0 additions & 1 deletion headers/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
#include <unistd.h>
#include <locale.h> // for setting locale
#include <sys/stat.h> // for file size
#include <ctype.h>
#include <list>
#include <time.h>
51 changes: 28 additions & 23 deletions utils/highlight.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#include "init.c"

const char *types[] = {"int", "char", "float", "double", "unsigned", "void", "const",
"size_t", "signed", "long", "enum", "static", "short", "extern", 0};
"size_t", "bool", "signed", "long", "enum", "static", "short", "extern"};
const char *defs[] = {"if", "else", "while", "for", "do", "return", "sizeof", "switch",
"goto", "case", "break", "struct", "default", "continue", 0};
"goto", "case", "break", "struct", "default", "continue", "true", "false"};
const char *oper[] = {"=", "+=", "+", "-", "-=", "*", "*=", "/", "/=", "%%", "&", "++",
"--", "==", "<", ">", "\'", 0};
"--", "==", "<", ">", "\'"};

#define RED "\x1b[1;31m"
#define GREEN "\x1b[1;32m"
#define YELLOW "\x1b[1;33m"
#define BLUE "\x1b[1;34m"
#define MAGENTA "\x1b[1;35m"
#define CYAN "\x1b[1;36m"
#define RESET "\x1b[0m"
#define DEFINC COLOR_CYAN
#define COMMENT COLOR_GREEN
#define TYPES COLOR_RED
#define OPER COLOR_YELLOW
#define DEFS COLOR_BLUE
#define STR COLOR_MAGENTA

#define nelems(x) (sizeof(x) / sizeof((x)[0]))
#define isvalid(ch) (ch < 65 || ch > 122)

struct res_t {
short len;
Expand All @@ -28,7 +30,7 @@ struct res_t get_category2(const char *line)

unsigned j = 0;
bool found = true;
for (unsigned i = 0; i < 14; ++i) {
for (unsigned i = 0; i < nelems(types); ++i) {
found = true;
for (j = 0; types[i][j] != 0; ++j)
if (types[i][j] != line[j])
Expand All @@ -38,7 +40,7 @@ struct res_t get_category2(const char *line)
res.type = 't';
}
}
for (unsigned i = 0; i < 14; ++i) {
for (unsigned i = 0; i < nelems(defs); ++i) {
found = true;
for (j = 0; defs[i][j] != 0; ++j)
if (defs[i][j] != line[j])
Expand All @@ -48,7 +50,7 @@ struct res_t get_category2(const char *line)
res.type = 'd';
}
}
for (unsigned i = 0; i < 17; ++i) {
for (unsigned i = 0; i < nelems(oper); ++i) {
found = true;
for (j = 0; oper[i][j] != 0; ++j)
if (oper[i][j] != line[j])
Expand All @@ -72,43 +74,46 @@ void apply(unsigned line)
for (unsigned i = 0; i < len2; ++i) {
if (str[i] == '#') { // define / include
wmove(text_win, line, i);
wchgat(text_win, -1, 0, COLOR_CYAN, 0);
wchgat(text_win, -1, 0, DEFINC, 0);
return;
} // comments
else if (str[i] == '/' && str[i + 1] == '/') {
wmove(text_win, line, i);
wchgat(text_win, -1, 0, COLOR_GREEN, 0);
wchgat(text_win, -1, 0, COMMENT, 0);
return;
} else if (str[i] == '/' && str[i + 1] == '*') {
previ = i;
i += 2;
wmove(text_win, line, previ);
while (str[i] != '*' && str[i + 1] != '/')
++i;
wchgat(text_win, i-previ+2, 0, COLOR_GREEN, 0);
wchgat(text_win, i++ - previ + 2, 0, COMMENT, 0);
} // string / char
else if (str[i] == '\'') {
previ = i++;
wmove(text_win, line, previ);
while (str[i] != '\'')
++i;
wchgat(text_win, i-previ+1, 0, COLOR_MAGENTA, 0);
wchgat(text_win, i - previ + 1, 0, STR, 0);
} else if (str[i] == '\"') {
previ = i++;
wmove(text_win, line, previ);
while (str[i] != '\"')
++i;
wchgat(text_win, i-previ+1, 0, COLOR_MAGENTA, 0);
wchgat(text_win, i - previ + 1, 0, STR, 0);
} // type (int, char) / keyword (if, return) / operator (==, +)
else {
wmove(text_win, line, i);
struct res_t res = get_category2(str + i);
if (res.type == 't')
wchgat(text_win, res.len, 0, COLOR_RED, 0);
else if (res.type == 'd')
wchgat(text_win, res.len, 0, COLOR_BLUE, 0);
if (res.type == 't' && isvalid(str[i + res.len])
&& (i == 0 ? 1 : isvalid(str[i - 1])))
wchgat(text_win, res.len, 0, TYPES, 0);
else if (res.type == 'd' && isvalid(str[i + res.len])
&& (i == 0 ? 1 : isvalid(str[i - 1])))
wchgat(text_win, res.len, 0, DEFS, 0);
else if (res.type == 'o')
wchgat(text_win, res.len, 0, COLOR_YELLOW, 0);
wchgat(text_win, res.len, 0, OPER, 0);
}
}
}

55 changes: 49 additions & 6 deletions wizard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ key_config() {
exec 3>&1 # open stdout fd
OUTPUT=$(DIALOG --title "Edit Keybindings" \
--form "Edit the fields" $HEIGHT $WIDTH 0 \
"Save:" 1 1 "$KEY_SAVE" 1 15 15 0 \
"Exit:" 2 1 "$KEY_EXIT" 2 15 15 0 \
"Home:" 3 1 "$KEY_HOME" 3 15 15 0 \
"End:" 4 1 "$KEY_END" 4 15 15 0 \
"Info:" 5 1 "$KEY_INFO" 5 15 15 0 \
"Refresh:" 6 1 "$KEY_REFRESH" 6 15 15 0 \
"Save" 1 1 "$KEY_SAVE" 1 15 15 0 \
"Exit" 2 1 "$KEY_EXIT" 2 15 15 0 \
"Home" 3 1 "$KEY_HOME" 3 15 15 0 \
"End" 4 1 "$KEY_END" 4 15 15 0 \
"Info" 5 1 "$KEY_INFO" 5 15 15 0 \
"Refresh" 6 1 "$KEY_REFRESH" 6 15 15 0 \
2>&1 1>&3)
exit_status=$?
case $exit_status in
Expand All @@ -72,6 +72,41 @@ key_config() {
sed -i "19s/$KEY_REFRESH/${outputarray[5]}/" headers/keybindings.h
}

color_config() {
mapfile -t colors < <(sed "s/.*COLOR_//g" utils/highlight.c | head -n 15 | tail -n 6)

exec 3>&1 # open stdout fd
OUTPUT=$(DIALOG --title "Edit Colors" \
--form "Edit the fields" $HEIGHT $WIDTH 0 \
"#include/define" 1 1 "${colors[0]}" 1 20 20 0 \
"Comments" 2 1 "${colors[1]}" 2 20 20 0 \
"Types" 3 1 "${colors[2]}" 3 20 20 0 \
"Operators" 4 1 "${colors[3]}" 4 20 20 0 \
"if/while" 5 1 "${colors[4]}" 5 20 20 0 \
"Strings" 6 1 "${colors[5]}" 6 20 20 0 \
2>&1 1>&3)
exit_status=$?
case $exit_status in
"$DIALOG_CANCEL")
clear
return
;;
"$DIALOG_ESC")
clear
return
;;
esac
exec 3>&- # delete fd

outputarray=($OUTPUT) # convert to array
# Replace the key* with the output of dialog
sed -i "10s/${colors[0]}/${outputarray[0]}/" utils/highlight.c
sed -i "11s/${colors[1]}/${outputarray[1]}/" utils/highlight.c
sed -i "12s/${colors[2]}/${outputarray[2]}/" utils/highlight.c
sed -i "13s/${colors[3]}/${outputarray[3]}/" utils/highlight.c
sed -i "14s/${colors[4]}/${outputarray[4]}/" utils/highlight.c
sed -i "15s/${colors[5]}/${outputarray[5]}/" utils/highlight.c
}

config_dialog() {
high_st="Disable"
Expand All @@ -87,6 +122,7 @@ config_dialog() {
"1" "Edit Keybindings" \
"2" "Install dependencies" \
"3" "$high_st syntax highlighting" \
"4" "Change colors of syntax highlighting" \
2>&1 1>&3) # redirect output streams
exit_status=$?
exec 3>&- # delete fd
Expand Down Expand Up @@ -129,6 +165,13 @@ config_dialog() {
sed -i 's/CXXFLAGS = -Ofast -fopenmp -march=native -flto -DHIGHLIGHT -lncursesw/CXXFLAGS = -Ofast -fopenmp -march=native -flto -lncursesw/g' Makefile
fi
;;
4 )
if [ $DIAL = "dialog" ]; then
color_config
else
display_result "dialog not installed, but you can still edit utils/highlighting.h lines 10-15"
fi
;;
esac
done
}
Expand Down

0 comments on commit 7617f6a

Please sign in to comment.