Hello Roberto,
The syntax with double quotes is fine, you have no special character
in your text to escape:
module-whatis "This module sets up software XXX $ver"
You may use an internal variable to use this description at different places:
set description "This module sets up software XXX $ver"
module-whatis $description
proc ModulesHelp {} {
puts stderr $::description
}
For the color, I would use a "color" proc like you did, but I suggest
to directly use ANSI SGR code [1] rather calling for tput:
return "\033\[1;31m$text\033\[0m"
Regards
Xavier
[1] https://en.wikipedia.org/wiki/ANSI_escape_code#Select_Graphic_Rendition_parameters
Le jeu. 29 mai 2025 à 10:32, InfnPi <rob...@pi...> a écrit :
>
> Here we go with another question, due to my lack of tcl knowledge.
>
> What i would like to achieve is to be able to use variables in text printed to stderr inside a tcl command like module-whatis.
>
> Very often in modulefiles you have to refer to the same information in more than one place and therefore it is convenient to use a variable.
>
> as an example in a module installing tool XXX version YYY you write something like:
>
> module-whatis (This module sets up software XXX version YYY}
>
> and
>
> proc ModulesHelp {} {
> puts stderr {This module sets up software XXX version YYY}
> }
>
> Instead of writing "Version YYY" any times in different places it would be nice to be able to use:
> set ver "Version YYY"
> and then
> module-whatis (This module sets up software XXX $ver}
>
> this would reduce errors and add maintainability to the modulefile, in the sense that if a new version ZZZ of software XXX comes out i would (probably) use the same modulefile just replacing a few general variables.
>
> Unfortunately in using
> module-watis {This module sets up software XXX $ver} will not expand the variable $ver.
>
> A workaround i found is to write it in the following way:
> module-whatis "This module sets up software XXX $ver"
> which expands variables (but you have to escape special characters).
>
> Is this correct or does it have some unwanted consequences?
>
>
> Along with this question there is a second one i would like to ask:
>
> Is there a way to print text to stderr using colors?
>
> I found a way to do it defining a procedure and some variables:
>
> proc color {color text} {
> return [exec tput setaf $color]$text[exec tput sgr0]
> }
> set red 1
> set green 2
> set yellow 3
> set blue 4
>
> and then in the module file i will use:
> module-whatis "This module sets up software [color $green XXX] $ver"
>
> which will print XXX in green.
>
> Is there a native way to do the same?
> Is it fine to use it in the way i described?
>
> Again, many thx in advance for all the answers.
>
> Roberto
|