Jakers326
Jakers326

Reputation: 89

Batch Loading screen with no Glitching/Lag

Here is a bit of my batch Loading script

Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛ               º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛÛ              º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ping localhost -n 2 > nul
cls
Echo                                Loading
Echo                                ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
Echo                                ºÛÛÛ             º
Echo                                ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
exit

Using the cls code to wipe the screen makes the screen look like a "Blink". Is there a better way to clear the screen for this batch file? TIA

Upvotes: 1

Views: 5694

Answers (1)

MC ND
MC ND

Reputation: 70933

Just far from perfect less glitching option: Instead of repaint the full screen, repaint only the loading line (but this has to be last painted line in the screen).

The basic idea is to ouput the line ending with a carriage return but without a line feed so the cursor is moved to the start of the line to write again over the same line.

@echo off
    setlocal enableextensions enabledelayedexpansion

    Rem Get a carriage return character
    set "CR=" & for /f %%a in ('copy /Z "%~f0" nul') do if not defined CR set "CR=%%a"
    rem The progress bar
    set "fill=[##########]"

    cls
    echo(:: computers were created to give humans time to think while waiting ....

    rem For each character in the fill
    for /l %%a in (2 1 11) do (
        rem Calculate the right part of the bar
        set "spaces=!fill:~%%a!"

        rem Output the left and right parts of the bar and carriage return
        <nul set/p ".=:: Loading something big !fill:~0,%%a!!spaces:#= !!CR!"

        rem Pause for a second
        ping -n 2 "" > nul
    )
    echo(
    echo(:: Done

If you can not change your code/design so the last line is the only one repainted, at least try to enclose the painting operation inside a block (code enclosed in parenthesis) so in each paint operation commands are only parsed once, and prepare everything before starting to paint. It will not avoid the glitches, but will be less evident

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /l %%a in (0 10 100) do (
        call :loadingScreen %%a
        >nul ping -n 2 "" 
    )
    echo(:: Done
    goto :eof

:loadingScreen percent
    setlocal enableextensions enabledelayedexpansion

    rem Prepare everything 
    set    "sb=+----------+"
    set "fill=^|##########^|"
    set    "eb=+----------+"
    set /a "chars=2+%~1/10"
    set "spaces=!fill:~%chars%!"
    set "loadBar=!fill:~0,%chars%!!spaces:#= !"

    rem Time to paint
    (   cls
        echo(:: computers were created to give humans time to think while waiting .... %time%
        echo(
        echo(                           %sb%
        echo(     Loading something big %loadBar%
        echo(                           %eb%
        echo(
    ) 
    goto :eof

Upvotes: 5

Related Questions