1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-13 01:03:03 +01:00

better fix for trailing spaces issue

This commit is contained in:
Jfreegman 2014-06-03 20:35:00 -04:00
parent 789c491c1e
commit 97dedd32fb
6 changed files with 31 additions and 16 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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);
}

View File

@ -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};

View File

@ -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;

View File

@ -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".