Terminal control/Inverse video: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Pascal (ncurses) example)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(43 intermediate revisions by 23 users not shown)
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 (or reverse video) followed by a word in normal video.

;Task:
Display a word in inverse video   (or reverse video)   followed by a word in normal video.
<br><br>

=={{header|11l}}==
{{trans|Python}}

<syntaxhighlight lang="11l">print("\033[7mReversed\033[m Normal")</syntaxhighlight>


=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
Line 13: Line 22:
SYS680
SYS680
</pre>
</pre>
<lang 6502asm>; C64 - Terminal control: Inverse Video
<syntaxhighlight lang="6502asm">; C64 - Terminal control: Inverse Video


; *** labels ***
; *** labels ***
Line 35: Line 44:
.byte $92 ; the REVERSE OFF control code
.byte $92 ; the REVERSE OFF control code
.null " normal" ; null terminated string
.null " normal" ; null terminated string
</syntaxhighlight>
</lang>

=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintInv(CHAR ARRAY a)
BYTE i

IF a(0)>0 THEN
FOR i=1 TO a(0)
DO
Put(a(i)%$80)
OD
FI
RETURN

PROC Main()
Position(2,2)

PrintInv("Inverse")
Print(" video")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Inverse_video.png Screenshot from Atari 8-bit computer]


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Reverse_Video is
procedure Reverse_Video is
Line 49: Line 79:
Put (Norm_Video & " Normal");
Put (Norm_Video & " Normal");
end Reverse_Video;
end Reverse_Video;
</syntaxhighlight>
</lang>

=={{header|ARM Assembly}}==
{{works with|https://akkit.org/info/gbatek.htm Game Boy Advance}}
This is a slightly different take on the age-old XOR technique to flip the colors of a monochrome graphic. While the Game Boy Advance uses 16 bits per pixel, we'll use a monochrome bitmap font, where each bit that's a 1 is an instruction to fill in a pixel and each bit that's a 0 is an instruction to leave it blank. By flipping the bits of the font itself, we can create the "inverse video" effect. The ARM's <code>EOR</code> instruction can't be used to write to memory directly; you would have to load from memory into a register first, apply the <code>EOR</code> operation with the desired value, and write back. Our method of flipping the bits of the font will save time.

The Game Boy Advance's video memory is very simple, a two-dimensional array of 16-bit values ranging from memory locations <code>0x06000000</code> to <code>0x06012BFF</code> represents each pixel of the screen. Write a 15-bit hex color value to an element of the array to turn the corresponding pixel to that color value.

<syntaxhighlight lang="arm assembly">;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Program Start

.equ ramarea, 0x02000000
.equ CursorX,ramarea
.equ CursorY,ramarea+1

ProgramStart:
mov sp,#0x03000000 ;Init Stack Pointer
mov r4,#0x04000000 ;DISPCNT -LCD Control
mov r2,#0x403 ;4= Layer 2 on / 3= ScreenMode 3
str r2,[r4]
bl ResetTextCursors ;set text cursors to top left of screen
adr r1,HelloWorld
mov r2,#0x7FFF
mov r11,#1
bl PrintString
adr r1,HelloWorld
mov r2,#0x7FFF
mov r11,#0
bl PrintString

forever:
b forever
BitmapFont:
.include "M:\SrcAll\BitmapFont.asm"
HelloWorld:
.byte "HELLO",255
.align 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintString: ;Print 255 terminated string
STMFD sp!,{r0-r12, lr}
PrintStringAgain:
ldrB r0,[r1],#1
cmp r0,#255
beq PrintStringDone ;Repeat until 255
bl printchar ;Print Char
b PrintStringAgain
PrintStringDone:
LDMFD sp!,{r0-r12, lr}
bx lr
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintChar:
;input: R1 = ADDR OF TEXT
; R2 = DESIRED COLOR (ABBBBBGGGGGRRRRR A=Alpha)
; CursorX = X POS OF WHERE TO DRAW
; CursorY = Y POS OF WHERE TO DRAW
; R11 = 1 FOR INVERTED TEXT, 0 FOR NORMAL TEXT

STMFD sp!,{r4-r12, lr}

mov r4,#0
mov r5,#0
mov r3,#CursorX
ldrB r4,[r3] ;X pos
mov r3,#CursorY
ldrB r5,[r3] ;Y pos
mov r3,#0x06000000 ;VRAM base
mov r6,r4,lsl #4 ;Xpos, 2 bytes per pixel, 8 bytes per char
add r3,r3,r6
;Ypos, 240 pixels per line,2 bytes per pixel, 8 lines per char
mov r4,r5,lsl #4
mov r5,r5,lsl #8
sub r6,r5,r4
mov r6,r6,lsl #4 ;ypos * 240 * 8 * 2 = ((((ypos << 8)-(ypos << 4)) << 3)<< 1
add r3,r3,r6
adr r4,BitmapFont ;Font source
subs r0,r0,#32 ;First Char is 32 (space)
beq LineDone ;if it's a space, just move the cursor without actually writing anything
add r4,r4,r0,asl #3 ;8 bytes per char
mov r6,#8 ;8 lines
DrawLine:
mov r7,#8 ;8 pixels per line
ldrb r8,[r4],#1 ;Load this piece of the letter
cmp r11,#1 ;does r11 = 1?
mvneq r8,r8 ;if so, flip the bits of r8 before printing.
mov r9,#0b100000000 ;Bit Mask for testing whether to fill
DrawPixel:
tst r8,r9 ;Is bit 1?
strneh r2,[r3] ;Yes? then fill pixel (HalfWord)
add r3,r3,#2
mov r9,r9,ror #1 ;Bitshift Mask
subs r7,r7,#1
bne DrawPixel ;Next Hpixel
add r3,r3,#480-16 ;Move Down a line (240 pixels * 2 bytes)
subs r6,r6,#1 ;-1 char (16 px)
bne DrawLine ;Next Vline
LineDone:
mov r3,#CursorX
ldrB r0,[r3]
add r0,r0,#1 ;Move across screen
strB r0,[r3]
mov r10,#30
cmp r0,r10
bleq NewLine
LDMFD sp!,{r4-r12, lr}
bx lr
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NewLine:
STMFD sp!,{r0-r12, lr}
mov r3,#CursorX
mov r0,#0
strB r0,[r3]
mov r4,#CursorY
ldrB r0,[r4]
add r0,r0,#1
strB r0,[r4]
LDMFD sp!,{r0-r12, pc}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ResetTextCursors:
STMFD sp!,{r4-r6,lr}
mov r4,#0
mov r5,#CursorX
mov r6,#CursorY
strB r4,[r5]
strB r4,[r6]
LDMFD sp!,{r4-r6,lr}
bx lr</syntaxhighlight>

{{out}}
[https://ibb.co/5ByNN9K Picture of output text]


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Call SetConsoleTextAttribute() to change foreground and background colors.
Call SetConsoleTextAttribute() to change foreground and background colors.
<lang AHK>DllCall( "AllocConsole" ) ; create a console if not launched from one
<syntaxhighlight lang="ahk">DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )


Line 66: Line 242:
SetConsoleTextAttribute(hConsole, Attributes){
SetConsoleTextAttribute(hConsole, Attributes){
return DllCall( "SetConsoleTextAttribute", UPtr, hConsole, UShort, Attributes)
return DllCall( "SetConsoleTextAttribute", UPtr, hConsole, UShort, Attributes)
}</lang>
}</syntaxhighlight>

=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
system ("tput rev")
system ("tput rev")
print "foo"
print "foo"
system ("tput sgr0")
system ("tput sgr0")
print "bar"
print "bar"
}</lang>
}</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
A delay is added because the screen redraws with the normal font after the program exits.
A delay is added because the screen redraws with the normal font after the program exits.


