36
\$\begingroup\$

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 , 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.
\$\endgroup\$
  • 1
    \$\begingroup\$ Can we output, say, twenty newlines before each output to 'clear' the screen? \$\endgroup\$ – Okx May 12 '17 at 11:11
  • 2
    \$\begingroup\$ @Okx Yes, if your language has no other way of clearing the screen. \$\endgroup\$ – lol May 12 '17 at 11:13
  • \$\begingroup\$ How much error can the delay be?(e.g. +- 0.5 seconds) I'd suggest 250 milliseconds error.... \$\endgroup\$ – stevefestl May 12 '17 at 12:50
  • 1
    \$\begingroup\$ Can I suggest not including the fixed time delay on future challenges? I find it's appeared on a lot of recent challenges, and each time I write the same ungolfable boilerplate to make the system wait n milleseconds. \$\endgroup\$ – xnor May 12 '17 at 20:23
  • 2
    \$\begingroup\$ Is the use of \r allowed, instead of literally clearing the screen? \$\endgroup\$ – phyrfox May 12 '17 at 21:06

48 Answers 48

1
\$\begingroup\$

S.I.L.O.S, 282 bytes

canvas # #
def # 1000 ! Obj N new M move > 10
newObj 0 # #
pen 255 255 255
N! 0 > >0
M! 1 > >
N! 0 > >0
M! 2 560 >
x=8
lblx
x-1
N! 0 > >
if x x
M! 3 20 >
M! 4 20 >0
M! 5 550 >
M! 6 550 >0
lbla
x=4
lbly
A=6+x
B=60+50*(((x-1)+a)%>)
M! A B 50 
x-1
if x y
refresh
a+1
a%>
wait >0
GOTO a

TIO currently does not support graphical output, but the official interpreter does. I think this is the first time I have gotten a chance to test out the graphical output functionality of SILOS.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

TI-Basic (TI-84 Plus CE), 58 bytes

"....      (6 spaces)
Ans+Ans
While 1
For(X,11,2,-1
ClrHome
Disp "["+sub(Ans,X,10)+"]
Wait .1
End
End

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ I wanted to submit a 68k version but realized that 1) None of my TI-92 Plus have a new enough AMS to use the clock, and 2) even if they did, the granularity is in seconds \$\endgroup\$ – Fox May 18 '17 at 1:24
1
\$\begingroup\$

APL (Dyalog), 44 bytes

{∇r⊣⎕DL.1⊣⎕←⎕TC[20⍴2],1⌽'][',r←¯1⌽⍵}10↑4⍴'.'

Try it online with a slightly modified version: ⎕← replaced by ∆_ which outputs with timestamps.

4⍴'.' four periods

10↑ take the first ten characters of that (pads with spaces)

{} apply the following function

¯1⌽ rotate one character from the tail to the head

r← assign that to r

'][', prepend the brackets

1⌽ rotate one character from the head to the tail

⎕TC[] index the list of Terminal Control characters with

  20⍴2 twenty repetitions of the number two (the second element is the newline)

⎕← output that

.1⊣ discard that in favour of the number 0.1

⎕DLDelay that many seconds (returns elapsed time)

r⊣ discard that in favour of r (which is then returned by the function)

 recurse (tail calls do not work up a stack, and can thus be repeated forever)

\$\endgroup\$
1
\$\begingroup\$

Japt, 31 22 bytes

Saved 9 bytes thanks to @obakaron

Li@O¬OpT=T?Té :'.²²+6î

Try it online!

\$\endgroup\$
  • \$\begingroup\$ p4 can be shortened to ²², Sp6 can be replaced by , and Oq can be replaced by \$\endgroup\$ – Oliver May 17 '17 at 13:23
1
\$\begingroup\$

Haskell, 132 bytes

import Control.Concurrent
main=mapM_(\x->putStrLn("\27[H\27[J["++x++"]")>>threadDelay(10^5))$iterate(\x->last x:init x)"....      "

First, we define a function r to rotate a string to the right. We begin with ".... " and construct an infinite list of its right-rotations, then for each x of these, print:

  • An ANSI escape sequence to clear the screen,
  • A left-bracket,
  • x itself,
  • A right-bracket,
  • And a newline character so the terminal will flush.

Then threadDelay waits 100000 microseconds before continuing onto the next element.

Since we are applying these operations to an infinite list, it will continue forever.

\$\endgroup\$
  • \$\begingroup\$ f x=last x:init x should be shorter than using reverse. \$\endgroup\$ – Laikoni May 17 '17 at 19:07
  • \$\begingroup\$ @Laikoni Wow, thanks for that! Saved a whole dozen bytes \$\endgroup\$ – Fox May 17 '17 at 19:13
  • \$\begingroup\$ Save another byte by moving the $ to the position of the .. Also is the newline needed if the screen is cleared anyway? If not then putStrLn can be shortened to putStr. \$\endgroup\$ – Laikoni May 17 '17 at 19:27
  • \$\begingroup\$ @Laikoni Right, don't know why I didn't see that. In my case, the Ln is needed, else the terminal never displays anything. I think that's terminal-dependent though — else I would just use a carriage return instead of the ANSI escape \$\endgroup\$ – Fox May 17 '17 at 19:28
1
\$\begingroup\$

shortC, 85 72 71 66 65 64 bytes

