On Tue, Nov 25, 2008 at 12:21:53AM +0100, Claudio M. Alessi wrote:
> On Thu, Nov 20, 2008 at 10:48:30PM +0100, Marc Andre Tanner wrote:
> > Problem is that strlen is used to calculate character width, which
> > doesn't work for utf8 therefore the statusbar content is not properly
> > aligned.
> I don't think i have the skills to fully solve the problem. I guess it
> would be useful a code like the following into the drawbar() function
> (replacing the strlen(3) call):
> 
>    l = mbstowcs(trash, stext, sizeof trash);
> 
> This works properly with the right alignment but not with the left one.
> 
> I'll RTFM and try to solve the problem, however if anyone is able to do
> the work it would be faster (better?) than me.

I also had to RTFM and as I am very tired right now I wouldn't be
surprised if there is a simpler way to accomplish it. Anyway attached is
a patch which seems to work for my limited test case. Please test,
review and post possible simplifications.

Thanks,
Marc

-- 
 Marc Andre Tanner >< http://www.brain-dump.org/ >< GPG key: CF7D56C0
diff --git a/config.h b/config.h
index dc0256b..10000ba 100644
--- a/config.h
+++ b/config.h
@@ -27,9 +27,8 @@
 #define BAR_ATTR        A_NORMAL
 #define BAR_FG          BLUE
 #define BAR_BG          -1
-/* true if the statusbar text should be right aligned,
- * set to false if you prefer it left aligned */
-#define BAR_ALIGN_RIGHT true
+/* determines whether the statusbar text should be right or left aligned */
+#define BAR_ALIGN	ALIGN_RIGHT
 /* separator between window title and window number */
 #define SEPARATOR " | "
 /* printf format string for the window title, first %s
diff --git a/dvtm.c b/dvtm.c
index afcb498..f18d573 100644
--- a/dvtm.c
+++ b/dvtm.c
@@ -126,6 +126,7 @@ static void zoom(const char *args[]);
 static void lock(const char *key[]);
 
 #ifdef CONFIG_STATUSBAR
+enum { ALIGN_LEFT, ALIGN_RIGHT };
 static void togglebar(const char *args[]);
 #endif
 
diff --git a/statusbar.c b/statusbar.c
index d918a5b..648514e 100644
--- a/statusbar.c
+++ b/statusbar.c
@@ -22,25 +22,28 @@ updatebarpos(void) {
 
 static void
 drawbar() {
-	int s, l, maxlen = width - 2;
-	char t = stext[maxlen];
+	wchar_t wbuf[sizeof stext];
+	int w, maxwidth = width - 2;
 	if (barpos == BarOff || !*stext)
 		return;
 	curs_set(0);
 	attrset(BAR_ATTR);
 	madtty_color_set(stdscr, BAR_FG, BAR_BG);
 	mvaddch(by, 0, '[');
-	stext[maxlen] = '\0';
-	l = strlen(stext);
-	if (BAR_ALIGN_RIGHT)
-		for (s = 0; s + l < maxlen; s++)
+	if (mbstowcs(wbuf, stext, sizeof stext) == -1)
+		return;
+	if ((w = wcswidth(wbuf, maxwidth)) == -1)
+		return;
+	if (BAR_ALIGN == ALIGN_RIGHT) {
+		for (int i = 0; i + w < maxwidth; i++)
 			addch(' ');
-	else
-		for (; l < maxlen; l++)
-			stext[l] = ' ';
+	}
 	addstr(stext);
-	stext[maxlen] = t;
-	addch(']');
+	if (BAR_ALIGN == ALIGN_LEFT) {
+		for (; w < maxwidth; w++)
+			addch(' ');
+	}
+	mvaddch(by, width - 1, ']');
 	attrset(NORMAL_ATTR);
 	if (sel)
 		curs_set(madtty_cursor(sel->term));

Reply via email to