Spinning rod animation/Text: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(16 intermediate revisions by 9 users not shown)
Line 143:
done</syntaxhighlight>
(Added an indent in the printf to better see the spinning rod).
 
=={{header|BASIC256}}==
<syntaxhighlight lang="freebasic">spinning$ = "|/-" + chr(92)
c = 1
 
while key = ""
cls
print chr(10) + " hit any key to end program "; mid(spinning$,c,1)
c += 1
pause .250 # in milliseconds
if c = 4 then c = 1
end while</syntaxhighlight>
 
=={{header|C}}==
Line 203 ⟶ 215:
(message "%c" char)
(sit-for 0.25)))</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure SpinningRod(Memo: TMemo);
var I: integer;
const CA: array [0..3] of char = ('|','/','-','\');
begin
LastKey:=#0;
for I:=0 to 1000 do
begin
Memo.SetFocus;
Memo.Lines.Clear;
Memo.Lines.Add(CA[I mod 4]+' - Press Any Key To Stop');
Sleep(250);
if (LastKey<>#0) or Application.Terminated then break;
Application.ProcessMessages;
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
/ - Press Any Key To Stop
Elapsed Time: 12.251 Sec.
</pre>
 
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=Lc09CgIxEAXgfk7xWCwUYcmuaOdJxEKyAwZMAkkQ8Qw2/tt5RY9gHtp8w4M3M3a0WmOJXJLdblJG83mfzqhcyJXcyJ08yJO8Gil6KNkdFQsjPu4VvcHMSAwozmsSAC4M9faYY4puAh+HDjsNsPUrC7zBUBu/zE2Ytp9LK/8gXw== Run it]
 
<syntaxhighlight>
c$[] = strchars "🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘"
textsize 60
move 20 30
on timer
ind = (ind + 1) mod1 len c$[]
text c$[ind]
timer 0.25
.
timer 0
</syntaxhighlight>
 
=={{header|Factor}}==
Line 343 ⟶ 402:
term <- setupTermFromEnv
bracket_ (cursorOff term) (cursorOn term) spin</syntaxhighlight>
 
=={{header|J}}==
Assuming linux as the host:
<syntaxhighlight lang=J>cout=: 1!:2&(<'/proc/self/fd/1')
dl=: 6!:3
spin=: {{ while. do. for_ch. y do. dl x [ cout 8 u:ch,CR end. end. }} 9 u:"1 ]</syntaxhighlight>
 
The initial task example becomes:
<syntaxhighlight lang=J>0.25 spin '|/-\'</syntaxhighlight>
 
Assuming you have terminal support for the hour wingdings, this would also work:
 
<syntaxhighlight lang=J>0.25 spin '🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕦🕧'</syntaxhighlight>
 
or
 
<syntaxhighlight lang=J>0.25 spin a.{~240 159 149&,&>144+i.24</syntaxhighlight>
 
Note also that you could animate lines of text rather than individual characters. For example, converting words to lines:
 
<syntaxhighlight lang=J>0.5 spin >;:'this is a test'</syntaxhighlight>
 
However, anything which takes multiple lines wouldn't work here. For that you'd need to clear the screen instead of using a simple carriage return. (Clearing the screen is completely doable, but the easy approaches are either embedded in gui mechanisms which sort of defeats the purpose of ascii art animation, or are not supported by some "purely textual" terminals.)
 
=={{header|Java}}==
Line 382 ⟶ 464:
})();
setInterval(rod, 250);
</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]] and [[#Python|Python]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
'''Generic Utilities'''
 
Invocation:
<pre>
jq --unbuffered -nrf spinning-rod-animation.jq
gojq -nrf spinning-rod-animation.jq
fq -nrf spinning-rod-animation.jq
</pre>
''' spinning-rod-animation.jq'''
<syntaxhighlight lang=jq>
def pause($seconds):
(now + $seconds)
| until( now > . ; .);
 
# Specify $n as null for an infinite spin
def animation($n):
def ESC: "\u001b";
def hide: "\(ESC)[?25l"; # hide the cursor
def restore: "\(ESC)[?25h"; # restore the cursor;
def a: "🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘";
 
hide,
"\(ESC)[2J\(ESC)[H", # clear, place cursor at top left corner
(range(0; $n // infinite) as $_
| a as $a
| pause(0.05)
| "\r\($a)" ),
restore;
 
animation(10)
</syntaxhighlight>
 
Line 420 ⟶ 538:
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
 
<syntaxhighlight lang="scheme">
{pre
{@ id="spin"
style="text-align:center;
font:bold 3.0em arial;"}
|}
 
{script
var i = 0,
c = "|/─\\";
var spin = function() {
document.getElementById("spin").innerHTML = c[i];
i = (i+1) % c.length;
};
 
setTimeout(spin,1);
setInterval(spin,250)
}
 
</syntaxhighlight>
 
=={{header|Lua}}==
Line 1,070 ⟶ 1,209:
100 NEXT
110 GOTO 60</syntaxhighlight>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec sym = '-' :: '\\' :: '|' :: '/' :: sym
 
let () = List.iter (fun c -> Printf.printf "%c%!\b" c; Unix.sleepf 0.25) sym</syntaxhighlight>
 
=={{header|Perl}}==
Line 1,343 ⟶ 1,487:
 
=={{header|Ruby}}==
Chars taken from the Raku example.
<syntaxhighlight lang="ruby">def spinning_rod
begin
printf("\033[?25l") # Hide cursor
%w[| / - \\]'🌑🌒🌓🌔🌕🌖🌗🌘'.chars.cycle do |rod|
print rod
sleep 0.25
print "\br"
end
ensure
Line 1,431 ⟶ 1,576:
wait
0.25</syntaxhighlight>
 
=={{header|True BASIC}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">DEF Inkey$
LOCAL t_arg1
 
IF key input then
GET KEY t_arg1
IF t_arg1 <= 255 then
LET inkey$ = chr$(t_arg1)
ELSE
LET inkey$ = chr$(0) & chr$(t_arg1-256)
END IF
ELSE
LET inkey$ = ""
END IF
END DEF
 
LET spinning$ = "|/-" & chr$(92)
 
DO while inkey$ = ""
CLEAR
PRINT
PRINT " hit any key to end program ";
PRINT (spinning$)[c:c+1-1]
LET c = c+1
PAUSE .25 ! in milliseconds
IF c = 4 THEN LET c = 1
LOOP
END</syntaxhighlight>
 
=={{header|Wee Basic}}==
Line 1,454 ⟶ 1,629:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdout
import "timer" for Timer
 
var ESC = "\u001b"
 
var a = "|/-\\"
System.write("%(ESC)\e[?25l") // hide the cursor
var start = System.clock
var asleep = 0
while (true) {
for (i in 0..3) {
System.write("%(ESC)\e[2J") // clear terminal
System.write("%(ESC)\e[0;0H") // place cursor at top left corner
for (j in 0..79) { // 80 character terminal width, say
System.write(a[i])
}
Stdout.flush()
Timer.sleep(250) // suspends both current fiber & System.clock
asleep = asleep + 250
}
Line 1,478 ⟶ 1,651:
if (now * 1000 + asleep - start * 1000 >= 20000) break
}
System.print("%(ESC)\e[?25h") // restore the cursor</syntaxhighlight>
 
=={{header|XPL0}}==
2,046

edits