Spinning rod animation/Text: Difference between revisions

m
syntax highlighting fixup automation
(Added XPL0 example.)
m (syntax highlighting fixup automation)
Line 44:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">L
L(rod) ‘\|/-’
print(rod, end' "\r")
sleep(0.25)</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Wait(BYTE frames)
BYTE RTCLOK=$14
frames==+RTCLOK
Line 77:
CH=$FF
CRSINH=0 ;show cursor
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Spinning_rod_animation_text.png Screenshot from Atari 8-bit computer]
Line 86:
=={{header|Ada}}==
{{trans|Go}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Spinning_Rod is
Line 108:
end loop;
Put (ASCII.ESC & "[?25h"); -- Restore the cursor
end Spinning_Rod;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Sadly, Algol 68 doesn't have a standard delay/sleep routine, so this sample delays with a busy loop. A loop of 2 000 000 gives a reasonable spinning rod on the machine I tried it on. Increase the outer loop maximum for a longer animation.
<langsyntaxhighlight lang="algol68">FOR i TO 1 000 DO # increase/decrease the TO value for a longer/shorter animation #
FOR d TO 2 000 000 DO SKIP OD; # adjust to change the spin rate #
print( ( CASE 1 + i MOD 4 IN "/", "-", "\", "|" ESAC, REPR 8 ) )
OD</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SPINNING_ROD_ANIMATION_TEXT.AWK
@load "time"
Line 129:
exit(0)
}
</syntaxhighlight>
</lang>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="qbasic">WHILE TRUE
PRINT CR$, TOKEN$("🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘", x);
x = IIF(x>7, 1, x+1)
SLEEP 250
WEND</langsyntaxhighlight>
 
=={{header|Bash}}==
<langsyntaxhighlight lang="bash">while : ; do
for rod in \| / - \\ ; do printf ' %s\r' $rod; sleep 0.25; done
done</langsyntaxhighlight>
(Added an indent in the printf to better see the spinning rod).
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <time.h>
 
Line 174:
printf("\033[?25h"); // restore the cursor
return 0;
}</langsyntaxhighlight>
 
=={{header|C Shell}}==
<langsyntaxhighlight lang="csh">while 1
foreach rod ('|' '/' '-' '\')
printf ' %s\r' $rod; sleep 0.25
end
end</langsyntaxhighlight>
(Added an indent in the printf to better see the spinning rod).
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">SPINROD
; spin 10 times with quarter-second wait
for i = 1:1:10 {
Line 196:
}
}
quit</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(while t
(dolist (char (string-to-list "\\|/-"))
(message "%c" char)
(sit-for 0.25)))</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: calendar combinators.extras formatting io sequences
threads ;
 
[
"\\|/-" [ "%c\r" printf flush 1/4 seconds sleep ] each
] forever</langsyntaxhighlight>
 
=={{header|Forth}}==
Tested in gforth 0.7.9
<langsyntaxhighlight lang="forth">
: rod
cr
Line 227:
;
rod
</syntaxhighlight>
</lang>
This one is designed to be embedded in a program when the user has to wait for some (silent) task to finish. It is designed as a coroutine, so the state of the spinner is preserved.
{{works with|4tH v3.64.0}}
<syntaxhighlight lang="text">
include lib/yield.4th
 
Line 252:
 
test
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 13-07-2018
' compile with: fbc -s console
 
Line 271:
Wend
 
End</langsyntaxhighlight>
 
=={{header|GlovePIE}}==
Because GlovePIE is a looping programming language, which means the script is ran over and over again in a looping fashion, this code loops again and again until it's stopped.
<langsyntaxhighlight lang="glovepie">debug="|"
wait 250 ms
debug="/"
Line 282:
wait 250 ms
debug="\"
wait 250 ms</langsyntaxhighlight>
 
=={{header|Go}}==
{{works with|Ubuntu 16.04}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 311:
}
fmt.Print("\033[?25h") // restore the cursor
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Uses the terminfo library to make the cursor invisible, if possible.
<langsyntaxhighlight lang="haskell">import Control.Concurrent (threadDelay)
import Control.Exception (bracket_)
import Control.Monad (forM_)
Line 342:
putStrLn "Spinning rod demo. Hit ^C to stop it.\n"
term <- setupTermFromEnv
bracket_ (cursorOff term) (cursorOn term) spin</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Go}}
<langsyntaxhighlight lang="java">public class SpinningRod
{
public static void main(String[] args) throws InterruptedException {
Line 367:
System.out.print("\033[?25h"); // restore the cursor
}
}</langsyntaxhighlight>
 
=={{header|Javascript}}==
Node JS:
<langsyntaxhighlight lang="javascript">
const rod = (function rod() {
const chars = "|/-\\";
Line 382:
})();
setInterval(rod, 250);
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">while true
for rod in "\|/-" # this needs to be a string, a char literal cannot be iterated over
print(rod,'\r')
Line 392:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.2.50
 
