Make a Windows style Loading bar by the following instructions.
(notice that this is different than Loading... Forever)
Your output should start by [.... ].
Every tick, you should wait 100 ms, then move each dots by one character right. if the dot is on the tenth character, move it to the first. Notice that you should clear the screen before outputting again. The output is ordered as the following:
[.... ]
[ .... ]
[ .... ]
[ .... ]
[ .... ]
[ .... ]
[ ....]
[. ...]
[.. ..]
[... .]
..Then it loops forever.
Rules
- This is code-golf, so the shortest answer wins
(削除) I doubt I would even accept a winning answer tho (削除ここまで) - Please provide a gif file of the loading bar in action if possible.
51 Answers 51
V, (削除) 17 (削除ここまで) (削除) 16 (削除ここまで) 15 bytes
i[ ́.¶ ]<esc>ògó$X|p
<esc> is 0x1b.
And the hexdump:
00000000: 695b b42e b620 5d1b f267 f324 587c 70 i[... ]..g.$X|p
Explanation
i " Insert
[ " a [
́. " 4 .s
¶<space> " 6 spaces
]<esc> " and a ]. Then return to normal mode
ò " Recursively do:
gó " Sleep for 100 milliseconds
$ " Go to the ]
X " Delete the character to the left of the ]
| " Go to the [
p " And paste the deleted character right after the [
" Implicit ending ò
-
\$\begingroup\$ How to test in Vim? \$\endgroup\$Pavel– Pavel2017年05月12日 17:58:03 +00:00Commented May 12, 2017 at 17:58
-
\$\begingroup\$ @Phoenix
i.... <esc>qq:sl 100m<CR>$X|P@qq@qshould work (<esc>is obviously the escape key and<CR>is a linefeed) (there are 6 spaces after the 4 dots) \$\endgroup\$user41805– user418052017年05月12日 18:00:42 +00:00Commented May 12, 2017 at 18:00 -
3\$\begingroup\$ Glad to see the
gófunction being useful. Nice answer BTW :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年05月12日 20:19:02 +00:00Commented May 12, 2017 at 20:19
CSS/HTML, (削除) 202 (削除ここまで) (削除) 190 (削除ここまで) 186 + 45 = (削除) 247 (削除ここまで) (削除) 235 (削除ここまで) 231 bytes
pre{position:relative}x{position:absolute;display:inline-block;width:10ch;height:1em;overflow:hidden}x>x{width:14ch;left:-10ch;animation:1s steps(10,end)infinite l}@keyframes l{to{left:0
<pre>[<x><x>.... ....</x></x> ]
Edit: Saved (削除) 12 (削除ここまで) 14 bytes thanks to @Luke.
-
\$\begingroup\$ Can't you save 6 bytes by renaming the animation to something like
b? \$\endgroup\$Luke– Luke2017年05月17日 15:38:10 +00:00Commented May 17, 2017 at 15:38 -
\$\begingroup\$ @Luke I can't believe I forgot to do that... \$\endgroup\$Neil– Neil2017年05月17日 15:50:05 +00:00Commented May 17, 2017 at 15:50
-
\$\begingroup\$ You can save 2 more bytes by dropping the
chat the end;0doesn't need a unit. \$\endgroup\$Luke– Luke2017年05月17日 17:00:55 +00:00Commented May 17, 2017 at 17:00 -
2\$\begingroup\$ How about changing
<x>to<span>(and in the CSS as well:xbecomesspanandx>xbecomesspan>*)? That saves thedisplay:inline-block;, but costs only 15 bytes. So a total of 6B are saved. \$\endgroup\$Luke– Luke2017年05月17日 19:58:49 +00:00Commented May 17, 2017 at 19:58 -
1\$\begingroup\$ @Luke I don't care about the display but I do want to avoid repeating the
position:absolute;. \$\endgroup\$Neil– Neil2017年05月17日 20:19:14 +00:00Commented May 17, 2017 at 20:19
PowerShell, (削除) 67 (削除ここまで) 66 Bytes
for($s='.'*4+' '*6;$s=-join($s[,9+0..8])){cls;"[$s]";sleep -m 100}
-1 by using shortened constructor thanks to Beatcracker
replaces the string with a copy of the string where the last char is put in front of the remaining chars, clears the screen, prints it, and then sleeps for 100 ms.
saved a lot of bytes by using the for loop constructor rather than wrap the logic inside the string.
-
1\$\begingroup\$ +1 for the
forloop trick and making me re-read about_Join. \$\endgroup\$beatcracker– beatcracker2017年05月13日 10:36:13 +00:00Commented May 13, 2017 at 10:36 -
1\$\begingroup\$ P.S. You can golf one more byte using
$s='.'*4+' '*6. \$\endgroup\$beatcracker– beatcracker2017年05月14日 17:10:23 +00:00Commented May 14, 2017 at 17:10 -
\$\begingroup\$ The script does not start by
[.... ]. You can fix it without penalty:for($s='.'*4+' '*6){cls;"[$s]";$s=-join($s[,9+0..8]);sleep -m 100}\$\endgroup\$mazzy– mazzy2018年11月06日 12:14:24 +00:00Commented Nov 6, 2018 at 12:14
Python 3, (削除) 99 (削除ここまで) (削除) 93 (削除ここまで) (削除) 85 (削除ここまで) 83+2 (-u flag) bytes
-12 bytes thanks to ovs
-2 bytes thanks to totallyhuman
import time
s=4*'.'+6*' '
while 1:print(end='\r[%s]'%s);time.sleep(.1);s=s[9]+s[:9]
-
\$\begingroup\$ Why do you have
flush=True? It works without for me \$\endgroup\$L3viathan– L3viathan2017年05月12日 13:48:20 +00:00Commented May 12, 2017 at 13:48 -
3\$\begingroup\$ @L3viathan because my (ubuntu) terminal wasn't flushing. This flushing behaviour is OS dependent =/ \$\endgroup\$Rod– Rod2017年05月12日 13:51:35 +00:00Commented May 12, 2017 at 13:51
-
1\$\begingroup\$ Save some bytes with
print(end='\r[%s]'%s,flush=1)\$\endgroup\$ovs– ovs2017年05月12日 13:57:36 +00:00Commented May 12, 2017 at 13:57 -
2\$\begingroup\$ You can remove flush entirely by using the
-ucommand line flag. Related SO question \$\endgroup\$ovs– ovs2017年05月12日 14:06:40 +00:00Commented May 12, 2017 at 14:06 -
1\$\begingroup\$ You can also save some bytes with
s[9]+s[:9]. \$\endgroup\$totallyhuman– totallyhuman2017年05月13日 17:08:35 +00:00Commented May 13, 2017 at 17:08
Windows batch , (削除) 201 (削除ここまで) 181 bytes
Turns out using the old-school method actually saves bytes!
@for %%p in (".... " " .... " " .... " " .... " " .... " " .... " " ...." ". ..." ".. .." "... .")do @echo [%%~p]&timeout 0 >nul&cls
@%0
Note:
get-screenrecorder.level
- low grade
get-gpu.level
- horrible
if get-screenrecorder.level == low grade || get-gpu.level == horrible {
say("GIF may not be accurate");
}
Please note that my GIF recorder skipped a few frames, making the loading bar jumps :(
-
1\$\begingroup\$ Rather than calculating the number of dots, if you just kept a variable with the dots and spaces and performed string manipulation on it you could probably get this down to 100 bytes. \$\endgroup\$Neil– Neil2017年05月12日 15:54:04 +00:00Commented May 12, 2017 at 15:54
-
\$\begingroup\$ I would try work on this, thanks for your tips :)! \$\endgroup\$stevefestl– stevefestl2017年05月12日 22:56:48 +00:00Commented May 12, 2017 at 22:56
-
\$\begingroup\$ timeout/t 0 >nul instead of ping 1.1 -n 1 -w 100>nul will be within the 100ms +/- 250ms timing requirement (should be around 25 - 100ms normally) so can save a few bytes there (ss64.com/nt/timeout.html) \$\endgroup\$Liam Daly– Liam Daly2017年05月15日 06:35:42 +00:00Commented May 15, 2017 at 6:35
-
1\$\begingroup\$ Also removing the
@echo offand replacing the do withdo @(echo %%~p&timeout/t 0 >nul&cls)will also work and should save 11 characters (200 bytes on my computer) \$\endgroup\$Liam Daly– Liam Daly2017年05月15日 06:51:49 +00:00Commented May 15, 2017 at 6:51
Javascript (ES6), 86 bytes
setInterval('with(console)clear(),log(`[${x=x[9]+x.slice(0,9)}]`)',100,x='... .')
-
\$\begingroup\$ @Shaggy codegolf.stackexchange.com/questions/118402/… #SelfPromotion :p \$\endgroup\$Arjun– Arjun2017年05月13日 05:54:32 +00:00Commented May 13, 2017 at 5:54
Mathematica, (削除) 67 (削除ここまで) 77 Bytes
+10 Bytes as I forgot the square brackets.
Animate["["<>".... "~StringRotateRight~n<>"]",{n,1,10,1},RefreshRate->10]
-
1\$\begingroup\$ Really, Mathematica has a built-in
Animate? :| \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年05月13日 10:08:55 +00:00Commented May 13, 2017 at 10:08 -
\$\begingroup\$ Yup, it will animate just about anything over a given variable. :) \$\endgroup\$Ian Miller– Ian Miller2017年05月13日 13:06:38 +00:00Commented May 13, 2017 at 13:06
-
\$\begingroup\$ This doesn't seem to include the rectangular brackets that most other answers do. \$\endgroup\$Mark S.– Mark S.2017年05月13日 14:26:37 +00:00Commented May 13, 2017 at 14:26
-
\$\begingroup\$ Oh rats, didn't look closely enough. Ok fixed. \$\endgroup\$Ian Miller– Ian Miller2017年05月14日 00:18:57 +00:00Commented May 14, 2017 at 0:18
C (gcc), (削除) 126 (削除ここまで) (削除) 125 (削除ここまで) (削除) 124 (削除ここまで) (削除) 123 (削除ここまで) (削除) 122 (削除ここまで) (削除) 121 (削除ここまで) (削除) 119 (削除ここまで) (削除) 118 (削除ここまで) (削除) 117 (削除ここまで) (削除) 114 (削除ここまで) 115 bytes
This one uses a bitmask to keep track of where the dots are.
I had to add another byte as I was only outputting 5 spaces before.
m=30;f(i){usleep(3<<15);system("clear");for(i=1;i<1920;i*=2)putchar(i^1?i&m?46:32:91);m+=m&512?m+1:m;f(puts("]"));}
-
51\$\begingroup\$ WHY is your command prompt font Comic Sans MS?!?!?! \$\endgroup\$MD XF– MD XF2017年05月12日 23:51:17 +00:00Commented May 12, 2017 at 23:51
-
\$\begingroup\$ Why has no one asked why it's pretending to be a Windows command prompt? \$\endgroup\$Oskar Skog– Oskar Skog2021年06月10日 17:37:50 +00:00Commented Jun 10, 2021 at 17:37
JavaScript (ES6) + HTML, (削除) 104 (削除ここまで) (削除) 85 (削除ここまで) 83 bytes
f=(s=".... ")=>setTimeout(f,100,s[9]+s.slice(0,9),o.value=`[${s}]`)
<input id=o
- Saved 2 bytes thanks to Johan's suggestion that I use an
inputinstead of apre.
Try It
Requires a closing > on the input tag in order to function in a Snippet.
(f=(s=".... ")=>setTimeout(f,100,s[9]+s.slice(0,9),o.value=`[${s}]`))()
<input id=o>
-
1\$\begingroup\$ Shouldn't there be 10 characters between the
[]s? \$\endgroup\$Neil– Neil2017年05月12日 13:46:22 +00:00Commented May 12, 2017 at 13:46 -
\$\begingroup\$ You're right, @Neil; there are 6 spaces - if I'm going to count things by eye, the least I could do is wear my glasses! \$\endgroup\$Shaggy– Shaggy2017年05月12日 13:48:37 +00:00Commented May 12, 2017 at 13:48
-
1\$\begingroup\$ Can't you use an
<input>instead of<pre>and thenvalueinstead ofinnerText? \$\endgroup\$Johan Karlsson– Johan Karlsson2017年05月12日 14:15:10 +00:00Commented May 12, 2017 at 14:15 -
\$\begingroup\$ Good call, @JohanKarlsson; that saves 2 bytes. \$\endgroup\$Shaggy– Shaggy2017年05月12日 14:18:48 +00:00Commented May 12, 2017 at 14:18
-
\$\begingroup\$ Hey! This is the same byte count:
s='.... ';setInterval(f=>{o.value='[${s=s[9]+s.slice(0,9)}]'},100);<input id=o, maybe someone can improve it (replace quotation mark with `) \$\endgroup\$Thomas Wagenaar– Thomas Wagenaar2017年05月12日 18:05:01 +00:00Commented May 12, 2017 at 18:05
Noodel, (削除) 16 (削除ここまで) (削除) 15 (削除ここまで) (削除) 14 (削除ここまで) 13 bytes
(削除)
[ CỤ‘Ṁ~ððÐ]ʠḷẸḍt
(削除ここまで)
(削除)
×ばつ6+ḷẸḍt
(削除ここまで)
(削除)
]ʠ6¤4.ȧ[ėÐḷẸḍt
(削除ここまで)
How it works
]ʠ6¤4.ȧ[ėÐḷẸḍt
]ʠ6¤4.ȧ[ėÐ # Set up for the animation.
] # Pushes the literal string "]" onto the stack.
ʠ # Move the top of the stack down by one such that the "]" will remain on top.
6¤ # Pushes the string "¤" six times onto the stack where "¤" represents a space.
4. # Pushes the string "." four times onto the stack.
ȧ # Take everything on the stack and create an array.
[ # Pushes on the string literal "[".
ė # Take what is on the top of the stack and place it at the bottom (moves the "[" to the bottom).
Ð # Pushes the stack to the screen which in Noodel means by reference.
ḷẸḍt # The main animation loop.
ḷ # Loop endlessly the following code.
Ẹ # Take the last character of the array and move it to the front.
ḍt # Delay for a tenth of a second.
# Implicit end of loop.
Update
[Ð]ıʠ6¤4.ḷėḍt
Don’t know why this took me a while to think of. Anyways, this places it at 13 bytes.
[Ð]ıʠ6¤4.ḷėḍt
[Ð]ıʠ6¤4. # Sets up the animation.
[ # Push on the character "["
Ð # Push the stack as an array (which is by reference) to the screen.
] # Push on the character "]"
ı # Jump into a new stack placing the "[" on top.
ʠ # Move the top of the stack down one.
6¤ # Push on six spaces.
4. # Push on four dots.
ḷėḍt # The main loop that does the animation.
ḷ # Loop the following code endlessly.
ė # Take the top of the stack and put it at the bottom.
ḍt # Delay for a tenth of a second.
<div id="noodel" code="[Ð]ıʠ6¤4.ḷėḍt" input="" cols="12" rows="2"></div>
<script src="https://tkellehe.github.io/noodel/noodel-latest.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>
-
2\$\begingroup\$ Never heard of Noodel before, but it seems to be the right tool for the right job! +1 \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年05月12日 13:22:06 +00:00Commented May 12, 2017 at 13:22
-
1\$\begingroup\$ @KevinCruijssen, ETHProductions has a good list with languages for code golfing:) \$\endgroup\$tkellehe– tkellehe2017年05月12日 13:31:11 +00:00Commented May 12, 2017 at 13:31
-
6\$\begingroup\$ Just when I thought I outgolfed you, I notice you have already golfed your solution twice \$\endgroup\$user41805– user418052017年05月12日 15:12:46 +00:00Commented May 12, 2017 at 15:12
-
\$\begingroup\$ @KritixiLithos, I was scared you were going to beat me so I spent forever trying to get to 14 bytes. But, now you are close again!! Dang!! \$\endgroup\$tkellehe– tkellehe2017年05月12日 15:16:10 +00:00Commented May 12, 2017 at 15:16
-
1\$\begingroup\$ @nitro2k01 Noodel uses its own code-page with 256 characters, which are all saved as a single byte in their own encoding. Similar as some other golfing languages do, like Jelly or 05AB1E. If you would save these characters as default UTF-8 encoding, they will indeed be 2 or 3 bytes instead, but in their own encoding they are 1 byte each. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年05月15日 09:07:29 +00:00Commented May 15, 2017 at 9:07
PHP, 67 bytes
for($s="... .";$s=substr($s.$s,9,10);usleep(1e5))echo"\r[$s]";
no comment
C#, (削除) 162 (削除ここまで) 157 bytes
()=>{for(string o="[.... ]";;){o=o.Insert(1,o[10]+"").Remove(11,1);System.Console.Write(o);System.Threading.Thread.Sleep(100);System.Console.Clear();}};
or as whole program for 177 bytes
namespace System{class P{static void Main(){for(string o="[.... ]";;){o=o.Insert(1,o[10]+"").Remove(11,1);Console.Write(o);Threading.Thread.Sleep(100);Console.Clear();}}}}
-
\$\begingroup\$ +1 Something to golf:
for(string o="[.... ]";;)can be golfed tovar o="[.... ]";for(;;). Or you can us a port of my Java 7 answer to golf the total some more:()=>{var o=".... "for(;;){o=(o+o).Substring(9,10);System.Console.Write("["+o+"]\n");System.Threading.Thread.Sleep(100);System.Console.Clear();}};\$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年05月12日 13:21:00 +00:00Commented May 12, 2017 at 13:21 -
\$\begingroup\$ Would string interpolation trim anymore off? Something like
$"[{o}]\n"\$\endgroup\$Marie– Marie2017年05月12日 13:54:47 +00:00Commented May 12, 2017 at 13:54 -
1\$\begingroup\$ If you replace
System.Console.Write(o)withSystem.Console.Write(o+"\r")you can remove theSystem.Console.Clear();\$\endgroup\$grabthefish– grabthefish2017年05月13日 20:03:38 +00:00Commented May 13, 2017 at 20:03
Pure bash, 68
s=${1:-.... }
printf "[$s]\r"
sleep .1
exec 0ドル "${s: -1}${s%?}"
Jelly, (削除) 28 (削除ここまで) 27 bytes
ṙ©-j@)[]ṭ"ÆȮœS.1®ß
897ṃ). Ç
How?
ṙ©-j@)[]ṭ"ÆȮœS.1®ß - Link 1, the infinite loop: list of characters s
ṙ - rotate s left by:
- - -1 (e.g. "... ." -> ".... ")
© - copy to the register and yield
)[] - literal ['[',']']
j@ - join with reversed @rguments
"Æ - literal '\r'
ṭ - tack (append the display text to the '\r')
Ȯ - print with no newline ending
.1 - literal 0.1
œS - sleep for 0.1 seconds then yield the printed text (unused)
® - recall the value from the register
ß - call this link (1) again with the same arity
897ṃ). Ç - Main link: no arguments
897 - literal 897
). - literal ['.',' ']
ṃ - base decompression: convert 897 to base ['.',' '] = "... ."
C (gcc), (削除) 202 (削除ここまで) (削除) 198 (削除ここまで) (削除) 196 (削除ここまで) (削除) 189 (削除ここまで) (削除) 96 (削除ここまで) (削除) 99 (削除ここまで) (削除) 88 (削除ここまで) (削除) 86 (削除ここまで) (削除) 79 (削除ここまで) (削除) 77 (削除ここまで) (削除) 75 (削除ここまで) (削除) 74 (削除ここまで) 73 bytes
Saved (削除) 7 (削除ここまで) 8 bytes thanks to Digital Trauma.
f(i){usleep(dprintf(2,"\r[%-10.10s]",".... ...."+i%10)<<13);f(i+9);}
Or, if your system's stdout doesn't need to be flushed after every write without a newline:
C (gcc), 70 bytes
f(i){usleep(printf("\r[%-10.10s]",".... ...."+i%10)<<13);f(i+9);}
How it works
usleep(sleeps for the next return value in microseconds.dprintf(2,prints to file descriptor 2, orstderr. This is necessary because whilestdoutis line-buffered (meaning output will not show until it prints a newline),stderris character-buffered (all output is shown immediately)."\rprints a carriage return (clears the current line).[%-10.10s]"is theprintfformat specifier for a string with exact length 10 (no matter what string provided the output will always be a string with length 10), padded with spaces to the right if necessary. This will be enclosed with brackets.".... ...."is the loading bar.+i%10offsets the loading bar by the current index modulo 10. For example, ifi == 3,i % 10is equal to 3. Offsetting the loading bar by 3 makes it equal to". ....".- When the offset-ed string is passed to the
printfformat specifier, it limits to a length of 10 if necessary and adds spaces to the end if necessary. Therefore, the loading bar will always be between[.... ]and[. ...].
-
\$\begingroup\$
i;f(){for(;;i=++i%10)usleep(7500*dprintf(2,"\r[%-10.10s]",".... ...."-i+10));}should work. \$\endgroup\$Christoph– Christoph2017年05月15日 11:08:13 +00:00Commented May 15, 2017 at 11:08 -
1\$\begingroup\$ Great golfing! Save 1 more byte with
f(i){usleep(dprintf(2,"\r[%-10.10s]",".... ...."+i%10)<<13);f(i+9);}\$\endgroup\$Digital Trauma– Digital Trauma2017年05月15日 19:05:28 +00:00Commented May 15, 2017 at 19:05 -
\$\begingroup\$ @DigitalTrauma The spaces in your code didn't render properly. However, I see what you meant, and thanks for the help! \$\endgroup\$MD XF– MD XF2017年05月15日 19:17:53 +00:00Commented May 15, 2017 at 19:17
MATL, 24 bytes
`1&Xx'['897B@YS46*93hhDT
Try it at MATL Online! Or see a gif from the offline compiler:
Explanation
` % Do...while
1&Xx % Pause for 0.1 s and clear screen
'[' % Push this character
897B % Push [1 1 1 0 0 0 0 0 0 1]
@ % Push current iteration index, 1-based
YS % Circularly shift the array by that amount
46* % Multiply by 46 (code point of '.')
93 % Push 93 (code point of ']')
hh % Concatenate horizontally twice. Numbers are interpreted as chars
% with the corresponding code points
D % Display
T % Push true. Used as loop condition. Results in an infinite loop
% End (implicit)
-
\$\begingroup\$ Your link crashes, meaning that I cannot kill it. \$\endgroup\$Leaky Nun– Leaky Nun2017年05月12日 11:29:22 +00:00Commented May 12, 2017 at 11:29
-
1\$\begingroup\$ @LeakyNun What do you mean exactly that it crashes? It works for me, and I can kill it. Sometimes there are timeout issues. If it doesn't start, try refreshing the page \$\endgroup\$Luis Mendo– Luis Mendo2017年05月12日 11:45:09 +00:00Commented May 12, 2017 at 11:45
Java 7, (削除) 139 (削除ここまで) 124 bytes
String s=".... ";void c()throws Exception{System.out.print("["+s+"]\r");s=(s+s).substring(9,19);Thread.sleep(100);c();}
- Mentioning of
\rthanks to @Phoenix.
The carriage return \r resets the 'cursor' back to the begin of the line, which can then be overwritten. Unfortunately, online compilers nor the Eclipse IDE doesn't support this, so I've added a gif at the end of this answer to show it from Windows Command Prompt.
Try it here. (Slightly modified so you won't have to wait for the time-out before viewing the result. Also, the TIO doesn't support carriage returns, so every line is printed without overwriting the previous line.)
Explanation:
String s=".... "; // Starting String ".... " on class level
void c() // Method without parameter nor return-type
throws Exception{ // throws-clause/try-catch is mandatory for Thread.sleep
System.out.print("["+s+"]\r"); // Print the String between square-brackets,
// and reset the 'cursor' to the start of the line
s=(s+s).substring(9,19); // Set `s` to the next String in line
Thread.sleep(100); // Wait 100 ms
c(); // Recursive call to same method
} // End of method
Output gif:
-
\$\begingroup\$ You can clear the line by replacing
printlnwithprintand outputting a carriage return. Might not work in your IDE's terminal, but it would work in any other sane one. \$\endgroup\$Pavel– Pavel2017年05月12日 18:00:05 +00:00Commented May 12, 2017 at 18:00 -
\$\begingroup\$ @Phoenix By carriage return you mean
\r\n? How doesSystem.out.print(someString+"\r\n);clear the console.. It's the same as usingSystem.out.println(someString);.. It simply goes to the next line, but doesn't remove any previous line printed.. :S \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年05月12日 19:31:35 +00:00Commented May 12, 2017 at 19:31 -
4\$\begingroup\$ No, I mean
\r, without\n. That resets the "cursor" to the beginning of the line so printing anything will overwrite that line. \$\endgroup\$Pavel– Pavel2017年05月12日 22:21:38 +00:00Commented May 12, 2017 at 22:21 -
\$\begingroup\$ @Phoenix Ah of course. Thanks. Modified my answer and added a gif to show the result. Too bad online compilers nor the Eclipse IDE aren't supporting this.. >.> \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年05月13日 10:36:42 +00:00Commented May 13, 2017 at 10:36
Python 2, (削除) 81 (削除ここまで) 78 bytes
-1 byte (noticing I missed use of %s when Rod submitted an almost identical Python 3 version at the same time!)
-2 bytes (using totallyhuman's idea - replace s[-1]+s[:-1] with s[9]+s[:9])
import time
s='.'*4+' '*6
while s:print'\r[%s]'%s,;s=s[9]+s[:9];time.sleep(.1)
-
\$\begingroup\$ How it's flushing the output? this is the reason why I'm using python3 on my answer (it would take more bytes to flush on python2) \$\endgroup\$Rod– Rod2017年05月12日 12:29:04 +00:00Commented May 12, 2017 at 12:29
-
\$\begingroup\$ @Rod the
\roverwrites the line and the,makes it print a tuple rather than a string - I saw it a while back somewhere and have used it before too. \$\endgroup\$Jonathan Allan– Jonathan Allan2017年05月12日 12:33:27 +00:00Commented May 12, 2017 at 12:33 -
1\$\begingroup\$ Yes, this is what I was doing, but the output wasn't being printed in real time (had to use
sys.stdout.flush()) \$\endgroup\$Rod– Rod2017年05月12日 12:36:58 +00:00Commented May 12, 2017 at 12:36 -
1\$\begingroup\$ Found the culprit : my ubuntu terminal :c \$\endgroup\$Rod– Rod2017年05月12日 12:39:27 +00:00Commented May 12, 2017 at 12:39
Go, (削除) 150 (削除ここまで) (削除) 145 (削除ここまで) (削除) 132 (削除ここまで) (削除) 129 (削除ここまで) 124 bytes
-5 bytes thanks to sudee.
I feel like I don't see enough Go here... But my answer is topping C so... pls halp golf?
package main
import(."fmt"
."time")
func main(){s:=".... ";for{Print("\r["+s+"]");Sleep(Duration(1e8));s=(s+s)[9:19];}}
-
1\$\begingroup\$ Not familiar with Go, but I would assume you can convert
100000000to10^8to save 5 bytes. \$\endgroup\$Grant Miller– Grant Miller2017年05月13日 05:33:05 +00:00Commented May 13, 2017 at 5:33 -
\$\begingroup\$ @goatmeal I tried that but it's apparently bitwise negation. I also tried
10**8which also gives an error. \$\endgroup\$totallyhuman– totallyhuman2017年05月13日 12:50:02 +00:00Commented May 13, 2017 at 12:50 -
3\$\begingroup\$ You can use scientific notation:
1e8. \$\endgroup\$sudee– sudee2017年05月14日 16:20:38 +00:00Commented May 14, 2017 at 16:20 -
1\$\begingroup\$ @sudee Aha, that would be the way to use large numbers. Thanks! \$\endgroup\$totallyhuman– totallyhuman2017年05月14日 16:24:06 +00:00Commented May 14, 2017 at 16:24
-
2\$\begingroup\$ @MDXF I should've phrased that differently, I meant my answer is being out-golfed by C. \$\endgroup\$totallyhuman– totallyhuman2017年05月15日 16:39:01 +00:00Commented May 15, 2017 at 16:39
VBA 32-bit, (削除) 159 (削除ここまで) (削除) 157 (削除ここまで) (削除) 143 (削除ここまで) (削除) 141 (削除ここまで) 134 Bytes
VBA does not have a built in function that allows for waiting for time periods less than one second so we must declare a function from kernel32.dll
32 Bit Declare Statement (41 Bytes)
Declare Sub Sleep Lib"kernel32"(ByVal M&)
64 Bit Declare Statement (49 Bytes)
Declare PtrSafe Sub Sleep Lib"kernel32"(ByVal M&)
Additionally, we must include a DoEvents flag to avoid the infinite loop from making Excel appear as non-responsive. The final function is then a subroutine which takes no input and outputs to the VBE immediate window.
Immediate Window function, 93 Bytes
Anonymous VBE immediate window function that takes no input and outputs to the range A1 on the ActiveSheet
s="... .... .":Do:DoEvents:Sleep 100:[A1]="["&Mid(s,10-i,10)&"]":i=(i+1)Mod 10:Loop
Old Version, 109 Bytes
Immediate window function that takes no input and outputs to the VBE immediate window.
s="... .... .":i=0:Do:DoEvents:Sleep 100:Debug.?"["&Mid(s,10-i,10)&"]":i=(i+1) Mod 10:Loop
Ungolfted and formatted
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal M&)
Sub a()
Dim i As Integer, s As String
s = "... .... ."
i = 0
Do
Debug.Print [REPT(CHAR(10),99]; "["; Mid(s, 10 - i, 10); "]"
DoEvents
Sleep 100
i = (i + 1) Mod 10
Loop
End Sub
-2 Bytes for removing whitespace
-30 Bytes for counting correctly
-14 Bytes for converting to immediate window function
Output
The gif below uses the full subroutine version because I was too lazy to rerecord this with the immediate window function.
-
\$\begingroup\$ What's that
aat the top of the output? \$\endgroup\$MD XF– MD XF2017年05月15日 15:44:25 +00:00Commented May 15, 2017 at 15:44 -
\$\begingroup\$ @MDXF that is the call to run subroutine
aas is listed above; this is functionally equivalant to the more verbosecall a(). \$\endgroup\$Taylor Raine– Taylor Raine2017年05月16日 03:27:22 +00:00Commented May 16, 2017 at 3:27 -
\$\begingroup\$ Ah, my bad. Just looking for bad submissions. Your's isn't, then, so +1 \$\endgroup\$MD XF– MD XF2017年05月16日 03:28:13 +00:00Commented May 16, 2017 at 3:28
05AB1E, 23 bytes
×ばつ,т.W
Explanation
×ばつJ # push the string ".... "
[ # forever do:
D # duplicate
...[ÿ], # interpolate the copy between brackets and print
Á # rotate the remaining copy right
×ばつ, # print 100 newlines
т.W # wait 100ms
Batch, (削除) 99 (削除ここまで) 98 bytes
Saved 1 byte thanks to SteveFest!
(I could remove \r from the code, but in the spirit of batch golfing, I won't.)
@SET s=....
:g
@CLS
@ECHO [%s%]
@SET s=%s:~-1%%s:~,-1%
@ping 0 -n 1 -w 100>nul
@GOTO g
There are four spaces after the first line.
The main logic is modifying the string. %s:~-1% is the last character of %s% and %s:~0,-1% is all but the last character of %s%. Thus, we are moving the last character to the front of the string, which rotates the string.
-
\$\begingroup\$ Aw... I've been looking for this... \$\endgroup\$stevefestl– stevefestl2017年05月12日 23:05:55 +00:00Commented May 12, 2017 at 23:05
-
1\$\begingroup\$ Golf 1 byte: the
0in the variable substring can be removed \$\endgroup\$stevefestl– stevefestl2017年05月12日 23:12:16 +00:00Commented May 12, 2017 at 23:12 -
\$\begingroup\$ You use
cmder. Nice job. \$\endgroup\$MD XF– MD XF2017年05月13日 03:21:14 +00:00Commented May 13, 2017 at 3:21 -
1\$\begingroup\$ @SteveFest Huh, TIL. Thanks! \$\endgroup\$Conor O'Brien– Conor O'Brien2017年05月13日 03:45:50 +00:00Commented May 13, 2017 at 3:45
-
1\$\begingroup\$ @MDXF It's the only reason I'm still sane :P \$\endgroup\$Conor O'Brien– Conor O'Brien2017年05月13日 03:46:21 +00:00Commented May 13, 2017 at 3:46
Ruby, (削除) 57 (削除ここまで) 56 bytes
s=?.*4+' '*6;loop{$><<"[%s]\r"%s=s[-1]+s.chop;sleep 0.1}
Heavily influenced by other answers here.
Saved a byte thanks to @manatwork. Also apparently I have trouble counting characters -- I use ST3 and apparently it will include newlines in the count of characters in the line if you're not attentive.
-
\$\begingroup\$ How does it work? Does this assume that the input is stored in
s? \$\endgroup\$Riker– Riker2017年05月12日 22:47:10 +00:00Commented May 12, 2017 at 22:47 -
\$\begingroup\$ @Riker He defines
sat the beginning of the program as 4.s and a few spaces \$\endgroup\$Conor O'Brien– Conor O'Brien2017年05月12日 22:50:55 +00:00Commented May 12, 2017 at 22:50 -
\$\begingroup\$
s[0..8]→s.chop\$\endgroup\$manatwork– manatwork2017年05月15日 08:53:41 +00:00Commented May 15, 2017 at 8:53
Perl, 69 bytes
-3 bytes thanks to @Dom Hastings.
$_="....".$"x6;{print"\ec[$_]
";select$a,$a,!s/(.*)(.)/2ドル1ドル/,.1;redo}
That select undef,undef,undef,.1 is the shortest way to sleep less than 1 second in Perl, and it takes a lot of bytes...
Slightly longer (79 bytes), there is:
@F=((".")x4,($")x6);{print"\ec[",@F,"]\n";@F=@F[9,0..8];select$a,$a,$a,.1;redo}
-
\$\begingroup\$ Evening, managed to get this down a little more 69 (or 68 with a literal ESC): gist.github.com/dom111/e3ff41c8bc835b81cbf55a9827d69992 I feel like the tried to use
!printbut you need parens so it ends up the same length :/ \$\endgroup\$Dom Hastings– Dom Hastings2017年05月15日 18:30:16 +00:00Commented May 15, 2017 at 18:30 -
\$\begingroup\$ @DomHastings Nice, thanks! You still know how to golf :D \$\endgroup\$Dada– Dada2017年05月16日 07:44:17 +00:00Commented May 16, 2017 at 7:44
Bash, (削除) 93 (削除ここまで) (削除) 90 (削除ここまで) 96 bytes
s="... .... ."
for((;;)){ for i in {0..9};do printf "\r[${s:10-i:10}]";sleep .1;done;}
couldn't get nested { } in for syntax
-
\$\begingroup\$ I intended to post a quite similar solution, but is pointless now. But may give some inspiration to improve your: pastebin.com/Ld6rryNX \$\endgroup\$manatwork– manatwork2017年05月12日 14:06:50 +00:00Commented May 12, 2017 at 14:06
-
\$\begingroup\$ much better! i'm not stealing from you, i knew i had to work out this one... \$\endgroup\$marcosm– marcosm2017年05月12日 14:19:23 +00:00Commented May 12, 2017 at 14:19
-
\$\begingroup\$ edited, printf padding can't help in shortening s. wraping the string as @DigitalTrauma looks better. \$\endgroup\$marcosm– marcosm2017年05月15日 13:27:10 +00:00Commented May 15, 2017 at 13:27
Groovy, 72 bytes
s="*"*4+" "*6
for(;;){print("["+s+"]"+"\n"*20);s=s[9]+s[0..8];sleep 100}
Explaination
s="*"*4+" "*6 //creates the string "**** "
for(;;){ //classic infinite loop
print("["+s+"]"+"\n"*20) //prints the string with [ at the beginning and ] at the end. After that some newlines
s=s[9]+s[0..8] //appends the final char of the string to beginning, creating a cycling illusion
sleep 100 //100 ms delay
}
-
\$\begingroup\$ Didn't know a proper way to clear the console in Groovy/Java. If someone has a way of doing it, please tell me \$\endgroup\$staticmethod– staticmethod2017年05月12日 18:48:46 +00:00Commented May 12, 2017 at 18:48
-
1\$\begingroup\$ You can use
\rto return the cursor to the start of the line. It appears that at least several answers are doing this. From there, you could delete the *20, saving 3 bytes. \$\endgroup\$phyrfox– phyrfox2017年05月12日 21:09:19 +00:00Commented May 12, 2017 at 21:09
Haskell (Windows), 159 bytes
import System.Process
import Control.Concurrent
main=mapM id[do system"cls";putStrLn('[':[".... "!!mod(i-n)10|i<-[0..9]]++"]");threadDelay(10^5)|n<-[0..]]
Explanation
mapM id sequentially perform each IO action in the following list
[ start a list comprehension where each element is...
do an IO operation where
system "cls"; we clear the screen by calling the windows builtin "cls"
putStrLn( then display the string...
'[': with '[' appended to
[ a list comprehension where each character is...
".... "!! the character in literal string ".... " at the index
mod(i-n)10 (i - n) % 10
|i<-[0..9]] where i goes from 0 to 9
++"]" and that ends with ']'
);
threadDelay(10^5) then sleep for 100,000 microseconds (100 ms)
|n<-[0..]] where n starts at 0 and increments without bound
Haskell's purity made generating the cycling dot pattern somewhat complex. I ended up creating a nested list comprehension that generated an infinite list of strings in the order they should be output, then went back added the appropriate IO operations.
Ruby, 61 bytes
If the spec were for the dots to scroll left instead of right, it would save 1 byte because rotate! with no arguments shifts the array once to the left.
s=[?.]*4+[' ']*6
loop{print ?[,*s,"]\r";s.rotate!9;sleep 0.1}
GNU sed (with exec extension), 64
Score includes +1 for -r flag.
s/^/[.... ]/
:
esleep .1
s/[^. ]*(.+)(.)].*/\c[c[2円1円]/p
b
c, 100
char *s=".... .... ";main(i){for(i=0;;i=(i+9)%10)dprintf(2,"[%.10s]\r",s+i),usleep(3<<15);}
-
\$\begingroup\$ Why print to
stderrusingdprintfand not just useprintf? \$\endgroup\$MD XF– MD XF2017年05月12日 23:58:48 +00:00Commented May 12, 2017 at 23:58 -
\$\begingroup\$ @MDXF Because by default
stderris character buffered, whereasstdoutis line buffered. Since I don't want to print any\n, then withprintf()I'd have to explicitlyfflush(stdout)as well as#include <stdio.h>\$\endgroup\$Digital Trauma– Digital Trauma2017年05月13日 00:27:29 +00:00Commented May 13, 2017 at 0:27 -
\$\begingroup\$ Good point, but actually, you wouldn't have to
#include <stdio.h>to flush STDOUT.fflush(0)flushes all buffers. \$\endgroup\$MD XF– MD XF2017年05月13日 00:28:22 +00:00Commented May 13, 2017 at 0:28 -
1\$\begingroup\$ Save three bytes by renaming
maintof, that counts. \$\endgroup\$MD XF– MD XF2017年05月13日 03:19:53 +00:00Commented May 13, 2017 at 3:19
Explore related questions
See similar questions with these tags.
\rallowed, instead of literally clearing the screen? \$\endgroup\$