Advertisement
T3RRYT3RR0R

Asynchronous animation

Jun 5th, 2021 (edited)
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.79 KB | None | 0 0
  1. :# Author: T3RRY Created: 03/06/2021
  2. :# Purpose: RGB / BW Spinner animation for load / waiting / processing indication while script continues to run.
  3. :# Utilises a second thread to display animation while the main script continues executing.
  4. :# Execution and termination of the thread is controlled using a value stored in an alternate data
  5. :# stream of this file:. 1 = run ; 0 = stop
  6.  
  7. @Echo off
  8.  
  9. :# thread label structure by Jeb - https://stackoverflow.com/a/68636825/12343998
  10.  Set^ "Params=%*"
  11.  for /F "tokens=3 delims=:" %%L in ("%~0") do goto %%L
  12.  
  13.  CD /D "%~dp0"
  14.  
  15. :# define title to revert to post animation
  16.  Set "Parent.title=%~n0"
  17.  
  18.  Call :defMacros
  19.  
  20.  If Errorlevel 1 (%= Quit if not NTFS =%
  21.   Exit /b
  22.  )
  23.  Cls
  24.  
  25. REM Allow easy comparison of Windows 10.10586 + vs Legacy versions.
  26.  If /I "%~1"=="/T" Set "Win10="
  27.  
  28. ===:# Usage Example.
  29.  
  30.  %Start.Thread:MSG=Stage 1 in progress% Supplemental info
  31.   For /l %%i in (1 1 100000)Do Set "var=%%i"
  32.  %End.Thread%
  33.  Echo([%var%] iterations actioned.
  34.  
  35.  %Start.Thread:MSG=Stage 2 in progress%
  36.   For /l %%i in (1 1 10000)Do Set "var=%%i"
  37.  %End.Thread%
  38.  Echo([%var%] iterations actioned.
  39.  
  40.  %Start.Thread:MSG=Stage 3 in progress%
  41.  Set "var=1"
  42.  %End.Thread%
  43.  Echo([%var%] iterations actioned.
  44.  
  45. :# End script cleanup
  46.  Powershell -c "remove-item -path '%~nx0' -Stream '*'"
  47.  CHCP %active.cp% > nul
  48.  @Pause
  49.  
  50. Goto :Eof
  51.  
  52. ===========================================================
  53. :AnimThread - Animation; RGB or BW spinner
  54.  
  55. :# frame control via elapsed time by Dave Benham - https://www.dostips.com/forum/viewtopic.php?f=3&t=4741
  56. :# Animation sequence. Symbols will not display correctly with certain fonts
  57. :# Recommended font: Lucida Console
  58. :# Alt 205 ═ 186 ║ 187 ╗ 200 ╚ 188 ╝ 201 ╔
  59. :# ╔═╗╔═╗╔═╗╔═╗
  60. :# ║/║║-║║\║║|
  61. :# ╚═╝╚═╝╚═╝╚═╝
  62. :#
  63. :# Framerate: ~ 25 fps. ('Delay' assigned as 4/100ths of a second.)
  64. :# NOTE - Multiple instances of this thread can not be run in parallel;
  65. :#        as Virtual terminal Cursor save operation will overide information stored by parallel threads
  66.  
  67. :# Title animation sequences
  68. :# 01021324354657687989  09192837545463728190
  69. :# ▬░▬░░░░░░░            ▬░░░░░░░░▬
  70. :# ░▬░▬░░░░░░            ░▬░░░░░░▬░
  71. :# ░░▬░▬░░░░░            ░░▬░░░░▬░░
  72. :# ░░░▬░▬░░░░            ░░░▬░░▬░░░
  73. :# ░░░░▬░▬░░░            ░░░░▬▬░░░░
  74. :# ░░░░░▬░▬░░            ░░░▬░░▬░░░
  75. :# ░░░░░░▬░▬░            ░░▬░░░░▬░░
  76. :# ░░░░░░░▬░▬            ░▬░░░░░░▬░
  77. :# ▬░▬░░░░░░░            ▬░░░░░░░░▬
  78.  
  79.  Setlocal EnableDelayedExpansion
  80.  CHCP 65001 > nul
  81.  If "%_Param%"=="MSG" Set "_Param=Executing"
  82.  If defined Win10 <nul set /p "=%\E%[1E%_Param%.%\E%[?25l%\E%[1E%\E%7"
  83.  Set "title.i=-1"
  84.  Set "T2[L]=0"         %= Rem Spinner Loop count    =%
  85.  Set "T2[chars]= |/-\" %= Rem Spinner characters    =%
  86.  Set "Delay=4"         %= Framerate in Centiseconds =%
  87.  Set "title.s=9 -1 0"
  88.  
  89.  If defined Win10 For /l %%. in ()Do (
  90.   %= Check completion status flagged in status stream of this file; =%
  91.   (
  92.    For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do (
  93.     if "%%G" == "0" (
  94.      <nul Set /P "=%\E%[?25h%\E%8%\E[K%"                  %= restore cursor    =%
  95.      %restore.CP% > nul                                   %= restore codepage  =%
  96.      <nul Set /P "=%\E%[7;32mCompleted.%\E%[0m%\E%[1E%\E%[M%\E%[K" %!!%
  97.      TITLE !Parent.title!
  98.      Exit                                                 %= exit thread       =%
  99.     )
  100.   )) 2> nul
  101.  
  102.   %= Calculate time elapsed =%
  103.   for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
  104.   if !tDiff! lss 0 set /a tDiff+=24*60*60*100
  105.  
  106.   if !tDiff! geq !delay! (
  107.    Set /A title.i+=1
  108.    If !title.i! GTR 9 Set "title.i=0"
  109.    Set "Bar=║09192837545463728190║" %= Title Anim Crosses Self =%
  110.    For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
  111.    Title Processing... !BAR! !Time! !Params!
  112.    %!!%
  113.    Set /A "T2[L]+=1"        %= Increment index point of spinner string =%
  114.    %= Output Spinner frame. Overwites previous output using saved cursor position =%
  115.    For /f "Delims=" %%v in ("!T2[L]!")Do <nul Set /p "=%\E%8%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╔═╗╔═╗╔═╗%\E%[0;38;2;150;;150m%\E%[K%\E%[E║%\E%[38;2;20;20;20;7m%\E%[48;2;255;;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;255;m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║║%\E%[38;2;20;20;20;7m%\E%[48;2;;100;255m!T2[chars]:~%%v,1!%\E%[0m%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m║%\E%[0m%\E%[K%\E%[E%\E%[38;2;20;20;20;7m%\E%[48;2;150;;150m╚═╝╚═╝╚═╝%\E%[0m%\E%[K%\E%[1G%\E%[2A%\E%7"
  116.    If !T2[L]! EQU 4 Set "T2[L]=0" %= Reset index point of spinner string =%
  117.    set /a t1=t2
  118.   )
  119.  )
  120.  
  121.  %= If not defined Win10 [ No Virtual Terminal support ] =%
  122.  For /l %%. in ()Do (
  123.   (For /F "UsebackQ Delims=" %%G in ("%~f0:Stream")Do if %%G EQU 0 (
  124.    %restore.CP% > nul
  125.    Cls
  126.    Echo(Complete.
  127.    TITLE !Parent.title!
  128.    Exit
  129.   )) 2> nul
  130.  
  131.   for /f "tokens=1-4 delims=:.," %%a in ("!time: =0!") do set /a "t2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100, tDiff=t2-t1"
  132.   if !tDiff! lss 0 set /a tDiff+=24*60*60*100
  133.  
  134.   if !tDiff! geq !delay! (
  135.    Set /A title.i+=1
  136.    If !title.i! GTR 9 Set "title.i=0"
  137.    Set "Bar=║01021324354657687989║" %= Title Anim Follows Self =%
  138.    For /l %%v in (0 1 9)Do If %%v NEQ !title.i! (Set "Bar=!Bar:%%v=░!")Else Set "Bar=!Bar:%%v=▬!"
  139.    Title Processing... !BAR! !Time!
  140.    Set /A "T2[L]+=1"
  141.    For /f "Delims=" %%v in ("!T2[L]!")Do (
  142.     Cls
  143.     Echo(%_Param%.
  144.     Echo(╔═╗╔═╗╔═╗╔═╗╔═╗╔═╗
  145.     Echo(!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!║║!T2[chars]:~%%v,1!
  146.     Echo(╚═╝╚═╝╚═╝╚═╝╚═╝╚═╝
  147.    )
  148.    If !T2[L]! EQU 4 Set "T2[L]=0"
  149.    set /a t1=t2
  150.   )
  151.  )
  152.  
  153. :defMacros
  154. :# OS Requirement tests
  155.  
  156. :# Verify NTFS drive
  157.  (Echo(verify.NTFS >"%~f0:Status") || (
  158.   Echo(This file must be located on an NTFS drive as it utilises alternate data streams.
  159.   Timeout /t 5
  160.   Exit /B 1
  161.  )
  162.  
  163. :# Windows Version control. Assigns flag true if system is windows 10 build GTR 10586
  164. :# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
  165. :# Version 1511 build number = 10.0.10586
  166.  Set "Win10="
  167.  For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "Win10=True"
  168.  
  169. :# If Win10 true ; Test if virtual terminal codes enabled ; enable if false
  170. :# removes win10 flag definition if version does not support Virtual Terminal sequences
  171. :# Reg values: https://devblogs.microsoft.com/commandline/understanding-windows-console-host-settings/
  172.  If defined Win10 (
  173.   Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel    REG_DWORD    0x1" > nul || (
  174.     Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
  175.   ) > Nul && (
  176.     Echo(CMD restart required to complete Virtual terminal sequence Initialization.
  177.     Pause
  178.     EXIT
  179.   ) || Set "Win10="
  180.  )
  181.  
  182. =====:# Store codepage to restore on script completion
  183.  For /f "tokens=4 Delims=: " %%1 in ('CHCP')Do Set "active.cp=%%1"
  184.  
  185. =====:# Define Escape character for Virtual terminal sequences
  186.  If Defined Win10 For /f "Delims=" %%e in ('Echo(Prompt $E^|Cmd')Do Set "\E=%%e"
  187.  
  188. ==================:# macros start and stop Thread via t/f flag written to alternate data stream.
  189. :# Start.Thread - Save current Codepage;
  190. :#                Remove status thread from this file if present;
  191. :#                Notify command has started;
  192. :#                Flag status 1 in Stream :!!
  193. :#                Use start /b with params to relaunch this script and execute second thread
  194. :#                executes infinite loop in AnimThread1 based on win10 virtual terminal support
  195.  
  196.  Set "Start.Thread=Set "_param=MSG"& (For /F "tokens=2 Delims=:" %%G in ('CHCP')Do Set "restore.CP=CHCP %%G")&((Echo(1)>"%~f0:Stream") & Start /AboveNormal /b "" "%~d0\:AnimThread:\..%~pnx0""
  197.  
  198. :# End.Thread  - Flag status 0 in Stream :!!
  199. :#               Execute a small delay to ensure thread has time to enable cursor display and clear animation.
  200. :#               [Delay of approx. 1/66th of a second]
  201.  
  202.  Set "End.Thread=((Echo(0)>"%~f0:Stream")&For /L %%i in (1 1 750)Do ((Call ))"
  203.  
  204. Exit /b 0
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement