Terminal control/Coloured text: Difference between revisions

From Rosetta Code
m (moved Coloured Text/Display to Terminal control/Coloured text: This is specifically a terminal control task.)
(No difference)

Revision as of 07:49, 9 September 2011

Terminal control/Coloured text is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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)

$ cat test.sh

UNIX Shell

<lang sh>#!/bin/sh

  1. 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

  1. Coloured text

tput setaf 1 #red echo "Red" tput setaf 4 #blue echo "Blue" tput setaf 3 # yellow echo "Yellow"

  1. Blinking

tput setab 1 # red background tput setaf 3 # yellow foreground

  1. 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>