Terminal control/Hiding the cursor: Difference between revisions
(Added Befunge example.) |
(PascalABC.NET) |
||
(44 intermediate revisions by 29 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Terminal control}} |
{{task|Terminal control}} |
||
[[Category:Terminal control]] |
|||
The task is to hide the cursor and show it again. |
The task is to hide the cursor and show it again. |
||
<br><br> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">PROC Wait(BYTE frames) |
|||
BYTE RTCLOK=$14 |
|||
frames==+RTCLOK |
|||
WHILE frames#RTCLOK DO OD |
|||
RETURN |
|||
PROC Main() |
|||
BYTE CRSINH=$02F0 ;Controls visibility of cursor |
|||
Print("Hiding the cursor...") |
|||
Wait(50) |
|||
CRSINH=1 PutE() ;put the new line character to force hide the cursor |
|||
Wait(50) |
|||
Print("Showing the cursor...") |
|||
Wait(50) |
|||
CRSINH=0 PutE() ;put the new line character to force show the cursor |
|||
Wait(50) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Hiding_the_cursor.png Screenshot from Atari 8-bit computer] |
|||
=={{header|Ada}}== |
|||
{{libheader|ANSIAda}} |
|||
<syntaxhighlight 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;</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">cursor false |
|||
print "hiding the cursor" |
|||
pause 2000 |
|||
cursor true |
|||
print "showing the cursor"</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Keep in mind that AHK is not built for the command line, so we must call the WinAPI directly. |
Keep in mind that AHK is not built for the command line, so we must call the WinAPI directly. |
||
< |
<syntaxhighlight lang="ahk">DllCall("AllocConsole") ; Create a console if not launched from one |
||
hConsole := DllCall("GetStdHandle", UInt, STDOUT := -11) |
hConsole := DllCall("GetStdHandle", UInt, STDOUT := -11) |
||
Line 20: | Line 75: | ||
FileAppend, `nCursor shown, CONOUT$ |
FileAppend, `nCursor shown, CONOUT$ |
||
MsgBox</ |
MsgBox</syntaxhighlight> |
||
=={{header|BaCon}}== |
|||
<syntaxhighlight lang="freebasic">' Hiding the cursor for an ANSI compliant terminal |
|||
CURSOR OFF |
|||
CURSOR ON</syntaxhighlight> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
{{works with|FreeBASIC}} |
|||
< |
<syntaxhighlight lang="qbasic">'hide the cursor: |
||
LOCATE , , 0 |
LOCATE , , 0 |
||
'wait for a keypress... |
'wait for a keypress... |
||
SLEEP |
SLEEP |
||
'show the cursor: |
'show the cursor: |
||
LOCATE , , 1</ |
LOCATE , , 1</syntaxhighlight> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
<lang |
<syntaxhighlight lang="applesoftbasic">WAIT 49152,128</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
32-bit BBC BASICs: |
|||
<lang bbcbasic> OFF : REM Hide the cursor (caret) |
|||
<syntaxhighlight lang="bbcbasic"> OFF : REM Hide the cursor |
|||
WAIT 400 |
WAIT 400 |
||
ON : REM Show the cursor again</ |
ON : REM Show the cursor again</syntaxhighlight> |
||
All BBC BASICs: |
|||
<syntaxhighlight 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</syntaxhighlight> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">SET CURSOR "off" |
|||
PAUSE 2 !Pause 2 seconds |
|||
SET CURSOR "on" |
|||
END</syntaxhighlight> |
|||
=={{header|Befunge}}== |
=={{header|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. |
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. |
||
< |
<syntaxhighlight lang="befunge">"l52?["39*,,,,,, >v |
||
"retnE sserP">:#,_v> |
"retnE sserP">:#,_v> |
||
"h52?["39*,,,,,,@ >~</ |
"h52?["39*,,,,,,@ >~</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
/* Please note that curs_set is terminal dependent. */ |
|||
/*30th August,2012 |
|||
Abhishek Ghosh |
|||
Please note that curs_set is terminal dependent.*/ |
|||
#include<curses.h> |
#include<curses.h> |
||
Line 69: | Line 138: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
|||
{{works with|Mono|1.2}} |
|||
{{works with|Visual C sharp|Visual C#|2003}} |
|||
<syntaxhighlight 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); |
|||
}</syntaxhighlight> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <Windows.h> |
#include <Windows.h> |
||
int main() |
int main() |
||
Line 84: | Line 166: | ||
SetConsoleCursorInfo(out, &cursorInfo); // Apply changes |
SetConsoleCursorInfo(out, &cursorInfo); // Apply changes |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
|||
{{works with|Mono|1.2}} |
|||
{{works with|Visual C sharp|Visual C#|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> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun sh (cmd) |
(defun sh (cmd) |
||
#+clisp (shell cmd) |
#+clisp (shell cmd) |
||
Line 114: | Line 183: | ||
(show-cursor t) |
(show-cursor t) |
||
(sleep 3) |
(sleep 3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|ncurses}}=== |
|||
To interface the ncurses C library from Lisp, the ''croatoan'' library is used. |
|||
<syntaxhighlight 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)))</syntaxhighlight> |
|||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">import time.* |
||
import console.* |
import console.* |
||
hide() |
hide() |
||
sleep( 2 Second ) |
sleep( 2 Second ) |
||
show()</ |
show()</syntaxhighlight> |
||
=={{header|Furor}}== |
|||
Hide: |
|||
<syntaxhighlight lang="furor"> |
|||
cursoroff |
|||
</syntaxhighlight> |
|||
Show: |
|||
<syntaxhighlight lang="furor"> |
|||
cursoron |
|||
</syntaxhighlight> |
|||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
uses CRT; |
|||
begin |
|||
GotoXY(60,15); |
|||
Print('PascalABC.NET'); |
|||
HideCursor; |
|||
Sleep(1000); |
|||
ShowCursor; |
|||
Sleep(1000); |
|||
end. |
|||
</syntaxhighlight> |
|||
=={{header|Peri}}== |
|||
Hide: |
|||
<syntaxhighlight lang="peri"> |
|||
###sysinclude system.uh |
|||
cursoroff |
|||
</syntaxhighlight> |
|||
Show: |
|||
<syntaxhighlight lang="peri"> |
|||
###sysinclude system.uh |
|||
cursoron |
|||
</syntaxhighlight> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="vb">Locate , , 0 'Hide the cursor |
|||
Sleep 500 'waiting half second or a keypress... |
|||
Locate , , 1 'Show the cursor again |
|||
Sleep</syntaxhighlight> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
===External command=== |
===External command=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 146: | Line 273: | ||
cmd.Stdout = os.Stdout |
cmd.Stdout = os.Stdout |
||
return cmd.Run() |
return cmd.Run() |
||
}</ |
}</syntaxhighlight> |
||
===Escape code=== |
===Escape code=== |
||
(Not sure if this is ANSI, but it worked for me.) |
(Not sure if this is ANSI, but it worked for me.) |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 161: | Line 288: | ||
fmt.Print("\033[?25h") |
fmt.Print("\033[?25h") |
||
time.Sleep(3 * time.Second) |
time.Sleep(3 * time.Second) |
||
}</ |
}</syntaxhighlight> |
||
===Ncurses=== |
===Ncurses=== |
||
{{libheader|Curses}} |
{{libheader|Curses}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 183: | Line 310: | ||
gc.Cursor(1) |
gc.Cursor(1) |
||
s.GetChar() |
s.GetChar() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
With the definitions of [[Terminal_control/Coloured_text#J]] |
With the definitions of [[Terminal_control/Coloured_text#J]] |
||
< |
<syntaxhighlight lang="j">smoutput HIDECURSOR |
||
usleep(4e6) NB. wait 4 seconds |
usleep(4e6) NB. wait 4 seconds |
||
smoutput SHOWCURSOR |
smoutput SHOWCURSOR |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
Also works with gojq, the Go implementation. |
|||
'''Invocation''': jq -nrR -f hiding-the-cursor.jq |
|||
<syntaxhighlight lang="jq"> |
|||
# Be busy for at least the given number of seconds, |
|||
# and emit the actual number of seconds that have elapsed. |
|||
def sleep($seconds): |
|||
now |
|||
| . as $now |
|||
| until( . - $now >= $seconds; now) |
|||
| . - $now ; |
|||
def demo: |
|||
def ESC: "\u001B"; |
|||
def reset: "\(ESC)[0H\(ESC)[0J\(ESC)[?25h"; |
|||
reset, |
|||
"Now you see it ...", |
|||
(sleep(2) | empty), |
|||
"\(ESC)[?25l", # hide the cursor |
|||
"... now you don't.", |
|||
"Press RETURN to reset the screen and cursor.", |
|||
(first(inputs) | empty), |
|||
reset; |
|||
demo |
|||
</syntaxhighlight> |
|||
=={{header|Julia}}== |
|||
{{trans|Perl}} |
|||
<syntaxhighlight 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() |
|||
</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
|||
{{Works with|Ubuntu|14.04}} |
|||
<syntaxhighlight 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 |
|||
}</syntaxhighlight> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9 |
||
local( |
local( |
||
Line 209: | Line 388: | ||
// wait for 4 seconds to give time discover the cursor is back |
// wait for 4 seconds to give time discover the cursor is back |
||
sleep(4000)</ |
sleep(4000)</syntaxhighlight> |
||
=={{header|Locomotive Basic}}== |
=={{header|Locomotive Basic}}== |
||
< |
<syntaxhighlight lang="locobasic">10 CURSOR 0: REM hide cursor |
||
20 FOR l = 1 TO 2000: REM delay |
20 FOR l = 1 TO 2000: REM delay |
||
30 NEXT l |
30 NEXT l |
||
40 CURSOR 1: REM show cursor</ |
40 CURSOR 1: REM show cursor</syntaxhighlight> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Run["tput civis"] (* Cursor hidden *) |
||
Pause[2] |
Pause[2] |
||
Run["tput cvvis"] (* Cursor Visible *)</ |
Run["tput cvvis"] (* Cursor Visible *)</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System.Console; |
||
using System.Threading.Thread; |
using System.Threading.Thread; |
||
Line 234: | Line 414: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header| |
=={{header|Nim}}== |
||
<lang perl6>run 'tput', 'civis'; |
|||
<syntaxhighlight lang="nim">import terminal |
|||
sleep 5; |
|||
run 'tput', 'cvvis';</lang> |
|||
echo "Cursor hidden. Press a key to show the cursor and exit." |
|||
stdout.hideCursor() |
|||
discard getCh() |
|||
stdout.showCursor() |
|||
</syntaxhighlight> |
|||
=={{header|Perl}}== |
|||
<syntaxhighlight 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</syntaxhighlight> |
|||
=={{header|Phix}}== |
|||
<!--<syntaxhighlight 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> |
|||
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">cursor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">UNDERLINE_CURSOR</span><span style="color: #0000FF;">)</span> |
|||
<!--</syntaxhighlight>--> |
|||
=={{header|PHP}}== |
|||
<syntaxhighlight lang="php">#!/usr/bin/php |
|||
<?php |
|||
echo "\e[?25l"; // Cursor hide escape sequence |
|||
sleep(5); |
|||
echo "\e[?25h"; // Cursor show escape sequence |
|||
sleep(5);</syntaxhighlight> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(call "tput" "civis") # Invisible |
||
(wait 1000) |
(wait 1000) |
||
(call "tput" "cvvis") # Visible</ |
(call "tput" "cvvis") # Visible</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">#cursorSize = 10 ;use a full sized cursor |
||
If OpenConsole() |
If OpenConsole() |
||
Line 260: | Line 466: | ||
EndIf |
EndIf |
||
ForEver |
ForEver |
||
EndIf </ |
EndIf </syntaxhighlight> |
||
Tested with <b>PB v5.60</b> |
|||
<syntaxhighlight 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</syntaxhighlight> |
|||
=={{header|Python}}== |
|||
With print: |
|||
<syntaxhighlight lang="python">print("\x1b[?25l") # hidden |
|||
print("\x1b[?25h") # shown |
|||
</syntaxhighlight> |
|||
With curses: |
|||
<syntaxhighlight 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() |
|||
</syntaxhighlight> |
|||
=={{header|Quackery}}== |
|||
{{trans|Python}} |
|||
On some platforms the cursor visibility will not change until the output buffer is flushed by for example a cr/lf. |
|||
<syntaxhighlight lang="quackery"> [ $ &print("\x1b[?25l",end='')& python ] is hide ( --> ) |
|||
[ $ &print("\x1b[?25h",end='')& python ] is show ( --> )</syntaxhighlight> |
|||
=={{header|R}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="r">cat("\x1b[?25l") # Hide |
|||
Sys.sleep(2) |
|||
cat("\x1b[?25h") # Show</syntaxhighlight> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(void (system "tput civis")) (sleep 2) (void (system "tput cvvis")) |
(void (system "tput civis")) (sleep 2) (void (system "tput cvvis")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<syntaxhighlight lang="raku" line>say 'Hiding the cursor for 5 seconds...'; |
|||
run 'tput', 'civis'; |
|||
sleep 5; |
|||
run 'tput', 'cvvis';</syntaxhighlight> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
{{works with|Regina REXX}} |
{{works with|Regina REXX}} |
||
< |
<syntaxhighlight 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.*/ |
z=rxfuncadd('sysloadfuncs', "regutil", 'sysloadfuncs') /*add a function lib.*/ |
||
if z\==0 then do /*test the return cod*/ |
if z\==0 then do /*test the return cod*/ |
||
Line 289: | Line 567: | ||
call syscurstate 'on' /*(unhide) the displaying of the cursor*/ |
call syscurstate 'on' /*(unhide) the displaying of the cursor*/ |
||
say 'showing of the cursor is now on' /*inform that the cursor is now showing*/ |
say 'showing of the cursor is now on' /*inform that the cursor is now showing*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 296: | Line 574: | ||
showing of the cursor is now on |
showing of the cursor is now on |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
# Project : Terminal control/Hiding the cursor |
|||
load "stdlib.ring" |
|||
# Linux |
|||
? "Hide Cursor using tput utility" |
|||
system("tput civis") # Invisible |
|||
sleep(10) |
|||
? "Show Cursor using tput utility" |
|||
system("tput cnorm") # Normal |
|||
</syntaxhighlight> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require "curses" |
||
include Curses |
include Curses |
||
Line 311: | Line 602: | ||
ensure |
ensure |
||
close_screen |
close_screen |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Scala}}== |
|||
<syntaxhighlight 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 |
|||
}</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang="tcl"> |
|||
<lang tcl>proc cursor {{state "normal"}} { |
|||
proc cursorVisibility {{state normal}} { |
|||
switch -- $state { |
switch -- $state { |
||
invisible {set op civis} |
|||
visible {set op cvvis} |
|||
normal {set op cnorm} |
|||
} |
} |
||
exec -- >@stdout tput $op |
|||
# Should be just: “exec tput $op” but it's not actually supported on my terminal... |
|||
} |
|||
exec sh -c "tput $op || true" |
|||
</syntaxhighlight> |
|||
}</lang> |
|||
Demo: |
|||
Demonstration code: |
|||
<lang |
<syntaxhighlight lang="tcl"> |
||
foreach x {visible invisible normal} { |
|||
puts "normal cursor" |
|||
cursorVisibility $x |
|||
after 3000 |
|||
puts -nonewline "$x cursor -> " |
|||
cursor invisible |
|||
flush stdout |
|||
puts "invisible cursor" |
|||
after 3000 |
after 3000 |
||
puts {} |
|||
cursor visible |
|||
} |
|||
puts "very visible cursor" |
|||
</syntaxhighlight> |
|||
after 3000 |
|||
cursor normal |
|||
puts "back to normal" |
|||
after 1000</lang> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
< |
<syntaxhighlight lang="sh">tput civis # Hide the cursor |
||
sleep 5 # Sleep for 5 seconds |
sleep 5 # Sleep for 5 seconds |
||
tput cnorm # Show the cursor</ |
tput cnorm # Show the cursor</syntaxhighlight> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
<syntaxhighlight lang="wren">import "timer" for Timer |
|||
System.print("\e[?25l") |
|||
Timer.sleep(3000) |
|||
System.print("\e[?25h") |
|||
Timer.sleep(3000)</syntaxhighlight> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
proc ShowCur(On); \Turn flashing cursor on or off |
proc ShowCur(On); \Turn flashing cursor on or off |
||
Line 358: | Line 664: | ||
if ChIn(1) then []; \wait for keystroke |
if ChIn(1) then []; \wait for keystroke |
||
ShowCur(true); \turn on flashing cursor |
ShowCur(true); \turn on flashing cursor |
||
]</ |
]</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
Hide the cursor for three seconds on an ANSI terminal |
Hide the cursor for three seconds on an ANSI terminal |
||
< |
<syntaxhighlight lang="zkl">print("\e[?25l"); |
||
Atomic.sleep(3); |
Atomic.sleep(3); |
||
print("\e[?25h");</ |
print("\e[?25h");</syntaxhighlight> |
||
{{omit from|ACL2}} |
{{omit from|ACL2}} |
Latest revision as of 21:08, 9 June 2024
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.
Action!
PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
WHILE frames#RTCLOK DO OD
RETURN
PROC Main()
BYTE CRSINH=$02F0 ;Controls visibility of cursor
Print("Hiding the cursor...")
Wait(50)
CRSINH=1 PutE() ;put the new line character to force hide the cursor
Wait(50)
Print("Showing the cursor...")
Wait(50)
CRSINH=0 PutE() ;put the new line character to force show the cursor
Wait(50)
RETURN
- Output:
Screenshot from Atari 8-bit computer
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;
Arturo
cursor false
print "hiding the cursor"
pause 2000
cursor true
print "showing the cursor"
AutoHotkey
Keep in mind that AHK is not built for the command line, so we must call the WinAPI directly.
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
BaCon
' Hiding the cursor for an ANSI compliant terminal
CURSOR OFF
CURSOR ON
BASIC
'hide the cursor:
LOCATE , , 0
'wait for a keypress...
SLEEP
'show the cursor:
LOCATE , , 1
Applesoft BASIC
WAIT 49152,128
BBC BASIC
32-bit BBC BASICs:
OFF : REM Hide the cursor
WAIT 400
ON : REM Show the cursor again
All BBC BASICs:
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
True BASIC
SET CURSOR "off"
PAUSE 2 !Pause 2 seconds
SET CURSOR "on"
END
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.
"l52?["39*,,,,,, >v
"retnE sserP">:#,_v>
"h52?["39*,,,,,,@ >~
C
/* Please note that curs_set is terminal dependent. */
#include<curses.h>
#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;
}
C#
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);
}
C++
#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
}
Common 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)
ncurses
To interface the ncurses C library from Lisp, the croatoan library is used.
(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)))
FunL
import time.*
import console.*
hide()
sleep( 2 Second )
show()
Furor
Hide:
cursoroff
Show:
cursoron
PascalABC.NET
uses CRT;
begin
GotoXY(60,15);
Print('PascalABC.NET');
HideCursor;
Sleep(1000);
ShowCursor;
Sleep(1000);
end.
Peri
Hide:
###sysinclude system.uh
cursoroff
Show:
###sysinclude system.uh
cursoron
FreeBASIC
Locate , , 0 'Hide the cursor
Sleep 500 'waiting half second or a keypress...
Locate , , 1 'Show the cursor again
Sleep
Go
External command
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()
}
Escape code
(Not sure if this is ANSI, but it worked for me.)
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)
}
Ncurses
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()
}
J
With the definitions of Terminal_control/Coloured_text#J
smoutput HIDECURSOR
usleep(4e6) NB. wait 4 seconds
smoutput SHOWCURSOR
jq
Also works with gojq, the Go implementation.
Invocation: jq -nrR -f hiding-the-cursor.jq
# Be busy for at least the given number of seconds,
# and emit the actual number of seconds that have elapsed.
def sleep($seconds):
now
| . as $now
| until( . - $now >= $seconds; now)
| . - $now ;
def demo:
def ESC: "\u001B";
def reset: "\(ESC)[0H\(ESC)[0J\(ESC)[?25h";
reset,
"Now you see it ...",
(sleep(2) | empty),
"\(ESC)[?25l", # hide the cursor
"... now you don't.",
"Press RETURN to reset the screen and cursor.",
(first(inputs) | empty),
reset;
demo
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()
Kotlin
// 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
}
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)
Locomotive Basic
10 CURSOR 0: REM hide cursor
20 FOR l = 1 TO 2000: REM delay
30 NEXT l
40 CURSOR 1: REM show cursor
Mathematica/Wolfram Language
Run["tput civis"] (* Cursor hidden *)
Pause[2]
Run["tput cvvis"] (* Cursor Visible *)
Nemerle
using System.Console;
using System.Threading.Thread;
module CursorVisibility
{
Main() : void
{
repeat(3) {
CursorVisible = !CursorVisible;
Sleep(5000);
}
}
}
Nim
import terminal
echo "Cursor hidden. Press a key to show the cursor and exit."
stdout.hideCursor()
discard getCh()
stdout.showCursor()
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
Phix
cursor(NO_CURSOR) sleep(1) cursor(UNDERLINE_CURSOR)
PHP
#!/usr/bin/php
<?php
echo "\e[?25l"; // Cursor hide escape sequence
sleep(5);
echo "\e[?25h"; // Cursor show escape sequence
sleep(5);
PicoLisp
(call "tput" "civis") # Invisible
(wait 1000)
(call "tput" "cvvis") # Visible
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
Tested with PB v5.60
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
Python
With print:
print("\x1b[?25l") # hidden
print("\x1b[?25h") # shown
With curses:
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()
Quackery
On some platforms the cursor visibility will not change until the output buffer is flushed by for example a cr/lf.
[ $ &print("\x1b[?25l",end='')& python ] is hide ( --> )
[ $ &print("\x1b[?25h",end='')& python ] is show ( --> )
R
cat("\x1b[?25l") # Hide
Sys.sleep(2)
cat("\x1b[?25h") # Show
Racket
#lang racket
(void (system "tput civis")) (sleep 2) (void (system "tput cvvis"))
Raku
(formerly Perl 6)
say 'Hiding the cursor for 5 seconds...';
run 'tput', 'civis';
sleep 5;
run 'tput', 'cvvis';
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. */
output
showing of the cursor is now off sleeping for three seconds ... showing of the cursor is now on
Ring
# Project : Terminal control/Hiding the cursor
load "stdlib.ring"
# Linux
? "Hide Cursor using tput utility"
system("tput civis") # Invisible
sleep(10)
? "Show Cursor using tput utility"
system("tput cnorm") # Normal
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
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
}
Tcl
proc cursorVisibility {{state normal}} {
switch -- $state {
invisible {set op civis}
visible {set op cvvis}
normal {set op cnorm}
}
exec -- >@stdout tput $op
}
Demo:
foreach x {visible invisible normal} {
cursorVisibility $x
puts -nonewline "$x cursor -> "
flush stdout
after 3000
puts {}
}
UNIX Shell
tput civis # Hide the cursor
sleep 5 # Sleep for 5 seconds
tput cnorm # Show the cursor
Wren
import "timer" for Timer
System.print("\e[?25l")
Timer.sleep(3000)
System.print("\e[?25h")
Timer.sleep(3000)
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
]
zkl
Hide the cursor for three seconds on an ANSI terminal
print("\e[?25l");
Atomic.sleep(3);
print("\e[?25h");
- Programming Tasks
- Terminal control
- Action!
- Ada
- ANSIAda
- Arturo
- AutoHotkey
- BaCon
- BASIC
- Applesoft BASIC
- BBC BASIC
- True BASIC
- Befunge
- C
- C sharp
- C++
- Common Lisp
- Ncurses
- FunL
- Furor
- PascalABC.NET
- Peri
- FreeBASIC
- Go
- Curses
- J
- Jq
- Julia
- Kotlin
- Lasso
- Locomotive Basic
- Mathematica
- Wolfram Language
- Nemerle
- Nim
- Perl
- Phix
- PHP
- PicoLisp
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- Ruby
- Scala
- Tcl
- UNIX Shell
- Wren
- XPL0
- Zkl
- ACL2/Omit
- GUISS/Omit
- Maxima/Omit
- ZX Spectrum Basic/Omit