2

我正在使用http://terminal.jcubic.pl/创建一个 jQuery 终端。

我想在用户成功登录后更改提示。例如。我想将提示从 # 更改为 root@mac:

我对 jQuery 有点陌生。请帮忙。

// 这就是我创建终端的方式

var terminal = $('#jq_terminal').terminal(function(command, term) {
        term.echo("you just typed " + command);
    }, {  
        login: f_login, 
        greetings: "You are authenticated",
        prompt: '#',
        name: 'shell',
            onBlur: function() {
                    return false;
                }
    });

// Login 
function f_login(user, password, callback) {    
    // make a soket.IO call here. SocketIO will call f_login_response
}

// Login Response here
function f_login_response(user, password, callback) {   
    // How do I change the terminal prompt here ?
    // I tried 
    terminal.prompt = '$'; // Does not work
}
4

1 回答 1

2

提示可以是一个函数:

var login = false;
var terminal = $('#jq_terminal').terminal(function(command, term) {
    term.echo("you just typed " + command);
}, {  
    login: f_login, 
    greetings: "You are authenticated",
    prompt: function(callback) {
       callback(login ? '#' : '$');
    },
    name: 'shell',
    onBlur: function() {
        return false;
    }
});

你也可以使用terminal.set_prompt('#');. 您无法在登录功能中访问终端,代码代码应该这样做authenticate.apply(self, ....),所以您可以这样做this.set_prompt(我需要解决这个问题),但如果您像以前一样将终端设置为 var (我在示例代码中),您应该能够访问终端。所以这应该工作:

function f_login_response(user, password, callback) {
   terminal.set_prompt('$');
   //or
   login = true;
}

此外,如果您想为登录用户和来宾使用两种类型的命令,那么您可以执行以下操作:

function(commad, term) {
   var cmd = $.terminal.parseCommand(command); // parseCommand is helper that process command line (you have also splitCommand that don't convert to numbers and regexes
   if (cmd.name == 'login') {
       term.push(function(command) {
          term.echo("You type normal command");
       }, {
           prompt: '#'
           // I need to add login option to `push` so you can do term.push('foo.php', {login:true});
       }).login(function(user, password, callback) {
          if (<< User ok >>) {
             callback('TOKEN'); 
          } else {
             callback(null); // callback have also second parameter if you use true it will not give error message
          }
       });
   } else {
       term.echo("You type guest command");
   }
}

PS:在unix$中为普通用户和#root用户。

编辑:从 0.8.3 版开始,您可以调用:

term.push('rpc_service.php', {login: true});

login(user, pass, callback) {
   this.set_prompt('#');
}
于 2014-03-06T15:17:22.327 回答