Terminal control/Hiding the cursor: Difference between revisions

From Rosetta Code
Content added Content deleted
(Ada version)
m (→‎{{header|Phix}}: added syntax colouring the hard way)
Line 336: Line 336:


=={{header|Phix}}==
=={{header|Phix}}==
<lang Phix>cursor(NO_CURSOR)
<!--<lang Phix>(notonline)-->
<span style="color: #7060A8;">cursor</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NO_CURSOR</span><span style="color: #0000FF;">)</span>
sleep(1)
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
cursor(UNDERLINE_CURSOR)</lang>
<span style="color: #7060A8;">cursor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">UNDERLINE_CURSOR</span><span style="color: #0000FF;">)</span>
<!--</lang>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==

Revision as of 02:12, 15 May 2021

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.

Ada

Library: ANSIAda

<lang Ada>with Ada.Text_Io;

with Ansi;

procedure Hiding is

  use Ada.Text_Io;

begin

  Put ("Hiding the cursor for 2.0 seconds...");
  delay 0.500;
  Put (Ansi.Hide);
  delay 2.000;
  Put ("And showing again.");
  delay 0.500;
  Put (Ansi.Show);
  delay 2.000;
  New_Line;

end Hiding;</lang>

Arturo

<lang rebol>cursor false print "hiding the cursor"

pause 2000

cursor true print "showing the cursor"</lang>

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>

BaCon

<lang freebasic>' Hiding the cursor for an ANSI compliant terminal CURSOR OFF CURSOR ON</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

32-bit BBC BASICs: <lang bbcbasic> OFF : REM Hide the cursor

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

All BBC BASICs: <lang bbcbasic> VDU 23,1,0;0;0;0; : REM Hide the cursor

     T%=TIME+400:REPEAT UNTIL TIME>T%
     VDU 23,1,1;0;0;0; : REM Show the cursor again</lang>

Befunge

Assuming a terminal with support for ANSI escape sequences, this hides the cursor, waits for the user to press Enter, and then shows the cursor again. <lang befunge>"l52?["39*,,,,,, >v "retnE sserP">:#,_v> "h52?["39*,,,,,,@ >~</lang>

C

