[pacman-dev] [PATCH] util.c: table_print_line: properly align texts involving CJK

Chih-Hsuan Yen yan12125 at gmail.com
Sat Sep 12 08:25:58 UTC 2020


Fixes FS#59229

Signed-off-by: Chih-Hsuan Yen <yan12125 at gmail.com>
---
 src/pacman/util.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index e9187529..b45ca22d 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -516,22 +516,32 @@ static void table_print_line(const alpm_list_t *line, short col_padding,
 			i++, curcell = alpm_list_next(curcell)) {
 		const struct table_cell_t *cell = curcell->data;
 		const char *str = (cell->label ? cell->label : "");
-		int cell_width;
+		int padding_width;
 
 		if(!has_data[i]) {
 			continue;
 		}
 
-		cell_width = (cell->mode & CELL_RIGHT_ALIGN ? (int)widths[i] : -(int)widths[i]);
-
 		if(need_padding) {
 			printf("%*s", col_padding, "");
 		}
 
 		if(cell->mode & CELL_TITLE) {
-			printf("%s%*s%s", config->colstr.title, cell_width, str, config->colstr.nocolor);
+			printf("%s", config->colstr.title);
+		}
+		/* Handle alignment manually here - for printf in C, "If the
+		 * precision is specified, no more than that many bytes are
+		 * written." [1]
+		 * [1] Section 7.21.6, N2176, final draft for ISO/IEC 9899:2017 (C18)
+		 * Fixes FS#59229. */
+		padding_width = (int)(widths[i] - string_length(str));
+		if (cell->mode & CELL_RIGHT_ALIGN) {
+			printf("%*s%s", padding_width, "", str);
 		} else {
-			printf("%*s", cell_width, str);
+			printf("%s%*s", str, padding_width, "");
+		}
+		if(cell->mode & CELL_TITLE) {
+			printf("%s", config->colstr.nocolor);
 		}
 		need_padding = 1;
 	}
-- 
2.28.0


More information about the pacman-dev mailing list