From f3f90ab93c1b01cc1956243d1dd7ec9b93224cb8 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Thu, 25 Jan 2018 00:00:00 -0600 Subject: [PATCH] top: allow translated field headers to determine width After wrestling with extra wide characters, supporting languages like zh_CN, sometimes default/minimum column widths might force a truncation of translated headers. So, this commit explores one way that such truncations could be avoided. It is designed so as to have minimal impact on existing code, ultimately affecting just one function. But it's off by default via its own #define. Signed-off-by: Jim Warner --- top/top.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- top/top.h | 1 + 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/top/top.c b/top/top.c index 30a750f8..1fbc06b1 100644 --- a/top/top.c +++ b/top/top.c @@ -2410,9 +2410,19 @@ static inline void widths_resize (void) { * This routine exists just to consolidate most of the messin' * around with the Fieldstab array and some related stuff. */ static void zap_fieldstab (void) { +#ifdef WIDEN_COLUMN + #define maX(E) ( (wtab[E].wnls > wtab[E].wmin) \ + ? wtab[E].wnls : wtab[E].wmin ) + static struct { + int wmin; // minimum field width (-1 == variable width) + int wnls; // translated header column requirements + int watx; // +1 == non-scalable auto sized columns + } wtab[EU_MAXPFLGS]; +#endif static int once; unsigned digits; char buf[8]; + int i; if (!once) { Fieldstab[EU_CPN].width = 1; @@ -2426,6 +2436,23 @@ static void zap_fieldstab (void) { = Fieldstab[EU_PGD].width = Fieldstab[EU_SID].width = Fieldstab[EU_TGD].width = Fieldstab[EU_TPG].width = digits; } +#ifdef WIDEN_COLUMN + // identify our non-scalable auto sized columns + wtab[EU_UED].watx = wtab[EU_UEN].watx = wtab[EU_URD].watx + = wtab[EU_URN].watx = wtab[EU_USD].watx = wtab[EU_USN].watx + = wtab[EU_GID].watx = wtab[EU_GRP].watx = wtab[EU_TTY].watx + = wtab[EU_WCH].watx = wtab[EU_NS1].watx = wtab[EU_NS2].watx + = wtab[EU_NS3].watx = wtab[EU_NS4].watx = wtab[EU_NS5].watx + = wtab[EU_NS6].watx = wtab[EU_LXC].watx = +1; + /* establish translatable header 'column' requirements + and ensure .width reflects the widest value */ + for (i = 0; i < EU_MAXPFLGS; i++) { + wtab[i].wmin = Fieldstab[i].width; + wtab[i].wnls = (int)strlen(N_col(i)) - utf8_delta(N_col(i)); + if (wtab[i].wmin != -1) + Fieldstab[i].width = maX(i); + } +#endif once = 1; } @@ -2441,6 +2468,27 @@ static void zap_fieldstab (void) { } } +#ifdef WIDEN_COLUMN + digits = (unsigned)snprintf(buf, sizeof(buf), "%u", (unsigned)smp_num_cpus); + if (wtab[EU_CPN].wmin < digits) { + if (5 < digits) error_exit(N_txt(FAIL_widecpu_txt)); + wtab[EU_CPN].wmin = digits; + Fieldstab[EU_CPN].width = maX(EU_CPN); + } + digits = (unsigned)snprintf(buf, sizeof(buf), "%u", (unsigned)Numa_node_tot); + if (wtab[EU_NMA].wmin < digits) { + wtab[EU_NMA].wmin = digits; + Fieldstab[EU_NMA].width = maX(EU_NMA); + } + + // and accommodate optional wider non-scalable columns (maybe) + if (!AUTOX_MODE) { + for (i = 0; i < EU_MAXPFLGS; i++) { + if (wtab[i].watx) + Fieldstab[i].width = Rc.fixed_widest ? Rc.fixed_widest + maX(i) : maX(i); + } + } +#else digits = (unsigned)snprintf(buf, sizeof(buf), "%u", (unsigned)smp_num_cpus); if (1 < digits) { if (5 < digits) error_exit(N_txt(FAIL_widecpu_txt)); @@ -2450,10 +2498,8 @@ static void zap_fieldstab (void) { if (2 < digits) { Fieldstab[EU_NMA].width = digits; } - - /* and accommodate optional wider non-scalable columns (maybe) */ + // and accommodate optional wider non-scalable columns (maybe) if (!AUTOX_MODE) { - int i; Fieldstab[EU_UED].width = Fieldstab[EU_URD].width = Fieldstab[EU_USD].width = Fieldstab[EU_GID].width = Rc.fixed_widest ? 5 + Rc.fixed_widest : 5; @@ -2468,6 +2514,7 @@ static void zap_fieldstab (void) { Fieldstab[i].width = Rc.fixed_widest ? 10 + Rc.fixed_widest : 10; } +#endif /* plus user selectable scaling */ Fieldstab[EU_VRT].scale = Fieldstab[EU_SWP].scale @@ -2479,6 +2526,7 @@ static void zap_fieldstab (void) { // lastly, ensure we've got proper column headers... calibrate_fields(); + #undef maX } // end: zap_fieldstab /*###### Library Interface #############################################*/ diff --git a/top/top.h b/top/top.h index 67106509..41c571f5 100644 --- a/top/top.h +++ b/top/top.h @@ -61,6 +61,7 @@ //#define USE_X_COLHDR /* emphasize header vs. whole col, for 'x' */ //#define VALIDATE_NLS /* validate the integrity of all nls tbls */ //#define VER_J_RCFILE /* increase # of fields, rcfile ver to 'j' */ +//#define WIDEN_COLUMN /* base column widths on translated header */ /*###### Notes, etc. ###################################################*/