From 97dedd32fb69dd5ca41fdf613d1b76d232a26d25 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Jun 2014 20:35:00 -0400 Subject: [PATCH] better fix for trailing spaces issue --- src/chat.c | 2 ++ src/groupchat.c | 2 ++ src/main.c | 1 + src/prompt.c | 2 ++ src/toxic_strings.c | 37 +++++++++++++++++++++---------------- src/toxic_strings.h | 3 +++ 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/chat.c b/src/chat.c index 0bf22ef..aec848c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -687,6 +687,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) /* RETURN key: Execute command or print line */ else if (key == '\n') { + rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); + uint8_t line[MAX_STR_SIZE]; if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) diff --git a/src/groupchat.c b/src/groupchat.c index 741e122..58e1f77 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -537,6 +537,8 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) /* RETURN key: Execute command or print line */ else if (key == '\n') { + rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); + uint8_t line[MAX_STR_SIZE]; if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) diff --git a/src/main.c b/src/main.c index 0cc86e7..854e397 100644 --- a/src/main.c +++ b/src/main.c @@ -591,6 +591,7 @@ int main(int argc, char *argv[]) while (true) { update_unix_time(); do_toxic(m, prompt); + // uint32_t st = MIN(tox_do_interval(m) * 1000, 20000); usleep(10000); } diff --git a/src/prompt.c b/src/prompt.c index bb003c3..3cc3f98 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -249,6 +249,8 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) /* RETURN key: execute command */ else if (key == '\n') { + rm_trailing_spaces_buf(ctx->line, &ctx->pos, &ctx->len); + wprintw(ctx->history, "\n"); uint8_t line[MAX_STR_SIZE] = {0}; diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 329791f..554e2c6 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -111,6 +111,22 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len) *len = 0; } +/* Removes trailing spaces from buf. */ +void rm_trailing_spaces_buf(wchar_t *buf, size_t *pos, size_t *len) +{ + if (*len <= 0) + return; + + if (buf[*len - 1] != ' ') + return; + + if (*pos == *len) + --(*pos); + + buf[--(*len)] = L'\0'; + rm_trailing_spaces_buf(buf, pos, len); +} + #define HIST_PURGE MAX_LINE_HIST / 4 /* shifts hist items back and makes room for HIST_PURGE new entries */ @@ -195,31 +211,19 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int uint8_t tmp[MAX_STR_SIZE]; snprintf(tmp, sizeof(tmp), "%s", ubuf); tmp[*pos] = '\0'; - int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": ", 0 = append nothing */ - const uint8_t *endchrs; - uint8_t *sub = strrchr(tmp, ' '); + int n_endchrs = 1; /* 1 = append space to end of match, 2 = append ": " */ if (!sub++) { sub = tmp; - n_endchrs = sub[0] == '/' ? 0 : 2; /* no end chars if command */ + + if (sub[0] != '/') /* make sure it's not a command */ + n_endchrs = 2; } if (string_is_empty(sub)) return -1; - switch(n_endchrs) { - case 0: - endchrs = ""; - break; - case 1: - endchrs = " "; - break; - case 2: - endchrs = ": "; - break; - } - int s_len = strlen(sub); const uint8_t *match; bool is_match = false; @@ -237,6 +241,7 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int return -1; /* put match in correct spot in buf and append endchars (space or ": ") */ + const uint8_t *endchrs = n_endchrs == 1 ? " " : ": "; int m_len = strlen(match); int strt = *pos - s_len; int diff = m_len - s_len + n_endchrs; diff --git a/src/toxic_strings.h b/src/toxic_strings.h index 84bb3f7..64612fd 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -38,6 +38,9 @@ void kill_buf(wchar_t *buf, size_t *pos, size_t *len); /* nulls buf and sets pos and len to 0 */ void reset_buf(wchar_t *buf, size_t *pos, size_t *len); +/* Removes trailing spaces from buf. */ +void rm_trailing_spaces_buf(wchar_t *buf, size_t *pos, size_t *len); + /* looks for the first instance in list that begins with the last entered word in buf according to pos, then fills buf with the complete word. e.g. "Hello jo" would complete the buffer with "Hello john".