diff console/libgnt/gntutils.c @ 14423:cfd5bcc06a7e

[gaim-migrate @ 17131] Try to make sure the strings are not too long to get out of the range of the screen. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sat, 02 Sep 2006 23:06:25 +0000
parents c4a32405af68
children c374f45f4c94
line wrap: on
line diff
--- a/console/libgnt/gntutils.c	Sat Sep 02 20:17:43 2006 +0000
+++ b/console/libgnt/gntutils.c	Sat Sep 02 23:06:25 2006 +0000
@@ -68,3 +68,32 @@
 	return str;
 }
 
+char *gnt_util_onscreen_fit_string(const char *string, int maxw)
+{
+	const char *start, *end;
+	GString *str;
+
+	if (maxw <= 0)
+		maxw = getmaxx(stdscr) - 4;
+
+	start = string;
+	str = g_string_new(NULL);
+
+	while (*start) {
+		if ((end = strchr(start, '\n')) != NULL ||
+			(end = strchr(start, '\r')) != NULL) {
+			if (gnt_util_onscreen_width(start, end) <= maxw) {
+				++end;
+			} else
+				end = NULL;
+		}
+		if (end == NULL)
+			end = gnt_util_onscreen_width_to_pointer(start, maxw, NULL);
+		str = g_string_append_len(str, start, end - start);
+		start = end;
+		if (*end && *end != '\n' && *end != '\r')
+			str = g_string_append_c(str, '\n');
+	}
+	return g_string_free(str, FALSE);
+}
+