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()
  • 相关阅读:
    一只小爬虫(转)
    easyui +ASP.NET 前后台乱码解决方法
    轻松搞定 easyui datagrid 二次加载的问题(转)
    easyui combobox默认选中项
    VS2010新建Web网站与新建Web应用程序的区别 (转)
    关于html+ashx开发中几个问题的解决方法 (转)
    如何使用.net访问Access数据库 (转)
    ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
    Ajax 中正常使用jquery-easyui (转)
    会动的文字Marquee应用(转)
  • 原文地址:https://www.cnblogs.com/vaal-water/p/3711862.html
Copyright © 2011-2022 走看看