const val ESC = "\u001b"
Line 418:
}
print("$ESC[?25h") // restore the cursor
}</langsyntaxhighlight>
 
 
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
--
-- Simple String Animation - semi-hard-coded variant - you can alter the chars table - update the count and run it...
Line 512:
_data.anim, _data.count, _data.index, _data.expiry = _tab, nil, nil, nil;
end
</syntaxhighlight>
</lang>
 
Usage:
<syntaxhighlight lang="lua">
<lang Lua>
-- 1 second delay, going backwards.
print( string.BasicAnimation( 1, true ) );
Line 527:
-- 1 second delay going backwards
print( string.BasicAnimation( 1 ) );
</syntaxhighlight>
</lang>
 
 
===Extended Modular Variant===
<syntaxhighlight lang="lua">
<lang Lua>
--
-- Simple String Animation - Josh 'Acecool' Moser under modified ACL - Free to use, modify and learn from.
Line 874:
return _char, _has_frame_advanced;
end
</syntaxhighlight>
</lang>
 
<br />
Line 880:
<br />
 
<syntaxhighlight lang="lua">
<lang Lua>
--
-- In some HUD element, or where text is output... such as 'Loading' ... try:
Line 896:
-- In this example print would be draw.Text or something along those lines.
print( 'Loading' .. string.SimpleAnimation( STRING_ANIMATION_DOTS ) );
</syntaxhighlight>
</lang>
 
<br />
Line 902:
<br />
 
<syntaxhighlight lang="lua">
<lang Lua>
--
-- Example - This is one way it could be used without rendering it every frame... Pseudo code and a task which is a good idea to do - when the map is added, add a function to register the animation and also go through each animation index and see which element is
Line 945:
end
end
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
n$=lambda$ n=1, a$="|/-\" -> {
Line 963:
CheckIt
 
</syntaxhighlight>
</lang>
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
n=1
Line 977:
CheckIt
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">chars = "|/\[Dash]\\";
pos = 1;
Dynamic[c]
Line 987:
c = StringTake[chars, {pos}];
Pause[0.25];
]</langsyntaxhighlight>
 
=={{header|MelonBasic}}==
<langsyntaxhighlight MelonBasiclang="melonbasic">Wait:0.25
Delete:1
Say:/
Line 1,001:
Wait:0.25
Delete:1
Goto:1</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
<langsyntaxhighlight lang="microsoftsmallbasic">a[1]="|"
a[2]="/"
a[3]="-"
Line 1,015:
Program.Delay(250)
EndFor
EndWhile</langsyntaxhighlight>
 
=={{header|MiniScript}}==
Control over the text cursor -- or indeed, whether there ''is'' a text cursor, or even text at all -- depends on the host environment. Here's a version that works with [https://miniscript.org/MiniMicro/ MiniMicro]:
<langsyntaxhighlight MiniScriptlang="miniscript">print "Press control-C to exit..."
while true
for c in "|/-\"
Line 1,025:
wait 0.25
end for
end while</langsyntaxhighlight>
 
And here's a version that will work with command-line MiniScript, running on a terminal that interprets standard VT100 escape sequences:
<langsyntaxhighlight MiniScriptlang="miniscript">while true
for c in "|/-\"
print c
Line 1,034:
print char(27) + "[2A" // move cursor up 2 lines
end for
end while</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
With some modifications.
<langsyntaxhighlight Nimlang="nim">import std/monotimes, times, os
 
const A = ["|", "/", "—", "\\"]
Line 1,055:
if (now - start).inSeconds >= 5:
break
echo "$\e[?25h" # Restore the cursor.</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
The 0.25 second delay assumes the program is running at 60 frames per second.
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 DIM A(4)
20 A(1)=236
30 A(2)=234
Line 1,069:
90 PAUSE 15
100 NEXT
110 GOTO 60</langsyntaxhighlight>
 
=={{header|Perl}}==
The statement <code>$| =1</code> is required in order to disable output buffering.
<langsyntaxhighlight lang="perl">$|= 1;
 
while () {
Line 1,080:
printf "\r ($_)";
}
}</langsyntaxhighlight>
 
Extending it for moon phases:
 
<langsyntaxhighlight lang="perl">$|=1;
binmode STDOUT, ":utf8";
 
