From: <ny...@us...> - 2007-01-04 02:54:07
|
Revision: 236 http://svn.sourceforge.net/pmplib/?rev=236&view=rev Author: nyaochi Date: 2007-01-03 18:54:06 -0800 (Wed, 03 Jan 2007) Log Message: ----------- Use wcwidth() to calculate the print length: some unicode characters (e.g., CJK) require two columns to print them in a console buffer. Print dotts (...) when the message is truncated to the console width. This revision passed a test with long Japanese filenames. Modified Paths: -------------- trunk/pmplib/frontend/easypmp/cui/console_posix.c trunk/pmplib/frontend/easypmp/cui/util.c trunk/pmplib/include/ucs2char.h trunk/pmplib/lib/ucs2/ucs2char_iconv.c Modified: trunk/pmplib/frontend/easypmp/cui/console_posix.c =================================================================== --- trunk/pmplib/frontend/easypmp/cui/console_posix.c 2007-01-04 01:39:08 UTC (rev 235) +++ trunk/pmplib/frontend/easypmp/cui/console_posix.c 2007-01-04 02:54:06 UTC (rev 236) @@ -183,33 +183,32 @@ */ int console_println(FILE *fp, const ucs2char_t* line, int min_width) { - int length = ucs2len(line); - const ucs2char_t* line_to_print; + const int margin = 5; - /* Check if writing to a terminal. */ - int writing_to_tty = is_tty(fp); + if (is_tty(fp)) { + int x = 0, width = 0; + const ucs2char_t* p = line; - if(!writing_to_tty) { - /* Write the whole line to the file, add \n. */ + while (*p) { + wchar_t c = (wchar_t)*p; + + if (window_width <= x + margin) { + int ndotts = MIN(3, window_width - x); + while (ndotts-- > 0) fputc('.', fp); + break; + } else { + /* I don't understand why fputwc(c, fp); doesn't work... */ + fputucs2c(*p, fp); + } + p++; + x += wcwidth(c); + } + fputc('\r', fp); + return (int)(p - line); + } else { fprints(fp, "%s\n", line); - return length; - } else if (length <= window_width) { - /* There's enough room to show the whole line. */ - fprints(fp, "%s\r", line); - return length; + return ucs2len(line); } - else { - /* Length of the longest string we might display: */ - const int max_trunc_length = MAX(window_width,min_width); - - /* Length of string we actually will display: */ - const size_t trunc_length = MIN(max_trunc_length, length); - - ucs2char_t *truncated_line = ucs2calloc(sizeof(ucs2char_t) * (trunc_length+1)); - ucs2ncpy(truncated_line, line, trunc_length); - fprints(fp, "%s\r", truncated_line); - return trunc_length; - } } Modified: trunk/pmplib/frontend/easypmp/cui/util.c =================================================================== --- trunk/pmplib/frontend/easypmp/cui/util.c 2007-01-04 01:39:08 UTC (rev 235) +++ trunk/pmplib/frontend/easypmp/cui/util.c 2007-01-04 02:54:06 UTC (rev 236) @@ -78,7 +78,7 @@ *p++ = ' '; ucs2cpy(p, msg); - console_clearln(fp); + //console_clearln(fp); console_println(fp, str, 0); return 0; } Modified: trunk/pmplib/include/ucs2char.h =================================================================== --- trunk/pmplib/include/ucs2char.h 2007-01-04 01:39:08 UTC (rev 235) +++ trunk/pmplib/include/ucs2char.h 2007-01-04 02:54:06 UTC (rev 236) @@ -123,6 +123,7 @@ UCS2API int ucs2stat_is_dir(const ucs2char_t *filename); UCS2API int ucs2stat_is_exist(const ucs2char_t *filename); +UCS2API ucs2char_t fputucs2c(ucs2char_t c, FILE *fp); UCS2API FILE *ucs2fopen(const ucs2char_t *filename, const char *mode); /** @} */ Modified: trunk/pmplib/lib/ucs2/ucs2char_iconv.c =================================================================== --- trunk/pmplib/lib/ucs2/ucs2char_iconv.c 2007-01-04 01:39:08 UTC (rev 235) +++ trunk/pmplib/lib/ucs2/ucs2char_iconv.c 2007-01-04 02:54:06 UTC (rev 236) @@ -165,7 +165,7 @@ { char buff[1024]; sprintf(buff, "%d", value); - mbstoucs2(string, 1024, buff, sizeof(buff)); + mbstoucs2(string, 1024, buff, strlen(buff)+1); return string; } @@ -241,6 +241,17 @@ } } +ucs2char_t fputucs2c(ucs2char_t c, FILE *fp) +{ + ucs2char_t ucs2str[2] = {c, 0}; + size_t mbs_size = ucs2tombs(NULL, 0, ucs2str, ucs2len(ucs2str)) + 1; + char* mbs = (char *)alloca(mbs_size * sizeof(char)); + if (mbs) { + ucs2tombs(mbs, mbs_size, ucs2str, ucs2len(ucs2str)+1); + fputs(mbs, fp); + } +} + FILE *ucs2fopen(const ucs2char_t *filename, const char *mode) { FILE *fp = NULL; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |