Skip to content

Commit fe5ef0c

Browse files
committed
Group whatis lines under module name and wrap them to terminal width
The whatis and search sub-commands print one output line per module-whatis entry, each prefixed by the module name right-aligned in a fixed-width column. A long description is sent as a single line that the terminal wraps at an arbitrary character, with the continuation flush left where it visually merges with the next entry. When a modulefile defines several module-whatis lines, the repeated name adds noise and hides where one module ends and the next begins. Report the module name once per matching modulefile, with its first whatis line beside it and the following ones aligned under the start of this text. Wrap text to the terminal width, cutting at whitespace so that words are kept whole and continuation lines stay attached to their module. SGR codes highlighting the search string are not counted in line length. A name longer than its column shifts the text start column for that module only. JSON output is unchanged. Fixes #720 Assisted-by: Claude:claude-fable-5-1 Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
1 parent b5b1c40 commit fe5ef0c

8 files changed

Lines changed: 144 additions & 16 deletions

File tree

‎NEWS.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Modules 5.8.0 (not yet released)
1818
* Add :mfcmd:`module-info modulename<module-info>` and :mfcmd:`module-info
1919
moduleversion<module-info>` modulefile commands to get the name part and
2020
the version part of currently evaluating modulefile. (fix issue #713)
21+
* Improve readability of :subcmd:`whatis` and :subcmd:`search` output: name of
22+
each matching modulefile is reported once, followed by its
23+
:mfcmd:`module-whatis` lines wrapped to the terminal width. (fix issue
24+
#720)
2125

2226

2327
.. _5.7 release notes:

‎doc/source/changes.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,11 @@ The following module sub-commands appeared on Modules 5.
10621062
of :subcmd:`reload` sub-command. No more support from ``MODULESBEGINENV``,
10631063
initial environment handling now covers this use case.
10641064

1065+
:subcmd:`search`, :subcmd:`whatis`
1066+
1067+
Starting Modules 5.8, name of each matching modulefile is reported once,
1068+
followed by its :mfcmd:`module-whatis` lines wrapped to the terminal width.
1069+
10651070
Environment
10661071
"""""""""""
10671072

‎doc/source/module.rst‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,10 @@ Module Sub-Commands
26772677
*string* in a case insensitive manner will be displayed. *string* may contain
26782678
wildcard characters.
26792679

2680+
Name of each matching *modulefile* is reported once, followed by its
2681+
*module-whatis* lines. This text is wrapped to the terminal width and its
2682+
continuation lines are aligned under the start of the first line text.
2683+
26802684
.. only:: html or latex
26812685

26822686
.. versionadded:: 4.0
@@ -2689,6 +2693,10 @@ Module Sub-Commands
26892693
.. versionchanged:: 4.6
26902694
Option :option:`--all`/:option:`-a` added
26912695

2696+
.. versionchanged:: 5.8
2697+
Module name is reported once with its *module-whatis* lines grouped
2698+
below and wrapped to terminal width
2699+
26922700
.. subcmd:: sh-to-mod shell script [arg...]
26932701

26942702
Evaluate with *shell* the designated *script* with defined *arguments* to
@@ -3172,6 +3180,10 @@ Module Sub-Commands
31723180
*modulefiles* matching *pattern*. *pattern* may contain wildcard characters.
31733181
If no *pattern* is specified, all :mfcmd:`module-whatis` lines will be shown.
31743182

3183+
Name of each matching *modulefile* is reported once, followed by its
3184+
*module-whatis* lines. This text is wrapped to the terminal width and its
3185+
continuation lines are aligned under the start of the first line text.
3186+
31753187
The parameter *pattern* may also be a symbolic modulefile name or a
31763188
modulefile alias. It may also leverage a specific syntax to finely select
31773189
module version (see `Advanced module version specifiers`_ section below).
@@ -3198,6 +3210,10 @@ Module Sub-Commands
31983210
Results from a multi *pattern* search are consolidated under a single
31993211
output
32003212

3213+
.. versionchanged:: 5.8
3214+
Module name is reported once with its *module-whatis* lines grouped
3215+
below and wrapped to terminal width
3216+
32013217

32023218
Modulefiles
32033219
^^^^^^^^^^^

‎tcl/report.tcl.in‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,55 @@ proc showModulePath {} {
16451645
}
16461646
}
16471647
1648+
# split text into lines whose visible length does not exceed max_len, cutting
1649+
# at whitespace position (SGR codes consume no length). A word longer than
1650+
# max_len is kept whole on its own line
1651+
proc wrapTextToWidth {text max_len} {
1652+
# empty text produces one empty line
1653+
if {$text eq {}} {
1654+
return [list {}]
1655+
}
1656+
set wrapped_list {}
1657+
foreach line [split $text \n] {
1658+
while {1} {
1659+
# walk through line until max_len visible characters are passed
1660+
set idx 0
1661+
set vis_len 0
1662+
set line_len [string length $line]
1663+
while {$idx < $line_len} {
1664+
if {[string range $line $idx $idx+1] eq "\033\[" && [set midx\
1665+
[string first m $line $idx]] != -1} {
1666+
set idx [expr {$midx + 1}]
1667+
} elseif {$vis_len == $max_len} {
1668+
break
1669+
} else {
1670+
incr idx
1671+
incr vis_len
1672+
}
1673+
}
1674+
# whole remaining line fits
1675+
if {$idx >= $line_len} {
1676+
lappend wrapped_list $line
1677+
break
1678+
}
1679+
# cut after word if no whitespace found before it
1680+
if {[set cut_idx [string last { } $line $idx]] < 1} {
1681+
set cut_idx [string first { } $line $idx]
1682+
}
1683+
if {$cut_idx == -1} {
1684+
lappend wrapped_list $line
1685+
break
1686+
}
1687+
lappend wrapped_list [string range $line 0 $cut_idx-1]
1688+
set line [string trimleft [string range $line $cut_idx+1 end]]
1689+
if {$line eq {}} {
1690+
break
1691+
}
1692+
}
1693+
}
1694+
return $wrapped_list
1695+
}
1696+
16481697
proc displayTableHeader {sgrkey args} {
16491698
foreach {title col_len} $args {
16501699
set col "- [sgr $sgrkey $title] "

‎tcl/subcmd.tcl.in‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,26 @@ proc cmdModuleSearch {args} {
399399
whatis a $::g_whatis 1]
400400
} else {
401401
set eltsgr [string map $matchmodmap $elt]
402-
foreach line $::g_whatis {
403-
set linesgr [string map $matchsearchmap $line]
404-
lappend display_list "[string repeat { } [expr {20 -\
405-
[string length $elt]}]]$eltsgr: $linesgr"
402+
set eltpad [string repeat { } [expr {20 - [string length\
403+
$elt]}]]
404+
# module name on first line only, whatis text of next lines
405+
# aligned under first line text and wrapped to term width
406+
set text_col [expr {[tcl::mathfunc::max 20 [string length\
407+
$elt]] + 2}]
408+
set text_pad [string repeat { } $text_col]
409+
set text_len [tcl::mathfunc::max [expr {[getState\
410+
term_columns] - $text_col}] 1]
411+
set first 1
412+
foreach entry $::g_whatis {
413+
foreach line [wrapTextToWidth [string map\
414+
$matchsearchmap $entry] $text_len] {
415+
if {$first} {
416+
set first 0
417+
lappend display_list "$eltpad$eltsgr: $line"
418+
} else {
419+
lappend display_list $text_pad$line
420+
}
421+
}
406422
}
407423
}
408424
}

‎testsuite/modules.50-cmds/730-linked_envvars.exp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ testouterr_cmd_re bash {display link/1} OK $tserr
573573
setenv_var TESTSUITE_LINKED_ENVVARS append1
574574
set tserr "$modlin $mpre $modlin
575575
\\s*link/1: link/1
576-
\\s*link/1: BAR: val2
577-
\\s*link/1: QUX: value"
576+
\\s*BAR: val2
577+
\\s*QUX: value"
578578
testouterr_cmd_re bash {whatis link/1} OK $tserr
579579

580580
setenv_var TESTSUITE_LINKED_ENVVARS prepend1

‎testsuite/modules.70-maint/055-whatis.exp‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,50 @@ set ts_csh_2 "$header\n$whatis_2: Testsuite-Modulefile"
5454
# module-whatis call produce a single module-whatis line, not one line per passed argument
5555
set ts_csh_3 "$header\n$whatis_3: Testsuite Whatis Modulefile"
5656
set ts_csh_4 "$header\n$whatis_4: Testsuite Whatis Modulefile"
57-
set ts_csh_5 "$header\n$whatis_5: Testsuite Whatis Modulefile\n$whatis_5: Second line of whatis tokens"
57+
# module name is reported once, next whatis lines are aligned under text of
58+
# first line
59+
set text_pad [string repeat { } 22]
60+
set ts_csh_5 "$header\n$whatis_5: Testsuite Whatis Modulefile\n${text_pad}Second line of whatis tokens"
5861
set ts_csh_6 "$err_path'$module_6'"
5962
set ts_csh_7 "$header\n$whatis_7: erroneous module test"
6063

6164
#
6265
# The tests
6366
#
6467

65-
testouterr_cmd_re "csh" "--terse whatis $module_1" "OK" "$ts_csh_1"
66-
testouterr_cmd_re "csh" "--terse whatis $module_2" "OK" "$ts_csh_2"
67-
testouterr_cmd_re "csh" "--terse whatis $module_3" "OK" "$ts_csh_3"
68+
testouterr_cmd_re "csh" "whatis $module_1" "OK" "$ts_csh_1"
69+
testouterr_cmd_re "csh" "whatis $module_2" "OK" "$ts_csh_2"
70+
testouterr_cmd_re "csh" "whatis $module_3" "OK" "$ts_csh_3"
6871

6972
skip_if_quick_mode
7073

71-
testouterr_cmd_re "csh" "--terse whatis $module_4" "OK" "$ts_csh_4"
72-
testouterr_cmd_re "csh" "--terse whatis $module_5" "OK" "$ts_csh_5"
74+
testouterr_cmd_re "csh" "whatis $module_4" "OK" "$ts_csh_4"
75+
testouterr_cmd_re "csh" "whatis $module_5" "OK" "$ts_csh_5"
76+
testouterr_cmd_re "csh" "whatis $module_5 $module_2" "OK" "$ts_csh_5\n$whatis_2: Testsuite-Modulefile"
7377

7478
# command nickname tests
75-
testouterr_cmd_re "csh" "--terse wh $module_5" "OK" "$ts_csh_5"
79+
testouterr_cmd_re "csh" "wh $module_5" "OK" "$ts_csh_5"
80+
81+
set module_8 "verssort/1.8-2015-12-01"
82+
set text_pad_8 [string repeat { } 25]
83+
84+
# whatis text is wrapped to terminal width, module name longer than its column
85+
# shifts text start column
86+
testouterr_cmd_re "csh" "-w 40 whatis $module_5" "OK" "$header\n$whatis_5: Testsuite Whatis\n${text_pad}Modulefile\n${text_pad}Second line of\n${text_pad}whatis tokens"
87+
testouterr_cmd_re "csh" "whatis $module_8" "OK" "$header\n$module_8: module version sort test"
88+
testouterr_cmd_re "csh" "-w 40 whatis $module_8" "OK" "$header\n$module_8: module version\n${text_pad_8}sort test"
89+
# word longer than remaining width is kept whole on its own line
90+
testouterr_cmd_re "csh" "-w 30 whatis $module_8" "OK" "$header\n$module_8: module\n${text_pad_8}version\n${text_pad_8}sort\n${text_pad_8}test"
91+
testouterr_cmd_re "csh" "-w 10 whatis $module_8" "OK" "$header\n$module_8: module\n${text_pad_8}version\n${text_pad_8}sort\n${text_pad_8}test"
92+
testouterr_cmd_re "csh" "-w 1 whatis $module_8" "OK" "$header\n$module_8: module\n${text_pad_8}version\n${text_pad_8}sort\n${text_pad_8}test"
93+
94+
# json output
95+
set ts_json_5 "{\"$modpathre\": {\n\"$module_5\": { \"name\": \"$module_5\", \"whatis\": \\\[ \"Testsuite Whatis Modulefile\", \"Second line of whatis tokens\" \\\]}\n}}"
96+
testouterr_cmd_re "csh" "-j whatis $module_5" "OK" "$ts_json_5"
7697

7798
# setup MODULEPATH with a trailing slash and an empty dir element
7899
setenv_path_var MODULEPATH {} "$modpath/"
79-
testouterr_cmd_re "csh" "--terse whatis $module_1" "OK" "$ts_csh_1"
100+
testouterr_cmd_re "csh" "whatis $module_1" "OK" "$ts_csh_1"
80101

81102
# restore MODULEPATH
82103
setenv_path_var MODULEPATH $modpath
@@ -115,7 +136,7 @@ setenv_loaded_module [list foo bar] "/path/to/foo"
115136
set ts_csh_8 "$err_loinconsist
116137
LOADEDMODULES=foo bar
117138
_LMFILES_=/path/to/foo"
118-
testouterr_cmd_re "csh" "--terse whatis $module_5" ERR $ts_csh_8
139+
testouterr_cmd_re "csh" "whatis $module_5" ERR $ts_csh_8
119140

120141
#
121142
# Cleanup

‎testsuite/modules.70-maint/060-apropos.exp‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ set header "$modlin $modpathre $modlin"
3535
set warn_opt "$warn_msgs: Unsupported option "
3636

3737
set ts_1 " whatis/lines: Testsuite Whatis Modulefile"
38-
set ts_1a " whatis/lines: Second line of whatis tokens"
38+
# module name is reported once, next whatis lines are aligned under text of
39+
# first line
40+
set text_pad [string repeat { } 22]
41+
set ts_1a "${text_pad}Second line of whatis tokens"
3942
# different behavior than C-version: on Tcl-version multiple arguments provided on a single
4043
# module-whatis call produce a single module-whatis line, not one line per passed argument
4144
set ts_2 " whatis/multiple: Testsuite Whatis Modulefile"
@@ -69,6 +72,20 @@ testouterr_cmd_re "csh" "apropos token" "ERR" "$header\n$ts_1\n$ts_1a"
6972
testouterr_cmd_re "csh" "keyword token" "ERR" "$header\n$ts_1\n$ts_1a"
7073
testouterr_cmd_re "csh" "search token" "ERR" "$header\n$ts_1\n$ts_1a"
7174

75+
# text is wrapped to terminal width
76+
testouterr_cmd_re "csh" "-w 45 search Whatis" "ERR" "$header\n whatis/lines: Testsuite Whatis\n${text_pad}Modulefile\n${text_pad}Second line of whatis\n${text_pad}tokens\n whatis/multiple: Testsuite Whatis\n${text_pad}Modulefile\n whatis/string: Testsuite Whatis\n${text_pad}Modulefile"
77+
78+
# highlighted search string does not count in wrapped line length
79+
setenv_var MODULES_COLOR always
80+
setenv_var MODULES_COLORS {hi=1:mp=1;94}
81+
set hi [sgr 1]
82+
set nohi [sgr 22]
83+
set header_sgr "$modlin [sgr {1;94}]$modpathre[sgr 0] $modlin"
84+
testouterr_cmd_re "csh" "-w 49 search token" "ERR" "$header_sgr\n whatis/lines: Testsuite Whatis Modulefile\n${text_pad}Second line of whatis\n${text_pad}${hi}token${nohi}s"
85+
testouterr_cmd_re "csh" "-w 50 search token" "ERR" "$header_sgr\n whatis/lines: Testsuite Whatis Modulefile\n${text_pad}Second line of whatis ${hi}token${nohi}s"
86+
setenv_var MODULES_COLOR never
87+
unsetenv_var MODULES_COLORS
88+
7289
#
7390
# test with erroneous modulefile
7491
# should not produce error message

0 commit comments

Comments
 (0)