Line 1,100:
# (3) `@{}` dereferences the created list.
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
=== console version ===
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (cursor, sleep)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"please_wait... "</span><span style="color: #0000FF;">)</span>
Line 1,115:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" \ndone"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- clear rod, "done" on next line</span>
<!--</langsyntaxhighlight>-->
=== GUI version ===
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Spinning_rod.htm here] (don't expect too much, improvements welcome).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Spinning_rod_animation.exw
Line 1,221:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(de rod ()
(until ()
Line 1,230:
(prin R (wait 250) "\r")(flush) ) ) )
(rod)
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from time import sleep
while True:
for rod in r'\|/-':
print(rod, end='\r')
sleep(0.25)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (anim)
Line 1,248:
(anim))
(anim)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,257:
This implementation will accept an array of elements to use as its throbber frames, or as a scrolling marquee and optionally a delay before it returns the next element.
 
<syntaxhighlight lang="raku" perl6line>class throbber {
has @.frames;
has $.delay is rw = 0;
Line 1,300:
print $scroll.next for ^95;
 
END { print "\e[?25h\n" } # clean up on exit</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,311:
::* &nbsp; ROO REXX
::* &nbsp; Regina REXX &nbsp; (see the programming note below.)
<langsyntaxhighlight lang="rexx">/*REXX program displays a "spinning rod" (AKA: trobbers or progress indicators). */
 
if 4=='f4'x then bs= "16"x /*EBCDIC? Then use this backspace chr.*/
Line 1,324:
end /*j*/
 
halt: say bs ' ' /*stick a fork in it, we're all done. */</langsyntaxhighlight>
Programming note: &nbsp; this REXX program makes use of &nbsp; '''DELAY''' &nbsp; BIF which delays (sleeps) for a specified amount of seconds.
<br>Some REXXes don't have a &nbsp; '''DELAY''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[DELAY.REX]].
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">load "stdlib.ring"
rod = ["|", "/", "-", "\"]
for n = 1 to len(rod)
Line 1,335:
sleep(0.25)
system("cls")
next</langsyntaxhighlight>
Output:
|
Line 1,343:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def spinning_rod
begin
printf("\033[?25l") # Hide cursor
Line 1,358:
puts "Ctrl-c to stop."
spinning_rod
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">fn main() {
let characters = ['|', '/', '-', '\\'];
let mut current = 0;
Line 1,374:
std::thread::sleep(std::time::Duration::from_millis(250)); // Sleep 250 ms.
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object SpinningRod extends App {
val start = System.currentTimeMillis
 
Line 1,393:
print("\033[?25h") // restore the cursor
 
}</langsyntaxhighlight>
 
=={{header|ScratchScript}}==
<langsyntaxhighlight ScratchScriptlang="scratchscript">print "|"
delay 0.25
clear
Line 1,406:
clear
print "\"
delay 0.25</langsyntaxhighlight>
 
=={{header|SimpleCode}}==
<syntaxhighlight lang="simplecode">dtxt
<lang SimpleCode>dtxt
|
wait
Line 1,430:
\
wait
0.25</langsyntaxhighlight>
 
=={{header|Wee Basic}}==
Since the "|" character isn't built into Wee Basic on the Nintendo DS, and it looks the part in Wee Basic on the Nintendo DS, the character "l" is used as a substitute. Also, since no working delay command has been found yet, a for loop is used to work around this problem.
<langsyntaxhighlight Weelang="wee Basicbasic">let loop=1
sub delay:
for i=1 to 10000
Line 1,450:
gosub delay:
wend
end</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">import "io" for Stdout
import "timer" for Timer
 
Line 1,478:
if (now * 1000 + asleep - start * 1000 >= 20000) break
}
System.print("%(ESC)[?25h") // restore the cursor</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">char I, Rod;
[Rod:= "|/-\ ";
loop for I:= 0 to 3 do
Line 1,489:
if KeyHit then quit;
];
]</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|C Shell}}
<langsyntaxhighlight lang="zkl">foreach n,rod in ((1).MAX, T("|", "/", "-", "\\")){
print(" %s\r".fmt(rod));
 
Atomic.sleep(0.25);
}</langsyntaxhighlight>
A loop foreach a,b in (c,d) translates to
foreach a in (c) foreach b in (d). n.MAX is a 64 bit int (9223372036854775807).
Line 1,503:
A more useful example would be a worker thread showing a "I'm working" display
(in another thread) and turning it off when that work is done.
<langsyntaxhighlight lang="zkl">fcn spin{ // this will be a thread that displays spinner
try{
foreach n,rod in ((1).MAX, "\\|/-"){
Line 1,510:
}
}catch{} // don't complain about uncaught exception that stops thread
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// main body of code
spinner:=spin.launch(); // start spinner thread, returns reference to thread
Atomic.sleep(10); // do stuff
vm.kick(spinner.value); // stop thread by throwing exception at it</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight ZXlang="zx Basicbasic">10 LET A$="|/-\"
20 FOR C=1 TO 4
30 PRINT AT 0,0;A$(C)
Line 1,523:
50 NEXT C
60 GOTO 20
</syntaxhighlight>
</lang>
10,333

edits