<lang c> /* 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>

C++

<lang cpp>

  1. include <Windows.h>

int main() {

 bool showCursor = false;
 
 HANDLE std_out = GetStdHandle(STD_OUTPUT_HANDLE); // Get standard output
 CONSOLE_CURSOR_INFO cursorInfo;                   // 
 GetConsoleCursorInfo(out, &cursorInfo);           // Get cursorinfo from output
 cursorInfo.bVisible = showCursor;                 // Set flag visible.
 SetConsoleCursorInfo(out, &cursorInfo);           // Apply changes

} </lang>

Common Lisp

<lang lisp> (defun sh (cmd)

 #+clisp (shell cmd)
 #+ecl (si:system cmd)
 #+sbcl (sb-ext:run-program "/bin/sh" (list "-c" cmd) :input nil :output *standard-output*)
 #+clozure (ccl:run-program "/bin/sh" (list "-c" cmd) :input nil :output *standard-output*))

(defun show-cursor (x)

 (if x (sh "tput cvvis") (sh "tput civis")))

(show-cursor nil) (sleep 3) (show-cursor t) (sleep 3) </lang>

ncurses

To interface the ncurses C library from Lisp, the croatoan library is used. <lang lisp>(defun hide-show-cursor ()

 (with-screen (scr :input-echoing nil :input-blocking t)
   (setf (cursor-visible-p scr) nil)
   (format scr "cursor-visible-p: ~A~%" (cursor-visible-p scr))
   (refresh scr)
   ;; wait for a keypress
   (get-char scr)
   (setf (cursor-visible-p scr) t)
   (format scr "cursor-visible-p: ~A" (cursor-visible-p scr))
   (refresh scr)
   (get-char scr)))</lang>

FunL

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

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

Furor

Hide: <lang Furor> cursoroff </lang> Show: <lang Furor> cursoron </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>

Julia

Translation of: Perl

<lang julia>const ESC = "\u001B" # escape code print("$ESC[?25l") # hide the cursor print("Enter anything, press RETURN: ") # prompt shown input = readline() # but no cursor print("$ESC[0H$ESC[0J$ESC[?25h") # reset, visible again sleep(3) println() </lang>

Kotlin

Works with: Ubuntu version 14.04

<lang scala>// version 1.1.2

fun main(args: Array<String>) {

   print("\u001B[?25l")      // hide cursor
   Thread.sleep(2000)        // wait 2 seconds before redisplaying cursor
   print("\u001B[?25h")      // display cursor
   Thread.sleep(2000)        // wait 2 more seconds before exiting

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

Nim

<lang nim>import terminal

echo "Cursor hidden. Press a key to show the cursor and exit." stdout.hideCursor() discard getCh() stdout.showCursor() </lang>

Perl

<lang perl>print "\e[?25l"; # hide the cursor print "Enter anything, press RETURN: "; # prompt shown $input = <>; # but no cursor print "\e[0H\e[0J\e[?25h"; # reset, visible again</lang>

Phix

cursor(NO_CURSOR)
sleep(1)
cursor(UNDERLINE_CURSOR)

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>

Tested with PB v5.60

<lang PureBasic>Procedure HideCursor ()

ConsoleCursor(0) 

EndProcedure

Procedure ShowCursor (CursorHeight.b = 1)

If CursorHeight > 10 : CursorHeight = 10 : EndIf
If CursorHeight < 1  : CursorHeight = 1  : EndIf
ConsoleCursor(CursorHeight)

EndProcedure

Procedure NL (NoL.b = 1)

For i = 1 To NoL : PrintN("") : Next

EndProcedure

If OpenConsole()

EnableGraphicalConsole(1)
Print("   full cursor > ")
ShowCursor(11) : Delay(4000) : NL()
Print(" hidden cursor > ")
HideCursor()   : Delay(4000) : NL()
Print("minimal cursor > ")
ShowCursor(-0.5)
Print("press [Enter] to continue ... ") : Input()

EndIf</lang>

Python

With print: <lang python>print("\x1b[?25l") # hidden print("\x1b[?25h") # shown </lang> With curses: <lang python>import curses import time

stdscr = curses.initscr() curses.curs_set(1) # visible time.sleep(2) curses.curs_set(0) # invisible time.sleep(2) curses.curs_set(1) # visible time.sleep(2) curses.endwin() </lang>

Racket

<lang racket>

  1. lang racket

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

Raku

(formerly Perl 6) <lang perl6>say 'Hiding the cursor for 5 seconds...'; run 'tput', 'civis'; sleep 5; run 'tput', 'cvvis';</lang>

REXX

Works with: Regina REXX

<lang rexx>/*REXX pgm calls a function in a shared library (regutil) to hide/show cursor.*/ z=rxfuncadd('sysloadfuncs', "regutil", 'sysloadfuncs') /*add a function lib.*/ if z\==0 then do /*test the return cod*/

              say 'return code'  z  "from rxfuncadd"    /*tell about bad RC. */
              exit z                                    /*exit this program. */
              end

call sysloadfuncs /*load the functions.*/

                                      /* [↓]   call a particular function.   */

call syscurstate 'off' /*hide the displaying of the cursor. */ say 'showing of the cursor is now off' /*inform that the cursor is now hidden.*/

                                      /* ··· and perform some stuff here ··· */

say 'sleeping for three seconds ...' /*inform the user of what we're doing. */ call sleep 3 /*might as well sleep for three seconds*/

call syscurstate 'on' /*(unhide) the displaying of the cursor*/ say 'showing of the cursor is now on' /*inform that the cursor is now showing*/

                                      /*stick a fork in it,  we're all done. */</lang>

output  

showing of the cursor is now off
sleeping for three seconds ...
showing of the cursor is now on

Ring

<lang ring>

  1. Project : Terminal control/Hiding the cursor

load "stdlib.ring"

  1. Linux

? "Hide Cursor using tput utility" system("tput civis") # Invisible sleep(10) ? "Show Cursor using tput utility" system("tput cnorm") # Normal </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>

Scala

<lang scala>object Main extends App {

 print("\u001B[?25l") // hide cursor
 Thread.sleep(2000) // wait 2 seconds before redisplaying cursor
 print("\u001B[?25h") // display cursor
 Thread.sleep(2000) // wait 2 more seconds before exiting

}</lang>

Tcl

<lang tcl> proc cursorVisibility Template:State normal {

   switch -- $state {

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

   }
   exec -- >@stdout tput $op

} </lang> Demo: <lang tcl> foreach x {visible invisible normal} {

   cursorVisibility $x
   puts -nonewline "$x cursor -> "
   flush stdout
   after 3000
   puts {}

} </lang>

UNIX Shell

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

Wren

Translation of: Go

<lang ecmascript>import "timer" for Timer

System.print("\e[?25l") Timer.sleep(3000) System.print("\e[?25h") Timer.sleep(3000)</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>

zkl

Translation of: Go

Hide the cursor for three seconds on an ANSI terminal <lang zkl>print("\e[?25l"); Atomic.sleep(3); print("\e[?25h");</lang>