zoukankan      html  css  js  c++  java
  • node 命令行输入控件 prompt.js

     
     
    function print(){
        console.log.apply(console , arguments)
        }
    
    var step
        ,_lstStp
        ,_onConfirmInput
        ,_secret
    
    var action,step_keys
        ,step_index = 0
    
    function confirmDo(input){
        if (!_onConfirmInput || ! _onConfirmInput[input] ) return print(_lstStp + ' 输入未响应')
    
        _onConfirmInput[input]()
        }
    
    function toConfirm(tip , onY , onN){
        _lstStp = step
        step = '_confirm'
        print(tip , '确认 y ,取消 n')
        if ('function' != typeof onY)
            _onConfirmInput = onY
        else
            _onConfirmInput = {
                'y' : onY
                ,'n' :onN
                }
        }
    
    var stdin = process.openStdin()
    process.stdin.resume()
    process.stdin.setEncoding('utf8')
    process.stdin.setRawMode(false)
    
    var userInput = ''
    process.stdin.on("data", function(input) {
        ///console.log('input:' ,input ,'user:' , userInput)
        if (!_secret) {
            userInput = input
            onInput()
            return
            }
        input = input + ""
        function onInput(){
                userInput = userInput.toString().trim()
                if (step in action)
                    action[step](userInput)
                else
                    print('enter:' ,userInput)
                userInput = ''
            }
        switch (input) {
            case "
    ": case "
    ": case "u0004":
                //process.stdin.setRawMode(false)
                print('
    ')
                onInput()
                break
            case "u0003":
                // Ctrl C
                print('Cancelled')
                process.exit()
                break
            default:
                if (_secret) {
                    process.stdout.write('*')
                    userInput += input
                }
                //process.stdout.write(_secret ? '*' : input)
                break
            }
        })
    
    exports.print = print
    exports.toConfirm = toConfirm
    
    exports.next = function(secret ){
        process.stdin.setRawMode(secret)
        step_index++
        step = step_keys[step_index]
        if (step){
            action[step] && action[step]()
            _secret = !!secret
        }else{
            process.stdin.resume()
    
            }
        }
    
    exports.init = function (actMap , firstStep){
        action = actMap
        action._confirm = confirmDo
        step_keys =  Object.keys(action)
        step = step_keys[step_index]
        }
    var prom = require('./prompt.js')
    var action = {
            'hPath' : hPath
            ,'hPort': hPort
            ,'hDomain' : hDomain
            }
    prom.init(action)
    
    function hDomain(domain){
        ........................
    
        }
    function hPort(port){
        ...........
        prom.next()
       
        }
    
    function  hPath(to){
        ...........
        prom.next()
    
        }
    prompt.print('sth.')
    hPath()
  • 相关阅读:
    网站调整为黑白的方法
    滚动条样式优化
    js点击页面其他地方如何隐藏div元素菜单
    微信分享网页时自定义标题、描述和图片
    纯CSS3美化单选按钮radio
    纯CSS3实现圆形进度条动画
    解决checkbox的attr(checked)一直为undefined问题
    jQuery – 鼠标经过(hover)事件的延时处理
    PC版模块滚动不显示滚动条效果
    上传文件样式美化
  • 原文地址:https://www.cnblogs.com/vaal-water/p/3711862.html
Copyright © 2011-2022 走看看