Terminal control/Inverse video

Revision as of 22:20, 29 August 2015 by rosettacode>J4 james (Added Befunge example.)

The task is to display a word in inverse video (or reverse video) followed by a word in normal video.

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

Ada

<lang 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; </lang>

AutoHotkey

Call SetConsoleTextAttribute() to change foreground and background colors. <lang AHK>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) }</lang>

AWK

<lang awk>BEGIN { system ("tput rev") print "foo" system ("tput sgr0") print "bar" }</lang>

Axe

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

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

BASIC

Applesoft BASIC

<lang ApplesoftBasic>INVERSE:?"ROSETTA";:NORMAL:?" CODE"</lang>

BBC BASIC

<lang bbcbasic> 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"</lang>

Alternative method using 'VDU code' strings: <lang bbcbasic> inverse$ = CHR$(17)+CHR$(128)+CHR$(17)+CHR$(15)

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

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>

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>

Run BASIC

<lang runbasic>' ---------- foo is reverse -------------- x$ = shell$("tput mr echo 'foo'")

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

ZX Spectrum Basic

<lang basic>10 INVERSE 1 20 PRINT "FOO"; 30 INVERSE 0 40 PRINT "BAR"</lang>

Befunge

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

C

<lang C>#include <stdio.h>

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

return 0; }</lang>

COBOL

<lang cobol> IDENTIFICATION DIVISION.

      PROGRAM-ID. terminal-reverse-video.
      PROCEDURE DIVISION.
          DISPLAY "Reverse-Video" WITH REVERSE-VIDEO
          DISPLAY "Normal"
          GOBACK
          .</lang>

FunL

<lang funl>import console.*

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

Go

External command

<lang go>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()

}</lang>

ANSI escape codes

<lang go>package main

import "fmt"

func main() {

   fmt.Println("\033[7mRosetta\033[m Code")

}</lang>

Ncurses

Library: Curses

<lang go>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()

}</lang>

J

Use the definitions given in Terminal_control/Coloured_text#J <lang 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.

</lang>

Lasso

<lang Lasso>local(esc = decode_base64('Gw=='))

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

Mathematica

<lang Mathematica>Run["tput mr"] Run["echo foo"] (* is displayed in reverse mode *) Run["tput me"] Run["echo bar"]</lang>

Nim

<lang nim>echo "\e[7mReversed\e[m Normal"</lang>

OCaml

Using the library ANSITerminal in the interactive loop:

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

  1. open ANSITerminal ;;
  2. print_string [Inverse] "Hello\n" ;;

Hello - : unit = ()</lang>

Perl 6

<lang perl6>say "normal"; run "tput", "rev"; say "reversed"; run "tput", "sgr0"; say "normal";</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>

Python

<lang Python>#!/usr/bin/env python

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


Racket

<lang racket>

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

</lang>

REXX

This version only works with PC/REXX. <lang rexx>/*REXX program to demonstrate reverse video. */ @day = 'day' @night = 'night' call scrwrite , 1, @day, , , 7 /*white on black.*/ call scrwrite , 1+length(@day), @night, , , 112 /*black on white.*/

                                      /*stick a fork in it, we're done.*/</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)"

}

  1. The things to print

set inReverse "foo" set inNormal "bar"

  1. Print those words

puts "[reverseVideo $inReverse] $inNormal"</lang>

TPP

<lang tpp>--revon This is inverse --revoff This is normal</lang>

UNIX Shell

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

Works with: Bourne Shell

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

XPL0

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

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

zkl

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