[pacman-dev] [PATCH 1/2] split "Packages (%zd):" message
Basically all translation messages that need colouring but _also_ happen to be format strings need to be split up. This makes it easy to conditionally embed colour codes into the output at runtime. Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index 3270c74..b5ee841 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -871,7 +871,7 @@ static void _display_targets(alpm_list_t *targets, int verbose) } /* print to screen */ - pm_asprintf(&str, _("Packages (%zd):"), alpm_list_count(targets)); + pm_asprintf(&str, "%s (%zd):", _("Packages"), alpm_list_count(targets)); printf("\n"); cols = getcols(fileno(stdout)); -- 1.8.1.4
Remove the format component of the "Total Download Size" and related messages. The heading will be colourized, the size won't. However since the length of these messages can vary by language, we need a pretty printer to format them nicely. Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> --- src/pacman/util.c | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index b5ee841..b7ee9ec 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -47,6 +47,11 @@ #include "callback.h" +typedef struct _table_row_t { + const char *label; + int size; +} table_row_t; + int trans_init(alpm_transflag_t flags, int check_valid) { int ret; @@ -824,15 +829,35 @@ static alpm_list_t *create_verbose_row(pm_target_t *target) return ret; } +static void _print_table(table_row_t *rows, int spacing) +{ + table_row_t *row; + int max_len = 0; + + /* max length line */ + for (row = rows; row && row->label; ++row) { + int len = string_length(row->label); + if(len > max_len) + max_len = len; + } + + max_len += spacing; + + for (row = rows; row && row->label; ++row) { + const char *units; + double s = humanize_size(row->size, 'M', 2, &units); + printf("%-*s %.2f %s\n", max_len, row->label, s, units); + } +} + /* prepare a list of pkgs to display */ static void _display_targets(alpm_list_t *targets, int verbose) { char *str; - const char *label; - double size; off_t isize = 0, rsize = 0, dlsize = 0; unsigned short cols; alpm_list_t *i, *rows = NULL, *names = NULL; + table_row_t tbl_rows[5], *row = tbl_rows; if(!targets) { return; @@ -897,24 +922,30 @@ static void _display_targets(alpm_list_t *targets, int verbose) free(str); if(dlsize > 0 || config->op_s_downloadonly) { - size = humanize_size(dlsize, 'M', 2, &label); - printf(_("Total Download Size: %.2f %s\n"), size, label); + row->label = _("Total Download Size:"); + row->size = dlsize; + ++row; } if(!config->op_s_downloadonly) { if(isize > 0) { - size = humanize_size(isize, 'M', 2, &label); - printf(_("Total Installed Size: %.2f %s\n"), size, label); + row->label = _("Total Installed Size:"); + row->size = isize; + ++row; } if(rsize > 0 && isize == 0) { - size = humanize_size(rsize, 'M', 2, &label); - printf(_("Total Removed Size: %.2f %s\n"), size, label); + row->label = _("Total Removed Size:"); + row->size = rsize; + ++row; } /* only show this net value if different from raw installed size */ if(isize > 0 && rsize > 0) { - size = humanize_size(isize - rsize, 'M', 2, &label); - printf(_("Net Upgrade Size: %.2f %s\n"), size, label); + row->label = _("Net Upgrade Size:"); + row->size = isize - rsize; + ++row; } } + row->label = NULL; + _print_table(tbl_rows, 2); } static int target_cmp(const void *p1, const void *p2) -- 1.8.1.4
On Feb 28, 2013 6:56 PM, "Simon Gomizelj" <simongmzlj@gmail.com> wrote:
Basically all translation messages that need colouring but _also_ happen to be format strings need to be split up.
This makes it easy to conditionally embed colour codes into the output at runtime.
Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> ---
I say this without really knowing how gettext works, but this looks like it closes a potential format string vulnerability.
src/pacman/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 3270c74..b5ee841 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -871,7 +871,7 @@ static void _display_targets(alpm_list_t *targets, int verbose) }
/* print to screen */ - pm_asprintf(&str, _("Packages (%zd):"), alpm_list_count(targets)); + pm_asprintf(&str, "%s (%zd):", _("Packages"), alpm_list_count(targets)); printf("\n");
cols = getcols(fileno(stdout)); -- 1.8.1.4
On 02/28/13 at 06:58pm, Dave Reisner wrote:
On Feb 28, 2013 6:56 PM, "Simon Gomizelj" <simongmzlj@gmail.com> wrote:
Basically all translation messages that need colouring but _also_ happen to be format strings need to be split up.
This makes it easy to conditionally embed colour codes into the output at runtime.
Signed-off-by: Simon Gomizelj <simongmzlj@gmail.com> ---
I say this without really knowing how gettext works, but this looks like it closes a potential format string vulnerability.
src/pacman/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 3270c74..b5ee841 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -871,7 +871,7 @@ static void _display_targets(alpm_list_t *targets, int verbose) }
/* print to screen */ - pm_asprintf(&str, _("Packages (%zd):"), alpm_list_count(targets)); + pm_asprintf(&str, "%s (%zd):", _("Packages"), alpm_list_count(targets)); printf("\n");
cols = getcols(fileno(stdout)); -- 1.8.1.4
The gettext documentation suggests using format strings with full sentences rather than this type of string concatenation [0], and it is capable of checking that translated format strings are compatible [1]. [0] http://www.gnu.org/software/gettext/manual/gettext.html#Preparing-Strings [1] http://www.gnu.org/software/gettext/manual/gettext.html#c_002dformat-Flag
participants (3)
-
andrew.gregory.8@gmail.com
-
Dave Reisner
-
Simon Gomizelj