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 <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2018-01-25 00:00:00 -06:00 committed by Craig Small
parent 3b53aba319
commit f3f90ab93c
2 changed files with 52 additions and 3 deletions

View File

@ -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 #############################################*/

View File

@ -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. ###################################################*/