Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ==========================================================================================================
- ::: * Proof of concept script for moving cursor along a permitted path only using ASCII escape codes * :::
- ==========================================================================================================
- @echo off & CLS & CD "%userprofile%"
- Rem [* Ensure safe temporary working directory - Failed Redirections can cause issues in the working directory *]
- If exist "%TEMP%\%~n0\Guard" (RMDIR "%TEMP%\%~n0\Guard" /S /Q 2> Nul || (Echo/Cleanup failed & Exit /B 1))
- Timeout 2 > Nul & CLS
- If not exist "%TEMP%\%~n0\Guard" MD "%TEMP%\%~n0\Guard"
- PUSHD "%TEMP%\%~n0\Guard" || (Echo/Safe working directory required & Exit /B)
- (Set \n=^^^
- %=Do not modify=%
- )
- for /F "delims=#" %%a in ('"prompt #$E# & for %%a in (1) do rem"') do set "\E=%%a"
- Rem [* max expected return length is 11 characters: "^]]###;###R". R is disregarded. only 10 iterations required *]
- Rem [* Additional redirections or piping within the CHKmove macro or any (code block) it's used in may cause failure. *]
- Set CHKmove=(Set "Opos=!Cpos!" ^& set "Cpos=" ^&Set "Char=" ^& For %%n in (1 2)Do if %%n==2 (%\n%
- For /L %%l in (2 1 12)Do (%\n%
- If not "!Char!" == "R" (%\n%
- ^<nul set /p "=%\E%[6n" %\n%
- FOR /L %%# in (1 1 %%l) DO pause ^< CON ^> NUL%\n%
- Set "Char=;"%\n%
- for /F "tokens=1 skip=1 delims=*" %%C in ('"REPLACE /W ? . < con"') DO (Set "char=%%C")%\n%
- If "!Cpos!" == "" (Set "Cpos=!Char!")Else set "Cpos=!Cpos!!char:R=!"%\n%
- )%\n%
- )%\n%
- Set "_V=F"^&(For %%G in (!Valid!)Do If /I "%%~G" == "!Cpos!" (Set "_V=T"))%\n%
- ))
- Rem [* 'Valid' variable is used as the source of valid positions to compare against in CHKmove macro *]
- Set Valid="20;21" "20;22" "20;23" "20;24" "20;25" "19;25" "18;25" "17;25" "16;25" "15;25" "15;24" "15;23" "15;22" "15;21" "15;21" "16;21" "17;21" "18;21" "19;21"
- Rem [* On [E]xit: restor cursor - return to previous WD - Delete scripts TEMP folder - End the local Environment *]
- Set "CLEANUP=ECHO/%\E%[?25h%\E%[21;1HDone. & POPD & RMDIR "%TEMP%\%~n0\Guard" /S /Q 2> Nul & Endlocal & Exit /B"
- Set "RestorePos=(set "Cpos=!Opos!"& <Nul Set /P "=%\E%[!Opos!H%\E%[!Col!mx%\E%[0m" &Set "_V=T")"
- Setlocal EnableDelayedExpansion
- For %%v in (!Valid!) Do <Nul Set /P "=%\E%[%%~vH%\E%[90m.%\E%[0m"
- Rem [* Hide cursor; Set and display Intial position *]
- <Nul Set /p "=%\E%[?25l%\E%[20;21Hx"&Set "Cpos=20;21"
- Rem [* Directional 'cursor move' sequences *]
- Set "D="&Set "W=%\E%D%\E%A"& Set "A=%\E%D%\E%D"& Set "S=%\E%B%\E%D"
- Set "Col=30"
- :loop
- If %COL% EQU 37 Set "Col=30"
- Set /A "Col+=1" & TITLE Move: [WASD] Exit: [E]
- Rem [* User input attempt move *]
- For /F "Delims=" %%O in ('Choice /N /C:WASDE')Do If "%%O" == "E" (%CLEANUP%)Else ( <Nul Set /p "=!%%O!")
- Rem [* move cursor using escape codes; test if position is valid -T:Display move -F:Restore Cursor to last saved valid position *]
- %CHKmove% & If "!_V!"=="T" (<Nul Set /p "=%\E%[!Col!mx%\E%[0m") Else (%RestorePos%)
- Goto :loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement