static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm) { int i, nbindings; const owl_keybinding *kb; nbindings = owl_list_get_size(&km->bindings); for (i=0; i<nbindings; i++) { char *kbstr; const owl_cmd *cmd; const char *tmpdesc, *desc = ""; kb = owl_list_get_element(&km->bindings, i); kbstr = owl_keybinding_tostring(kb); owl_fmtext_append_normal(fm, OWL_TABSTR); owl_fmtext_append_normal(fm, kbstr); owl_fmtext_append_spaces(fm, 11-strlen(kbstr)); g_free(kbstr); owl_fmtext_append_normal(fm, " - "); if (kb->desc && *kb->desc) { desc = kb->desc; } else if ((cmd=owl_function_get_cmd(kb->command)) && (tmpdesc = owl_cmd_get_summary(cmd))) { desc = tmpdesc; } owl_fmtext_append_normal(fm, desc); if (kb->command && *(kb->command)) { owl_fmtext_append_normal(fm, " ["); owl_fmtext_append_normal(fm, kb->command); owl_fmtext_append_normal(fm, "]"); } owl_fmtext_append_normal(fm, "\n"); } }
/* Truncate the message so that each line begins at column 'acol' and * ends at 'bcol' or sooner. The first column is number 0. The new * message is placed in 'out'. The message is expected to end in a * new line for now. * * NOTE: This needs to be modified to deal with backing up if we find * a SPACING COMBINING MARK at the end of a line. If that happens, we * should back up to the last non-mark character and stop there. * * NOTE: If a line ends at bcol, we omit the newline. This is so printing * to ncurses works. */ void owl_fmtext_truncate_cols(const owl_fmtext *in, int acol, int bcol, owl_fmtext *out) { const char *ptr_s, *ptr_e, *ptr_c, *last; int col, st, padding, chwidth; /* copy the default attributes */ out->default_attrs = in->default_attrs; out->default_fgcolor = in->default_fgcolor; out->default_bgcolor = in->default_bgcolor; last=in->textbuff+in->textlen-1; ptr_s=in->textbuff; while (ptr_s <= last) { ptr_e=strchr(ptr_s, '\n'); if (!ptr_e) { /* but this shouldn't happen if we end in a \n */ break; } if (ptr_e == ptr_s) { owl_fmtext_append_normal(out, "\n"); ++ptr_s; continue; } col = 0; st = 0; padding = 0; chwidth = 0; ptr_c = ptr_s; while(ptr_c < ptr_e) { gunichar c = g_utf8_get_char(ptr_c); if (!owl_fmtext_is_format_char(c)) { chwidth = mk_wcwidth(c); if (col + chwidth > bcol) break; if (col >= acol) { if (st == 0) { ptr_s = ptr_c; padding = col - acol; ++st; } } col += chwidth; chwidth = 0; } ptr_c = g_utf8_next_char(ptr_c); } if (st) { /* lead padding */ owl_fmtext_append_spaces(out, padding); if (ptr_c == ptr_e) { /* We made it to the newline. */ _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff); } else if (chwidth > 1) { /* Last char is wide, truncate. */ _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1); owl_fmtext_append_normal(out, "\n"); } else { /* Last char fits perfectly, We skip to the next char and back * up a byte to make sure we get it all. */ ptr_c = g_utf8_next_char(ptr_c); _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1); } } else { owl_fmtext_append_normal(out, "\n"); } ptr_s = g_utf8_next_char(ptr_e); } }