From 69efce1d95198c265b21b42bf9067ae1fbf8505c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Sat, 13 Aug 2022 00:11:32 -0300 Subject: [PATCH] Add print_prompt funtion. --- ef.c | 17 ++++------------- util.c | 13 +++++++++++++ util.h | 4 ++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/ef.c b/ef.c index cb29001..2a293fa 100644 --- a/ef.c +++ b/ef.c @@ -165,10 +165,6 @@ int main(int argc, char **argv) } keypad(prompt, TRUE); - /* initial prompt */ - mvwaddstr(prompt, 0, 0, "> "); - wrefresh(prompt); - /* index in entries, index in matched; * this function could include a wclrtoeol call, but that only hides indexing issues */ #define WRITELIST(idx, idxm) \ @@ -188,6 +184,8 @@ int main(int argc, char **argv) char *name = NULL; /* search tokens */ struct str_array toks = { 0 }; + print_prompt(prompt, &toks, true); + /* index inside set of matches */ size_t index_in_matched = 0; for (;;) { @@ -317,16 +315,9 @@ int main(int argc, char **argv) continue; } - werase(prompt); - mvwaddstr(prompt, 0, 0, ">"); - for (size_t i = 0; i < toks.n; i++) { - const char *e = get_entry(&toks, i); - waddch(prompt, ' '); - waddstr(prompt, e); - } /* show space if name is NULL */ - if (!name) waddch(prompt, ' '); - wrefresh(prompt); + print_prompt(prompt, &toks, !name); + /* name==NULL means the search results won't change, * so we don't need to search again */ if (!name) continue; diff --git a/util.c b/util.c index dcc6768..82181f8 100644 --- a/util.c +++ b/util.c @@ -23,6 +23,19 @@ void read_entries_from_stream(struct str_array *a, int delim, FILE *input) } } +void print_prompt(WINDOW *w, struct str_array *a, bool show_space) +{ + werase(w); + mvwaddstr(w, 0, 0, ">"); + for (size_t i = 0; i < a->n; i++) { + const char *e = get_entry(a, i); + waddch(w, ' '); + waddstr(w, e); + } + if (show_space) waddch(w, ' '); + wrefresh(w); +} + void *xmalloc(size_t s) { void *r = malloc(s); if (r) return r; diff --git a/util.h b/util.h index 3941594..9d15a1e 100644 --- a/util.h +++ b/util.h @@ -1,11 +1,15 @@ #ifndef UTIL_H #define UTIL_H +#include #include +#include + #include "string-array.h" void read_entries_from_stream(struct str_array *, int, FILE *); +void print_prompt(WINDOW *, struct str_array *, bool); void *xmalloc(size_t); void *xrealloc(void *, size_t);