summaryrefslogtreecommitdiffstats
path: root/screen-write.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-06-25 15:47:07 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-06-25 15:47:07 +0000
commit1e06ec41dc0bc9e99674d11ad3c810b579450e12 (patch)
treee34f92ea38935457d09b658b5df1edeeaae8ec65 /screen-write.c
parent0828e06ad7680512e67437ca91ccd8c75cd852b5 (diff)
Add a UTF-8 aware string length function and make UTF-8 in
status-left/status-right work properly. At the moment any top-bit-set characters are assumed to be UTF-8: a status-utf8 option to configure this will come shortly.
Diffstat (limited to 'screen-write.c')
-rw-r--r--screen-write.c116
1 files changed, 111 insertions, 5 deletions
diff --git a/screen-write.c b/screen-write.c
index 92dc607d..08156d54 100644
--- a/screen-write.c
+++ b/screen-write.c
@@ -1,4 +1,4 @@
-/* $Id: screen-write.c,v 1.45 2009-05-04 17:58:27 nicm Exp $ */
+/* $OpenBSD: screen-write.c,v 1.2 2009/06/03 16:05:46 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -52,20 +52,126 @@ screen_write_putc(
screen_write_cell(ctx, gc, NULL);
}
+/* Calculate string length. */
+size_t printflike1
+screen_write_strlen(const char *fmt, ...)
+{
+ va_list ap;
+ char *msg;
+ u_char *ptr, utf8buf[4];
+ size_t left, size = 0;
+
+ va_start(ap, fmt);
+ xvasprintf(&msg, fmt, ap);
+ va_end(ap);
+
+ ptr = msg;
+ while (*ptr != '\0') {
+ if (*ptr > 0x7f) { /* Assume this is UTF-8. */
+ memset(utf8buf, 0xff, sizeof utf8buf);
+
+ left = strlen(ptr);
+ if (*ptr >= 0xc2 && *ptr <= 0xdf && left >= 2) {
+ memcpy(utf8buf, ptr, 2);
+ ptr += 2;
+ } else if (*ptr >= 0xe0 && *ptr <= 0xef && left >= 3) {
+ memcpy(utf8buf, ptr, 3);
+ ptr += 3;
+ } else if (*ptr >= 0xf0 && *ptr <= 0xf4 && left >= 4) {
+ memcpy(utf8buf, ptr, 4);
+ ptr += 4;
+ } else {
+ *utf8buf = *ptr;
+ ptr++;
+ }
+ size += utf8_width(utf8buf);
+ } else {
+ size++;
+ ptr++;
+ }
+ }
+
+ return (size);
+}
+
/* Write string. */
void printflike3
screen_write_puts(
struct screen_write_ctx *ctx, struct grid_cell *gc, const char *fmt, ...)
{
va_list ap;
- char *msg, *ptr;
va_start(ap, fmt);
- xvasprintf(&msg, fmt, ap);
+ screen_write_vnputs(ctx, -1, gc, fmt, ap);
va_end(ap);
+}
- for (ptr = msg; *ptr != '\0'; ptr++)
- screen_write_putc(ctx, gc, (u_char) *ptr);
+/* Write string with length limit (-1 for unlimited). */
+void printflike4
+screen_write_nputs(struct screen_write_ctx *ctx,
+ ssize_t maxlen, struct grid_cell *gc, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ screen_write_vnputs(ctx, maxlen, gc, fmt, ap);
+ va_end(ap);
+}
+
+void
+screen_write_vnputs(struct screen_write_ctx *ctx,
+ ssize_t maxlen, struct grid_cell *gc, const char *fmt, va_list ap)
+{
+ char *msg;
+ u_char *ptr, utf8buf[4];
+ size_t left, size = 0;
+ int width;
+
+ xvasprintf(&msg, fmt, ap);
+
+ ptr = msg;
+ while (*ptr != '\0') {
+ if (*ptr > 0x7f) { /* Assume this is UTF-8. */
+ memset(utf8buf, 0xff, sizeof utf8buf);
+
+ left = strlen(ptr);
+ if (*ptr >= 0xc2 && *ptr <= 0xdf && left >= 2) {
+ memcpy(utf8buf, ptr, 2);
+ ptr += 2;
+ } else if (*ptr >= 0xe0 && *ptr <= 0xef && left >= 3) {
+ memcpy(utf8buf, ptr, 3);
+ ptr += 3;
+ } else if (*ptr >= 0xf0 && *ptr <= 0xf4 && left >= 4) {
+ memcpy(utf8buf, ptr, 4);
+ ptr += 4;
+ } else {
+ *utf8buf = *ptr;
+ ptr++;
+ }
+
+ width = utf8_width(utf8buf);
+ if (maxlen > 0 && size + width > (size_t) maxlen) {
+ while (size < (size_t) maxlen) {
+ screen_write_putc(ctx, gc, ' ');
+ size++;
+ }
+ break;
+ }
+ size += width;
+
+ gc->flags |= GRID_FLAG_UTF8;
+ screen_write_cell(ctx, gc, utf8buf);
+ gc->flags &= ~GRID_FLAG_UTF8;
+
+ } else {
+ if (maxlen > 0 && size > (size_t) maxlen)
+ break;
+
+ size++;
+ screen_write_putc(ctx, gc, *ptr);
+ ptr++;
+ }
+ }
xfree(msg);
}