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!
-
4\$\begingroup\$ Can submissions wait 0.25 seconds before initially displaying output? \$\endgroup\$ETHproductions– ETHproductions2016年11月27日 20:42:02 +00:00Commented Nov 27, 2016 at 20:42
-
2\$\begingroup\$ No, but thanks for mentioning that, I'll add it to the rules @ETHproductions \$\endgroup\$FlipTack– FlipTack2016年11月27日 20:43:09 +00:00Commented Nov 27, 2016 at 20:43
-
\$\begingroup\$ Is a trailing newline (after the animating symbol) acceptable? \$\endgroup\$Copper– Copper2016年11月27日 20:43:58 +00:00Commented Nov 27, 2016 at 20:43
-
\$\begingroup\$ Of course :) @Copper \$\endgroup\$FlipTack– FlipTack2016年11月27日 20:44:52 +00:00Commented Nov 27, 2016 at 20:44
-
1\$\begingroup\$ @TheBitByte it means that, theoretically, nothing inside your program will cause it to error - such as a counter overflowing or reaching maximum recursion depth. \$\endgroup\$FlipTack– FlipTack2016年12月15日 06:57:23 +00:00Commented Dec 15, 2016 at 6:57
112 Answers 112
PHP, 58 bytes
for(;;usleep(25e4))echo"\rLoading... ","\\|/-"[$i=++$i%4];
uses carriage return = overwrites the whole line. Run with -r.
-
\$\begingroup\$ @user59178: The assignment is needed to avoid integer overflow. Can you tell me how to run code with single and double quotes with
-r? \$\endgroup\$Titus– Titus2016年11月28日 14:29:01 +00:00Commented Nov 28, 2016 at 14:29 -
\$\begingroup\$ Those are both excellent points, I probably should have actually tried running it, rather than just looking at the code.:-) \$\endgroup\$user59178– user591782016年11月28日 15:36:03 +00:00Commented Nov 28, 2016 at 15:36
-
1\$\begingroup\$ On 64-Bit php, it would take over 73 billion years to overflow. I think that's acceptably close to forever. Also, 57, no
-rrequired:Loading... <?for(;;usleep(25e4))echo'\|/-'[$i=++$i%4],~÷;\$\endgroup\$primo– primo2016年12月02日 12:45:38 +00:00Commented Dec 2, 2016 at 12:45 -
\$\begingroup\$ @primo: I tried
^H, but it doesn´t seem to expand to chr(8) everywhere. And no idea what it depends on. \$\endgroup\$Titus– Titus2016年12月08日 15:52:22 +00:00Commented Dec 8, 2016 at 15:52 -
\$\begingroup\$ If using
-r, it will likely depend on the terminal. \$\endgroup\$primo– primo2016年12月08日 19:54:12 +00:00Commented Dec 8, 2016 at 19:54
Pascal, (削除) 116 (削除ここまで) (削除) 114 (削除ここまで) (削除) 107 (削除ここまで) 105 bytes
where is my head.. Thanks to @manatwork for shaving few bytes!
uses crt;var c:char;begin while 1=1do for c in'|/-\'do begin Write(#13'Loading... ',c);Delay(250)end;end.
Ungolfed:
uses
crt; // CRT unit has Delay function
var
c: char;
begin
while 1=1 do
for c in '|/-\' do
begin
Write(#13'Loading... ', c);
Delay(250)
end;
end.
-
1\$\begingroup\$
True→1=1and that way you can remove the following space too. \$\endgroup\$manatwork– manatwork2016年11月28日 10:26:44 +00:00Commented Nov 28, 2016 at 10:26 -
1\$\begingroup\$ Better use a single output statement rewriting the entire line:
Write(#13'Loading... ',c);. (BTW, no need for,between character and string literals.) \$\endgroup\$manatwork– manatwork2016年11月28日 10:32:51 +00:00Commented Nov 28, 2016 at 10:32 -
\$\begingroup\$ Oh, and no need for the
;in front of 1stend. \$\endgroup\$manatwork– manatwork2016年11月28日 10:38:03 +00:00Commented Nov 28, 2016 at 10:38 -
\$\begingroup\$ Got it. have to eat something.... \$\endgroup\$hdrz– hdrz2016年11月28日 10:57:54 +00:00Commented Nov 28, 2016 at 10:57
Java, (削除) 173 (削除ここまで) 115 bytes
- Version 2.0
Changed to lambda function/Thanks to @Xanderhall and @manatwork/115 bytes:
()->{System.out.print("Loading... ");for(int i=0;;Thread.sleep(250))System.out.print("\b"+"\\|/-".charAt(i++&3));}
- Version 1.0
Initial Version/173 bytes:
class A{public static void main(String[]a)throws Exception{System.out.print("Loading... ");for(int i=0;;){System.out.print("\b"+"\\|/-".charAt(i++%4));Thread.sleep(250);}}}
-
6\$\begingroup\$ "Your program should be able to run indefinitely." – This will terminate when reaches
Integer.MAX_VALUEafter 17++ years with "java.lang.StringIndexOutOfBoundsException: String index out of range: -3". \$\endgroup\$manatwork– manatwork2016年11月28日 11:08:39 +00:00Commented Nov 28, 2016 at 11:08 -
\$\begingroup\$ Can't test but will it
i&3do? \$\endgroup\$Linnea Gräf– Linnea Gräf2016年11月28日 14:27:26 +00:00Commented Nov 28, 2016 at 14:27 -
1\$\begingroup\$ Also, you are allowed to submit a function. You can get rid of the class declaration and just have
void a(){myfunction}format. \$\endgroup\$Xanderhall– Xanderhall2016年11月28日 14:39:29 +00:00Commented Nov 28, 2016 at 14:39
Ruby, (削除) 66 (削除ここまで) (削除) 59 (削除ここまで) (削除) 58 (削除ここまで) 57 bytes
I saved 7 bytes when I remembered ruby's loop syntax. -1 byte thanks to manatwork (changed print to $><<)! -1 byte thanks to daniero!
loop{$><<"Loading... #{'|/-\\'[$.=-~$.%4]}\r";sleep 0.25}
Decently self-explanatory. (削除) Uses the nice fact that I had to rework the string, so now the '...' strings don't need to have double-escapes (削除ここまで)\ is at the end and must be escaped.
-
\$\begingroup\$
print→$><<\$\endgroup\$manatwork– manatwork2016年11月28日 09:30:47 +00:00Commented Nov 28, 2016 at 9:30 -
\$\begingroup\$ @manatwork Oh, cool! \$\endgroup\$Conor O'Brien– Conor O'Brien2016年11月28日 12:09:21 +00:00Commented Nov 28, 2016 at 12:09
-
Noodel, noncompeting (削除) 24 (削除ここまで) 25 bytes
Cannot compete because Noodel was born after the challenge.
(削除)
"|gAĖọẸ.?a5‘|/-\+ʂḷạÇḍ/4
(削除ここまで)
Had to add a byte because messed up the compression algorithm:(
"Loading...¤‘|/-\+ʂḷạÇḍ/4
How it works
"Loading...¤ # Creates a string that is "Loading...¤" that is placed into the pipe.
‘|/-\ # Creates a character array ["|", "/", "-", "\"]
+ʂ # Adds two items in the pipe which will add the string to each character in the array. The 'ʂ' forces it to prepend rather than append.
ḷ # Unconditionally Loop everything up until a new line or end of program.
ạ # Basic animation, iterates through an object moving to the next index every call on that object and returns what is at that index.
Ç # Clears the screen then prints what is in the front of the pipe and dumps what was displayed.
ḍ/4 # Delays for the specified amount of time (/4 => 1/4 => 0.25s)
Stepping The Pipe
-->
--> "Loading...¤"
--> ["|", "/", "-", "\"], "Loading...¤"
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]
Loop ----------------------------------------------------------------------------------------
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]
--> "Loading...¤|", ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:0>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:0>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:0>
Loop ----------------------------------------------------------------------------------------
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:0>
--> "Loading...¤/", ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:1>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:1>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:1>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:1>
Loop ----------------------------------------------------------------------------------------
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:1>
--> "Loading...¤-", ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:2>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:2>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:2>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:2>
Loop ----------------------------------------------------------------------------------------
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:2>
--> "Loading...¤\", ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:3>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:3>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:3>
--> ["Loading...¤|", "Loading...¤/", "Loading...¤-", "Loading...¤\"]<frame:3>
There currently is not a version of Noodel that supports the syntax used in this answer. Here is a script that is supported:
23 bytes
Loading...¤"Ƈḟḋḣ+sḷạÇḍq
<div id="noodel" code="Loading...¤"Ƈḟḋḣ+sḷạÇḍq" input="" cols="12" rows="2"></div>
<script src="https://tkellehe.github.io/noodel/release/noodel-1.1.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>
-
\$\begingroup\$ This is a pretty neat language. Welcome to PPCG :) \$\endgroup\$FlipTack– FlipTack2016年12月28日 00:01:24 +00:00Commented Dec 28, 2016 at 0:01
-
\$\begingroup\$ @FlipTack Thank you:) This place is like a giant playground:)lol \$\endgroup\$tkellehe– tkellehe2016年12月28日 00:40:52 +00:00Commented Dec 28, 2016 at 0:40
TI-BASIC, 65 bytes
While 1:remainder(A+1,4)→A:Output(1,1,"Loading... "+sub("|/-\",A+1,1:Wait 1/4:End
TI CONNECT CE sadly does not allow screen recording so you'll have to enjoy my amazing camera skills instead.
Ungolfed:
While 1
remainder(A+1,4)→A
Output(1,1,"Loading... "+sub("|/-\",A+1,1
Wait 1/4
End
Explained:
While 1 Loop through everything forever
remainder(A+1,4)→A Set A to (A+1) mod 4
Output(1,1 Print at the first row, first column
"Loading..." Loading...
+ Concatenate string to...
sub( .... ,A+1,1 The A+1th characer of...
"|/-\" This string
Wait 1/4 Pause for 0.25 seconds
End End loop, go back to while.
According to this meta post, TI-BASIC is scored by token byte count, not characters. While is stored as a 1 byte token, and so is Output, while remainder is two bytes. It's kind of hard to count the tokens, so the best way is just to go to TI CONNECT and see how much space the program takes on the calculator.
Befunge, 60 bytes
" ...gnidaoL">:#,_v
"|/"<v*:*6"}",,8<
"\-"^>:#->#1_$:#^ _
Since Befunge doesn't have anything like a sleep command, the delay is approximated with a long running loop. This will obviously need to be adjusted depending on the speed of the system on which it is run.
You can test it on the codingground website.
Wonder, 50 bytes
f\@(ol ++"�Loading... ":#0"\|/-";%%25e4;f +1#0);f0
Replace � with the actual carriage return \r.
Explanation
f\@(...);f0: Infinitely recursive function f.
:#0"\|/-": Modular indexing using the function argument and the string "\|/-".
ol ++"�Loading... ": Return cursor to beginning of line, concatenate previous result to Loading... , and output.
%%25e4: Sleep for 250000 nanoseconds.
f +1#0: Call f on the argument incremented.
-
\$\begingroup\$ Would this reach maximum recursion depth and crash? \$\endgroup\$FlipTack– FlipTack2016年11月29日 06:51:42 +00:00Commented Nov 29, 2016 at 6:51
-
\$\begingroup\$ I don't think so, although I haven't tried running it for very long. Sort of weird, given that I should know how the interpreter works. \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年11月29日 06:56:07 +00:00Commented Nov 29, 2016 at 6:56
C#, (削除) 165 (削除ここまで) 123 bytes
Quite a few bytes saved thanks to raznagul !
z=>{Console.Write("Loading... ");for(int i=0;;i++){Console.Write(@"|/-\"[i%=4]+"\b");System.Threading.Thread.Sleep(250);}};
Anonymous function with no return type. The integer parameter z is only used as a placeholder in order to slash 1 byte off.
If input was allowed, the value 0 (or any other multiple of 4 to start from the same character) could be used as the iterator, reducing the byte count to 116 bytes:
i=>{Console.Write("Loading... ");for(;;i++){Console.Write(@"|/-\"[i%=4]+"\b");System.Threading.Thread.Sleep(250);}};
Full program with ungolfed function:
using System;
public class Program
{
public static void Main()
{
Action<int> a = z =>
{
Console.Write("Loading... ");
for (int i=0;;i++)
{
Console.Write(@"|/-\"[i%=4]+"\b");
System.Threading.Thread.Sleep(250);
}
};
a(0);
}
}
No GIF for now, since I'm having trouble with some dependencies on a slightly older Linux distro... The cursor is displayed on the last character, this behavior can be changed by adding a space in the Loading... string.
-
\$\begingroup\$ I think this doesn't fulfill the third rule a
i++will eventually throw an overflow exception. \$\endgroup\$raznagul– raznagul2016年11月28日 15:41:28 +00:00Commented Nov 28, 2016 at 15:41 -
2\$\begingroup\$ It won't overflow, since
iis assigned the modulo of division by 4 when printing the line (i%=4). \$\endgroup\$adrianmp– adrianmp2016年11月28日 15:57:00 +00:00Commented Nov 28, 2016 at 15:57 -
1\$\begingroup\$ +1 this is indeed clever. You can save a lot of bytes by printing
Loadingonce and then printing "\b" instead of setting the cursor position. Also you don't have to definer. You can use@"|/-\"[...]instead. \$\endgroup\$raznagul– raznagul2016年11月28日 16:10:26 +00:00Commented Nov 28, 2016 at 16:10 -
1\$\begingroup\$ I don't know if it is allowed. But you add on byte for the initialization of
zand then youzinstead ofi, in total saving 6 bytes. \$\endgroup\$raznagul– raznagul2016年11月28日 16:16:24 +00:00Commented Nov 28, 2016 at 16:16
Befunge 98, 61 bytes
Requires the HRTI (High Resolution Timers) fingerprint.
"ITRH"4(v
"g2:%4+1<,M',*93,aj*-d0`T**::?'M,kb"Loading...
|/-\
Waits 250047 (63^3) microseconds using a busy loop, using the M (mark) and T (returns the number of microseconds since the last mark) instructions from the "HRTI" fingerprint.
After each line, it outputs "\n\eM", to force a flush, and reposition the cursor.
Version for terminals using 8-bit encodings with proper support for C1 control codes (59 bytes):
"ITRH"4(v
"g2:%4+1<,+f~',aj*-d0`T**::?'M,kb"Loading...
|/-\
This version outputs "\n\x8d" after each line. "\x8d" is the 8-bit equivalent of 7-bit "\eM", and is supported by e.g: xterm +u8 (xterm not in UTF-8 mode).
Batch, (削除) 88 (削除ここまで) (削除) 81 (削除ここまで) 77 bytes
:1
@FOR %%G IN (/,-,,円^|) DO @(echo Loading...%%G
timeout/t 1 >a
cls)
@goto 1
(My first answer on Code Golf...)
-
\$\begingroup\$ The
@echo offline is costing you ten bytes. Would it be cheaper to simply place an@on each command individually? Or is there some reason that doesn't work? \$\endgroup\$user62131– user621312016年12月04日 08:48:11 +00:00Commented Dec 4, 2016 at 8:48 -
\$\begingroup\$ @ais523 It's giving me some errors... I'm working on it \$\endgroup\$undo– undo2016年12月04日 08:50:11 +00:00Commented Dec 4, 2016 at 8:50
-
\$\begingroup\$ @ais523 Done! Turns out if you're using
DO ( )withFOR, you just need to put a single@:DO @( )\$\endgroup\$undo– undo2016年12月04日 08:57:00 +00:00Commented Dec 4, 2016 at 8:57 -
1\$\begingroup\$ Since nobody's said it, welcome to code-golf! Nice answer \$\endgroup\$FlipTack– FlipTack2016年12月04日 16:29:40 +00:00Commented Dec 4, 2016 at 16:29
-
\$\begingroup\$ Still better than my 100-byte bash answer! \$\endgroup\$ckjbgames– ckjbgames2017年02月03日 15:13:52 +00:00Commented Feb 3, 2017 at 15:13
Turing machine simulator, 189 bytes
0 * L r 1
1 * o r 2
2 * a r 3
3 * d r 4
4 * i r 5
5 * n r 6
6 * g r 7
7 * . r 8
8 * . r 9
9 * . r A
A * * r B
B - \ * C
B / - * C
B | / * C
B * | * C
C * * * D
D * * * E
E * * * F
F * * * B
Do not run on full speed, or it will not wait for ~0.25 s1.
1Dunno if actually within 10% of 0.25 s. Last 4 lines do the waiting job approximately.
PowerShell, (削除) 116 (削除ここまで) (削除) 77 (削除ここまで) (削除) 69 (削除ここまで) 67 bytes
for(){[char[]]'|/-\'|%{write-host -n `rLoading... $_;sleep -m 250}}
cleaner than cls?
-
1\$\begingroup\$ Welcome to PPCG! \$\endgroup\$FlipTack– FlipTack2016年12月15日 19:14:05 +00:00Commented Dec 15, 2016 at 19:14
C, (削除) 119 (削除ここまで) (削除) 118 (削除ここまで) (削除) 102 (削除ここまで) (削除) 91 (削除ここまで) 88 bytes
#import<windows.h>
i;f(){for(;;)Sleep(19*printf("\rLoading... %c","\\|/-"[i=(i+1)%4]));}
-
1\$\begingroup\$ Could
char *cbechar*c? \$\endgroup\$Adalynn– Adalynn2016年11月28日 21:20:40 +00:00Commented Nov 28, 2016 at 21:20 -
1\$\begingroup\$ Can you move the assignment into the index,
for(;;){printf("\b%c",c[i=i>2?0:i+1])...? \$\endgroup\$Adalynn– Adalynn2016年11月28日 21:39:03 +00:00Commented Nov 28, 2016 at 21:39 -
1\$\begingroup\$ Have a look at my submission...
i=i>2?0:i+1is identicallyi=(i+1)%4. He can use incrementation in the index, then modulo in the loop. \$\endgroup\$LambdaBeta– LambdaBeta2016年11月28日 21:41:08 +00:00Commented Nov 28, 2016 at 21:41 -
1\$\begingroup\$ He can also save a byte (I think) by completely flushing with
\rso he doesn't need the extra space in the "Loading... " string. \$\endgroup\$LambdaBeta– LambdaBeta2016年11月28日 21:42:50 +00:00Commented Nov 28, 2016 at 21:42 -
1\$\begingroup\$ I don't know C, but would
i=++i%4work? \$\endgroup\$FlipTack– FlipTack2016年12月28日 18:04:57 +00:00Commented Dec 28, 2016 at 18:04
ES8 + HTML, 68 bytes
setInterval(e=>a.innerHTML='|/-\\'[++i%4],i=250)
Loading... <a id=a>-
C89, 71 bytes
main(i){for(;;)usleep(4<<printf("Loading... %c\n[A","/-\\|"[i++&3]));}
Using printable characters the format argument to printf(3) would be "Loading... %c\n\x1b[A".
C (gcc) -O3, 65 bytes
The same but as a function. Requires tail-call optimization, otherwise it would overflow the stack eventually.
i;f(){f(usleep(4<<printf("Loading... %c\n[A","/-\\|"[i++&3])));}
/* main(){f();} */
C++11, (削除) 209 (削除ここまで) (削除) 207 (削除ここまで) (削除) 180 (削除ここまで) (削除) 175 (削除ここまで) 164 bytes
This might have been a good use for the new C++ literals, saving std::chrono::milliseconds(250) to just write 250ms or 0.25s, but unfortunately this requires using namespace std::chrono; which is longer in the end.
-2 bytes thanks to Flp.Tkc for using #import instead of #include. Saving lot more thanks to Flp.Tkc, learning about \r and \b. -2 bytes thanks to myself for c[++i%=4]. -5 bytes thanks to Roman Gräf. -9 bytes thanks to kvill for indexing into the string literal directly.
#import<iostream>
#import<thread>
int main(int i){A:std::cout<<"\rLoading... "<<"|/-\\"[++i%=4];std::this_thread::sleep_for(std::chrono::milliseconds(250));goto A;}
Golfed your initial example. If it does not work in your console, you have to add <<flush to see any output for +7 bytes.
Ungolfed:
#import <iostream>
#import <thread>
int main(int i) {
A:
std::cout << "\rLoading... " << "|/-\\"[++i%=4];
std::this_thread::sleep_for(std::chrono::milliseconds(250));
goto A;
}
-
\$\begingroup\$ Will
ioverflow? \$\endgroup\$Adalynn– Adalynn2016年11月28日 13:43:13 +00:00Commented Nov 28, 2016 at 13:43 -
\$\begingroup\$ @ZacharyT Ah, yes it would and negative % positive = negative in C++. Rolled back. \$\endgroup\$Karl Napf– Karl Napf2016年11月28日 14:15:46 +00:00Commented Nov 28, 2016 at 14:15
-
1\$\begingroup\$ Tip: use
#import<...>to save a couple bytes :) \$\endgroup\$FlipTack– FlipTack2016年11月28日 15:08:19 +00:00Commented Nov 28, 2016 at 15:08 -
1\$\begingroup\$ No need to define
c, just use...<<"|/-\\"[++i%=4]for another couple of bytes. \$\endgroup\$kvill– kvill2016年11月28日 18:47:38 +00:00Commented Nov 28, 2016 at 18:47 -
1\$\begingroup\$ @kvill Thanks! That feels a little pythonish. \$\endgroup\$Karl Napf– Karl Napf2016年11月28日 19:03:14 +00:00Commented Nov 28, 2016 at 19:03
Nim, (削除) 81 (削除ここまで) 80 bytes
import os
while 1>0:
for c in "|/-\\":stdout.write("\rLoading... ",c);sleep 250
This may require flushing with stdout.flushFile in some terminals.
-
\$\begingroup\$ don't know nim but can't you write the for loop directly after the
while 1>0;? Also can you maybe remove the whitespace betweeninand'|/-\\":? \$\endgroup\$Linnea Gräf– Linnea Gräf2016年11月28日 20:00:50 +00:00Commented Nov 28, 2016 at 20:00 -
\$\begingroup\$ @RomanGräf Thanks, but those changes are beyond Nim! \$\endgroup\$kvill– kvill2016年11月28日 20:53:27 +00:00Commented Nov 28, 2016 at 20:53
PowerShell 57 Bytes
for(){'|','\','-','/'|%{"loading...$_";sleep -m 250;cls}}
-
2\$\begingroup\$ Welcome to PPCG! You can save a byte by using a
chararray instead of explicitly calling out each character --[char[]]'|\-/'\$\endgroup\$AdmBorkBork– AdmBorkBork2016年11月29日 16:53:11 +00:00Commented Nov 29, 2016 at 16:53 -
\$\begingroup\$ Right, @TimmyD, Thx! \$\endgroup\$jyao– jyao2016年11月29日 17:21:45 +00:00Commented Nov 29, 2016 at 17:21
Ruby, (削除) 75 (削除ここまで) 72 bytes
$><<'Loading... '
c='|/-\\'.chars
loop{$><<?\b+c.rotate![0]
sleep 0.25}
Mathematica, (削除) 90 bytes (削除ここまで) 85 bytes
Significantly longer than A Simmons' answer, but with fewer visual frills.
t=0;RunScheduledTask[t=Mod[t+1,4],1/4];Dynamic["Loading... "<>"|"["/","-","\\"][[t]]]
-
\$\begingroup\$ Wow!
"|"["/", "-", "\\"][[t]]looks awesome. nice one! :) \$\endgroup\$Dan Oak– Dan Oak2016年11月29日 04:54:42 +00:00Commented Nov 29, 2016 at 4:54 -
2\$\begingroup\$ Nice job! I count only 86 bytes (using the two-byte
[[and]]instead of the three-byte〚and〛). You can also save a byte with1/4in place of0.25. \$\endgroup\$Greg Martin– Greg Martin2016年11月30日 05:47:49 +00:00Commented Nov 30, 2016 at 5:47
Dyalog APL, 45 bytes
Uses ─ instead of -.
{⍵⊣⎕DL÷4⊣⍞←⍵,⍨⊃⎕TC} ̈⍣≢'|/─\'⊣⍞←'Loading... '
⍞←'Loading... ' print the string without newline
'|/─\'⊣ replace it with the string of bars
{...} ̈⍣≢ indefinitely apply the below function on each character
⊃⎕TC first Terminal Control character (backspace)
⍵,⍨ prepend the argument (a bar character)
⍞← output that without newline (this overwrites the previous bar)
4⊣ replace that with a four
÷ invert that (yielding 0.25)
⎕DL DeLay that many seconds
⍵⊣ replace with (and return) the original argument
T-SQL, (削除) 239 (削除ここまで) (削除) 182 (削除ここまで) (削除) 162 (削除ここまで) (削除) 159 (削除ここまで) (削除) 153 (削除ここまで) (削除) 152 (削除ここまで) 149 bytes
Golfed:
DECLARE @c CHAR(5)='|/-\',@s CHAR,@ INT WHILE 1=1BEGIN SET @=1WHILE @<4BEGIN SET @s=(SELECT SUBSTRING(@c,@,1))PRINT'Loading... '+@s SET @=@+1 END END
Ungolfed
DECLARE @c CHAR(5) = '|/-\',
@s CHAR(1),
@ INT;
WHILE (1 = 1)
BEGIN
SET @ = 1;
WHILE (@ < 4)
BEGIN
SET @s = (SELECT SUBSTRING(@c, @, 1));
PRINT 'Loading... ' + @s;
SET @ = @i + 1;
END
END
Clojure, (削除) 93 (削除ここまで) 92 bytes
#(do(print"Loading... -")(doseq[c(cycle"\\|/-")](print(str"\b"c))(flush)(Thread/sleep 250)))
Basically the Haskell answer (I swear I didn't cheat!).
It must be run in a console. IDE's REPL (Intellij) just prints a garbage character in place of the "\b".
And it's late, and I've never even created a GIF before, so I'm going to have to pass on that part.
HTML/CSS 135 Bytes
(削除) Like my previous answer, but doesn't use a monospace font, saving 6 bytes (not 100% if that's allowed, so I separated the answers for separate voting). (削除ここまで)
Update - a non monospace font is allowed. This works!
@keyframes l{0%{content:'|'}25%{content:'/'}50%{content:'-'}75%{content:'\\'}}a:after{animation:l 1s infinite;content:'|'}
<a>Loading...
-
\$\begingroup\$ The 0% frame seems unnecessary. \$\endgroup\$manatwork– manatwork2016年11月29日 08:41:16 +00:00Commented Nov 29, 2016 at 8:41
-
\$\begingroup\$ @manatwork there so that it can repeat infinitely. \$\endgroup\$Ben Aubin– Ben Aubin2016年12月13日 18:53:30 +00:00Commented Dec 13, 2016 at 18:53
awk, 46 bytes
In awk, with some help from ANSI codes and the rotor comes piped in:
{while(i=1+i%4)print"Loading... "$i"033円[1A"}
Try it:
$ echo \|/-\\|awk -F '' '{while(i=1+i%4)print"Loading... "$i"033円[1A"}'
Loading...[|/-\]
(削除) One byte comes off if . I didn't wait to see if NF is replaced with 4 (削除ここまで)i iterates to oblivion.
-
1\$\begingroup\$ Does this have the space between the
...and the cycling chars? \$\endgroup\$FlipTack– FlipTack2016年12月28日 09:38:21 +00:00Commented Dec 28, 2016 at 9:38 -
\$\begingroup\$ @FlipTack Missed that, I stand corrected. \$\endgroup\$James Brown– James Brown2016年12月28日 11:14:14 +00:00Commented Dec 28, 2016 at 11:14
TI-Basic (CE/CSE only), 57 bytes
:ClrHome
:Disp "LOADING...
:For(A,1,5
:A-4(A=5→A
:Output(1,12,sub("+/-*",A,1
:Pause .25
:End
Notes:
Many commands in TI-Basic are 1-2 byte tokens, which may make it appear to be a byte miscount.
Due to TI-Basic's very limited character set, the following characters have been replaced:
|\with+*.This will only run correctly on the newest version of a TI-84+ CE or CSE.
Commodore 64 (or VIC-20), 123 Bytes
0 A$="/|\-":A=1:FORI=0TO1STEP0:PRINT"{home}LOADING..."MID$(A,ドルA,1):A=A+1:GOSUB2:ON-(A>4)GOTO0
1 NEXT
2 FORX=0TO99:NEXT:RETURN
Using print 38911-(fre(0)-65536*(fre(0)<0)) tells me that I have consumed 123 bytes of the computers memory (on the C64); this will probably work on other variants of Commodore BASIC, such as the BASIC 7; you will need to use BASIC keyword abbreviations to enter it on a real C64 or VIC-20.
The BASIC listing from an emulator screen grab
In order to make it infinite*, you will need to disable the RUN/STOP key with a POKE, I think it's POKE 808,234 - that will mean you can't break into the BASIC listing without an Action Replay or a soft reset or something. The time delay can be altered in line 2 - increase the FOR X counter as required.
C++, 109 Bytes
There should be a lot to improve, it's my first golf code. Feedback is greatly appreciated :)
#import<stdio.h>
#import<unistd.h>
int b;main(){for(;;usleep(2500))printf("\rLoading...%c","-\\|/"[++b%=4]);}
Charcoal, 34 bytes
Loading... A0αHW250¦1«A+1ααP§|/-\α
Try it online! Refresh command has changed since so it is different on TIO. Link to verbose code for explanation.
Explore related questions
See similar questions with these tags.