Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit c082196

Browse files
jiangxingitster
authored andcommitted
Add utf8_fprintf helper that returns correct number of columns
Since command usages can be translated, they may include utf-8 encoded strings, and the output in console may not align well any more. This is because strlen() is different from strwidth() on utf-8 strings. A wrapper utf8_fprintf() can help to return the correct number of columns required. Signed-off-by: Jiang Xin <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Reviewed-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d41784 commit c082196

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

parse-options.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cache.h"
44
#include "commit.h"
55
#include "color.h"
6+
#include "utf8.h"
67

78
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
89
const char * const *usagestr,
@@ -491,7 +492,7 @@ static int usage_argh(const struct option *opts, FILE *outfile)
491492
s = literal ? "[%s]" : "[<%s>]";
492493
else
493494
s = literal ? " %s" : " <%s>";
494-
return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
495+
return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
495496
}
496497

497498
#define USAGE_OPTS_WIDTH 24
@@ -550,7 +551,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
550551
if (opts->long_name)
551552
pos += fprintf(outfile, "--%s", opts->long_name);
552553
if (opts->type == OPTION_NUMBER)
553-
pos += fprintf(outfile, "-NUM");
554+
pos += utf8_fprintf(outfile, _("-NUM"));
554555

555556
if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
556557
!(opts->flags & PARSE_OPT_NOARG))

utf8.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,27 @@ int same_encoding(const char *src, const char *dst)
430430
return !strcasecmp(src, dst);
431431
}
432432

433+
/*
434+
* Wrapper for fprintf and returns the total number of columns required
435+
* for the printed string, assuming that the string is utf8.
436+
*/
437+
int utf8_fprintf(FILE *stream, const char *format, ...)
438+
{
439+
struct strbuf buf = STRBUF_INIT;
440+
va_list arg;
441+
int columns;
442+
443+
va_start(arg, format);
444+
strbuf_vaddf(&buf, format, arg);
445+
va_end(arg);
446+
447+
columns = fputs(buf.buf, stream);
448+
if (0 <= columns) /* keep the error from the I/O */
449+
columns = utf8_strwidth(buf.buf);
450+
strbuf_release(&buf);
451+
return columns;
452+
}
453+
433454
/*
434455
* Given a buffer and its encoding, return it re-encoded
435456
* with iconv. If the conversion fails, returns NULL.

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ int utf8_strwidth(const char *string);
88
int is_utf8(const char *text);
99
int is_encoding_utf8(const char *name);
1010
int same_encoding(const char *, const char *);
11+
int utf8_fprintf(FILE *, const char *, ...);
1112

1213
int strbuf_add_wrapped_text(struct strbuf *buf,
1314
const char *text, int indent, int indent2, int width);

0 commit comments

Comments
 (0)