Spinning rod animation/Text: Difference between revisions
Added missing <lang> tag. |
|||
Line 969: | Line 969: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
import std/monotimes, times, os |
<lang Nim>import std/monotimes, times, os |
||
const A = ["|", "/", "—", "\\"] |
const A = ["|", "/", "—", "\\"] |
Revision as of 21:33, 12 April 2021
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
An animation with the following frames in the following order (if certain characters aren't available or can't be used correctly in the programming language, alternate characters can replace any of these frames) must animate with a delay of 0.25 seconds between each frame, with the previous frame being cleared before the next frame appears:
- |
- /
- - or ─
- \
A stand-alone version that loops and/or a version that doesn't loop can be made. These examples can also be converted into a system used in game development which is called on a HUD or GUI element requiring it to be called each frame to output the text, and advance the frame when the frame delay has passed. You can also use alternate text such as the . animation ( . | .. | ... | .. | repeat from . ) or the logic can be updated to include a ping/pong style where the frames advance forward, reach the end and then play backwards and when they reach the beginning they start over ( technically, you'd stop one frame prior to prevent the first frame playing twice, or write it another way ).
There are many different ways you can incorporate text animations. Here are a few text ideas - each frame is in quotes. If you can think of any, add them to this page! There are 2 examples for several of these; the first is the base animation with only unique sets of characters. The second consists of the primary set from a - n and doubled, minus the first and last element ie: We only want the center. This way an animation can play forwards, and then in reverse ( ping ponging ) without having to code that feature. For the animations with 3 elements, we only add 1, the center. with 4, it becomes 6. with 10, it becomes 18.
We don't need the second option for some of the animations if they connect smoothly, when animated, back to the first element. ... doesn't connect with . cleanly - there is a large leap. The rotating pipe meets the first perfectly so it isn't necessary, etc..
- Dots - Option A requires ping / pong enabled script. Option B just adds the elements in the center.
- '.', '..', '...'
- '.', '..', '...', '..'
- Pipe - This has the uniform sideways pipe instead of a hyphen to prevent non-uniform sizing.
- '|', '/', '─', '\'
- Stars - Option A requires ping / pong enabled script. Option B just adds the elements from the center.
- '⁎', '⁑', '⁂'
- '⁎', '⁑', '⁂', '⁑'
- Clock - These need to be ordered. I haven't done this yet as the application I was testing the system in doesn't support these wingdings / icons. But this would look quite nice and you could set it up to go forward, or backward during an undo process, etc..
- '🕛', '🕧', '🕐', '🕜', '🕑', '🕝', '🕒', '🕞', '🕓', '🕟', '🕔', '🕠', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕡', '🕢', '🕣', '🕤', '🕥', '🕦'
- Arrows:
- '⬍', '⬈', '➞', '⬊', '⬍', '⬋', '⬅', '⬉'
- Bird - This looks decent but may be missing something.
- '︷', '︵', '︹', '︺', '︶', '︸'
- '︷', '︵', '︹', '︺', '︶', '︸', '︶', '︺', '︹', '︵'
- Plants - This isn't quite complete
- '☘', '❀', '❁'
- '☘', '❀', '❁', '❀'
- Eclipse - From Raku Throbber post author
- '🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘'
- Dots - Option A requires ping / pong enabled script. Option B just adds the elements in the center.
11l
<lang 11l>L
L(rod) ‘\|/-’ print(rod, end' "\r") sleep(0.25)</lang>
Ada
<lang Ada>with Ada.Text_IO;
procedure Spinning_Rod is
use Ada.Text_IO;
type Index_Type is mod 4; Rod : constant array (Index_Type) of Character := ('|', '/', '-', '\'); Index : Index_Type := 0; Char : Character; Avail : Boolean;
begin
Put (ASCII.ESC & "[?25l"); -- Hide the cursor loop Put (ASCII.ESC & "[2J"); -- Clear Terminal Put (ASCII.ESC & "[0;0H"); -- Place Cursor at Top Left Corner Get_Immediate (Char, Avail); exit when Avail; Put (Rod (Index)); Index := Index_Type'Succ (Index); delay 0.250; end loop; Put (ASCII.ESC & "[?25h"); -- Restore the cursor
end Spinning_Rod;</lang>
AWK
<lang AWK>
- syntax: GAWK -f SPINNING_ROD_ANIMATION_TEXT.AWK
@load "time" BEGIN {
while (1) { printf(" %s\r",substr("|/-\\",x++%4+1,1)) sleep(.25) } exit(0)
} </lang>
BaCon
<lang qbasic>WHILE TRUE
PRINT CR$, TOKEN$("🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘", x); x = IIF(x>7, 1, x+1) SLEEP 250
WEND</lang>
Bash
<lang bash>while : ; do
for rod in \| / - \\ ; do printf ' %s\r' $rod; sleep 0.25; done
done</lang> (Added an indent in the printf to better see the spinning rod).
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).
Caché ObjectScript
<lang Caché ObjectScript>SPINROD
; spin 10 times with quarter-second wait for i = 1:1:10 { for j = 1:1:4 { set x = $case(j,1:"|",2:"/",3:"-",:"\") ; $char(8) backspaces on the terminal write $char(8)_x hang 0.25 } } quit</lang>
Emacs Lisp
<lang Lisp> (while t
(mapcar (lambda(n)(princ n)(sit-for 0.25) (backward-delete-char 1)) (list "\\" "|" "-" "/") ) )
</lang>
Factor
<lang factor>USING: calendar combinators.extras formatting io sequences threads ;
[
"\\|/-" [ "%c\r" printf flush 1/4 seconds sleep ] each
] forever</lang>
Forth
Tested in gforth 0.7.9 <lang forth>
- rod
cr begin [char] \ emit 250 ms 13 emit [char] | emit 250 ms 13 emit [char] - emit 250 ms 13 emit [char] / emit 250 ms 13 emit key? until
rod </lang>
FreeBASIC
<lang freebasic>' version 13-07-2018 ' compile with: fbc -s console
Dim As String spinning_rod = "|/-" + Chr(92) Dim As UInteger c
While InKey <> "" : Wend
While InKey = ""
Cls Print Print " hit any key to end program "; Chr(spinning_rod[c And 3]) c += 1 Sleep(250) ' in milliseconds
Wend
End</lang>
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>
Haskell
Uses the terminfo library to make the cursor invisible, if possible. <lang haskell>import Control.Concurrent (threadDelay) import Control.Exception (bracket_) import Control.Monad (forM_) import System.Console.Terminfo import System.IO (hFlush, stdout)
-- Use the terminfo database to write the terminal-specific characters -- for the given capability. runCapability :: Terminal -> String -> IO () runCapability term cap =
forM_ (getCapability term (tiGetOutput1 cap)) (runTermOutput term)
-- Control the visibility of the cursor. cursorOff, cursorOn :: Terminal -> IO () cursorOff term = runCapability term "civis" cursorOn term = runCapability term "cnorm"
-- Print the spinning cursor. spin :: IO () spin = forM_ (cycle "|/-\\") $ \c ->
putChar c >> putChar '\r' >> hFlush stdout >> threadDelay 250000
main :: IO () main = do
putStrLn "Spinning rod demo. Hit ^C to stop it.\n" term <- setupTermFromEnv bracket_ (cursorOff term) (cursorOn term) spin</lang>
Java
<lang java>public class SpinningRod {
public static void main(String[] args) throws InterruptedException { String a = "|/-\\"; System.out.print("\033[2J"); // hide the cursor long start = System.currentTimeMillis(); while (true) { for (int i = 0; i < 4; i++) { System.out.print("\033[2J"); // clear terminal System.out.print("\033[0;0H"); // place cursor at top left corner for (int j = 0; j < 80; j++) { // 80 character terminal width, say System.out.print(a.charAt(i)); } Thread.sleep(250); } long now = System.currentTimeMillis(); // stop after 20 seconds, say if (now - start >= 20000) break; } System.out.print("\033[?25h"); // restore the cursor }
}</lang>
JavaScript
Node JS: <lang javascript> const rod = (function rod() {
const chars = "|/-\\"; let i=0; return function() { i= (i+1) % 4; // We need to use process.stdout.write since console.log automatically adds a \n to the end of lines process.stdout.write(` ${chars[i]}\r`); }
})(); setInterval(rod, 250); </lang>
Julia
<lang julia>while true
for rod in "\|/-" # this needs to be a string, a char literal cannot be iterated over print(rod,'\r') sleep(0.25) end
end </lang>
Kotlin
<lang scala>// Version 1.2.50
const val ESC = "\u001b"
fun main(args: Array<String>) {
val a = "|/-\\" print("$ESC[?25l") // hide the cursor val start = System.currentTimeMillis() while (true) { for (i in 0..3) { print("$ESC[2J") // clear terminal print("$ESC[0;0H") // place cursor at top left corner for (j in 0..79) { // 80 character terminal width, say print(a[i]) } Thread.sleep(250) } val now = System.currentTimeMillis() // stop after 20 seconds, say if (now - start >= 20000) break } print("$ESC[?25h") // restore the cursor
}</lang>
Lua
<lang Lua> -- -- Simple String Animation - semi-hard-coded variant - you can alter the chars table - update the count and run it... --
-- The basic animation runtime controller. This is where you assign the active animation ( you could create a simple function to replace the animation table and nil count, index and expiry to extend this system to allow multiple animations - -- and since you can replace after a previous has been output, it would appear as though you were running different animations at the same time - that wouldn't be async compatible though )... -- So you can either activate the animation you want permanently, or create a simple function to update the animation table and reset the control variables... ie: ( function string.SetBasicAnimation( _tab_of_chars ) string.__basic_anim_runtime.anim = _tab_of_chars; string.__basic_anim_runtime.index = nil; string.__basic_anim_runtime.count = nil; string.__basic_anim_runtime.expiry = nil; end ) string.__basic_anim_runtime = { -- The animation - can not ping pong... requires full sequence. Resets to first after full sequence. Dots animation.. Requires center segment because of ping / pong anim = { '.', '..', '...', '..' };
-- Pipes animation - This performs a complete rotation, no need to add extra segments. -- anim = { '|', '/', '─', '\\' };
-- Stars - This is reversible so requires the center segments.. -- anim = { '⁎', '⁑', '⁂', '⁑' };
-- Clock - This still needs to be ordered... -- anim = { '🕛', '🕧', '🕐', '🕜', '🕑', '🕝', '🕒', '🕞', '🕓', '🕟', '🕔', '🕠', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕡', '🕢', '🕣', '🕤', '🕥', '🕦' };
-- Arrows - This does a complete circle and doesn't need to reverse -- anim = { '⬍', '⬈', '➞', '⬊', '⬍', '⬋', '⬅', '⬉' };
-- Bird Flying - this is reversible so it requires all.. 1 2 3 4 5 6 5 4 3 2 -- anim = { '︷', '︵', '︹', '︺', '︶', '︸', '︶', '︺', '︹', '︵' };
-- Plants - Set as reversible, requires all.. -- anim = { '☘', '❀', '❁', '❀' };
-- Eclipse - From Raku Throbber post author -- anim = { '🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘' }; };
--
-- The basic animator function - accepts a numerical delay and a boolean backwards switch.. It only accepts a single animation from the helper local table above..
--
-- Argument - _delay - <Number> - Accepts a time, in seconds with fraction support, that designates how long a frame should last. Optional. If no number given, it uses the default value of 1 / 8 seconds.
-- Argument - _play_backwards - <Boolean> - Toggles whether or not the animation plays backwards or forwards. Default is forwards. Optional.
--
-- RETURN: <String> - Character( s ) of the current animation frame - if the frame is invalid, it returns an empty string.
-- RETURN: <Boolean> - Has Advanced Frame Controller - set to true if this call resulted in a new frame / index being assigned with new character( s )
--
function string.BasicAnimation( _delay, _play_backwards )
-- Setup delay - make sure it is a number and Reference our runtime var
local _delay = ( ( type( _delay ) == 'number' ) and _delay or 1 / 8 ), string.__basic_anim_runtime, os.clock( );
-- cache our count so we count once per refresh. _data.count = ( type( _data.count ) == 'number' ) and _data.count or #_data.anim;
-- Setup our helpers... local _expiry, _index, _chars, _count, _has_advanced_frame = _data.expiry, ( _data.index or ( _play_backwards and _data.count or 1 ) ), _data.anim, _data.count, false;
-- If expiry has occurred, advance... Expiry can be nil the first call, this is ok because it will just use the first character - or the last if playing backwards. if ( _expiry and _expiry < _time ) then -- Advance.. _index, _has_advanced_frame = ( ( _index + ( 1 * ( _play_backwards and -1 or 1 ) ) ) % ( _count + 1 ) ), true;
-- If 0, add 1 otherwise keep the same. _index = _index < 1 and ( _play_backwards and _count or 1 ) or _index;
-- Update the index _data.index = _index; end
-- Update the trackers and output the char. -- Note: This is best done in the loop, but since we are checking the expiry above I decided to integrate it here. _data.expiry = ( not _data.expiry or _has_advanced_frame ) and _time + _delay or _data.expiry;
-- Return the character at the index or nothing. return _chars[ _index ] or , _has_advanced_frame; end
--
-- Helper / OPTIONAL FUNCTION - Updates the animation and resets the controlling variables
--
-- Argument: _tab -
- This is the table containing the animation characters... ie: { '.', '..', '...', '..' } would be a valid entry. Requires at least 2 entries. -- function string.SetBasicAnimation( _tab ) -- Prevent non tables, empty tables, or tables with only 1 entry. if not ( type( _tab ) == 'table' and #_tab > 1 ) then return error( 'Can not update basic animation without argument #1 as a table, with at least 2 entries...' ); end -- Helper local _data = string.__basic_anim_runtime; -- Update the animation table and Clear the controllers... _data.anim, _data.count, _data.index, _data.expiry = _tab, nil, nil, nil; end </lang> Usage: <lang Lua> -- 1 second delay, going backwards. print( string.BasicAnimation( 1, true ) ); -- Default delay going backwards print( string.BasicAnimation( nil, true ) ); -- Default delay, moving forwards print( string.BasicAnimation( ) ); -- 1 second delay going backwards print( string.BasicAnimation( 1 ) ); </lang>Lua - Extended Modular Variant
<lang Lua> -- -- Simple String Animation - Josh 'Acecool' Moser under modified ACL - Free to use, modify and learn from. --
--[[
This set of helpers is meant to be called every frame / render.. It can either be used this way and output via means of drawing the text directly, or by updating a panel and invalidating the layout, when the animation changes as a return allows you to do this.
A few recommendations:
Alter and add in an advance frame function - then you can have a separate system advance the frame of the unique id and make the clock tell time to the best of its limitations, or make the clock go backwards during an undo action..
You can set up a direction override so you can make the animation go in reverse and only in reverse for the undo action...
With the advance frame function you can advance the frame based on user interactions..
You can also improve the __anim_data table by making a single table within: [ _id ] = { dir = 1, pos = 1, paused = false, and_extend_it = true, cooldown = 0 }
etc.. etc.. etc...
I do hope this has been easy to understand and educational.
]]
-- -- Enumeration -- Here, we are simply setting up variable names with meaning where the value means nothing - the value must be unique for the table. This lets us reference the animation we want without having to remember 0, 1, 2, or some other number which makes code look messy. --
-- This is for the pipe animation | / ─ \ STRING_ANIMATION_PIPE = 0;
-- This is for the dot animation . .. ... .. and stacked stars STRING_ANIMATION_DOTS = 1;
-- Stacking stars STRING_ANIMATION_STAR_DOTS = 2;
-- This is for the clock animation STRING_ANIMATION_CLOCK = 3;
-- Arrow STRING_ANIMATION_ARROW = 4;
-- Bird in flight STRING_ANIMATION_BIRD = 5;
-- Flower STRING_ANIMATION_FLOWER = 6;
-- Eclipse - From Raku Throbber post author STRING_ANIMATION_ECLIPSE = 7;
-- Add more... You can also create a function to create enums quickly.. I may include that example at some point in the future.
--
-- Configuration
-- Basic configuration
--
-- The default delay between frames - the function used allows you to override this, as does the table which lets you set up a delay for that particular animation - this is just the default in case a delay has not been set for either location. As this value is only used as a fallback, it is set as local. local CFG_STRING_ANIMATION_DEFAULT_DELAY = 1 / 8;
-- The animation map which defines each animation, if it ping / pongs ( starts at beginning, animates through to end, animates through to start and repeats ) or not ( animates from start to end and resets ). -- Note: Count is set to -1 ( can also be 0 ) so that if you are working in an environment with live-update support the table will be recreated and the entries can be recounted ( this is really only needed if you remove local from the declaration, and do X = X or { } to allow re-using a pre-existing element in a live-update supported application.. Otherwise, count can be completely removed from this map and have it generated the first time string.Animate is called ) local MAP_TIME_ANIMATIONS = { [ STRING_ANIMATION_DOTS ] = { anim = { '.', '..', '...' }; reversible = true; delay = 1 / 3; count = -1; }; [ STRING_ANIMATION_PIPE ] = { anim = { '|', '/', '─', '\\' }; reversible = false; delay = 1 / 3; count = -1; }; [ STRING_ANIMATION_STAR_DOTS ] = { anim = { '⁎', '⁑', '⁂' }; reversible = true; delay = 1 / 8; count = -1; }; [ STRING_ANIMATION_CLOCK ] = { anim = { '🕛', '🕧', '🕐', '🕜', '🕑', '🕝', '🕒', '🕞', '🕓', '🕟', '🕔', '🕠', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕡', '🕢', '🕣', '🕤', '🕥', '🕦' }; reversible = false; delay = 1 / 8; count = -1; }; [ STRING_ANIMATION_ARROW ] = { anim = { '⬍', '⬈', '➞', '⬊', '⬍', '⬋', '⬅', '⬉' }; reversible = false; delay = 1 / 8; count = -1; }; [ STRING_ANIMATION_BIRD ] = { anim = { '︷', '︵', '︹', '︺', '︶', '︸' }; reversible = true; delay = 1 / 8; count = -1; }; [ STRING_ANIMATION_FLOWER ] = { anim = { '☘', '❀', '❁' }; reversible = true; delay = 1 / 8; count = -1; }; [ STRING_ANIMATION_ECLIPSE ] = { anim = { '🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘' }; reversible = false; delay = 1 / 8; count = -1; }; };
-- The default animation type if type isn't set, or if the animation doesn't exist in the table... MAP_TIME_ANIMATIONS.default = MAP_TIME_ANIMATIONS[ STRING_ANIMATION_DOTS ];
--
-- Runtime data - this table structure keeps track of the direction, which frame it is on, etc..
--
-- Animation data - We are extending the string library with an internal table using my notation. string.__anim_data = { -- Runtime animation data runtime = { -- Cooldown management - we only need time started and duration; from which we can calculate everything else. Another option for this simple system is to simply set the expiry. cooldowns = { -- default = { started = -1, duration = 0 }
-- Expiry only variant... So the time which the cooldown expires goes here. default = 0; };
-- The current index for a set id... IE: Which character we are currently showing. index = { default = 1; };
-- The current direction of travel for the animation ( moving forward through animation = 1, or moving backwards = -1 ). dir = { default = 1; };
-- Whether or not the animation is paused. paused = { default = false; }; } };
--
-- Runtime data - this table structure keeps track of the direction, which frame it is on, etc..
--
-- Animation data - We are extending the string library with an internal table using my notation. string.__anim_data = { -- Runtime animation data runtime = { -- Cooldown management - we only need time started and duration; from which we can calculate everything else. Another option for this simple system is to simply set the expiry. cooldowns = { -- default = { started = -1, duration = 0 }
-- Expiry only variant... So the time which the cooldown expires goes here. default = 0; };
-- The current index for a set id... IE: Which character we are currently showing. index = { default = 1; };
-- The current direction of travel for the animation ( moving forward through animation = 1, or moving backwards = -1 ). dir = { default = 1; };
-- Whether or not the animation is paused. paused = { default = false; }; } };
--
--Updates the pause state of the animation
--
function string.SetTextAnimationPauseState( _type, _id, _value )
string.__anim_data.runtime.paused[ _id || 'default' ] = ( isbool( _value ) && _value || false );
end
--
-- Returns the pause state, or returns false if nil
--
function string.GetTextAnimationPauseState( _type, _id )
return string.__anim_data.runtime.paused[ _id || 'default' ] || false;
end
--
-- Pauses the animation
--
function string.PauseTextAnimation( _type, _cycle_delay, _id )
string.SetTextAnimationPauseState( _type, _id, true );
end
--
-- Unpauses the animation
--
function string.UnPauseTextAnimation( _type, _cycle_delay, _id )
string.SetTextAnimationPauseState( _type, _id, false );
end
--
-- Toggles the pause state of the animation
--
function string.ToggleTextAnimation( _type, _cycle_delay, _id )
string.SetTextAnimationPauseState( _type, _id, !string.GetTextAnimationPauseState( _type, _id ) )
end
--
-- This keeps track of the animation by setting up the time tracking, checking it, etc..
-- It returns whether or not a cooldown exists. Basic time management. If a cooldown doesn't exist, and the delay argument is assigned, then it will create a new cooldown for that amount of time but it will return false so the frame can advance.
--
function string.HasAnimationCooldown( _id, _delay )
-- Fetch the animation data table..
local _data = string.__anim_data.runtime;
-- Fetch the cooldowns local _cooldowns = _data.cooldowns;
-- Fetch our cooldown, if it exists.. use default if no id given as this can be called outside the scope of the other function... local _cooldown = _cooldowns[ _id or 'default' ] or -1;
-- A helper which references the current time, in seconds, since the epoch. This should have decimal places? -- Note: os.time( ) locks you into 1 second intervals although 0 seconds would work though meaning every call would update the frame and become illegible if rendering many frames per second.. --local _time = os.time( );
-- This returns the time since Lua / PC started with millisecond precision. Since we don't need any sort of time output, this is perfect to allow animation that isn't restricted to whole second intervals. -- If we wanted to, we could also use this in conjunction with os.time( ) to get a more precise output by getting the current time.. -- -- There are many different options to do this... -- -- We could use math.fmod( os.cllock( ), 1 ) which returns the decimals of a number / float. -- We could math.floor this number and subtract it from itself, then add the remainder ( decimals ) to os.time( ) which would give us more precision in os.time. -- Another way is to subtract math.floor of this number from os.time( ) and then add this entire value to os.time value. Either way you end up with the same result - but this option means subtracting against a larger number. -- Another option is casting to string, exploding on ., recasting to number and dividing to put back into decimal format. -- We could use string matching via Patterns ( similar to RegEx ). -- There is also string.sub after finding the index of the decimal. -- -- etc.. etc.. etc... -- -- The best way is usually the simplest. We don't need to tell time - we just need to measure the passage of time, ie delta T. If we needed time, I'd probably use fmod, unless benchmarking showed it costs more than subtracting and adding. -- local _time = os.clock( );
-- Setup a helper to see if we have the cooldown.. If our cooldown is a larger number than our current time, we haven't passed it yet and it exists. local _has_cooldown = _cooldown > _time;
-- If we do not have a cooldown, but we have a delay assigned, setup a new cooldown if ( !_has_cooldown and type( _delay ) == 'number' ) then -- Create in the expiry format - time plus delay.... _cooldowns[ _id ] = _time + _delay; end
-- Return whether or not we have a cooldown; ignoring if we created one this frame. return _has_cooldown; end
--
-- Animations a string based on our registry and registry options - we are extending the default string library. Be careful when doing this and always ensure you aren't overwriting an important function. As string.Animation doesn't exist, we can add it - but if used we must monitor updates so that if it is added to Lua that we either change the name, or use the new function.
--
-- Basic Usage: This is meant to be called every render frame. You can also micro-cache the data as I went ahead and added the second return which tells you when the character was updated for use in elements that invalidate the layout ( and may cause flickering if you invalidate too frequently ), etc.. It can only update the frame characters if it is being called.
--
-- Argument: _type - <Number> - This is the ENUMerated STRING_ANIMATION_* which lets the function know which animation to reference from the MAP_ table.
-- Argument: _id - <String> - This isn't required - defaults to 'default'. If you want to run multiple animations at the same time, use it or you will advance frames at incorrect times for the animation - the cooldown will be set to whichever delay is called.. So it would be possible to have a delay of 0.5 and a delay of .99 and the first frame advance will happen at 0.5 seconds, then 0.99 will trigger. then 1.0 and 1.5 will be skipped.
-- Argument: _cycle_delay_override - <Number> - This is the delay you wish to use for the animation to advance the frame of the current animation. This is optional. By default it uses the MAP_ table delay in the animation table. If no animation is provided in the MAP_ table, or here, then the default CFG_ animation delay will be used.
--
-- RETURNS: _char - <String> - This returns the character( s ) for the current frame of the current animation as a string. You can append them to another string, or just display them.
-- RETURNS: _has_frame_advanced - <Boolean> - Returns true if the character( s ) ha(s/ve) changed since the last time the function was called.
--
function string.SimpleAnimation( _type, _id, _cycle_delay_override )
--
-- Notes: An id isn't required - if no id is provided, 'default' will be used. If you try running 2 separate animations with the same name, one will overwrite the other and the wrong frame will be shown. You can update the internal table to make it so each type has its own id if you want. But this example lets you set a unique id period. So you can show one animation with default, be done with it, and flip to another without having to worry about adding overhead by re-using an existing id.
--
-- Ensure the type provided is a number... This prevents it from running... or, as I've chosen - we test and if it doesn't work we use our default. -- assert( isnumber( _type ), '[ string.Animation ] _type must be a number!' );
-- Check the type and ensure it is a number. If it isn't or the map address doesn't exist, we use the default which is handled below. local _type = ( ( type( _type ) == 'number' and MAP_TIME_ANIMATIONS[ _type ] ) and _type or nil );
-- Setup the id to identify this animation as local _id = ( type( _id ) == 'string' and _id or 'default' );
-- The data local _data = ( MAP_TIME_ANIMATIONS[ _type ] or MAP_TIME_ANIMATIONS.default );
-- The animation table local _anim = _data.anim;
-- If we don't know how many elements there are to the animation, count the entries and cache the result so we don't have to do it again.
if ( !_data.count or _data.count < 1 ) then _data.count = #_data.anim; end
-- The characters. local _count = _data.count;
-- If there is nothing to animate - output nothing. if ( _count < 1 ) then return ; end
-- The animation runtime data table local _runtime = string.__anim_data.runtime;
-- This is the direction of travel - we use it to multiply against our +1 so it is + ( 1 * dir ) to make it add or subtract. local _dir = _runtime.dir[ _id ] or 1;
-- The runtime index table for all ids local _indexes = _runtime.index;
-- The runtime character index table for the id in question - the first character is used by default. local _index = _indexes[ _id ] or 1;
-- Has the frame advanced? local _has_frame_advanced = false;
-- Fetch whether or not the animation is paused... -- went ahead and added it. local _paused = string.GetTextAnimationPauseState( _type, _id );
-- This returns whether or not the cooldown exists, or has expired. It will automatically re-create it so the next frame this will return t rue if the delay is longer than a frame. local _has_cooldowm = string.HasAnimationCooldown( 'string_animation_' .. _id, ( type( _cycle_delay_override ) == 'number' and _cycle_delay_override or ( type( _runtime.delay ) == 'number' and _runtime.delay or CFG_STRING_ANIMATION_DEFAULT_DELAY ) ) );
-- If the text isn't paused, and there isn't a cooldown ( ie, the delay has expired and we can advance to the next frame ) then continue... if ( !_paused and !_has_cooldowm ) then -- If we actually have more than 1 character so that we can advance the frame and it not be a waste of time by simply advancing / reverting to the same frame - then we run the logic.... If there is only 1 frame ( why? ) then we skip this logic. if ( _count > 1 ) then -- Update the reference - this makes it so you can re-use ids with different animations and different count values and it will just wrap around to what it should be. _indexes[ _id ] = ( _index + _dir ) % ( _count + 1 );
-- Debugging, if you want to see it. -- print( '[ string.Animation ] Type: ' .. _type .. '; Delay: ' .. _cycle_delay_override .. '; ID: ' .. _id .. '; I: ' .. tostring( _index ) .. '; Char: ' .. tostring( _char ) .. '; Paused: ' .. tostring( _paused ) );
-- If we are at the limits - decide what to do based on options. This isn't necessary - we could simply use % notation if ( _indexes[ _id ] <= 1 or _indexes[ _id ] > _count + 1 ) then if ( _data.reversible ) then -- ping pong; _indexes[ _id ] = ( _indexes[ _id ] == 0 ) and _count - 1 or _indexes[ _id ];
-- Revert _runtime.dir[ _id ] = _dir * -1; else -- Reset _indexes[ _id ] = ( _indexes[ _id ] == 0 ) and 1 or _indexes[ _id ]; end end
-- Update the _index helper so that our character will be the current instead of having to wait until the next frame. _index = _indexes[ _id ] or 1;
-- The frame has advanced - update the var so we can use it to perform an action if necessary.. _has_frame_advanced = true; end end
-- The frame character( s ) local _char = _anim[ _index ] or ;
-- Return the character( s ) from the current animation frame return _char, _has_frame_advanced; end </lang>
Usage
<lang Lua> -- -- In some HUD element, or where text is output... such as 'Loading' ... try: -- This would be called every frame and would result in Loading., Loading.., Loading..., Loading.., Loading. --
-- This one will use a unique id: loading_dots and have a 1 second delay between frames. -- In this example print would be draw.Text or something along those lines. print( 'Loading' .. string.SimpleAnimation( STRING_ANIMATION_DOTS, 'loading_dots', 1 ) );
-- or
-- This one will use id default, and use the pre-defined delay which is 1/3 because it is defined in the map. If it wasn't, it would use the default of 1/8...
-- In this example print would be draw.Text or something along those lines.
print( 'Loading' .. string.SimpleAnimation( STRING_ANIMATION_DOTS ) );
</lang>
Alternatively: using the button example earlier with pseudo code and the new feature so we only update an element when the text changes as the button handles rendering itself and we don't want to cause flickering due to invalidating the layout when it isn't necessary...
<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 -- the largest then save the largest segment in another key so we can, based on font size, etc.. get the width / height of the resulting output so we can ensure our elements don't have to do ugly sliding action when rendering... --
--
-- Initialization function
--
function PANEL:Initialize( )
-- Create the button and set the base text ( assume first arg ) as the language key ( also assume it handles referencing the correct word in GetBaseText )
self.button = self:CreateButton( '#lang_loading' );
-- Ensure size is correct self.button:SizeToContents( );
-- Give it a little additional space -- Task: Create a helper function which returns the largest section of the animation so we can get the size and ensure we set the button to the max possible size self.button:SetWidth( self.button:GetWidth( ) + 25 ); end
--
-- Our render panel function
--
function PANEL:Paint( _w, _h )
-- Get the animation text and let us know if it has advanced
local _anim, _has_advanced = string.SimpleAnimation( STRING_ANIMATION_DOTS, 'button_loading_dots' );
-- If the animation text has changed - update the button... If we added enough buffer room it shouldn't need to be resized ( which would look ugly anyway; hence the need for a function to get the largest size and for optimization, check when the animation is created instead of when used )... -- OR simple check the first use - which would be too late for this example though... unless we resize to max as it happens then never shrink. if ( _has_advanced ) then -- Update the text - Note: Assume Set/Get BaseText is used to store text and isn't output or changed with SetText. self.button:SetText( self.button:GetBaseText( ) .. _anim );
-- Invalidate the button as it has changed self.button:Invalidate( );
-- Make sure the panel knows that something has changed within it. self:InvalidateLayout( ); end end </lang>
M2000 Interpreter
<lang M2000 Interpreter> Module Checkit {
n$=lambda$ n=1, a$="|/-\" -> { =mid$(a$, n, 1) n++ if n>4 then n=1 } \\ 1000 is 1 second Every 250 { \\ Print Over: erase line before print. No new line append. Print Over n$() }
} CheckIt
</lang>
<lang M2000 Interpreter> Module Checkit {
n=1 a$="|/-\" Every 250 { Print Over mid$(a$, n, 1) n++ if n>4 then n=1 }
} CheckIt
</lang>
MelonBasic
<lang MelonBasic>Wait:0.25 Delete:1 Say:/ Wait:0.25 Delete:1 Say:- Wait:0.25 Delete:1 Say:\ Wait:0.25 Delete:1 Goto:1</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>
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 MiniMicro: <lang MiniScript>print "Press control-C to exit..." while true
for c in "|/-\" text.setCell 0, 0, c wait 0.25 end for
end while</lang>
And here's a version that will work with command-line MiniScript, running on a terminal that interprets standard VT100 escape sequences: <lang MiniScript>while true
for c in "|/-\" print c wait 0.25 print char(27) + "[2A" // move cursor up 2 lines end for
end while</lang>
Nim
<lang Nim>import std/monotimes, times, os
const A = ["|", "/", "—", "\\"] stdout.write "$\e[?25l" # Hide the cursor. let start = getMonoTime() while true:
for s in A: stdout.write "$\e[2J" # Clear terminal. stdout.write "$\e[0;0H" # Place cursor at top left corner. for _ in 1..40: stdout.write s & ' ' stdout.flushFile os.sleep(250) let now = getMonoTime() if (now - start).inSeconds >= 5: break
echo "$\e[?25h" # Restore the cursor.</lang>
NS-HUBASIC
The 0.25 second delay assumes the program is running at 60 frames per second. <lang NS-HUBASIC>10 DIM A(4) 20 A(1)=236 30 A(2)=234 40 A(3)=235 50 A(4)=233 60 FOR I=1 TO 4 70 CLS 80 PRINT CHR$(A(I)) 90 PAUSE 15 100 NEXT 110 GOTO 60</lang>
Perl
The statement $| =1
is required in order to disable output buffering.
<lang perl>$|= 1;
while () {
for (qw[ | / - \ ]) { select undef, undef, undef, 0.25; printf "\r ($_)"; }
}</lang>
Phix
<lang Phix>puts(1,"please_wait... ") cursor(NO_CURSOR) for i=1 to 10 do -- (approx 10 seconds)
for j=1 to 4 do printf(1," \b%c\b",`|/-\`[j]) sleep(0.25) end for
end for puts(1," \ndone") -- clear rod, "done" on next line </lang>
PicoLisp
<lang Lisp> (de rod ()
(until () (for R '(\\ | - /) (prin R (wait 250) "\r")(flush) ) ) )
(rod) </lang>
Python
<lang python>from time import sleep while True:
for rod in r'\|/-': print(rod, end='\r') sleep(0.25)</lang>
Racket
<lang racket>
- lang racket
(define (anim)
(for ([c "\\|/-"]) (printf "~a\r" c) (sleep 0.25)) (anim))
(anim) </lang>
Raku
(formerly Perl 6)
Traditionally these are know as throbbers or progress indicators.
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.
<lang perl6>class throbber {
has @.frames; has $.delay is rw = 0; has $!index = 0; has Bool $.marquee = False; method next { $!index = ($!index + 1) % +@.frames; sleep $.delay if $.delay; if $!marquee { ("\b" x @.frames) ~ @.frames.rotate($!index).join; } else { "\b" ~ @.frames[$!index]; } }
}
my $rod = throbber.new( :frames(< | / - \ >), :delay(.25) ); print "\e[?25lLong running process... "; print $rod.next for ^20;
my $clock = throbber.new( :frames("🕐" .. "🕛") ); print "\r\nSomething else with a delay... "; until my $done {
# do something in a loop; sleep 1/12; print "\b", $clock.next; $done = True if $++ >= 60;
}
my $moon = throbber.new( :frames('🌑🌒🌓🌔🌕🌖🌗🌘'.comb) ); print "\r\nGonna be a long night... "; until my $end {
# do something in a loop; sleep 1/8; print "\b", $moon.next; $end = True if $++ >= 60;
}
my $scroll = throbber.new( :frames('PLEASE STAND BY... '.comb), :delay(.1), :marquee ); print "\r\nEXPERIENCING TECHNICAL DIFFICULTIES: { $scroll.frames.join }"; print $scroll.next for ^95;
END { print "\e[?25h\n" } # clean up on exit</lang>
REXX
This REXX program would work for all REXXes if there was a common way to sleep (suspend) execution for fractional seconds.
This REXX version will work for:
- Personal REXX
- PC REXX
- R4 REXX
- ROO REXX
- Regina REXX (see the programming note below.)
<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.*/
else bs= "08"x /* ASCII? " " " " " */
signal on halt /*jump to HALT when user halts pgm.*/ $= '│/─\' /*the throbbing characters for display.*/
do j=1 /*perform until halted by the user. */ call charout , bs || substr($, 1 + j//length($), 1) call delay .25 /*delays a quarter of a second. */ if result==1 then leave /*see if HALT was issued during DELAY*/ end /*j*/
halt: say bs ' ' /*stick a fork in it, we're all done. */</lang>
Programming note: this REXX program makes use of DELAY BIF which delays (sleeps) for a specified amount of seconds.
Some REXXes don't have a DELAY BIF, so one is included here ──► DELAY.REX.
Ring
<lang ring>load "stdlib.ring" rod = ["|", "/", "-", "\"] for n = 1 to len(rod)
see rod[n] + nl sleep(0.25) system("cls")
next</lang> Output:
| / - \
Ruby
<lang ruby>def spinning_rod
begin printf("\033[?25l") # Hide cursor %w[| / - \\].cycle do |rod| print rod sleep 0.25 print "\b" end ensure printf("\033[?25h") # Restore cursor end
end
puts "Ctrl-c to stop." spinning_rod </lang>
Rust
<lang rust>fn main() {
let characters = ['|', '/', '-', '\\']; let mut current = 0;
println!("{}[2J", 27 as char); // Clear screen.
loop { println!("{}[;H{}", 27 as char, characters[current]); // Move cursor to 1,1 and output the next character. current += 1; // Advance current character. if current == 4 {current = 0;} // If we reached the end of the array, start from the beginning. std::thread::sleep(std::time::Duration::from_millis(250)); // Sleep 250 ms. }
}</lang>
Scala
<lang Scala>object SpinningRod extends App {
val start = System.currentTimeMillis
def a = "|/-\\"
print("\033[2J") // hide the cursor
while (System.currentTimeMillis - start < 20000) { for (i <- 0 until 4) { print("\033[2J\033[0;0H") // clear terminal, place cursor at top left corner for (j <- 0 until 80) print(a(i)) // 80 character terminal width, say Thread.sleep(250) } } print("\033[?25h") // restore the cursor
}</lang>
ScratchScript
<lang ScratchScript>print "|" delay 0.25 clear print "/" delay 0.25 clear print "-" delay 0.25 clear print "\" delay 0.25</lang>
SimpleCode
<lang SimpleCode>dtxt | wait 0.25 ctxt reset dtxt / wait 0.25 ctxt reset dtxt - wait 0.25 ctxt reset dtxt \ wait 0.25</lang>
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. <lang Wee Basic>let loop=1 sub delay: for i=1 to 10000 next cls 1 return while loop=1 print 1 "l" gosub delay: print 1 "/" gosub delay: print 1 "-" gosub delay: print 1 "\" gosub delay: wend end</lang>
Wren
<lang ecmascript>import "io" for Stdout import "timer" for Timer
var ESC = "\u001b"
var a = "|/-\\" System.write("%(ESC)[?25l") // hide the cursor var start = System.clock var asleep = 0 while (true) {
for (i in 0..3) { System.write("%(ESC)[2J") // clear terminal System.write("%(ESC)[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 } var now = System.clock // stop after 20 seconds, say if (now * 1000 + asleep - start * 1000 >= 20000) break
} System.print("%(ESC)[?25h") // restore the cursor</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).
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. <lang zkl>fcn spin{ // this will be a thread that displays spinner
try{ foreach n,rod in ((1).MAX, "\\|/-"){ print(" ",rod,"\r");
Atomic.sleep(0.25);
} }catch{} // don't complain about uncaught exception that stops thread
}</lang> <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</lang>
ZX Spectrum Basic
<lang ZX Basic>10 LET A$="|/-\" 20 FOR C=1 TO 4 30 PRINT AT 0,0;A$(C) 40 PAUSE 4 50 NEXT C 60 GOTO 20 </lang>
- Programming Tasks
- Spinning rod animation/Text
- 11l
- Ada
- AWK
- BaCon
- Bash
- C
- C Shell
- Caché ObjectScript
- Emacs Lisp
- Factor
- Forth
- FreeBASIC
- GlovePIE
- Go
- Haskell
- Java
- JavaScript
- Julia
- Kotlin
- Lua
- Lua - Extended Modular Variant
- M2000 Interpreter
- MelonBasic
- Microsoft Small Basic
- MiniScript
- Nim
- NS-HUBASIC
- Perl
- Phix
- PicoLisp
- Python
- Racket
- Raku
- REXX
- Ring
- Ruby
- Rust
- Scala
- ScratchScript
- SimpleCode
- Wee Basic
- Wren
- Zkl
- ZX Spectrum Basic