Terminal control/Inverse video: Difference between revisions
(UNIX Shell: Use the old termcap names. Also show standout mode. Add C Shell.) |
|||
Line 1: | Line 1: | ||
{{task|Terminal control}}[[Category:Terminal control]] |
{{task|Terminal control}}[[Category:Terminal control]] |
||
The task is to display a word in inverse video followed by a word in normal video. |
The task is to display a word in inverse video (or reverse video) followed by a word in normal video. |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 72: | Line 72: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Use the [http://invisible-island.net/ncurses/man/tput.1.html tput(1)] utility to write the escape sequences that enable or disable reverse video. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
echo 'foo' |
|||
⚫ | |||
echo 'bar'</lang> |
|||
If the system supports terminfo, then <code>tput rev</code> and <code>tput sgr0</code> also work. (All recent systems have terminfo, except NetBSD, but [http://blog.netbsd.org/tnf/entry/terminfo_has_replaced_termcap NetBSD 6 will have terminfo].) The shorter names <code>mr</code> and <code>me</code> are the backward-compatible names from termcap. |
|||
If the terminal cannot do reverse video, then ''tput'' will fail with a message to standard error. |
|||
<lang bash>$ TERM=dumb tput mr |
|||
tput: Unknown terminfo capability `mr'</lang> |
|||
Some programs use the ''standout mode'', which might look exactly like reverse video. (The escape sequences might be identical!) |
|||
<lang bash>tput so # enter standout mode |
|||
echo 'foo' |
|||
tput se # exit standout mode |
|||
echo 'bar'</lang> |
|||
If the system supports terminfo, then <code>tput smso</code> and <code>tput rmso</code> also work. |
|||
==={{header|C Shell}}=== |
|||
<lang csh>tput mr |
|||
echo 'foo' |
echo 'foo' |
||
tput me |
|||
⚫ | |||
echo 'bar'</lang> |
echo 'bar'</lang> |
Revision as of 23:15, 21 August 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
The task is to display a word in inverse video (or reverse video) followed by a word in normal video.
AWK
<lang awk>BEGIN { system ("tput rev") print "foo" system ("tput sgr0") print "bar" }</lang>
BASIC
Locomotive Basic
The firmware routine at &bb9c (TXT INVERSE) swaps the current Locomotive BASIC PEN and PAPER colors:
<lang locobasic>10 CALL &bb9c:PRINT "inverse"; 20 CALL &bb9c:PRINT "normal"</lang>
ZX Spectrum Basic
<lang basic>10 INVERSE 1 20 PRINT "FOO"; 30 INVERSE 0 40 PRINT "BAR"</lang>
C
<lang C>#include <stdio.h>
int main() { printf("\033[7mReversed\033[m Normal\n");
return 0; }</lang>
PicoLisp
<lang PicoLisp>(prin "abc") (call "tput" "rev") (prin "def") # These three chars are displayed in reverse video (call "tput" "sgr0") (prinl "ghi")</lang>
PureBasic
<lang PureBasic>If OpenConsole()
ConsoleColor(0, 15) ;use the colors black (background) and white (forground) PrintN("Inverse Video") ConsoleColor(15, 0) ;use the colors white (background) and black (forground) PrintN("Normal Video") Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
EndIf</lang>
Tcl
This only works on Unix terminals. <lang tcl># Get how the terminal wants to do things... set videoSeq(reverse) [exec tput rev] set videoSeq(normal) [exec tput rmso] proc reverseVideo str {
global videoSeq return "$videoSeq(reverse)${str}$videoSeq(normal)"
}
- The things to print
set inReverse "foo" set inNormal "bar"
- Print those words
puts "[reverseVideo $inReverse] $inNormal"</lang>
UNIX Shell
Use the tput(1) utility to write the escape sequences that enable or disable reverse video.
<lang bash>#!/bin/sh tput mr # foo is reversed echo 'foo' tput me # bar is normal video echo 'bar'</lang>
If the system supports terminfo, then tput rev
and tput sgr0
also work. (All recent systems have terminfo, except NetBSD, but NetBSD 6 will have terminfo.) The shorter names mr
and me
are the backward-compatible names from termcap.
If the terminal cannot do reverse video, then tput will fail with a message to standard error.
<lang bash>$ TERM=dumb tput mr tput: Unknown terminfo capability `mr'</lang>
Some programs use the standout mode, which might look exactly like reverse video. (The escape sequences might be identical!)
<lang bash>tput so # enter standout mode echo 'foo' tput se # exit standout mode echo 'bar'</lang>
If the system supports terminfo, then tput smso
and tput rmso
also work.
C Shell
<lang csh>tput mr echo 'foo' tput me echo 'bar'</lang>