Batch File: Typewriter Effect

后端 未结 4 1462
無奈伤痛
無奈伤痛 2020-12-06 23:38

I just got some code to do this effect, but I need to it to type faster, like a letter every half a second.

for %%i in (h e l l o o o o o o o o o o o o o o)          


        
4条回答
  •  孤街浪徒
    2020-12-06 23:59

    The ping solution in the comments above is a worthwhile solution for computers with installed network interfaces (as almost all have).

    @echo off
    setlocal
    
    for %%i in (h e l l o o o o o o o o o o o o o o) do (
       set /p "=%%i"nul
    )
    echo;
    goto :EOF
    

    However, it appears that the minimum wait time recognized with this method is 500ms. If you change the 500 to a lower value, you still pause a half second between letters. If you want finer control, or if your computer has no network interface, you'll have to borrow from another runtime environment -- JScript, for example.

    @if (@CodeSection == @Batch) @then
    
    @echo off
    setlocal
    
    for %%i in (h e l l o o o o o o o o o o o o o o) do (
       set /p "=%%i"

    Note on the choice of IP to ping: For the -w switch to work as intended, the IP you ping must result in "Request timed out". You can use a non-existent LAN IP such as a 10.x.x.x or 192.168.x.x. But for widespread deployment, if you can't be certain that those ranges are unused, a link local IP in the range of 169.254 should work just fine for this purpose. Please do not use an IP in historical bogon space like 1.1.1.1 or 1.2.3.4. Just because such addresses don't reply doesn't mean your packets aren't adding to network congestion somewhere.

    Eventually as IPv4 addresses draw ever nearer to complete exhaustion, people need to be more conscientious of polluting the Internet with bogus traffic. It could be that 1.1.1.1 and 1.2.3.4 will never be useful to anyone because they are so often abused by casual scripters. But that's no reason to add to the mistreatment of those addresses. See this page for further reading, and please, save the bogons.


    Jack.bat

    Just to see how far I could take the typewriter effect, I wrote a script that outputs text similar to the X screensaver "Jack". It outputs the same line over and over, and randomly introduces typographical errors. Run it and you'll be mesmerized, rooting for the script to complete a line without any typos.

    @if (@CodeSection == @Batch) @then
    
    @echo off
    setlocal
    
    cls
    color 70
    
    call :split chars "All work and no play makes Jack a dull boy."
    
    :begin
    for %%i in (%chars%) do call :type "%%~i"
    echo;
    goto begin
    
    :split  
    setlocal enabledelayedexpansion
    set "line="
    set "str=%~2"
    for /L %%I in (0,1,43) do set line=!line! "!str:~%%I,1!"
    endlocal & set %~1=%line%
    goto :EOF
    
    :type 
    cscript /nologo /e:JScript "%~f0" "%~1"
    goto :EOF
    
    @end
    // end batch / begin JScript chimera
    function pause() { WSH.Sleep(Math.random() * 250 + 100); }
    function odds(num) { return !(Math.round(Math.random() * num) % num) }
    function backspace() { WSH.StdOut.Write(String.fromCharCode(8)); }
    
    pause();
    
    if (odds(15)) {
        WSH.StdOut.Write(String.fromCharCode(Math.round(Math.random() * 95 + 32)));
        pause();
        if (!odds(20)) {
            backspace();
            pause();
        }
    }
    
    if (odds(300)) WSH.Echo('');
    if (!odds(400)) WSH.StdOut.Write(WSH.Arguments(0));
    

提交回复
热议问题