summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-12-01 00:31:01 +0100
committerLouis Burda <quent.burda@gmail.com>2021-12-20 15:31:03 +0100
commitd68d0f39ab7675745e7d177f6774736f7ec58783 (patch)
tree9f0cf6732517619e370186f288be172eead73fb0 /util.c
parent02141177f8055a2d0edace481d631adbcb4b4c47 (diff)
downloadtmus-d68d0f39ab7675745e7d177f6774736f7ec58783.tar.gz
tmus-d68d0f39ab7675745e7d177f6774736f7ec58783.zip
Replace readline with own implementation for integration
Diffstat (limited to 'util.c')
-rw-r--r--util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..b9f37de
--- /dev/null
+++ b/util.c
@@ -0,0 +1,43 @@
+#define _XOPEN_SOURCE 600
+
+#include "util.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include <wctype.h>
+
+int
+strnwidth(const char *s, int n)
+{
+ mbstate_t shift_state;
+ wchar_t wc;
+ size_t wc_len;
+ size_t width = 0;
+
+ memset(&shift_state, '\0', sizeof shift_state);
+
+ for (size_t i = 0; i < n; i += wc_len) {
+ wc_len = mbrtowc(&wc, s + i, MB_CUR_MAX, &shift_state);
+ if (!wc_len) {
+ break;
+ } else if (wc_len >= (size_t)-2) {
+ width += MIN(n - 1, strlen(s + i));
+ break;
+ } else {
+ width += iswcntrl(wc) ? 2 : MAX(0, wcwidth(wc));
+ }
+ }
+
+done:
+ return width;
+}
+
+void
+assert(int cond, const char *file, int line, const char *condstr)
+{
+ if (cond) return;
+
+ fprintf(stderr, "Assertion failed %s:%i (%s)", file, line, condstr);
+ exit(1);
+}