Re: [PATCH] fdisk: fix format list for localization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 08, 2010 at 10:55:25AM +0000, Pádraig Brady wrote:
> On 07/03/10 15:51, Makoto Kato wrote:
>> l (list) command is localizable text using gettext.
>> When you localize it, list result isn't formated well.
>>
>> If current locale is UTF-8, one character may use 2 or 3 bytes
>> even if output width is one.
>> ---
>>   fdisk/fdisk.c |   30 ++++++++++++++++++++++++++++--
>>   1 files changed, 28 insertions(+), 2 deletions(-)
>>
>> diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
>> index 09efd1c..7373a00 100644
>> --- a/fdisk/fdisk.c
>> +++ b/fdisk/fdisk.c
>> @@ -25,6 +25,7 @@
>>   #include "nls.h"
>>   #include "blkdev.h"
>>   #include "common.h"
>> +#include "widechar.h"
>>   #include "fdisk.h"
>>   #include "wholedisk.h"
>>
>> @@ -559,6 +560,11 @@ void list_types(struct systypes *sys)
>>   {
>>   	unsigned int last[4], done = 0, next = 0, size;
>>   	int i;
>> +#ifdef HAVE_WIDECHAR
>> +	int n, width;
>> +	wchar_t wc_buf[15]; /* max length */
>> +	char mb_buf[45]; /* max length * UTF-8 max length */
>
>         #define lengthof(x) (sizeof(x)/sizeof(*x))
> 	wchar_t wc_buf[16]; /* max length + NUL */
> 	char mb_buf[lengthof(wc_buf)*MB_LEN_MAX];
>
>> +#endif
>>
>>   	for (i = 0; sys[i].name; i++);
>>   	size = i;
>> @@ -568,8 +574,28 @@ void list_types(struct systypes *sys)
>>   	i = done = 0;
>>
>>   	do {
>> -		printf("%c%2x  %-15.15s", i ? ' ' : '\n',
>> -		        sys[next].type, _(sys[next].name));
>> +		printf("%c%2x  ", i ? ' ' : '\n', sys[next].type);
>> +#ifdef HAVE_WIDECHAR
>> +		/* the width of format string with non-ascii character
>> +		   doesn't work correctly. We need calcurate it */
>> +		n = mbstowcs(wc_buf, _(sys[next].name), 15);
>> +		width = wcswidth(wc_buf, n);
>> +		while (width>  15)
>> +			width = wcswidth(wc_buf, --n);
>> +		if (n>  0&&  width>  0) {
>> +			/* Don't mix using printf and wprintf */
>> +			wc_buf[n] = '\0';
>> +			wcstombs(mb_buf, wc_buf, 45);
>> +			printf("%s", mb_buf);
>> +			for (n = 0; n<  15 - width; n++)
>> +				printf(" ");
>> +		} else {
>> +			/* convert error */
>> +			printf("%-15.15s", _(sys[next].name));
>> +		}
>> +#else
>> +		printf("%-15.15s", _(sys[next].name));
>> +#endif
>
> Actually this multibyte alignment is probably best
> done by reusing the code from cal. This has already
> been refactored out for coreutils here:
>
> http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=gl/lib/mbsalign.c;hb=HEAD

 Good work. It would be nice to have this code in lib/mdalign.c and
 use it in cal.c and fdisk.c ;-)
 
    Karel


-- 
 Karel Zak  <kzak@xxxxxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux