跳转到内容

Rebol 编程/输入

来自维基教科书,开放的书籍,开放的世界
INPUT /hide 

从控制台输入字符串。 换行符将被移除。

INPUT 是一个函数值。

  • /hide -- 用 * 字符遮蔽输入

源代码

[编辑 | 编辑源代码]
input: func [
    {Inputs a string from the console. New-line character is removed.} 
    /hide "Mask input with a * character" 
    /local console-port buffer char ignore ignore-chars
][
    either hide [
        console-port: open/binary [scheme: 'console] 
        buffer: make string! 10 
        ignore: make binary! 10 
        ignore-chars: charset ["^@^["] 
        while [
            wait console-port 
            char: to-char first console-port 
            (char <> newline) and (char <> #"^M")
        ] [
            any [
                all [char = #"^H" 
                    either not empty? buffer [
                        clear back tail buffer 
                        prin ["^H ^H"]
                    ] [true] 
                    true
                ] 
                all [find ignore-chars char 
                    read-io console-port ignore 3 
                    true
                ] 
                all [append buffer char 
                    prin #"*" 
                    false
                ]
            ]
        ] 
        prin newline 
        close console-port 
        buffer
    ] [
        pick system/ports/input 1
    ]
]
华夏公益教科书