Introduction
Introduction Statistics Contact Development Disclaimer Help
add a print utf-8 function that can directly print the data - sfeed_curses - sf…
git clone git://git.codemadness.org/sfeed_curses
Log
Files
Refs
README
LICENSE
---
commit a9deda50bcc3a23a8876237cd72b69c62ac21db0
parent a8843881faa9f53eaa614a162e8e3e89094f92c5
Author: Hiltjo Posthuma <[email protected]>
Date: Tue, 16 Mar 2021 18:03:12 +0100
add a print utf-8 function that can directly print the data
This avoids unnecesary buffering and an empty line when the line is very long.
Diffstat:
M sfeed_curses.c | 60 +++++++++++++++++++++++++++++…
1 file changed, 57 insertions(+), 3 deletions(-)
---
diff --git a/sfeed_curses.c b/sfeed_curses.c
@@ -430,12 +430,66 @@ utf8pad(char *buf, size_t bufsiz, const char *s, size_t l…
return 0;
}
+/* print `len' columns of characters. If string is shorter pad the rest with
+ * characters `pad`. */
+void
+printutf8pad(FILE *fp, const char *s, size_t len, int pad)
+{
+ wchar_t wc;
+ size_t col = 0, i, slen;
+ int inc, rl, w;
+
+ if (!len)
+ return;
+
+ slen = strlen(s);
+ for (i = 0; i < slen; i += inc) {
+ inc = 1; /* next byte */
+ if ((unsigned char)s[i] < 32) {
+ continue; /* skip control characters */
+ } else if ((unsigned char)s[i] >= 127) {
+ rl = mbtowc(&wc, s + i, slen - i < 4 ? slen - i : 4);
+ inc = rl;
+ if (rl < 0) {
+ mbtowc(NULL, NULL, 0); /* reset state */
+ inc = 1; /* invalid, seek next byte */
+ w = 1; /* replacement char is one width */
+ } else if ((w = wcwidth(wc)) == -1) {
+ continue;
+ }
+
+ if (col + w > len || (col + w == len && s[i + inc])) {
+ fputs("\xe2\x80\xa6", fp); /* ellipsis */
+ col++;
+ break;
+ } else if (rl < 0) {
+ fputs("\xef\xbf\xbd", fp); /* replacement */
+ col++;
+ continue;
+ }
+ fwrite(&s[i], 1, rl, fp);
+ col += w;
+ } else {
+ /* optimization: simple ASCII character */
+ if (col + 1 > len || (col + 1 == len && s[i + 1])) {
+ fputs("\xe2\x80\xa6", fp); /* ellipsis */
+ col++;
+ break;
+ }
+ putc(s[i], fp);
+ col++;
+ }
+
+ }
+ for (; col < len; ++col)
+ putc(pad, fp);
+}
+
void
printpad(const char *s, int width)
{
- char buf[1024];
- if (utf8pad(buf, sizeof(buf), s, width, ' ') != -1)
- ttywrite(buf);
+ printutf8pad(stdout, s, width, ' ');
+ fflush(stdout);
}
void
You are viewing proxied material from codemadness.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.