Terminal control/Hiding the cursor

From Rosetta Code
Revision as of 01:36, 19 December 2011 by rosettacode>Crazyfirex (Add AutoHotkey example)
Task
Terminal control/Hiding the cursor
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to hide the cursor and show it again.

AutoHotkey

Keep in mind that AHK is not built for the command line, so we must call the WinAPI directly. <lang AHK>DllCall("AllocConsole") ; Create a console if not launched from one hConsole := DllCall("GetStdHandle", UInt, STDOUT := -11)

VarSetCapacity(cci, 8)  ; CONSOLE_CURSOR_INFO structure DllCall("GetConsoleCursorInfo", UPtr, hConsole, UPtr, &cci) NumPut(0, cci, 4) DllCall("SetConsoleCursorInfo", UPtr, hConsole, UPtr, &cci)

FileAppend, Cursor hidden for 3 seconds..., CONOUT$ ; Prints to stdout Sleep 3000

NumPut(1, cci, 4) DllCall("SetConsoleCursorInfo", UPtr, hConsole, UPtr, &cci)

FileAppend, `nCursor shown, CONOUT$ MsgBox</lang>

Locomotive Basic

<lang locobasic>10 CURSOR 0: REM hide cursor 20 FOR l = 1 TO 2000: REM delay 30 NEXT l 40 CURSOR 1: REM show cursor</lang>

PicoLisp

<lang PicoLisp>(call "tput" "civis") # Invisible (wait 1000) (call "tput" "cvvis") # Visible</lang>

PureBasic

<lang PureBasic>#cursorSize = 10 ;use a full sized cursor

If OpenConsole()

 Print("Press any key to toggle cursor: ")
 EnableGraphicalConsole(1)
 height = #cursorSize
 ConsoleCursor(height)
 Repeat
   If Inkey()
     height ! #cursorSize
     ConsoleCursor(height)
   EndIf
 ForEver

EndIf </lang>

Tcl

<lang tcl>proc cursor Template:State "normal" {

   switch -- $state {

"normal" {set op "cnorm"} "invisible" {set op "civis"} "visible" {set op "cvvis"}

   }
   # Should be just: “exec tput $op” but it's not actually supported on my terminal...
   exec sh -c "tput $op || true"

}</lang> Demonstration code: <lang tcl>cursor normal puts "normal cursor" after 3000 cursor invisible puts "invisible cursor" after 3000 cursor visible puts "very visible cursor" after 3000 cursor normal puts "back to normal" after 1000</lang>

UNIX Shell

<lang sh>tput civis # Hide the cursor sleep 5 # Sleep for 5 seconds tput cnorm # Show the cursor</lang>