aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-06-12 22:24:59 -0700
committerBruce Hill <bruce@bruce-hill.com>2019-06-12 22:24:59 -0700
commitedfe8a19be7b4454e0bb799358c8c566a7e33c68 (patch)
tree6fdd76f1a279ff8d42abe4f962dbec4e289991a3
parentb9440f4b8ad9b1b5684facc409e13d963cb37f11 (diff)
Cleaned up a bit, fixed an issue with the boldness/dimness state being wrong sometimes
-rw-r--r--ask.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/ask.c b/ask.c
index 22d329f..8dfee8e 100644
--- a/ask.c
+++ b/ask.c
@@ -87,35 +87,37 @@ static int fputc_escaped(char c, FILE *out)
}
}
-static int draw_line(FILE *out, const char *line, const char *patt, int cursor)
+static int draw_line(FILE *out, const char *option, const char *patt, int cursor)
{
- size_t linelen = strlen(line), patlen = strlen(patt);
- int dim = 0;
- int p = 0;
- int run = 0;
+ static const char *dim = "\033[22;2m", *bold = "\033[22;1m", *normal = "\033[22m";
+ fputs(normal, out);
+ const char *state = normal;
+ const char *matchstyle = option ? bold : normal;
+ if (!option) option = patt;
+ size_t linelen = strlen(option), patlen = strlen(patt);
+ int p = 0, run = 0;
int *cache = calloc((linelen+1)*(patlen+1), sizeof(int));
- int backtrack = 0;
- int to_start = 0;
+ int backtrack = 0, to_start = 0;
for (int i = 0; i < (int)linelen; i++) {
- if (EQ(patt[p], line[i]) &&
- run + lcs(line,patt,(int)linelen,(int)patlen,i,p,cache)
- >= lcs(line,patt,(int)linelen,(int)patlen,i+1,p,cache)) {
- if (dim) {
- fputs("\033[22;1m", out);
- dim = 0;
+ if (EQ(patt[p], option[i]) &&
+ run + lcs(option,patt,(int)linelen,(int)patlen,i,p,cache)
+ >= lcs(option,patt,(int)linelen,(int)patlen,i+1,p,cache)) {
+ if (state != matchstyle) {
+ fputs(matchstyle, out);
+ state = matchstyle;
}
- int len = fputc_escaped(line[i], out);
+ int len = fputc_escaped(option[i], out);
if (p >= cursor) backtrack += len;
else to_start += len;
++run;
++p;
} else {
run = 0;
- if (!dim) {
- fputs("\033[22;2m", out);
- dim = 1;
+ if (state != dim) {
+ fputs(dim, out);
+ state = dim;
}
- int len = fputc_escaped(line[i], out);
+ int len = fputc_escaped(option[i], out);
if (p >= cursor) backtrack += len;
else to_start += len;
}
@@ -175,11 +177,7 @@ static char *get_input(FILE *in, FILE *out, const char *prompt, const char *init
fputs("\033[0m", out);
backtrack = 1;
} else {
- if (picked) {
- backtrack = draw_line(out, picked, buf, b);
- } else {
- backtrack = draw_line(out, buf, buf, b);
- }
+ backtrack = draw_line(out, picked, buf, b);
}
fflush(out);
int key;