From 97abb638fe62d5b8a264beab7d000cb9e067eb9a Mon Sep 17 00:00:00 2001 From: speedie Date: Fri, 28 Jul 2023 23:15:05 +0200 Subject: [PATCH] Fix issue #21 by not drawing chars that don't fit --- libs/draw.c | 19 +++++++++++++++--- spmenu.c | 55 ++++++++++++++++++----------------------------------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/libs/draw.c b/libs/draw.c index 0f45677..75d3332 100644 --- a/libs/draw.c +++ b/libs/draw.c @@ -473,10 +473,23 @@ int drawinput(int x, int y, int w) { free(censort); } else if (!passwd) { if (strlen(tx.text)) { - apply_fribidi(tx.text); - draw_text(draw, x, y, w, sp.bh, sp.lrpad / 2, isrtl ? fribidi_text : tx.text, 0, pango_input ? True : False, col_inputfg, col_inputbg, alpha_inputfg, alpha_inputbg); + char ptext[BUFSIZ]; + char *p; - curpos = TEXTW(tx.text) - TEXTW(&tx.text[sp.cursor]); + memcpy(ptext, tx.text, BUFSIZ); + + p = ptext; + + while (TEXTW(p) > sp.maxlen) { + p++; + } + + memmove(ptext, p, strlen(p) + 1); + + apply_fribidi(ptext); + draw_text(draw, x, y, w, sp.bh, sp.lrpad / 2, isrtl ? fribidi_text : ptext, 0, pango_input ? True : False, col_inputfg, col_inputbg, alpha_inputfg, alpha_inputbg); + + curpos = TEXTW(ptext) - TEXTW(&ptext[sp.cursor]); } else if (!hidepretext && pretext != NULL) { apply_fribidi(pretext); draw_text(draw, x + fw, y, w, sp.bh, sp.lrpad / 2, isrtl ? fribidi_text : pretext, 0, pango_pretext ? True : False, col_pretextfg, col_pretextbg, alpha_pretextfg, alpha_pretextbg); diff --git a/spmenu.c b/spmenu.c index 3314cdc..8be3979 100644 --- a/spmenu.c +++ b/spmenu.c @@ -141,6 +141,8 @@ struct sp { int listcount; int listchanged; + int maxlen; // max length of text + size_t cursor; // cursor width int ignoreconfkeys; // can be set globally if you don't want to override keybinds with config file keys @@ -363,29 +365,30 @@ void recalculatenumbers(void) { void calcoffsets(void) { int i, offset; + int numberw = 0; + int modew = 0; + int larroww = 0; + int rarroww = 0; + int capsw = 0; + + if (!hidematchcount) numberw = pango_numbers ? TEXTWM(tx.numbers) : TEXTW(tx.numbers); + if (!hidemode) modew = pango_mode ? TEXTWM(tx.modetext) : TEXTW(tx.modetext); + if (!hidelarrow) larroww = pango_leftarrow ? TEXTWM(leftarrow) : TEXTW(leftarrow); + if (!hiderarrow) rarroww = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow); + if (!hidecaps) capsw = pango_caps ? TEXTWM(tx.capstext) : TEXTW(tx.capstext); + + if (!strcmp(tx.capstext, "")) { + capsw = 0; + } if (lines > 0) { offset = lines * columns * sp.bh; } else { // no lines, therefore the size of items must be decreased to fit the menu elements - int numberw = 0; - int modew = 0; - int larroww = 0; - int rarroww = 0; - int capsw = 0; - - if (!hidematchcount) numberw = pango_numbers ? TEXTWM(tx.numbers) : TEXTW(tx.numbers); - if (!hidemode) modew = pango_mode ? TEXTWM(tx.modetext) : TEXTW(tx.modetext); - if (!hidelarrow) larroww = pango_leftarrow ? TEXTWM(leftarrow) : TEXTW(leftarrow); - if (!hiderarrow) rarroww = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow); - if (!hidecaps) capsw = pango_caps ? TEXTWM(tx.capstext) : TEXTW(tx.capstext); - - if (!strcmp(tx.capstext, "")) { - capsw = 0; - } - offset = sp.mw - (sp.promptw + sp.inputw + larroww + rarroww + modew + numberw + capsw + menumarginh); } + sp.maxlen = sp.mw - (sp.promptw + modew + numberw + capsw + menumarginh); + // calculate which items will begin the next page for (i = 0, nextitem = currentitem; nextitem; nextitem = nextitem->right) { nextitem->nsgrtext = get_text_n_sgr(nextitem); @@ -463,26 +466,6 @@ void insert(const char *str, ssize_t n) { if (strlen(tx.text) + n > sizeof tx.text - 1) return; // length of text should not exceed size - int numberw = 0; - int modew = 0; - int larroww = 0; - int rarroww = 0; - int capsw = 0; - - // add width - if (!hidelarrow) larroww = TEXTW(leftarrow); - if (!hiderarrow) rarroww = TEXTW(rightarrow); - if (!hidemode) modew = MAX(MAX(TEXTW(normtext), TEXTW(instext)), TEXTW(regextext)); - if (!hiderarrow) rarroww = TEXTW(rightarrow); - if (!hidematchcount) numberw = TEXTW(tx.numbers); - if (!hidecaps) capsw = MAX(TEXTW(capslockontext), TEXTW(capslockofftext)); - - if (TEXTW(str) + TEXTW(tx.text) >= sp.inputw && selecteditem) { - return; - } else if (TEXTW(str) + TEXTW(tx.text) >= sp.mw - (sp.promptw + (!lines ? larroww : 0) + (!lines ? rarroww : 0) + modew + numberw + capsw + menumarginh)) { - return; - } - static char l[BUFSIZ] = ""; if (requirematch) {