Advertisement
T3RRYT3RR0R

Capture cursor Pos

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