Terminal control/Coloured text: Difference between revisions
m (remove irrelevance) |
(→Tcl: Added implementation) |
||
Line 7: | Line 7: | ||
* Setting of the background colour |
* Setting of the background colour |
||
* How to cause blinking or flashing (if supported by the terminal) |
* How to cause blinking or flashing (if supported by the terminal) |
||
=={{header|Tcl}}== |
|||
This only works on Unix terminals as it delegates to the system <tt>tput</tt> 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> |
|||
== {{header|UNIX Shell}} == |
== {{header|UNIX Shell}} == |
Revision as of 12:28, 20 September 2011
The task is to display a 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)
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>