当前位置: 首页 > 工具软件 > node-prompt > 使用案例 >

node 命令行输入控件 prompt.js

宇文俊风
2023-12-01
 
 

 

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 "\n": case "\r": case "\u0004":
            //process.stdin.setRawMode(false)
            print('\n')
            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()

 

转载于:https://www.cnblogs.com/vaal-water/p/3711862.html

 类似资料: