Skip to content

Commit

Permalink
feat: highlight numbers by coloring each digit red
Browse files Browse the repository at this point in the history
  • Loading branch information
cezelot committed Sep 5, 2024
1 parent 1185d33 commit 7aff4e8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SRC_FILES := main.c \
editor_operations.c \
find.c \
options.c \
syntax_highlighting.c \
file/file_io.c \
file/save_file_operations.c \
input/input.c \
Expand Down
19 changes: 14 additions & 5 deletions src/eve.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eve.h */
/* */
/* Created: 2023/11/27 17:17:10 by cezelot */
/* Updated: 2024/08/19 18:13:42 by cezelot */
/* Updated: 2024/09/05 08:07:47 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara, Alberto Rodriguez */
/* */
Expand Down Expand Up @@ -69,11 +69,17 @@ enum e_editor_key {
PAGE_DOWN
};

enum e_editor_highlight {
HL_NORMAL = 0,
HL_NUMBER
};

typedef struct s_editor_row {
char *chars;
char *render;
int size;
int rsize;
int size;
int rsize;
char *chars;
char *render;
unsigned char *hl;
} t_erow;

typedef struct s_editor_config {
Expand Down Expand Up @@ -128,6 +134,9 @@ int write_file(int fd, int len, char *buf, t_env *env);
void find(t_env *env);
// ---------------------------------------------------------------- options.c --
void parse_options(int ac, char **av, int *option_index);
// ---------------------------------------------------- syntax_highlighting.c --
int syntax_to_color(int hl);
void update_syntax(t_erow *row);
// ------------------------------------------------------------------ input.c --
void move_cursor(t_env *env, int key);
void handle_keypress(t_env *env);
Expand Down
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eve - simple terminal-based text editor */
/* */
/* Created: 2023/11/26 12:20:29 by cezelot */
/* Updated: 2024/08/17 18:47:42 by alberrod */
/* Updated: 2024/09/05 08:50:15 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand Down Expand Up @@ -49,7 +49,8 @@ close_editor(t_env *env)
free(env->filename);
while (i < env->numrows) {
free(env->row[i].chars);
free(env->row[i++].render);
free(env->row[i].render);
free(env->row[i++].hl);
}
free(env->row);
}
Expand Down
40 changes: 36 additions & 4 deletions src/output/output_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* output_2.c */
/* */
/* Created: 2024/05/29 10:19:15 by cezelot */
/* Updated: 2024/05/30 18:25:02 by cezelot */
/* Updated: 2024/09/05 10:25:30 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand Down Expand Up @@ -37,19 +37,51 @@ set_status_message(t_env *env, const char *format, ...)
env->statusmsg_time = time(NULL);
}

static void
print_color_escape_sequence(t_abuf *abuf, int color)
{
char buf[16];
int clen;

clen = snprintf(buf, sizeof(buf), "\x1b[%dm", color);
abuf_append(abuf, buf, clen);
}

void
display_text_buffer(t_env *env, t_abuf *abuf, int filerow)
{
int len;
unsigned char *hl;
char *s;
int len;
int color = 0;
int current_color = -1;

hl = &env->row[filerow].hl[env->coloff];
s = &env->row[filerow].render[env->coloff];
len = env->row[filerow].rsize - env->coloff;
if (len < 0) {
len = 0;
}
if (len > env->screencols) {
len = env->screencols;
}
abuf_append(abuf, &env->row[filerow].render[env->coloff], len);
for (int i = 0; i < len; ++i) {
if (hl[i] == HL_NORMAL) {
if (current_color != -1) {
abuf_append(abuf, "\x1b[39m", 5);
current_color = -1;
}
abuf_append(abuf, &s[i], 1);
} else {
color = syntax_to_color(hl[i]);
if (color != current_color) {
current_color = color;
print_color_escape_sequence(abuf, current_color);
}
abuf_append(abuf, &s[i], 1);
}
}
abuf_append(abuf, "\x1b[39m", 5);
}

void
Expand All @@ -60,7 +92,7 @@ display_welcome_message(t_env *env, t_abuf *abuf)
int padding;

welcomelen = snprintf(welcome, sizeof(welcome),
"eve editor -- version %s", VERSION);
"eve - version %s", VERSION);
if (welcomelen > env->screencols) {
welcomelen = env->screencols;
}
Expand Down
4 changes: 3 additions & 1 deletion src/row_operations/row_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* row_operations.c */
/* */
/* Created: 2023/12/31 18:08:27 by cezelot */
/* Updated: 2024/07/23 19:07:08 by cezelot */
/* Updated: 2024/09/01 16:06:43 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand Down Expand Up @@ -74,6 +74,7 @@ update_row(t_erow *row)
}
row->render[i] = '\0';
row->rsize = i;
update_syntax(row);
}

/* Insert the character C into CHARS, at position INDEX,
Expand Down Expand Up @@ -120,6 +121,7 @@ insert_row(t_env *env, char *str, size_t len, int index)
env->row[index].chars[len] = '\0';
env->row[index].rsize = 0;
env->row[index].render = NULL;
env->row[index].hl = NULL;
update_row(&env->row[index]);
env->numrows++;
env->dirty++;
Expand Down
3 changes: 2 additions & 1 deletion src/row_operations/row_operations_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* row_operations_2.c */
/* */
/* Created: 2024/07/22 11:34:50 by cezelot */
/* Updated: 2024/07/27 14:50:42 by cezelot */
/* Updated: 2024/09/01 10:20:29 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
Expand Down Expand Up @@ -78,6 +78,7 @@ free_row(t_erow *row)
{
free(row->render);
free(row->chars);
free(row->hl);
}

/* Delete ROW at INDEX. */
Expand Down
55 changes: 55 additions & 0 deletions src/syntax_highlighting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* syntax_highlighting.c */
/* */
/* Created: 2024/09/01 15:58:19 by cezelot */
/* Updated: 2024/09/01 16:17:10 by cezelot */
/* */
/* Copyright (C) 2024 Ismael Benjara */
/* */
/* This file is part of eve. */
/* */
/* eve is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* eve is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with eve. If not, see <https://www.gnu.org/licenses/>. */
/* */
/* ************************************************************************** */

#include "eve.h"

/* Highlight the characters of ROW. */
void
update_syntax(t_erow *row)
{
int i = 0;

row->hl = realloc(row->hl, row->rsize);
if (row->hl == NULL)
return ;
memset(row->hl, HL_NORMAL, row->rsize);
while (i < row->rsize) {
if (isdigit(row->render[i])) {
row->hl[i] = HL_NUMBER;
}
++i;
}
}

/* Map values in hl to ANSI color codes. */
int
syntax_to_color(int hl)
{
if (hl == HL_NUMBER) {
return (31);
}
return (37);
}

0 comments on commit 7aff4e8

Please sign in to comment.