Terminal control/Hiding the cursor: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎Ncurses: add libheader)
(added FunL)
Line 77: Line 77:
System.Threading.Thread.Sleep(5000);
System.Threading.Thread.Sleep(5000);
}</lang>
}</lang>

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

hide()
sleep( 2 Second )
show()</lang>


=={{header|Go}}==
=={{header|Go}}==

Revision as of 21:20, 15 July 2014

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>

BASIC

Works with: QBasic

<lang qbasic>'hide the cursor: LOCATE , , 0 'wait for a keypress... SLEEP 'show the cursor: LOCATE , , 1</lang>

Applesoft BASIC

<lang ApplesoftBasic>WAIT 49152,128</lang>

BBC BASIC

<lang bbcbasic> OFF : REM Hide the cursor (caret)

     WAIT 400
     ON  : REM Show the cursor again</lang>

C

<lang c> /*30th August,2012 Abhishek Ghosh

Please note that curs_set is terminal dependent.*/

  1. include<curses.h>
  2. include<stdio.h>

int main () {

 printf
   ("At the end of this line you will see the cursor, process will sleep for 5 seconds.");
 napms (5000);
 curs_set (0);
 printf
   ("\nAt the end of this line you will NOT see the cursor, process will again sleep for 5 seconds.");
 napms (5000);
 printf ("\nGoodbye.");
 return 0;

} </lang>

C#

Works with: Mono version 1.2
Works with: Visual C# version 2003

<lang csharp>static void Main(string[] args) {

   Console.Write("At the end of this line you will see the cursor, process will sleep for 5 seconds.");
   System.Threading.Thread.Sleep(5000);
   Console.CursorVisible = false;
   Console.WriteLine();
   Console.Write("At the end of this line you will not see the cursor, process will sleep for 5 seconds.");
   System.Threading.Thread.Sleep(5000);

}</lang>

FunL

<lang funl>import time.* import console.*

hide() sleep( 2 Second ) show()</lang>

Go

External command

<lang go>package main

import (

   "os"
   "os/exec"
   "time"

)

func main() {

   tput("civis") // hide
   time.Sleep(3 * time.Second)
   tput("cvvis") // show
   time.Sleep(3 * time.Second)

}

func tput(arg string) error {

   cmd := exec.Command("tput", arg)
   cmd.Stdout = os.Stdout
   return cmd.Run()

}</lang>

Escape code

(Not sure if this is ANSI, but it worked for me.) <lang go>package main

import (

   "fmt"
   "time"

)

func main() {

   fmt.Print("\033[?25l")
   time.Sleep(3 * time.Second)
   fmt.Print("\033[?25h")
   time.Sleep(3 * time.Second)

}</lang>

Ncurses

Library: Curses

<lang go>package main

import (

   "log"
   "time"
   gc "code.google.com/p/goncurses"

)

func main() {

   s, err := gc.Init()
   if err != nil {
       log.Fatal("init:", err)
   }
   defer gc.End()
   gc.Cursor(0)
   time.Sleep(3 * time.Second)
   gc.Cursor(1)
   s.GetChar()

}</lang>

J

With the definitions of Terminal_control/Coloured_text#J <lang J>smoutput HIDECURSOR usleep(4e6) NB. wait 4 seconds smoutput SHOWCURSOR </lang>

Lasso

<lang Lasso>#!/usr/bin/lasso9

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

// hide the cursor stdout(#esc + '[?25l')

// wait for 4 seconds to give time discover the cursor is gone sleep(4000)

// show the cursor stdout(#esc + '[?25h')

// wait for 4 seconds to give time discover the cursor is back sleep(4000)</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>

Mathematica

<lang Mathematica>Run["tput civis"] (* Cursor hidden *) Pause[2] Run["tput cvvis"] (* Cursor Visible *)</lang>

Nemerle

<lang Nemerle>using System.Console; using System.Threading.Thread;

module CursorVisibility {

   Main() : void
   {
       repeat(3) {
           CursorVisible = !CursorVisible;
           Sleep(5000);
       }
   }

}</lang>

Perl 6

<lang perl6>run 'tput', 'civis'; sleep 5; run 'tput', 'cvvis';</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>

Racket

<lang racket>

  1. lang racket

(void (system "tput civis")) (sleep 2) (void (system "tput cvvis")) </lang>

Ruby

<lang ruby>require "curses" include Curses

init_screen begin

 curs_set(1) #visible cursor
 sleep 3
 curs_set(0) #invisible cursor
 sleep 3
 curs_set(1) #visible cursor
 sleep 3

ensure

 close_screen

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

XPL0

<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations

proc ShowCur(On); \Turn flashing cursor on or off int On; \true = cursor on; false = cursor off int CpuReg; [CpuReg:= GetReg; \access CPU registers CpuReg(0):= $0100; \AX:= $0100 CpuReg(2):= if On then $0007 else $2000; SoftInt($10); \Call BIOS interrupt $10 ]; \ShowCur

[ShowCur(false); \turn off flashing cursor if ChIn(1) then []; \wait for keystroke ShowCur(true); \turn on flashing cursor ]</lang>