Commits

Anonymous committed f743254

so close

  • Participants
  • Parent commits e1269e5
  • Branches utf8

Comments (0)

Files changed (1)

 	return ret;
 }
 
+int width(char *buf, int *ind, int len, int crement)
+{
+	int nb, mask;
+	wchar_t wc;
+
+	if (len == 0)
+		return 0;
+
+	for (; *ind > 0 && *ind < len  &&
+		(buf[*ind] & 0xc0) != 0xc0 &&
+		(buf[*ind] & 0x80); *ind += crement)
+		;
+	// skip the first, if it's ascii we still want one byte (nb = 1)
+	for (nb = 1, mask = 0x40; buf[*ind] & mask; nb++, mask >>= 1)
+		;
+	if (mbtowc(&wc, buf + *ind, nb) < 0)
+		warnx("failed multibyte to wide char conversion");
+	if ((nb = wcwidth(wc)) < 0)
+		warnx("failed to find width of wide char");
+
+	return nb;
+}
+
+int nbytes(char *buf, int len, int cols)
+{
+	int i, n;
+
+	for (n = 0, i = width(buf, &n, len, 1); i <= cols && n < len; n++, i += width(buf, &n, len, 1))
+		;
+
+	return n;
+}
+
 void print_usr(void)
 {
 	int cols, left_prompt_len, right_prompt_len = 0;
 	struct winsize ws;
 
-	/*
 	if (ioctl(STDOUT, TIOCGWINSZ, &ws) < 0)
 		err(1, "failed ioctl");
 
-	cols = ws.ws_col - 1;
+	cols = ws.ws_col;
 
 	erase_usr();
 
 	}
 
 	//my_write(STDOUT, usrbuf + start, MIN(cols, len));
-	my_write(STDOUT, usrbuf + start, len);
+	my_write(STDOUT, usrbuf + start, nbytes(usrbuf + start, len - start, cols));
 
-	if (start + cols < len)
+	if (start + cols < wlen)
 		my_write(STDOUT, scrl_prompt_right, right_prompt_len);
 
-		*/
-
+	/*
 	erase_usr();
+	printf("\nlen = %d wlen = %d nbytes = %d\n", len, wlen, nbytes(usrbuf, len, 80));
 	left_prompt_len = strlen(nrml_prompt);
 	my_write(STDOUT, nrml_prompt, left_prompt_len);
 	my_write(STDOUT, usrbuf, len);
+	*/
 	cursor_beg();
 	cursor_fwd(left_prompt_len + wcur - start);
 	fflush(stdout);
 }
 
-int width(char *buf, int *ind, int len, int crement)
-{
-	int nb, mask;
-	wchar_t wc;
-
-	for (; *ind > 0 && *ind < len  &&
-		(buf[*ind] & 0xc0) != 0xc0 &&
-		(buf[*ind] & 0x80); *ind += crement)
-		;
-	// skip the first, if it's ascii we still want one byte (nb = 1)
-	for (nb = 1, mask = 0x40; buf[*ind] & mask; nb++, mask >>= 1)
-		;
-	if (mbtowc(&wc, buf + *ind, nb) < 0)
-		warnx("failed multibyte to wide char conversion");
-	if ((nb = wcwidth(wc)) < 0)
-		warnx("failed to find width of wide char");
-
-	return nb;
-}
-
 void run()
 {
 	static int escape = 0, wstart = 0, in_mb = 0;