From 35cb8b8cfbd141d4ba2e3f1435a9550864d9e2e1 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 2 Jan 2021 18:25:03 -0500 Subject: blinking cursor --- buffer.c | 22 +++++++++++++++++----- config.c | 41 ++++++++++++++++++++++++++++------------- main.c | 3 +-- ted.cfg | 5 +++++ 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/buffer.c b/buffer.c index 0e13ee5..8ad3733 100644 --- a/buffer.c +++ b/buffer.c @@ -1774,11 +1774,23 @@ void buffer_render(TextBuffer *buffer, float x1, float y1, float x2, float y2) { text_chars_end(font); { // render cursor - if (buffer_clip_rect(buffer, &cursor_rect)) { - gl_color_rgba(colors[COLOR_CURSOR]); - glBegin(GL_QUADS); - rect_render(cursor_rect); - glEnd(); + float time_on = settings->cursor_blink_time_on; + float time_off = settings->cursor_blink_time_off; + bool is_on = true; + if (time_off > 0) { + double absolute_time = time_get_seconds(); + float period = time_on + time_off; + // time in period + double t = fmod(absolute_time, period); + is_on = t < time_on; // are we in the "on" part of the period? + } + if (is_on) { + if (buffer_clip_rect(buffer, &cursor_rect)) { + gl_color_rgba(colors[COLOR_CURSOR]); + glBegin(GL_QUADS); + rect_render(cursor_rect); + glEnd(); + } } } } diff --git a/config.c b/config.c index 6dc821e..8431da3 100644 --- a/config.c +++ b/config.c @@ -190,9 +190,9 @@ void config_read(Ted *ted, char const *filename) { default: { char *equals = strchr(line, '='); if (equals) { - char *key = line; + char const *key = line; *equals = '\0'; - char *value = equals + 1; + char const *value = equals + 1; while (isspace(*key)) ++key; while (isspace(*value)) ++value; if (equals != line) { @@ -254,32 +254,47 @@ void config_read(Ted *ted, char const *filename) { config_err(cfg, "Expected ':' for key action. This line should look something like: %s = :command.", key); } } break; - case SECTION_CORE: + case SECTION_CORE: { + char const *endptr; + long long const integer = strtoll(value, (char **)&endptr, 10); + bool const is_integer = *endptr == '\0'; + double const floating = strtod(value, (char **)&endptr); + bool const is_floating = *endptr == '\0'; + if (streq(key, "tab-width")) { - int n = atoi(value); - if (n > 0 && n < 100) { - settings->tab_width = (u8)n; + if (is_integer && integer > 0 && integer < 100) { + settings->tab_width = (u8)integer; } else { config_err(cfg, "Invalid tab width: %s.", value); } } else if (streq(key, "cursor-width")) { - int n = atoi(value); - if (n > 0 && n < 100) { - settings->cursor_width = (u8)n; + if (is_integer && integer > 0 && integer < 100) { + settings->cursor_width = (u8)integer; } else { config_err(cfg, "Invalid cursor width: %s.", value); } } else if (streq(key, "undo-save-time")) { - int n = atoi(value); - if (n > 0 && n < 200) { - settings->undo_save_time = (u8)n; + if (is_integer && integer > 0 && integer < 200) { + settings->undo_save_time = (u8)integer; } else { config_err(cfg, "Invalid undo save time: %s.", value); } + } else if (streq(key, "cursor-blink-time-on")) { + if (is_floating && floating >= 0 && floating < 1000) { + settings->cursor_blink_time_on = (float)floating; + } else { + config_err(cfg, "Invalid cursor blink time: %s.", value); + } + } else if (streq(key, "cursor-blink-time-off")) { + if (is_floating && floating >= 0 && floating < 1000) { + settings->cursor_blink_time_off = (float)floating; + } else { + config_err(cfg, "Invalid cursor blink time: %s.", value); + } } else { config_err(cfg, "Unrecognized core setting: %s.", key); } - break; + } break; } } } else { diff --git a/main.c b/main.c index 9ece8ac..05341d5 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,4 @@ // @TODO: -// - select all (:select-all) -// - blinking cursor (cursor-blink-time) // - text size (text-size, :increase-text-size, :decrease-text-size) #include "base.h" no_warn_start @@ -18,6 +16,7 @@ no_warn_end #include "util.c" #include "colors.c" typedef struct { + float cursor_blink_time_on, cursor_blink_time_off; u32 colors[COLOR_COUNT]; u8 tab_width; u8 cursor_width; diff --git a/ted.cfg b/ted.cfg index 4132f6e..80dab7f 100644 --- a/ted.cfg +++ b/ted.cfg @@ -1,6 +1,11 @@ [core] tab-width = 4 +# cursor width in pixels cursor-width = 1 +# time to blink cursor for (i.e. it will be on for cursor-blink-time-on seconds, then off for cursor-blink-time-off seconds) +# set -off to 0 to disable blinking +cursor-blink-time-on = 0.5 +cursor-blink-time-off = 0.3 # if you do a bunch of typing, then undo, it will generally # undo the past this many seconds of editing. undo-save-time = 6 -- cgit v1.2.3