码迷,mamicode.com
首页 > 其他好文 > 详细

Prompting with a Default Option 创建默认提示

时间:2014-07-29 14:15:28      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   strong   io   

Introduction

This tutorial centers around various ways to prompt for user input with a default option available upon the user pressing Enter.

By http://lee-mac.com/promptwithdefault.html 

Note 1: On Prompt String Format

To achieve correct display of available options when Dynamic Input is enabled (i.e. DYNMODE = 1), the prompt string must be formatted in the following way:

[Option1/Option2/Option3] <Option1>

In the above example, Options 1, 2 & 3 are available for user selection with Option 1 as a default option - selected upon the user pressing Enter.

Note 2: On Global Variables

In the examples that follow, global variables are used to effectively ‘remember‘ a default option after program completion.

可以使用全局变量让程序将用户上次选择的选项作为默认选项。

To indicate where global variables are utilised, asterisks are included in variable names;

为了更清晰快速的看出全局变量,可在其前面添加(几个)*星号。//[‘?st?r?sks]n. 星号,星状物( asterisk的名词复数 )

this furthermore decreases the risk of variable names clashing with unlocalised variables from other programs.

这进一步避免了与其他程序中的局部变量重名的危险。

Case 1: Forcing User Input (No Default)

(initget 1 "Alpha Beta Gamma")
(setq ans (getkword "\nChoose [Alpha/Beta/Gamma]: "))

Bit code 1 of the initget function prevents the user from responding to the request by pressing Enter, thus forcing the user to select from the Alpha, Beta & Gamma options.

Case 2: Preset Default Option

In this case, a default option is available, however it is the same option everytime. The default option is hard-coded into the prompt string and may be selected following the user pressing Enter at the prompt.

 

Version 1

(initget "Alpha Beta Gamma")
(setq ans (cond ( (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: ") ) ( "Alpha" )))

If the user presses enter at the prompt, the getkword expression returns nil and so the cond function moves on to evaluate the next test condition: this is the default string "Alpha" which is a non-nil value and is hence returned by the cond function.

如果用户Getword返回nil(即用户没有做出选择),则将默认选择设置为 "Alpha"

Version 2

(initget "Alpha Beta Gamma")
(setq ans (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: "))
(if (not ans) (setq ans "Alpha"))

This second version uses the same logic as the first, however the conditional operator cond is replaced by an if statement. Hence, if the variable ‘ans‘ is nil, the default option is bound to it.

效果同Version 1,但是使用了If函数

Case 3: Dynamic Default

In this case, the variable *ans* is intended to be global (not localised in the function definition in which it is used). Since it is global, it does not lose any value bound to it following program completion.

 

Version 1(If)

(if (not *ans*) (setq *ans* "Alpha"))
(initget "Alpha Beta Gamma")
(setq *ans*
  (cond
    (
      (getkword
        (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ")
      )
    )
    ( *ans* )
  )
)

 

Version2 (Or)

(or *ans* (setq *ans* "Alpha"))
(initget "Alpha Beta Gamma")
(setq *ans*
  (cond
    (
      (getkword
        (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ")
      )
    )
    ( *ans* )
  )
)

A subtle variation on the previous example, this code replaces the if statement with the or function.

The or function will keep evaluating supplied expressions until either there are no more expressions to evaluate, or an expression returns a non-nil value.

Hence in the above example, should the *ans* variable be nil, the or function will proceed to evaluate the next expression, setting the *ans* variable to the first-time default value and in turn returning a non-nil value.

 

Version3 (Cond

(initget "Alpha Beta Gamma")
(setq *ans*
  (cond
    (
      (getkword
        (strcat "\nChoose [Alpha/Beta/Gamma] <"
          (setq *ans*
            (cond ( *ans* ) ( "Alpha" ))
          )
          ">: "
        )
      )
    )
    ( *ans* )
  )
)

Since the getkword statement is the first test expression supplied to the cond function, it is evaluated first, and, whilst using the strcat function to construct the getkword prompt string, the code ensures the variable *ans* has a value using the same logic as the previous examples.

Should the user now press Enter at the resultant prompt, this value is returned in the second test expression of the cond function, and is used in the next evaluation of the program providing an effectively ‘remembered‘ default.

 

Beyond the Examples

I have demonstrated how to prompt for user input with a default option available.

In all the examples, I have used the getkword function to prompt the user, however, the methods & logic illustrated above could be applied to the majority of the other getXXX user input functions (such as getreal, getdist etc.).

(setq *ans*
  (cond
    (
      (getint
        (strcat "\nChoose a Number <"
          (itoa
            (setq *ans*
              (cond ( *ans* ) ( 1 ))
            )
          )
          ">: "
        )
      )
    )
    ( *ans* )
  )
)

Note :

the use of itoa (Integer to ASCII) to convert the default integer value of *ans* to a string to be used in the concatenation of the prompt string.

cond

(cond [(test result ...) ...])

cond 函数的参数可以为任意数目的表。它按顺序对每一个表的第一项求值,直到其中之一的返回值不是 nil 为止。该函数接着对该项后续的其他表达式求值。

返回值 :

被执行的结果处理表达式中最后一个表达式的值。如果子表中只有一个表达式(即 result 不存在),则返回 test 的值。如果未指定参数,cond 返回 nil。

;;求绝对值
(cond 
   ((minusp a) (- a)) 
   (t a)
)

Prompting with a Default Option 创建默认提示,布布扣,bubuko.com

Prompting with a Default Option 创建默认提示

标签:style   blog   http   color   使用   os   strong   io   

原文地址:http://www.cnblogs.com/Giraffie/p/3875291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!