<lang axe>Fix 3
<syntaxhighlight lang="axe">Fix 3
Disp "INVERTED"
Disp "INVERTED"
Fix 2
Fix 2
Disp "REGULAR",i
Disp "REGULAR",i
Pause 4500</lang>
Pause 4500</syntaxhighlight>

=={{header|BaCon}}==
<syntaxhighlight lang="freebasic">COLOR INVERSE
PRINT "a word"
COLOR RESET
PRINT "a word"</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==


==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang ApplesoftBasic>INVERSE:?"ROSETTA";:NORMAL:?" CODE"</lang>
<syntaxhighlight lang="applesoftbasic">INVERSE:?"ROSETTA";:NORMAL:?" CODE"</syntaxhighlight>


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
<lang bbcbasic> COLOUR 128 : REM Black background
<syntaxhighlight lang="bbcbasic"> COLOUR 128 : REM Black background
COLOUR 15 : REM White foreground
COLOUR 15 : REM White foreground
PRINT "Inverse";
PRINT "Inverse";
COLOUR 128+15 : REM White background
COLOUR 128+15 : REM White background
COLOUR 0 : REM Black foreground
COLOUR 0 : REM Black foreground
PRINT " video"</lang>
PRINT " video"</syntaxhighlight>
Alternative method using 'VDU code' strings:
Alternative method using 'VDU code' strings:
<lang bbcbasic> inverse$ = CHR$(17)+CHR$(128)+CHR$(17)+CHR$(15)
<syntaxhighlight lang="bbcbasic"> inverse$ = CHR$(17)+CHR$(128)+CHR$(17)+CHR$(15)
normal$ = CHR$(17)+CHR$(128+15)+CHR$(17)+CHR$(0)
normal$ = CHR$(17)+CHR$(128+15)+CHR$(17)+CHR$(0)
PRINT inverse$ + "Inverse" + normal$ + " video"</lang>
PRINT inverse$ + "Inverse" + normal$ + " video"</syntaxhighlight>

==={{header|Commodore BASIC}}===

Commodore computers have defined a "reverse" character set in character ROM. This can be accessed through control characters reserved in the ASCII (PETSCII) character table. To enable reverse characters, print CHR$(18) ("Reverse On"). Reverse characters will continue until a "Reverse Off" CHR$(146) is printed, or until a newline (carriage return CHR$(13)) which may also occur at the end of a print statement.

<syntaxhighlight lang="gwbasic">5 rem inverse video
10 print chr$(18);"reverse on";chr$(146);" reverse off"
20 print
25 rem newline (cr) also terminates reverse mode
30 print chr$(18);"this is reversed... ";:print "so is this."
40 print
50 print chr$(18);"this is reversed... ":print "this is not."
60 print
70 print chr$(18);"this is reversed... ";chr$(13);"this is not."</syntaxhighlight>


==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Color 0, 15 ' usa los colores blanco (fondo) y negro (primer plano)
Locate 2, 2 : Print "Video inverso"
Color 15, 0 ' usa los colores negro (fondo) y blanco (primer plano)
Locate 3, 2 : Print "Video normal"
Sleep</syntaxhighlight>



==={{header|Locomotive Basic}}===
==={{header|Locomotive Basic}}===
The firmware routine at &bb9c (TXT INVERSE) swaps the current Locomotive BASIC PEN and PAPER colors:
The firmware routine at &bb9c (TXT INVERSE) swaps the current Locomotive BASIC PEN and PAPER colors:


<lang locobasic>10 CALL &bb9c:PRINT "inverse";
<syntaxhighlight lang="locobasic">10 CALL &bb9c:PRINT "inverse";
20 CALL &bb9c:PRINT "normal"</lang>
20 CALL &bb9c:PRINT "normal"</syntaxhighlight>


==={{header|PureBasic}}===
==={{header|PureBasic}}===
<lang PureBasic>If OpenConsole()
<syntaxhighlight lang="purebasic">If OpenConsole()
ConsoleColor(0, 15) ;use the colors black (background) and white (forground)
ConsoleColor(0, 15) ;use the colors black (background) and white (forground)
PrintN("Inverse Video")
PrintN("Inverse Video")
Line 116: Line 322:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>


