106
16
Your challenge is to make an infinite loading screen, that looks like this:
Or, to be more specific:
- Take no input.
- Output
Loading...
, with a trailing space, but no trailing newline. - Infinitely cycle through the chars
|
,/
,-
and\
: every 0.25 seconds, overwrite the last one with the next in the sequence. You can overwrite just the last character, or delete and rewrite the whole line, as longLoading...
remains unchanged.
Rules
- The output text must look exactly as specified. Trailing newlines/spaces are acceptable.
- You should not wait 0.25 seconds before initially showing output - the first frame should be printed as soon as the program is run.
- Your program should be able to run indefinitely. For example, if you use a counter for frames, the counter should never cause an error by exceeding the maximum in your language.
- Although the waiting period between each "frame" should be 0.25 seconds, obviously this will never be exact - an error margin of 10% or so is allowed.
- You may submit a function, but it must print to
stdout
. - You can submit an answer in a non-console (but still text-based) environment, as long as it is capable of producing the loading animation.
- This is code-golf, so the shortest solution (in bytes) wins. Standard code-golf loopholes apply.
- If possible, please provide a gif of your loading screen in action.
Example
Here is the C++ code I used to create the example (ungolfed):
#include <iostream>
#include <string>
#include <thread>
using namespace std;
int main() {
string cycle = "|/-\\";
int i = 0;
cout << "Loading... ";
while (true) {
// Print current character
cout << cycle[i];
// Sleep for 0.25 seconds
this_thread::sleep_for(chrono::milliseconds(250));
// Delete last character, then increase counter.
cout << "\b";
i = ++i % 4;
}
}
May the best golfer win!
3Can submissions wait 0.25 seconds before initially displaying output? – ETHproductions – 2016-11-27T20:42:02.277
2No, but thanks for mentioning that, I'll add it to the rules @ETHproductions – FlipTack – 2016-11-27T20:43:09.787
Is a trailing newline (after the animating symbol) acceptable? – Copper – 2016-11-27T20:43:58.183
Of course :) @Copper – FlipTack – 2016-11-27T20:44:52.513
@FlpTkc what if you have the slowest language ever? – Destructible Lemon – 2016-11-27T22:08:19.500
@DestructibleWatermelon as long as somewhere in your code there is something like
sleep( 1/4 )
, i.e. your algorithm is correct, the amount of time that the other operations take isn't overly relevant, as long as the animation will actually run. Or, get a faster language :P – FlipTack – 2016-11-27T22:10:20.897I was just thinking really hard, why your example program prints \ two times in a row. :) – raznagul – 2016-11-28T13:56:10.030
@raznagul you need the double backslash, otherwise C++ thinks I'm trying to use a control character :P – FlipTack – 2016-11-28T15:11:01.627
I didn't know characters could be deleted from the console once printed. Is
\b
cross-platform\language? I tried printing(print "Hello\bWorld")
in a Clojure REPL, and it printed a garbage character for the backspace. – Carcigenicate – 2016-11-29T00:19:47.577Nvm. It has to be running in a windows console, not a REPL, to work. – Carcigenicate – 2016-11-29T00:32:34.747
For non-stdout implementations, does the font have to be monospace? – Ben Aubin – 2016-11-29T03:27:32.973
@penne12 No, you can use the default font – FlipTack – 2016-11-29T06:50:36.630
May I substitute
—
for-
? – Adám – 2016-11-29T08:49:14.607@Adám yes, that's acceptable - sorry for the late reply. – FlipTack – 2016-11-30T17:42:27.713
Can we start at any position in the animation? – Titus – 2016-12-08T15:40:43.283
Yes, as long as you cycle through the right characters @Titus – FlipTack – 2016-12-08T16:13:39.777
Does "run forever" mean the CS sense (where the halting problem comes into play), or just run forever normally? – Buffer Over Read – 2016-12-15T03:35:28.647
1@TheBitByte it means that, theoretically, nothing inside your program will cause it to error - such as a counter overflowing or reaching maximum recursion depth. – FlipTack – 2016-12-15T06:57:23.383
Could you add a scoreboard/leader board snippet? – ckjbgames – 2017-02-01T17:52:39.727