diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-08-28 12:59:05 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2024-09-09 18:16:07 +0200 |
commit | 4c6b6814abfc9c022c6ea8d1e23097c2a774fde5 (patch) | |
tree | 26ea9ad6d2777c080c1733b55fc7d30180c335f5 /src/interface-curses/curses-utils.c | |
parent | fdc185b8faaae44d67f85d2c5a9b9fa48d3e2859 (diff) | |
download | sciteco-4c6b6814abfc9c022c6ea8d1e23097c2a774fde5.tar.gz |
input and displaying of Unicode characters is now possible (refs #5)
* All non-ASCII characters are inserted as Unicode.
On Curses, this also requires a properly set up locale.
* We still do not need any widechar Curses, as waddch() handles
multibyte characters on ncurses.
We will see whether there is any Curses variant that strictly requires
wadd_wch().
If this will be an exception, we might keep both widechar and non-widechar
support.
* By convention gsize is used exclusively for byte sizes.
Character offsets or lengths use int or long.
Diffstat (limited to 'src/interface-curses/curses-utils.c')
-rw-r--r-- | src/interface-curses/curses-utils.c | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/src/interface-curses/curses-utils.c b/src/interface-curses/curses-utils.c index e7c8659..c751afd 100644 --- a/src/interface-curses/curses-utils.c +++ b/src/interface-curses/curses-utils.c @@ -29,7 +29,21 @@ #include "string-utils.h" #include "curses-utils.h" -gsize +/** + * Render UTF-8 string with TECO character representations. + * + * Strings are cut off with `...` at the end if necessary. + * The mapping is similar to teco_view_set_representations(). + * + * @param win The Curses window to write to. + * @param str The string to format. + * @param len The length of the string in bytes. + * @param max_width The maximum width to consume in + * the window in characters. If smaller 0, take the + * entire remaining space in the window. + * @return Number of characters actually written. + */ +guint teco_curses_format_str(WINDOW *win, const gchar *str, gsize len, gint max_width) { int old_x, old_y; @@ -42,6 +56,12 @@ teco_curses_format_str(WINDOW *win, const gchar *str, gsize len, gint max_width) while (len > 0) { /* + * NOTE: It shouldn't be possible to meet any string, + * that is not valid UTF-8. + */ + gsize clen = g_utf8_next_char(str) - str; + + /* * NOTE: This mapping is similar to * teco_view_set_representations(). */ @@ -85,12 +105,18 @@ teco_curses_format_str(WINDOW *win, const gchar *str, gsize len, gint max_width) chars_added++; if (chars_added > max_width) goto truncate; - waddch(win, *str); + /* + * FIXME: This works with UTF-8 on ncurses, + * since it detects multi-byte characters. + * However on other platforms wadd_wch() may be + * necessary, which requires a widechar Curses variant. + */ + waddnstr(win, str, clen); } } - str++; - len--; + str += clen; + len -= clen; } return getcurx(win) - old_x; @@ -108,23 +134,43 @@ truncate: return getcurx(win) - old_x; } -gsize -teco_curses_format_filename(WINDOW *win, const gchar *filename, - gint max_width) +/** + * Render UTF-8 filename. + * + * This cuts of overlong filenames with `...` at the beginning, + * possibly skipping any drive letter. + * Control characters are escaped, but not highlighted. + * + * @param win The Curses window to write to. + * @param filename Null-terminated filename to render. + * @param max_width The maximum width to consume in + * the window in characters. If smaller 0, take the + * entire remaining space in the window. + * @return Number of characters actually written. + */ +guint +teco_curses_format_filename(WINDOW *win, const gchar *filename, gint max_width) { int old_x = getcurx(win); g_autofree gchar *filename_printable = teco_string_echo(filename, strlen(filename)); - size_t filename_len = strlen(filename_printable); + glong filename_len = g_utf8_strlen(filename_printable, -1); if (max_width < 0) max_width = getmaxx(win) - old_x; - if (filename_len <= (size_t)max_width) { + if (filename_len <= max_width) { + /* + * FIXME: This works with UTF-8 on ncurses, + * since it detects multi-byte characters. + * However on other platforms wadd_wch() may be + * necessary, which requires a widechar Curses variant. + */ waddstr(win, filename_printable); - } else { - const gchar *keep_post = filename_printable + filename_len - - max_width + 3; + } else if (filename_len >= 3) { + const gchar *keep_post; + keep_post = g_utf8_offset_to_pointer(filename_printable + strlen(filename_printable), + -max_width + 3); #ifdef G_OS_WIN32 const gchar *keep_pre = g_path_skip_root(filename_printable); |