==={{header|Run BASIC}}===
==={{header|Run BASIC}}===
<lang runbasic>' ---------- foo is reverse --------------
<syntaxhighlight lang="runbasic">' ---------- foo is reverse --------------
x$ = shell$("tput mr
x$ = shell$("tput mr
echo 'foo'")
echo 'foo'")
Line 126: Line 332:
x$ = shell$("tput me
x$ = shell$("tput me
echo 'bar'")
echo 'bar'")
wait</lang>
wait</syntaxhighlight>

==={{header|Sinclair ZX81 BASIC}}===
Inverse video is available from the keyboard (accessed with <code>SHIFT</code><code>9</code>), so the normal way to do this would be just
<syntaxhighlight lang="basic">PRINT "FOOBAR"</syntaxhighlight>
but with the 'foo' in inverse video and the 'bar' in normal video.

If this won't work (say, if we may want to use inverse video with string variables rather than string literals), we can use a small subroutine—relying on the fact that the ZX81 character set uses the high bit of each character code to select normal or inverse video.
<syntaxhighlight lang="basic">10 LET S$="FOO"
20 GOSUB 50
30 PRINT S$;"BAR"
40 STOP
50 FOR I=1 TO LEN S$
60 LET S$(I)=CHR$ (128+CODE S$(I))
70 NEXT I
80 RETURN</syntaxhighlight>
Note that this subroutine assumes the source string is not already in inverse video: if it could be, you will need to test each character before you attempt to convert it.


==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">print color("black","white") "Video inverso"
// o también
print reverse "Video inverso"

print color("white","black") "Video normal"</syntaxhighlight>


=== {{header|ZX Spectrum Basic}} ===
==={{header|ZX Spectrum Basic}}===
<lang basic>10 INVERSE 1
<syntaxhighlight lang="basic">10 INVERSE 1
20 PRINT "FOO";
20 PRINT "FOO";
30 INVERSE 0
30 INVERSE 0
40 PRINT "BAR"</lang>
40 PRINT "BAR"</syntaxhighlight>


=={{header|Befunge}}==
=={{header|Befunge}}==
Assuming a terminal with support for ANSI escape sequences.
Assuming a terminal with support for ANSI escape sequences.
<lang befunge>0"lamroNm["39*"esrevnIm7["39*>:#,_$@</lang>
<syntaxhighlight lang="befunge">0"lamroNm["39*"esrevnIm7["39*>:#,_$@</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<lang C>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int main()
int main()
Line 146: Line 376:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. terminal-reverse-video.
PROGRAM-ID. terminal-reverse-video.


Line 157: Line 387:


GOBACK
GOBACK
.</lang>
.</syntaxhighlight>

=={{header|Common Lisp}}==
==={{header|ncurses}}===
To interface the ncurses C library from Lisp, the ''croatoan'' library is used.
<syntaxhighlight lang="lisp">(defun reverse-attribute ()
(with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
(add-string scr "Reverse" :attributes '(:reverse))
(add-string scr " Normal" :attributes '())
(refresh scr)
(get-char scr)))</syntaxhighlight>

=={{header|Forth}}==
Developed with Gforth 0.7.9
<syntaxhighlight lang="forth">: Reverse #27 emit "[7m" type ;
: Normal #27 emit "[m" type ;

: test cr Reverse ." Reverse " cr Normal ." Normal " ;
test
</syntaxhighlight>


=={{header|FunL}}==
=={{header|FunL}}==
<lang funl>import console.*
<syntaxhighlight lang="funl">import console.*


println( "${REVERSED}This is reversed.$RESET This is normal." )</lang>
println( "${REVERSED}This is reversed.$RESET This is normal." )</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
===External command===
===External command===
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 185: Line 434:
cmd.Stdout = os.Stdout
cmd.Stdout = os.Stdout
return cmd.Run()
return cmd.Run()
}</lang>
}</syntaxhighlight>
===ANSI escape codes===
===ANSI escape codes===
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 193: Line 442:
func main() {
func main() {
fmt.Println("\033[7mRosetta\033[m Code")
fmt.Println("\033[7mRosetta\033[m Code")
}</lang>
}</syntaxhighlight>
===Ncurses===
===Ncurses===
{{libheader|Curses}}
{{libheader|Curses}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 215: Line 464:
s.Println(" Code")
s.Println(" Code")
s.GetChar()
s.GetChar()
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Use the definitions given in [[Terminal_control/Coloured_text#J]]
Use the definitions given in [[Terminal_control/Coloured_text#J]]
<syntaxhighlight lang="j">
<lang J>
;:';:,#.*."3,(C.A.)/\/&.:;:' NB. some output beforehand
;:';:,#.*."3,(C.A.)/\/&.:;:' NB. some output beforehand
attributes REVERSEVIDEO NB. does as it says
attributes REVERSEVIDEO NB. does as it says
Line 225: Line 474:
attributes OFF NB. no more blinky flashy
attributes OFF NB. no more blinky flashy
parseFrench=:;:,#.*."3,(C.A.)/\/&.:;: NB. just kidding! More output.
parseFrench=:;:,#.*."3,(C.A.)/\/&.:;: NB. just kidding! More output.
</syntaxhighlight>
</lang>

=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''

'''Invocation:'''
<pre>
jq -nr -f terminal-control-inverse-video.jq
</pre>
<syntaxhighlight lang=jq>
# Be busy for at least the given number of seconds,
# and emit the actual number of seconds that have elapsed.
# The reason for defining sleep/1 is that it allows the idiom:
# E | F, (sleep(1) as $elapsed | CONTINUE_WITH_E_AS_INPUT)
def sleep($seconds):
now
| . as $now
| until( . - $now >= $seconds; now)
| . - $now ;

def demo:
def ESC: "\u001B";
"\(ESC)[7mInverse",
(sleep(2) | "\(ESC)[0mNormal");

demo
</syntaxhighlight>

=={{header|Julia}}==
Use within the Julia REPL command line.
<syntaxhighlight lang="julia">using Crayons.Box

println(WHITE_FG, BLACK_BG, "Normal")
println(WHITE_BG, BLACK_FG, "Reversed")
println(WHITE_FG, BLACK_BG, "Normal")
</syntaxhighlight>

=={{header|Kotlin}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">// version 1.1.2

fun main(args: Array<String>) {
println("\u001B[7mInverse\u001B[m Normal")
}</syntaxhighlight>


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>local(esc = decode_base64('Gw=='))
<syntaxhighlight lang="lasso">local(esc = decode_base64('Gw=='))


stdout( #esc + '[7m Reversed Video ' + #esc + '[0m Normal Video ')</lang>
stdout( #esc + '[7m Reversed Video ' + #esc + '[0m Normal Video ')</syntaxhighlight>


=={{header|Mathematica}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Run["tput mr"]
<syntaxhighlight lang="mathematica">Run["tput mr"]
Run["echo foo"] (* is displayed in reverse mode *)
Run["echo foo"] (* is displayed in reverse mode *)
Run["tput me"]
Run["tput me"]
Run["echo bar"]</lang>
Run["echo bar"]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang="nim">import terminal
<lang nim>echo "\e[7mReversed\e[m Normal"</lang>

stdout.styledWrite("normal ", styleReverse, "inverse", resetStyle, " normal\n")</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 245: Line 540:
Using the library [http://forge.ocamlcore.org/projects/ansiterminal/ ANSITerminal] in the interactive loop:
Using the library [http://forge.ocamlcore.org/projects/ansiterminal/ ANSITerminal] in the interactive loop:


<lang ocaml>$ ocaml unix.cma -I +ANSITerminal ANSITerminal.cma
<syntaxhighlight lang="ocaml">$ ocaml unix.cma -I +ANSITerminal ANSITerminal.cma


# open ANSITerminal ;;
# open ANSITerminal ;;
# print_string [Inverse] "Hello\n" ;;
# print_string [Inverse] "Hello\n" ;;
Hello
Hello
- : unit = ()</lang>
- : unit = ()</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 256: Line 551:
{{libheader|Curses}}
{{libheader|Curses}}
Using Free Pascal and ncurses. On some systems linking to the libtinfo library may be necessary.
Using Free Pascal and ncurses. On some systems linking to the libtinfo library may be necessary.
<lang pascal>program InverseVideo;
<syntaxhighlight lang="pascal">program InverseVideo;
{$LINKLIB tinfo}
{$LINKLIB tinfo}
uses
uses
Line 270: Line 565:
endwin;
endwin;
end.
end.
</syntaxhighlight>
</lang>

=={{header|Perl}}==
Like Raku.
<syntaxhighlight lang="perl">print "normal\n";
system "tput rev";
print "reversed\n";
system "tput sgr0";
print "normal\n";</syntaxhighlight>

=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Inverse_Video.exw
-- ================================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">text_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">bk_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">WHITE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Inverse"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">text_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">WHITE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">bk_color</span><span style="color: #0000FF;">(</span><span style="color: #004600;">BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" Video"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\npress enter to exit"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->


=={{header|Perl 6}}==
<lang perl6>say "normal";
run "tput", "rev";
say "reversed";
run "tput", "sgr0";
say "normal";</lang>
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(prin "abc")
<syntaxhighlight lang="picolisp">(prin "abc")
(call "tput" "rev")
(call "tput" "rev")
(prin "def") # These three chars are displayed in reverse video
(prin "def") # These three chars are displayed in reverse video
(call "tput" "sgr0")
(call "tput" "sgr0")
(prinl "ghi")</lang>
(prinl "ghi")</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang Python>#!/usr/bin/env python
<syntaxhighlight lang="python">#!/usr/bin/env python


print "\033[7mReversed\033[m Normal"</lang>
print "\033[7mReversed\033[m Normal"</syntaxhighlight>

=={{header|Quackery}}==

<syntaxhighlight lang="quackery"> [ $ 'print("\033[7m", end="")' python ] is inversetext ( --> )

[ $ 'print("\033[m", end="")' python ] is regulartext ( --> )

inversetext say "inverse video"
regulartext say " normal text"</syntaxhighlight>




=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require (planet neil/charterm:3:0))
(require (planet neil/charterm:3:0))
Line 303: Line 626:
(charterm-normal)
(charterm-normal)
(charterm-display "World"))
(charterm-display "World"))
</syntaxhighlight>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>say "normal";
run "tput", "rev";
say "reversed";
run "tput", "sgr0";
say "normal";</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
This version only works with PC/REXX.
This version only works with PC/REXX and Personal Rexx.
<lang rexx>/*REXX program to demonstrate reverse video. */
<syntaxhighlight lang="rexx">/*REXX program demonstrates the showing of reverse video to the display terminal. */
@day = 'day'
@day = 'day'
@night = 'night'
@night = 'night'

call scrwrite , 1, @day, , , 7 /*white on black.*/
call scrwrite , 1+length(@day), @night, , , 112 /*black on white.*/
call scrwrite , 1, @day, , , 7 /*display to terminal: white on black.*/
/*stick a fork in it, we're done.*/</lang>
call scrwrite , 1+length(@day), @night, , , 112 /* " " " black " white.*/

exit 0 /*stick a fork in it, we're all done. */</syntaxhighlight><br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
nverse = char(17)+char(128)+char(17)+char(15)
nverse = char(17)+char(128)+char(17)+char(15)
normal = char(17)+char(128+15)+char(17)+char(0)
normal = char(17)+char(128+15)+char(17)+char(0)
see inverse + " inverse " + normal + " video"
see inverse + " inverse " + normal + " video"
</syntaxhighlight>
</lang>

=={{header|Ruby}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="ruby">puts "\033[7mReversed\033[m Normal"</syntaxhighlight>

=={{header|Scala}}==
{{Works with|Ubuntu|14.04}}
<syntaxhighlight lang="scala">object Main extends App {
println("\u001B[7mInverse\u001B[m Normal")
}</syntaxhighlight>

=={{header|Standard ML}}==
<syntaxhighlight lang="sml">val () = print "\^[[7mReversed\^[[m Normal\n"</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
This only works on Unix terminals.
This only works on Unix terminals.
<lang tcl># Get how the terminal wants to do things...
<syntaxhighlight lang="tcl"># Get how the terminal wants to do things...
set videoSeq(reverse) [exec tput rev]
set videoSeq(reverse) [exec tput rev]
set videoSeq(normal) [exec tput rmso]
set videoSeq(normal) [exec tput rmso]
Line 336: Line 682:


# Print those words
# Print those words
puts "[reverseVideo $inReverse] $inNormal"</lang>
puts "[reverseVideo $inReverse] $inNormal"</syntaxhighlight>


=={{header|TPP}}==
=={{header|TPP}}==
<lang tpp>--revon
<syntaxhighlight lang="tpp">--revon
This is inverse
This is inverse
--revoff
--revoff
This is normal</lang>
This is normal</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 349: Line 695:
{{works with|Bourne Shell}}
{{works with|Bourne Shell}}


<lang bash>#!/bin/sh
<syntaxhighlight lang="bash">#!/bin/sh
tput mr # foo is reversed
tput mr # foo is reversed
echo 'foo'
echo 'foo'
tput me # bar is normal video
tput me # bar is normal video
echo 'bar'</lang>
echo 'bar'</syntaxhighlight>


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 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.
Line 359: Line 705:
If the terminal cannot do reverse video, then ''tput'' will fail with a message to standard error.
If the terminal cannot do reverse video, then ''tput'' will fail with a message to standard error.


<lang bash>$ TERM=dumb tput mr
<syntaxhighlight lang="bash">$ TERM=dumb tput mr
tput: Unknown terminfo capability `mr'</lang>
tput: Unknown terminfo capability `mr'</syntaxhighlight>


Some programs use the ''standout mode'', which might look exactly like reverse video. (The escape sequences might be identical!)
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
<syntaxhighlight lang="bash">tput so # enter standout mode
echo 'foo'
echo 'foo'
tput se # exit standout mode
tput se # exit standout mode
echo 'bar'</lang>
echo 'bar'</syntaxhighlight>


If the system supports terminfo, then <code>tput smso</code> and <code>tput rmso</code> also work.
If the system supports terminfo, then <code>tput smso</code> and <code>tput rmso</code> also work.


==={{header|C Shell}}===
==={{header|C Shell}}===
<lang csh>tput mr
<syntaxhighlight lang="csh">tput mr
echo 'foo'
echo 'foo'
tput me
tput me
echo 'bar'</lang>
echo 'bar'</syntaxhighlight>

=={{header|Wren}}==
<syntaxhighlight lang="wren">System.print("\e[7mInverse")
System.print("\e[0mNormal")</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
Line 381: Line 731:
it provides many combinations of foreground and background colors.
it provides many combinations of foreground and background colors.


<lang XPL0>include c:\cxpl\codes;
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
[Attrib($70);
[Attrib($70);
Text(6, "Inverse");
Text(6, "Inverse");
Line 387: Line 737:
Text(6, " Video");
Text(6, " Video");
CrLf(6);
CrLf(6);
]</lang>
]</syntaxhighlight>

=={{header|Z80 Assembly}}==
{{trans|Locomotive Basic}}
<syntaxhighlight lang="z80">.org &8000
PrintChar equ &BB5A
InvertTextColors equ &BB9C


;main

call InvertTextColors

ld hl, HelloAddr
call PrintString

call InvertTextColors

ld hl,HelloAddr
jp PrintString ;and return to basic after that.


HelloAddr: byte "Hello",0

PrintString:
ld a,(hl)
or a
ret z
call PrintChar
inc hl
jp PrintString</syntaxhighlight>



=={{header|zkl}}==
=={{header|zkl}}==
There is no explicit support for terminals/video. But, assuming an ANSI terminal:
There is no explicit support for terminals/video. But, assuming an ANSI terminal:
<lang zkl>println("\e[7mReversed\e[m Normal");</lang>
<syntaxhighlight lang="zkl">println("\e[7mReversed\e[m Normal");</syntaxhighlight>


{{omit from|ACL2}}
{{omit from|ACL2}}

Latest revision as of 12:02, 13 February 2024

Task
Terminal control/Inverse video
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Display a word in inverse video   (or reverse video)   followed by a word in normal video.

11l

Translation of: Python
print("\033[7mReversed\033[m Normal")

6502 Assembly

Works with: [VICE]

This example has been written for the C64 and uses the STROUT BASIC routine. Compile with the Turbo Macro Pro cross assembler:

tmpx -i inverse-video.s -o inverse-video.prg

Run with:

SYS680
; C64 - Terminal control: Inverse Video

; *** labels ***

strout          = $ab1e

; *** main ***

                *=$02a8         ; sys 680
                
                lda #<str       ; Address of the message to print - low byte
                ldy #>str       ; Address high byte
                jsr strout      ; Print a null terminated string.
                rts    
                
; *** data ***

str             .byte $12       ; the REVERSE ON control code
                                ; see https://en.wikipedia.org/wiki/PETSCII
                .text "reversed"
                .byte $92       ; the REVERSE OFF control code
                .null " normal" ; null terminated string

Action!

PROC PrintInv(CHAR ARRAY a)
  BYTE i

  IF a(0)>0 THEN
    FOR i=1 TO a(0)
    DO
      Put(a(i)%$80)
    OD
  FI
RETURN

PROC Main()
  Position(2,2)

  PrintInv("Inverse")
  Print(" video")
RETURN
Output:

Screenshot from Atari 8-bit computer

Ada

with Ada.Text_IO; use  Ada.Text_IO;

procedure Reverse_Video is 

   Rev_Video  : String := Ascii.ESC & "[7m";
   Norm_Video : String := Ascii.ESC & "[m";

begin
   Put (Rev_Video & "Reversed");
   Put (Norm_Video & " Normal");
end Reverse_Video;

ARM Assembly

Works with: [Game Boy Advance]

This is a slightly different take on the age-old XOR technique to flip the colors of a monochrome graphic. While the Game Boy Advance uses 16 bits per pixel, we'll use a monochrome bitmap font, where each bit that's a 1 is an instruction to fill in a pixel and each bit that's a 0 is an instruction to leave it blank. By flipping the bits of the font itself, we can create the "inverse video" effect. The ARM's EOR instruction can't be used to write to memory directly; you would have to load from memory into a register first, apply the EOR operation with the desired value, and write back. Our method of flipping the bits of the font will save time.

The Game Boy Advance's video memory is very simple, a two-dimensional array of 16-bit values ranging from memory locations 0x06000000 to 0x06012BFF represents each pixel of the screen. Write a 15-bit hex color value to an element of the array to turn the corresponding pixel to that color value.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
; Program Start

	.equ ramarea, 0x02000000
	.equ CursorX,ramarea		
	.equ CursorY,ramarea+1

	
ProgramStart:
	mov sp,#0x03000000			;Init Stack Pointer
	
	mov r4,#0x04000000  		        ;DISPCNT -LCD Control
	mov r2,#0x403    			;4= Layer 2 on / 3= ScreenMode 3
	str r2,[r4]         	
	bl ResetTextCursors			;set text cursors to top left of screen
		
	adr r1,HelloWorld
	mov r2,#0x7FFF
	mov r11,#1
	bl PrintString
	
	adr r1,HelloWorld
	mov r2,#0x7FFF
	mov r11,#0
	bl PrintString
	

forever:
	b forever
BitmapFont:
        .include "M:\SrcAll\BitmapFont.asm"
	
HelloWorld:
	.byte "HELLO",255
	.align 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintString:					;Print 255 terminated string 
	STMFD sp!,{r0-r12, lr}
PrintStringAgain:
		ldrB r0,[r1],#1
		cmp r0,#255
		beq PrintStringDone		;Repeat until 255
		bl printchar 			;Print Char
		b PrintStringAgain
PrintStringDone:
	LDMFD sp!,{r0-r12, lr}
	bx lr
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintChar:
	;input: R1 = ADDR OF TEXT
	;	R2 = DESIRED COLOR (ABBBBBGGGGGRRRRR   A=Alpha)
	;	CursorX = X POS OF WHERE TO DRAW
	;	CursorY = Y POS OF WHERE TO DRAW
        ;       R11 = 1 FOR INVERTED TEXT, 0 FOR NORMAL TEXT

	STMFD sp!,{r4-r12, lr}

		mov r4,#0
		mov r5,#0
		
		mov r3,#CursorX
		ldrB r4,[r3]			;X pos
		mov r3,#CursorY
		ldrB r5,[r3]			;Y pos
		
		mov r3,#0x06000000 		;VRAM base
		
		mov r6,r4,lsl #4		;Xpos, 2 bytes per pixel, 8 bytes per char
		add r3,r3,r6
		
	;Ypos, 240 pixels per line,2 bytes per pixel, 8 lines per char
		
		mov r4,r5,lsl #4		
		mov r5,r5,lsl #8
		sub r6,r5,r4
		mov r6,r6,lsl #4		;ypos * 240 * 8 * 2 = ((((ypos << 8)-(ypos << 4)) << 3)<< 1
		add r3,r3,r6
		
		adr r4,BitmapFont 		;Font source
		
		subs r0,r0,#32			;First Char is 32 (space)
		beq LineDone			;if it's a space, just move the cursor without actually writing anything
		add r4,r4,r0,asl #3		;8 bytes per char
		
		mov r6,#8			;8 lines 
DrawLine:
		mov r7,#8 			;8 pixels per line
		ldrb r8,[r4],#1			;Load this piece of the letter
		cmp r11,#1			;does r11 = 1?
		mvneq r8,r8			;if so, flip the bits of r8 before printing.
		mov r9,#0b100000000		;Bit Mask for testing whether to fill
				
DrawPixel:
		tst r8,r9			;Is bit 1?
		strneh r2,[r3]			;Yes? then fill pixel (HalfWord)
		add r3,r3,#2
		mov r9,r9,ror #1		;Bitshift Mask
		subs r7,r7,#1
		bne DrawPixel			;Next Hpixel
		
		add r3,r3,#480-16	        ;Move Down a line (240 pixels * 2 bytes) 
		subs r6,r6,#1			;-1 char (16 px)
		bne DrawLine			;Next Vline
		
LineDone:	
		mov r3,#CursorX
		ldrB r0,[r3]	
		add r0,r0,#1			;Move across screen
		strB r0,[r3]	
		mov r10,#30
		cmp r0,r10
		bleq NewLine
	LDMFD sp!,{r4-r12, lr}
	bx lr
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NewLine:
	STMFD sp!,{r0-r12, lr}
		mov r3,#CursorX
		mov r0,#0
		strB r0,[r3]
		mov r4,#CursorY
		ldrB r0,[r4]
		add r0,r0,#1
		strB r0,[r4]
	LDMFD sp!,{r0-r12, pc}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ResetTextCursors:
	STMFD sp!,{r4-r6,lr}
		mov r4,#0
		mov r5,#CursorX
		mov r6,#CursorY
		strB r4,[r5]
		strB r4,[r6]
	LDMFD sp!,{r4-r6,lr}
	bx lr
Output:

Picture of output text

AutoHotkey

Call SetConsoleTextAttribute() to change foreground and background colors.

DllCall( "AllocConsole" ) ; create a console if not launched from one
hConsole := DllCall( "GetStdHandle", int, STDOUT := -11 )

SetConsoleTextAttribute(hConsole, 0x70) ; gray background, black foreground
FileAppend, Reversed`n, CONOUT$ ; print to stdout

SetConsoleTextAttribute(hConsole, 0x07) ; black background, gray foreground
FileAppend, Normal, CONOUT$

MsgBox

SetConsoleTextAttribute(hConsole, Attributes){
	return DllCall( "SetConsoleTextAttribute", UPtr, hConsole, UShort, Attributes)
}

AWK

BEGIN {
system ("tput rev")
print "foo"
system ("tput sgr0")
print "bar"
}

Axe

A delay is added because the screen redraws with the normal font after the program exits.

Fix 3
Disp "INVERTED"
Fix 2
Disp "REGULAR",i
Pause 4500

BaCon

COLOR INVERSE
PRINT "a word"
COLOR RESET
PRINT "a word"

BASIC

Applesoft BASIC

INVERSE:?"ROSETTA";:NORMAL:?" CODE"

BBC BASIC

      COLOUR 128        : REM Black background
      COLOUR 15         : REM White foreground
      PRINT "Inverse";
      COLOUR 128+15     : REM White background
      COLOUR 0          : REM Black foreground
      PRINT " video"

Alternative method using 'VDU code' strings:

      inverse$ = CHR$(17)+CHR$(128)+CHR$(17)+CHR$(15)
      normal$ = CHR$(17)+CHR$(128+15)+CHR$(17)+CHR$(0)
      PRINT inverse$ + "Inverse" + normal$ + " video"

Commodore BASIC

Commodore computers have defined a "reverse" character set in character ROM. This can be accessed through control characters reserved in the ASCII (PETSCII) character table. To enable reverse characters, print CHR$(18) ("Reverse On"). Reverse characters will continue until a "Reverse Off" CHR$(146) is printed, or until a newline (carriage return CHR$(13)) which may also occur at the end of a print statement.

5 rem inverse video
10 print chr$(18);"reverse on";chr$(146);" reverse off"
20 print
25 rem newline (cr) also terminates reverse mode
30 print chr$(18);"this is reversed... ";:print "so is this."
40 print
50 print chr$(18);"this is reversed... ":print "this is not."
60 print 
70 print chr$(18);"this is reversed... ";chr$(13);"this is not."


FreeBASIC

Color 0, 15 ' usa los colores blanco (fondo) y negro (primer plano)
Locate 2, 2 : Print "Video inverso"
Color 15, 0 ' usa los colores negro (fondo) y blanco (primer plano)
Locate 3, 2 : Print "Video normal"
Sleep


Locomotive Basic

The firmware routine at &bb9c (TXT INVERSE) swaps the current Locomotive BASIC PEN and PAPER colors:

10 CALL &bb9c:PRINT "inverse";
20 CALL &bb9c:PRINT "normal"

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

Run BASIC

' ---------- foo is reverse --------------
x$ = shell$("tput mr
echo 'foo'")

' ---------- bar is normal --------------
x$ = shell$("tput me     
echo 'bar'")
wait

Sinclair ZX81 BASIC

Inverse video is available from the keyboard (accessed with SHIFT9), so the normal way to do this would be just

PRINT "FOOBAR"

but with the 'foo' in inverse video and the 'bar' in normal video.

If this won't work (say, if we may want to use inverse video with string variables rather than string literals), we can use a small subroutine—relying on the fact that the ZX81 character set uses the high bit of each character code to select normal or inverse video.

10 LET S$="FOO"
20 GOSUB 50
30 PRINT S$;"BAR"
40 STOP
50 FOR I=1 TO LEN S$
60 LET S$(I)=CHR$ (128+CODE S$(I))
70 NEXT I
80 RETURN

Note that this subroutine assumes the source string is not already in inverse video: if it could be, you will need to test each character before you attempt to convert it.


Yabasic

print color("black","white") "Video inverso"
// o también
print reverse "Video inverso"

print color("white","black") "Video normal"

ZX Spectrum Basic

10 INVERSE 1
20 PRINT "FOO";
30 INVERSE 0
40 PRINT "BAR"

Befunge

Assuming a terminal with support for ANSI escape sequences.

0"lamroNm["39*"esrevnIm7["39*>:#,_$@

C

#include <stdio.h>

int main()
{
	printf("\033[7mReversed\033[m Normal\n");

	return 0;
}

COBOL

       IDENTIFICATION DIVISION.
       PROGRAM-ID. terminal-reverse-video.

       PROCEDURE DIVISION.
           DISPLAY "Reverse-Video" WITH REVERSE-VIDEO
           DISPLAY "Normal"

           GOBACK
           .

Common Lisp

ncurses

To interface the ncurses C library from Lisp, the croatoan library is used.

(defun reverse-attribute ()
  (with-screen (scr :input-blocking t :input-echoing nil :cursor-visible nil)
    (add-string scr "Reverse" :attributes '(:reverse))
    (add-string scr " Normal" :attributes '())
    (refresh scr)
    (get-char scr)))

Forth

Developed with Gforth 0.7.9

: Reverse          #27  emit "[7m" type ; 
: Normal           #27 emit "[m" type ; 

: test cr Reverse ."   Reverse " cr  Normal ."  Normal  " ; 
test

FunL

import console.*

println( "${REVERSED}This is reversed.$RESET This is normal." )

Go

External command

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    tput("rev")
    fmt.Print("Rosetta")
    tput("sgr0")
    fmt.Println(" Code")
}

func tput(arg string) error {
    cmd := exec.Command("tput", arg)
    cmd.Stdout = os.Stdout
    return cmd.Run()
}

ANSI escape codes

package main

import "fmt"

func main() {
    fmt.Println("\033[7mRosetta\033[m Code")
}

Ncurses

Library: Curses
package main

import (
    "log"

    gc "code.google.com/p/goncurses"
)

func main() {
    s, err := gc.Init()
    if err != nil {
        log.Fatal("init:", err)
    }
    defer gc.End()
    s.AttrOn(gc.A_REVERSE)
    s.Print("Rosetta")
    s.AttrOff(gc.A_REVERSE)
    s.Println(" Code")
    s.GetChar()
}

J

Use the definitions given in Terminal_control/Coloured_text#J

   ;:';:,#.*."3,(C.A.)/\/&.:;:' NB. some output beforehand
   attributes REVERSEVIDEO      NB. does as it says
   2 o.^:a:0                    NB. solve the fixed point equation cos(x) == x
   attributes OFF               NB. no more blinky flashy
   parseFrench=:;:,#.*."3,(C.A.)/\/&.:;:  NB. just kidding!  More output.

jq

Works with: jq

Also works with gojq, the Go implementation of jq, and with fq.

Invocation:

 jq -nr -f terminal-control-inverse-video.jq
# Be busy for at least the given number of seconds,
# and emit the actual number of seconds that have elapsed.
# The reason for defining sleep/1 is that it allows the idiom:
#  E | F, (sleep(1) as $elapsed | CONTINUE_WITH_E_AS_INPUT)
def sleep($seconds):
  now
  | . as $now
  | until( .  - $now >= $seconds; now)
  | . - $now ;

def demo:
  def ESC: "\u001B";
  "\(ESC)[7mInverse",
  (sleep(2) | "\(ESC)[0mNormal");

demo

Julia

Use within the Julia REPL command line.

using Crayons.Box

println(WHITE_FG, BLACK_BG, "Normal")
println(WHITE_BG, BLACK_FG, "Reversed")
println(WHITE_FG, BLACK_BG, "Normal")

Kotlin

Works with: Ubuntu version 14.04
// version 1.1.2

fun main(args: Array<String>) {
    println("\u001B[7mInverse\u001B[m Normal")
}

Lasso

local(esc = decode_base64('Gw=='))

stdout( #esc + '[7m Reversed Video ' + #esc + '[0m Normal Video ')

Mathematica/Wolfram Language

Run["tput mr"]
Run["echo foo"] (* is displayed in reverse mode *)
Run["tput me"]
Run["echo bar"]

Nim

import terminal

stdout.styledWrite("normal ", styleReverse, "inverse", resetStyle, " normal\n")

OCaml

Using the library ANSITerminal in the interactive loop:

$ ocaml unix.cma -I +ANSITerminal ANSITerminal.cma

# open ANSITerminal ;;
# print_string [Inverse] "Hello\n" ;;
Hello
- : unit = ()

Pascal

Works with: Free_Pascal
Library: Curses

Using Free Pascal and ncurses. On some systems linking to the libtinfo library may be necessary.

program InverseVideo;
{$LINKLIB tinfo}
uses
  ncurses;
begin
  initscr;
  attron(A_REVERSE);
  printw('reversed');
  attroff(A_REVERSE);
  printw(' normal');
  refresh;
  getch;
  endwin;
end.

Perl

Like Raku.

print "normal\n";
system "tput rev";
print "reversed\n";
system "tput sgr0";
print "normal\n";

Phix

--
-- demo\rosetta\Inverse_Video.exw
-- ================================
--
with javascript_semantics
text_color(BLACK)
bk_color(WHITE)
printf(1,"Inverse")
text_color(WHITE)
bk_color(BLACK)
printf(1," Video")
printf(1,"\n\npress enter to exit")
{} = wait_key()

PicoLisp

(prin "abc")
(call "tput" "rev")
(prin "def")  # These three chars are displayed in reverse video
(call "tput" "sgr0")
(prinl "ghi")

Python

#!/usr/bin/env python

print "\033[7mReversed\033[m Normal"

Quackery

  [ $ 'print("\033[7m", end="")' python ] is inversetext ( --> )

  [ $ 'print("\033[m", end="")' python ]  is regulartext ( --> )

  inversetext say "inverse video" 
  regulartext say " normal text"


Racket

#lang racket
(require (planet neil/charterm:3:0))
 
(with-charterm
 (charterm-clear-screen)
 (charterm-cursor 0 0)
 (charterm-inverse)
 (charterm-display "Hello")
 (charterm-normal)
 (charterm-display "World"))

Raku

(formerly Perl 6)

say "normal";
run "tput", "rev";
say "reversed";
run "tput", "sgr0";
say "normal";

REXX

This version only works with PC/REXX and Personal Rexx.

/*REXX program demonstrates the showing of  reverse  video  to the display terminal.    */
@day   = 'day'
@night = 'night'

call scrwrite , 1, @day, , , 7                   /*display to terminal:  white on black.*/
call scrwrite , 1+length(@day), @night, , , 112  /*   "     "     "      black  " white.*/

exit 0                                           /*stick a fork in it,  we're all done. */



Ring

nverse = char(17)+char(128)+char(17)+char(15)
normal = char(17)+char(128+15)+char(17)+char(0)
see  inverse + " inverse " + normal + " video"

Ruby

Works with: Ubuntu version 14.04
puts "\033[7mReversed\033[m Normal"

Scala

Works with: Ubuntu version 14.04
object Main extends App {
    println("\u001B[7mInverse\u001B[m Normal")
}

Standard ML

val () = print "\^[[7mReversed\^[[m Normal\n"

Tcl

This only works on Unix terminals.

# 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"

TPP

--revon
This is inverse
--revoff
This is normal

UNIX Shell

Use the tput(1) utility to write the escape sequences that enable or disable reverse video.

Works with: Bourne Shell
#!/bin/sh
tput mr     # foo is reversed
echo 'foo'
tput me     # bar is normal video
echo 'bar'

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.

$ TERM=dumb tput mr
tput: Unknown terminfo capability `mr'

Some programs use the standout mode, which might look exactly like reverse video. (The escape sequences might be identical!)

tput so     # enter standout mode
echo 'foo'
tput se     # exit standout mode
echo 'bar'

If the system supports terminfo, then tput smso and tput rmso also work.

C Shell

tput mr
echo 'foo'
tput me
echo 'bar'

Wren

System.print("\e[7mInverse")
System.print("\e[0mNormal")

XPL0

Output device 6 is similar to the normal console screen (device 0), but it provides many combinations of foreground and background colors.

include c:\cxpl\codes;
[Attrib($70);
Text(6, "Inverse");
Attrib($07);
Text(6, " Video");
CrLf(6);
]

Z80 Assembly

Translation of: Locomotive Basic
.org &8000
PrintChar equ &BB5A
InvertTextColors equ &BB9C


;main

call InvertTextColors

ld hl, HelloAddr
call PrintString

call InvertTextColors

ld hl,HelloAddr
jp PrintString      ;and return to basic after that.


HelloAddr: byte "Hello",0

PrintString:
ld a,(hl)
or a
ret z
call PrintChar
inc hl
jp PrintString


zkl

There is no explicit support for terminals/video. But, assuming an ANSI terminal:

println("\e[7mReversed\e[m Normal");