Spinning rod animation/Text: Difference between revisions
Childishbeat (talk | contribs) m (A GlovePIE section has been added.) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 104:
EndWhile</lang>
=={{header|Perl 6}}==
{{works with|Rakudo|2018.05}}
Traditionally this is know as a [[wp:throbber|throbber]] or progress indicator.
This will accept an array of elements to use as its throbber frames, and optionally a delay before it returns the next element.
<lang perl6>class throbber {
has @.members;
has $.delay is rw = 0;
has $!index = 0;
method next {
$!index = ($!index + 1) % +@.members;
sleep $.delay if $.delay;
"\b" ~ @.members[$!index];
}
}
my $rod = throbber.new( :members(< | / - \ >), :delay(.25) );
print "\e[?25lLong running process... ";
print $rod.next for ^20;
my $clock = throbber.new( :members("🕐" .. "🕛") );
print "\b \nSomething else with a delay... ";
until my $done {
# do something in a loop;
sleep 1/12; # simulate processing delay
print $clock.next;
$done = True if $++ >= 60;
}
END { print "\b \e[?25h\n" } # clean up on exit</lang>
=={{header|zkl}}==
|
Revision as of 22:10, 11 June 2018
- Task
An animation with the following frames in the following order must animate with a delay of 0.25 seconds between each frame, with the previous frame being cleared before the next frame appears:
- |
- /
- -
- \
A version that loops and/or a version that doesn't loop can be made.
C
<lang c>#include <stdio.h>
- include <time.h>
int main() {
int i, j, ms = 250; const char *a = "|/-\\"; time_t start, now; struct timespec delay; delay.tv_sec = 0; delay.tv_nsec = ms * 1000000L; printf("\033[?25l"); // hide the cursor time(&start); while(1) { for (i = 0; i < 4; i++) { printf("\033[2J"); // clear terminal printf("\033[0;0H"); // place cursor at top left corner for (j = 0; j < 80; j++) { // 80 character terminal width, say printf("%c", a[i]); } fflush(stdout); nanosleep(&delay, NULL); } // stop after 20 seconds, say time(&now); if (difftime(now, start) >= 20) break; } printf("\033[?25h"); // restore the cursor return 0;
}</lang>
C Shell
<lang csh>while 1
foreach rod ('|' '/' '-' '\') printf ' %s\r' $rod; sleep 0.25 end
end</lang> (Added an indent in the printf to better see the spinning rod).
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. <lang glovepie>debug="|" wait 250 ms debug="/" wait 250 ms debug="-" wait 250 ms debug="\" wait 250 ms</lang>
Go
<lang go>package main
import (
"fmt" "time"
)
func main() {
a := `|/-\` fmt.Printf("\033[?25l") // hide the cursor start := time.Now() for { for i := 0; i < 4; i++ { fmt.Print("\033[2J") // clear terminal fmt.Printf("\033[0;0H") // place cursor at top left corner for j := 0; j < 80; j++ { // 80 character terminal width, say fmt.Printf("%c", a[i]) } time.Sleep(250 * time.Millisecond) } if time.Since(start).Seconds() >= 20.0 { // stop after 20 seconds, say break } } fmt.Print("\033[?25h") // restore the cursor
}</lang>
Microsoft Small Basic
<lang microsoftsmallbasic>a[1]="|" a[2]="/" a[3]="-" a[4]="\" b=0 While b=0
For c=1 To 4 TextWindow.Clear() TextWindow.WriteLine(a[c]) Program.Delay(250) EndFor
EndWhile</lang>
Perl 6
Traditionally this is know as a throbber or progress indicator.
This will accept an array of elements to use as its throbber frames, and optionally a delay before it returns the next element.
<lang perl6>class throbber {
has @.members; has $.delay is rw = 0; has $!index = 0; method next { $!index = ($!index + 1) % +@.members; sleep $.delay if $.delay; "\b" ~ @.members[$!index]; }
}
my $rod = throbber.new( :members(< | / - \ >), :delay(.25) ); print "\e[?25lLong running process... "; print $rod.next for ^20;
my $clock = throbber.new( :members("🕐" .. "🕛") );
print "\b \nSomething else with a delay... ";
until my $done {
# do something in a loop; sleep 1/12; # simulate processing delay print $clock.next; $done = True if $++ >= 60;
}
END { print "\b \e[?25h\n" } # clean up on exit</lang>
zkl
<lang zkl>foreach n,rod in ((1).MAX, T("|", "/", "-", "\\")){
print(" %s\r".fmt(rod)); Atomic.sleep(0.25);
}</lang> 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).