f(i){Udprintf(2,"\r[%-10.10s]","....      ...."+i%10)<<13);f(i+9

Explanation:

f(i){                                                           function that takes integer
     U                                                          sleep
      dprintf(2,                                                print unbuffered
                "\r[%-10.10s]",                                 string with exactly 10 characters, wrapped with spaces
                               "....    ...."                   a string that, when printed with exactly 10 characters, will always have output that is in the loading screen
                                             +i%10              scroll through the string
                                                  )<<13);       sleep 10<<13 microseconds
                                                         f(i+9  recursion :D
\$\endgroup\$
1
\$\begingroup\$

VBA (Excel), 109 bytes

Using Immediate Window and Cell A1 as output

a="...      .":Do:b=Right(a,1):a=b &Left(a,9):[A1]="["& a &"]":c=Timer+.1:Do While Timer<c:DoEvents:Loop:Loop
\$\endgroup\$
  • \$\begingroup\$ Do While:...:Loop -> While:...:Wend for -3 bytes \$\endgroup\$ – Taylor Scott Mar 7 at 14:18
0
\$\begingroup\$

CJam, 39 bytes

'.4*S6*+{_a`'"-oDcoes{es1$m100<}g;1m>}h

Can't make a GIF right now...

How it works

'.4*S6*+       e# Push a string containing 4 dots and 6 spaces
{              e# Do:
 _             e#  Copy the string
 a`            e#  Wrap it in an array and take its string representation: ["....      "]
 '"-           e#  Remove quote characters from it: [....      ]
 o             e#  Print it
 Dco           e#  Print a carriage return (ASCII 13), moving the cursor back to the start
 es            e#  Push the current time in milliseconds
 {es1$m100<}g  e#  Loop until the current time is at least 100 more than that time
 ;             e#  Delete the old time
 1m>           e#  Rotate the string 1 space to the right
}h             e# Repeat while the TOS is truthy. The string being printed is non-empty, so
               e#   it is always truthy, and will repeat forever.
\$\endgroup\$
0
\$\begingroup\$

Pyth, 28 bytes

J+*4\.*6d#.d.1p"\r"pj=.>J1`Y
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 61 bytes

$_='....      ';loop {"^M[$_]".print;s/(.*)(.)/$1$0/;sleep .1}

(That ^M is a single carriage return character.)

I really tried to base the output on a lazy infinite list, but couldn't nearly approach the terseness of this solution.

\$\endgroup\$
0
\$\begingroup\$

C, 74 73 bytes

i;f(){usleep(printf("\r[%-10.10s]","....      ...."+i%10)*7692);f(i+=9);}

Modified and improved version of https://codegolf.stackexchange.com/a/120321.

Displays correct output on MinGW.

\$\endgroup\$
0
\$\begingroup\$

C, 98 93 Bytes

main(){char b[]="x...      .";for(;;)*b=b[10],usleep(12<<printf("\r[%s]",memcpy(b+1,b,10)));}

View Video

\$\endgroup\$
  • \$\begingroup\$ @MDXF, Besides screwing up the pointer arithmetics, gcc does not allow the following on the global scope: b[]="x... ."; \$\endgroup\$ – Johan du Toit May 15 '17 at 17:46
0
\$\begingroup\$

Octave, 71 bytes

s="...       ."';do;printf('[%s]\r',s=circshift(s,1));pause(.1);until 0

For once my Octave answer is not MATLAB compatible. Specifically this solution uses

  • Both " and ' for quotes, to transpose a string
    • MATLAB: ('asd')'
    • Octave: "asd"'
  • Octave specific do ... until 0 structure, one char shorter than while 1 ... end
  • Octave's printf,one char shorter than the fprintf in MATLAB

Loading image...

\$\endgroup\$
0
\$\begingroup\$

C, 91 bytes

main(i){for(char*s="....      ....    ";;i+=9)usleep(dprintf(2,"[%.10s]\r",s+i%10-1)<<13);}

If it doesn't have to begin at the left: (89 bytes)

main(i){for(char*s="....      ....    ";;i+=9)usleep(dprintf(2,"[%.10s]\r",s+i%10)<<13);}
\$\endgroup\$
  • 2
    \$\begingroup\$ Something not quite right with this, sometimes it displays [a.... ] \$\endgroup\$ – cleblanc May 15 '17 at 13:51
  • \$\begingroup\$ -7 bytes: eliminate char*s= and use the raw string in dprintf, e.g. ".... .... "+i%10-1. \$\endgroup\$ – MD XF May 15 '17 at 17:11
0
\$\begingroup\$

R, 100 bytes

z="....      "
repeat{flush.console()
cat("\r[",z,"]",sep="")
z=sub("(.)(.+)(.)","\\3\\1\\2",z)
Sys.sleep(.1)}
\$\endgroup\$
0
\$\begingroup\$

F# (Mono), 122 114 118 110 bytes

let rec t s=printf"[%s]\r"s;System.Threading.Thread.Sleep(100);t((string s.[8])+s.Substring(0,8))
t"      ..."

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JS (ES6), 179 bytes

x=`[....      ]
[ ....     ]
[  ....    ]
[   ....   ]
[    ....  ]
[     .... ]
[      ....]
[.      ...]
[..      ..]
[...      .]`.split`
`;c=0;setTimeout(y=>alert(x[++c]),100)
\$\endgroup\$
0
\$\begingroup\$

Powershell, 64 bytes

for(){cls;"[$('...      .'*2|% s*g(9-$i++%10)10)]";sleep -m 100}

Where $string|% s*g $From $Length is a shortcut to summon $string.SubString($From,$Length)

This solution has one small disadvantage:

  • there will be an overflow exception after the Windows style Loading bar has worked for 988 million years.

Powershell, 65 bytes

for(){9..0|%{cls;"[$('...      .'*2|% s*g $_ 10)]";sleep -m 100}}

This Windows style Loading bar is an Immortal Eldar from Valinor.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.