Skip to content

Commit

Permalink
make history abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
daanx committed Jul 19, 2021
1 parent 8b07065 commit 304b69d
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 134 deletions.
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ internal char* mem_strndup( alloc_t* mem, const char* s, ssize_t n);

#define mem_zalloc_tp(mem,tp) (tp*)mem_zalloc(mem,ssizeof(tp))
#define mem_malloc_tp_n(mem,tp,n) (tp*)mem_malloc(mem,(n)*ssizeof(tp))
#define mem_zalloc_tp_n(mem,tp,n) (tp*)mem_zalloc(mem,(n)*ssizeof(tp))


#endif // RP_COMMON_H
34 changes: 18 additions & 16 deletions src/editline.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "tty.h"
#include "env.h"
#include "stringbuf.h"
#include "history.h"

#if defined(_WIN32)
#else
Expand Down Expand Up @@ -182,7 +183,8 @@ static void edit_write_prompt( rp_env_t* env, editor_t* eb, ssize_t row, bool in
term_write(env->term, eb->prompt_text);
}
else {
term_writef(env->term, "%*c", str_column_width(eb->prompt_text,eb->is_utf8), ' ' );
ssize_t w = str_column_width(eb->prompt_text,eb->is_utf8);
term_writef(env->term, w, "%*c", w, ' ' );
}
term_attr_reset( env->term );
if (env->prompt_color != RP_DEFAULT_COLOR) term_color( env->term, env->prompt_color );
Expand Down Expand Up @@ -659,11 +661,11 @@ static const char* help[] = {
static void edit_show_help(rp_env_t* env, editor_t* eb) {
edit_clear(env, eb);
for (ssize_t i = 0; help[i] != NULL && help[i+1] != NULL; i += 2) {
if (help[i][0] == 0) {
term_writef(env->term, "\x1B[90m%s\x1B[0m\r\n", help[i+1]);
if (help[i][0] == 0) {
term_writef(env->term, 256, "\x1B[90m%s\x1B[0m\r\n", help[i+1]);
}
else {
term_writef(env->term, " \x1B[97m%-13s\x1B[0m%s%s\r\n", help[i], (help[i+1][0] == 0 ? "" : ": "), help[i+1]);
term_writef(env->term, 256, " \x1B[97m%-13s\x1B[0m%s%s\r\n", help[i], (help[i+1][0] == 0 ? "" : ": "), help[i+1]);
}
}
eb->cur_rows = 0;
Expand All @@ -679,11 +681,11 @@ static void edit_show_help(rp_env_t* env, editor_t* eb) {
static void edit_history_at(rp_env_t* env, editor_t* eb, int ofs )
{
if (eb->modified) {
history_update(env, sbuf_string(eb->input)); // update first entry if modified
history_update(env->history, sbuf_string(eb->input)); // update first entry if modified
eb->history_idx = 0; // and start again
eb->modified = false;
}
const char* entry = history_get(&env->history,eb->history_idx + ofs);
const char* entry = history_get(env->history,eb->history_idx + ofs);
debug_msg( "edit: history: at: %d + %d, found: %s\n", eb->history_idx, ofs, entry);
if (entry == NULL) {
term_beep(env->term);
Expand Down Expand Up @@ -746,7 +748,7 @@ static void hsearch_done( alloc_t* mem, hsearch_t* hs ) {
static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
// update history
if (eb->modified) {
history_update(env, sbuf_string(eb->input)); // update first entry if modified
history_update(env->history, sbuf_string(eb->input)); // update first entry if modified
eb->history_idx = 0; // and start again
eb->modified = false;
}
Expand Down Expand Up @@ -774,7 +776,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
hsearch_push( eb->mem, &hs, hidx, match_pos, match_len, true);
char c = initial[ipos + next]; // terminate temporarily
initial[ipos + next] = 0;
if (history_search( &env->history, hidx, initial, true, &hidx, &match_pos )) {
if (history_search( env->history, hidx, initial, true, &hidx, &match_pos )) {
match_len = ipos + next;
}
else if (ipos + next >= initial_len) {
Expand All @@ -793,7 +795,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {

// Incremental search
again:
hentry = history_get(&env->history,hidx);
hentry = history_get(env->history,hidx);
snprintf(buf,32,"\x1B[97m%zd. ", hidx);
sbuf_append(eb->extra, buf );
sbuf_append(eb->extra, "\x1B[90m" ); // dark gray
Expand All @@ -811,7 +813,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
sbuf_clear(eb->extra);
if (c == KEY_ESC || c == KEY_BELL /* ^G */ || c == KEY_CTRL_C) {
c = 0;
sbuf_replace( eb->input, history_get(&env->history,0) );
sbuf_replace( eb->input, history_get(env->history,0) );
eb->pos = old_pos;
}
else if (c == KEY_ENTER) {
Expand All @@ -832,7 +834,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
else if (c == KEY_CTRL_R || c == KEY_TAB || c == KEY_UP) {
// search backward
hsearch_push(&env->alloc, &hs, hidx, match_pos, match_len, false);
if (!history_search( &env->history, hidx+1, sbuf_string(eb->input), true, &hidx, &match_pos )) {
if (!history_search( env->history, hidx+1, sbuf_string(eb->input), true, &hidx, &match_pos )) {
hsearch_pop(&env->alloc,&hs,NULL,NULL,NULL,NULL);
term_beep(env->term);
};
Expand All @@ -841,7 +843,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
else if (c == KEY_CTRL_S || c == KEY_SHIFT_TAB || c == KEY_DOWN) {
// search forward
hsearch_push(&env->alloc, &hs, hidx, match_pos, match_len, false);
if (!history_search( &env->history, hidx-1, sbuf_string(eb->input), false, &hidx, &match_pos )) {
if (!history_search( env->history, hidx-1, sbuf_string(eb->input), false, &hidx, &match_pos )) {
hsearch_pop(&env->alloc, &hs,NULL,NULL,NULL,NULL);
term_beep(env->term);
};
Expand Down Expand Up @@ -881,7 +883,7 @@ static void edit_history_search(rp_env_t* env, editor_t* eb, char* initial ) {
goto again;
}
// search for the new input
if (history_search( &env->history, hidx, sbuf_string(eb->input), true, &hidx, &match_pos )) {
if (history_search( env->history, hidx, sbuf_string(eb->input), true, &hidx, &match_pos )) {
match_len = sbuf_len(eb->input);
}
else {
Expand Down Expand Up @@ -1155,7 +1157,7 @@ static char* edit_line( rp_env_t* env, const char* prompt_text )
edit_write_prompt(env, &eb, 0, false);

// always a history entry for the current input
history_push(env, "");
history_push(env->history, "");

// process keys
code_t c; // current key code
Expand Down Expand Up @@ -1343,9 +1345,9 @@ static char* edit_line( rp_env_t* env, const char* prompt_text )
sbuf_free(eb.extra);

// update history
history_update(env,res);
history_update(env->history, res);
if (res != NULL && (res[0] == 0 || res[1] == 0)) rp_history_remove_last(env); // no empty or single-char entries
history_save(env);
history_save(env->history);
return res;
}

22 changes: 2 additions & 20 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "term.h"
#include "tty.h"
#include "stringbuf.h"
#include "history.h"

#define RP_MAX_HISTORY (200)
#define RP_MAX_COMPLETIONS_TO_SHOW (1000)
Expand All @@ -38,17 +39,6 @@ typedef struct completions_s {
completion_t* elems;
} completions_t;

//-------------------------------------------------------------
// History
//-------------------------------------------------------------

typedef struct history_s {
ssize_t count; // current number of entries in use
ssize_t len; // size of elems (realloc'd on demand)
const char** elems; // history items (up to count)
const char* fname; // history file
bool allow_duplicates; // allow duplicate entries?
} history_t;

//-------------------------------------------------------------
// Environment
Expand All @@ -60,7 +50,7 @@ struct rp_env_s {
tty_t* tty;
alloc_t alloc;
completions_t completions;
history_t history;
history_t* history;
const char* prompt_marker;
rp_color_t prompt_color;
char multiline_eol;
Expand All @@ -71,14 +61,6 @@ struct rp_env_s {

internal char* rp_editline(rp_env_t* env, const char* prompt_text);

internal void history_done( rp_env_t* env );
internal void history_load( rp_env_t* env );
internal void history_save( rp_env_t* env );
internal bool history_push( rp_env_t* env, const char* entry );
internal bool history_update( rp_env_t* env, const char* entry );
internal const char* history_get( const history_t* env, ssize_t n );
internal bool history_search( const history_t* h, ssize_t from, const char* search, bool backward, ssize_t* hidx, ssize_t* hpos);

internal void completions_done( rp_env_t* env );
internal void completions_clear( rp_env_t* env );
internal void completions_push(rp_env_t* env, const char* display, const char* replacement, ssize_t delete_before, ssize_t delete_after);
Expand Down
Loading

0 comments on commit 304b69d

Please sign in to comment.