Terminal control/Coloured text
The task is to display a word in various colours on the terminal. The system palette, or colours such as Red, Green, Blue, Magenta, Cyan, and Yellow can be used.
Optionally demonstrate:
- How the system should determine if the terminal supports colour
- Setting of the background colour
- How to cause blinking or flashing (if supported by the terminal)
AutoHotkey
AutoHotkey is not written for the command line, so we need to use the WinAPI directly. For simplicity, this example demonstrates only the foreground colours. <lang AHK>DllCall( "AllocConsole" ) ; create a console if not launched from one hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 ) Loop 15 SetConsoleTextAttribute(hConsole, A_Index) ,WriteConsole(hConsole, "AutoHotkey`n")
MsgBox
SetConsoleTextAttribute(hConsole, Attributes){ return DllCall( "SetConsoleTextAttribute", UPtr, hConsole, UShort, Attributes) } WriteConsole(hConsole, text){ VarSetCapacity(out, 16) If DllCall( "WriteConsole", UPtr, hConsole, Str, text, UInt, StrLen(text) , UPtrP, out, uint, 0 ) return out return 0 }</lang>
Mathematica
Delegating to tput on terminal enabled OS(Mac Os, Linux) <lang Mathematica>Run["tput setaf 1"]; Print["Coloured Text"]; Run["tput setaf 2"]; Print["Coloured Text"]; Run["tput setaf 3"]; Print["Coloured Text"]</lang>
PicoLisp
<lang PicoLisp>(unless (member (sys "TERM") '("linux" "xterm" "xterm-color" "rxvt"))
(quit "This application requires a colour terminal") )
- Coloured text
(for X '((1 . "Red") (4 . "Blue") (3 . "Yellow"))
(call 'tput "setaf" (car X)) (prinl (cdr X)) )
- Blinking
(out '(tput "-S")
(prinl "setab 1^Jsetaf 3^Jblink") )
(prin "Flashing text")
(call 'tput 'sgr0) # reset (prinl)</lang>
PureBasic
<lang purebasic>If OpenConsole()
PrintN("Background color# 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15") PrintN(" -----------------------------------------------") Define Foreground, Background For Foreground = 0 To 15 ConsoleColor(7, 0) ;grey foreground, black background Print("Foreground color# " + RSet(Str(Foreground), 2, "0") + " ") For Background = 0 To 15 ConsoleColor(Foreground, Background) Print(RSet(Str(Foreground), 2, "0")) ConsoleColor(7, 0) ;grey foreground, black background Print(" ") Next PrintN("") Next ConsoleColor(7, 0) ;grey foreground, black background Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
Tcl
This only works on Unix terminals as it delegates to the system tput command. <lang tcl># Utility interfaces to the low-level command proc capability cap {expr {![catch {exec tput -S << $cap}]}} proc colorterm {} {expr {[capability setaf] && [capability setab]}} proc tput args {exec tput -S << $args >/dev/tty} array set color {black 0 red 1 green 2 yellow 3 blue 4 magenta 5 cyan 6 white 7} proc foreground x {exec tput -S << "setaf $::color($x)" > /dev/tty} proc background x {exec tput -S << "setab $::color($x)" > /dev/tty} proc reset {} {exec tput sgr0 > /dev/tty}
- Demonstration of use
if {[colorterm]} {
foreground blue background yellow puts "Color output" reset
} else {
puts "Monochrome only"
}
if {[capability blink]} {
tput blink puts "Blinking output" reset
} else {
puts "Steady only"
}</lang>
UNIX Shell
<lang sh>#!/bin/sh
- Check if the terminal supports colour
case $TERM in
linux) ;; rxvt) ;; *) echo "HW65000 This application requires a colour terminal" >&2 exit 252 #ERLHW incompatible hardware ;;
esac
- Coloured text
tput setaf 1 #red echo "Red" tput setaf 4 #blue echo "Blue" tput setaf 3 # yellow echo "Yellow"
- Blinking
tput setab 1 # red background tput setaf 3 # yellow foreground
- tput blink # enable blinking (but does not work on some terminals)
echo "Flashing text"
tput sgr0 # reset everything before exiting</lang>
ZX Spectrum Basic
The ZX Spectrum will always output colour. However if the television is black and white, these will show as various levels of luminence corresponding to the numerical colour value. <lang zxbasic>10 FOR l=0 TO 7 20 READ c$: REM get our text for display 30 INK l: REM set the text colour 40 PRINT c$ 50 NEXT l 60 PAPER 2: REM red background 70 INK 6: REM yellow forground 80 FLASH 1: REM activate flashing 90 PRINT "Flashing!": REM this will flash red and yellow (alternating inverse) 100 PAPER 7: INK 0: FLASH 0: REM normalize colours before exit 110 STOP
900 DATA "Black","Blue","Red","Magenta","Green","Cyan","Yellow","White"</lang>