Indeed this seems the best option to me.
What would be nice, in order to make it usable site wide and out of the box, would be to define in colors some "standard" nicknames for colors like:
red=1;31:green=1;32:yellow=1;33:blue=1;34
Roberto
On 30 May 2025 at 15:24 +0200, Xavier Delaruelle <xav...@gm...>, wrote:
> If you use Modules v5.2 or newer version, you may also rely on the
> "sgr" internal procedure to render text graphically.
>
> First, you need to give access to this internal procedure within the
> modulefile evaluation context. Add the following line in the
> siteconfig.tcl script (usually installed in /etc/environment-modules):
>
> set modulefile_extra_cmds {sgr sgr}
>
> Two arguments should be given to sgr proc:
>
> sgr color_key text
>
> You may rely on the predefined color keys or add your own (through the
> "colors" configuration option):
>
> $ ml config colors
> Modules Release 5.5.0 (2024-11-11)
>
> - Config. name ---------.- Value (set by if default overridden) ---------------
> colors
> hi=1:db=2:tr=2:se=2:er=91:wa=93:me=95:in=94:mp=1;94:di=94:al=96:va=93:sy=95:de=4:cm=92:aL=100:L=90;47:H=2:F=41:nF=43:S=46:sS=44:kL=30;48;5;109
>
> For example, if you want the text to be rendered like warning message,
> the modulefile will be:
>
> #%Module
> module-whatis "This is an example using the [sgr wa XX] version"
>
> Another advantages to use "sgr" proc is that it does not graphically
> render text if color mode is disabled by user.
>
> Regards,
> Xavier
>
> Le ven. 30 mai 2025 à 14:48, InfnPi <rob...@pi...> a écrit :
> >
> > So I finally managed (with the help of Xavier) to do what i wanted: Write some text to stderr using colors.
> >
> > Here is a simple script that defines and uses a procedure to print out red text using ANSI codes.
> > One thing to be careful about is that being the return value included in quotes (which get evaluated) you need to escape [ inside the return string.
> >
> > ---
> > #%Module
> >
> > proc Red {text} {
> > return "\033\[1;31m$text\033\[0m"
> > }
> >
> > module-whatis "This is an example using the [Red procedure] to color text."
> > ---
> >
> > Roberto
> >
> > Sent with Spark
> > On 29 May 2025 at 19:27 +0200, Xavier Delaruelle <xav...@gm...>, wrote:
> >
> > 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
> >
> >
> >
> > _______________________________________________
> > Modules-interest mailing list
> > Mod...@li...
> > https://lists.sourceforge.net/lists/listinfo/modules-interest
> >
> > _______________________________________________
> > Modules-interest mailing list
> > Mod...@li...
> > https://lists.sourceforge.net/lists/listinfo/modules-interest
>
>
> _______________________________________________
> Modules-interest mailing list
> Mod...@li...
> https://lists.sourceforge.net/lists/listinfo/modules-interest
|