批处理脚本:与高亮选择导航菜单 [英] Batch script: navigation menu with highlighted selection

查看:187
本文介绍了批处理脚本:与高亮选择导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迷恋凭借纯命令行的模仿的8位时代的程序功能的想法。所以我想做出能与标称选择菜单选项数组(因为不存在arrowkeys没有键盘映射),左,右方向键和Enter键确认。为了保持清晰有我写的一个工作示例。 请注意对于ANSI转义codeS我用了很多,不知道如果ESC控制字符将在浏览器中显示。

I am obsessed with idea of mimicking functionality of 8-bit era programs by virtue of pure command line. So I want to make an array of menu options which can be selected with nominal (since there are no keymap for arrowkeys) left and right keys and confirmed with Enter. To keep things clear there is a working example I wrote. Please pay attention for ANSI escape codes I use A LOT, not sure if ESC control character would display in browser.

@ECHO OFF

ECHO Chose and option with [36mZ[0m for left, [36mX[0m for right, [36mC[0m for confirm

:PICKMENU
SETLOCAL
SET menumsg="[7mExit [0m Next"
SET menumsgDef="[7mExit [0m Next"
SET menumsgNxt="[0mExit [7m Next"
:subpick
CHOICE /N /C zxc /m:[3;1H%menumsg%
IF %errorlevel% EQU 0 ECHO Just in case of errors & GOTO PICKMENU
IF %errorlevel% EQU 1 SET menumsg=%menumsgDef% & GOTO subpick
IF %errorlevel% EQU 2 SET menumsg=%menumsgNxt% & GOTO subpick
IF %errorlevel% EQU 3 GOTO confirm
:confirm
IF %menumsg% EQU %menumsgNxt% (ECHO [0mNext option is chosen & GOTO PICKMENU) ELSE (ECHO ^Exit is chosen! & TIMEOUT 2 >NUL & EXIT)
ENDLOCAL 

PAUSE

我的问题开始当我试着在某种功能,在那里我可以传递的,而不是使每一个我需要的菜单或对话框时hodgie code论据来包装这个菜单:

My problem begins when I try to wrap this menu in some kind of a function, where I could pass arguments instead of making hodgie code every time I need menu or dialog:

@ECHO OFF

REM ----So I start with setting variables with idea of argument string tokenisation on the mind----

SET $teststring=OptionOne OptionTwo OptionThree 
SET /A $currentitem=1

REM ----I would use function that I found here on Stackoverflow to count amount of options in my string.----
REM ----This is magic for me, without this solution I would be using FOR loop and delims----

CALL :COUNTOPTIONS %$teststring%

:COUNTOPTIONS
SETLOCAL EnableDelayedExpansion
SET /A Count=0
:repeatme
IF NOT "%1"=="" (
SET /A Count+=1
SHIFT
GOTO :repeatme
)
ENDLOCAL & SET $optionsamount=%Count%

ECHO The number of words is %$optionsamount%

PAUSE

:PICKCURRENT

REM ----So I got maximum number of options, I guess it would be useful since by design user could stumble upon the
REM ----edge of menu by occasionaly pressing "left" or "right" controls too much. Thats how I limit the variable...

IF %$currentitem% LSS 1 SET /A $currentitem=1
IF %$currentitem% GTR %$optionsamount% SET /A $currentitem=%$optionsamount%
ECHO %$optionsamount%

REM ----This is where I completely lost it. I am trying to echo "options" string as separated items to the target location...-----

FOR /F "tokens=*" %%A IN ("%$teststring%") DO ECHO [1;1H%%A

REM ----...and echo "currently selected" item into the same place...

FOR /F "tokens=%$currentitem%" %%B IN ("%$itemstring%") DO SET $selector=[1;1H[7m%%B[0m

REM ----...and make it interactive with choice----

CHOICE /N /C zxc /m:%$selector%
IF %errorlevel% EQU 0 ECHO oops & GOTO PICKITEM
IF %errorlevel% EQU 1 SET /A "$currentitem=$currentitem-1" & GOTO PICKCURRENT
IF %errorlevel% EQU 2 SET /A "$currentitem=$currentitem+1" & GOTO PICKCURRENT
IF %errorlevel% EQU 3 GOTO confirm

PAUSE

(我铲剧本结构有利于说明我在做什么的)。

(I shoveled script structure in favor of illustrating what am I doing).

显然,我的问题是缺乏了解循环和功能,但ss64和激烈的google搜索通读参考并不那么启发为止。所以我会AP preciate指向教育材料,以及对我的code注释。我想强调的是,问题的声明并不意味着额外的库或资源工具包的使用。

Obviously, my problem is in lack of understanding loops and functions, but reading trough reference on ss64 and intense googling were not that enlightening so far. So I would appreciate pointing to education materials as well as comments on my code. I'd like to emphasize that the problem statement doesn't imply usage of additional libraries or resource kits.

推荐答案

好吧,我能找到自己的解决方案就在几个不眠之夜天:

Well, I was able to find solution myself just in few sleepless days:

@ECHO OFF
:SELECTLOOP
SETLOCAL EnableDelayedExpansion
SET /A current=1
:subpick
ECHO ------
SET /A count=0
FOR /F "delims=" %%M IN ("ONE TWO THREE FOUR FIVE") DO FOR %%N IN (%%M) DO (
SET /A count+=1
SET str!count!=%%N
IF !count! NEQ !current! (ECHO %%N) ELSE (ECHO ^>%%N^<))

CHOICE /N /C zxc /m:"------"
IF %errorlevel% EQU 0 ECHO error message & GOTO SELECTLOOP
IF %errorlevel% EQU 1 set /A current-=1 & GOTO subcheck
IF %errorlevel% EQU 2 set /A current+=1 & GOTO subcheck 
IF %errorlevel% EQU 3 (ECHO !current! IS CONFIRMED) & (TIMEOUT 3 >NUL)

:subcheck
CLS
IF !current! LSS 1 SET /A current=1
IF !current! GTR !count! SET /A current=!count!
GOTO subpick

PAUSE

这篇关于批处理脚本:与高亮选择导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