Syntax highlighting and comment

This commit is contained in:
Arthur Barraux
2026-01-11 19:41:30 +01:00
parent 815114923d
commit fc93832130
11 changed files with 623 additions and 25 deletions
+63 -2
View File
@@ -1,11 +1,30 @@
/**
* @file output.c
* @brief Screen rendering and output module for the Beluga text editor
* @details Handles all screen updates, cursor positioning, status bar rendering,
* and display synchronization using ANSI escape sequences
*/
#include "../include/output.h"
#include "../include/syntax_highlighter.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern struct editorConfig E;
/**
* @brief Renders all visible rows to the screen buffer
* @details Draws file content with syntax highlighting, handles line wrapping,
* and displays tilde characters (~) for empty lines. Shows welcome message
* when no file is open.
* @param ab Pointer to append buffer structure for accumulating output
* @note Respects E.row_offset and E.col_offset for scrolling
* @note Clears to end of each line after content
* @see editorRefreshScreen()
*/
void editorDrawRows(struct abuf *ab) {
int y;
char welcome[80];
@@ -41,13 +60,22 @@ void editorDrawRows(struct abuf *ab) {
len = 0;
if (len > E.screencols)
len = E.screencols;
abAppend(ab, &E.row[file_row].render[E.col_offset], len);
char * highlighted = highlight_line(&E.row[file_row].render[E.col_offset], &E.row[file_row].rsize);
abAppend(ab, highlighted, E.row[file_row].rsize);
free(highlighted);
}
abAppend(ab, ERASE_END_LINE, 3);
abAppend(ab, "\r\n", 2);
}
}
/**
* @brief Updates scroll offsets to keep cursor visible on screen
* @details Adjusts E.row_offset and E.col_offset to ensure the cursor remains
* within the visible viewport. Also updates E.rx (rendered x-coordinate).
* @note Updates global editor state E
* @see editorRowCxToRx()
*/
void editorScroll() {
E.rx = E.cursor_x;
if (E.cursor_y < E.numrows) {
@@ -68,6 +96,13 @@ void editorScroll() {
}
}
/**
* @brief Renders the status bar at the bottom of the screen
* @details Displays filename, line count, dirty flag, and current cursor position
* in an inverted color bar. Right-aligns the cursor position indicator.
* @param ab Pointer to append buffer structure for accumulating output
* @note Uses ANSI escape codes for color inversion
*/
void editorDrawStatusBar(struct abuf *ab) {
int len, render_len;
char status[80], render_status[80];
@@ -95,6 +130,13 @@ void editorDrawStatusBar(struct abuf *ab) {
abAppend(ab, "\r\n", 2);
}
/**
* @brief Renders the message bar below the status bar
* @details Displays temporary status messages for a limited time (5 seconds).
* Only displays message if within time window and within screen width.
* @param ab Pointer to append buffer structure for accumulating output
* @note Messages are set by editorSetStatusMessage()
*/
void editorDrawMessageBar(struct abuf *ab) {
int msg_len = strlen(E.status_msg);
abAppend(ab, ERASE_END_LINE, 3);
@@ -106,6 +148,16 @@ void editorDrawMessageBar(struct abuf *ab) {
}
}
/**
* @brief Performs complete screen refresh and buffer synchronization
* @details Clears screen, redraws all visible content (rows, status bar, message bar),
* positions cursor, and writes accumulated buffer to stdout. This is the main
* rendering function called each frame.
* @note Updates global editor state E (via editorScroll())
* @see editorDrawRows()
* @see editorDrawStatusBar()
* @see editorDrawMessageBar()
*/
void editorRefreshScreen() {
editorScroll();
struct abuf ab = ABUF_INIT;
@@ -123,11 +175,20 @@ void editorRefreshScreen() {
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, SHOW_CURSOR, 6);
write(STDOUT_FILENO, ab.b, ab.len);
abFree(&ab);
}
/**
* @brief Sets a temporary status message for display
* @details Formats and stores a message that will be displayed in the message bar
* for 5 seconds. Uses printf-style variable argument formatting.
* @param fmt Printf-style format string
* @param ... Variable arguments for format string
* @note Updates global editor state E (status_msg, status_msg_time)
* @see editorDrawMessageBar()
*/
void editorSetStatusMessage(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);