Domanda

I am using http://terminal.jcubic.pl/ to create a jQuery terminal.

I want to change the prompt after user successfully logged in. For an example. I want to change the prompt to root@mac from #:

I am bit new to jQuery. Please help.

// This is how i create my terminal

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
}
È stato utile?

Soluzione

The prompt can be a function:

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;
    }
});

you can also use terminal.set_prompt('#');. You can't access terminal in login function the code code should do authenticate.apply(self, ....) so you can do this.set_prompt (I need to fix that) but you should be able to access terminal if you assing it to var like you did (and I in example code). So this should work:

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

Also if you want to have two type of commands for logged in users and guests then you can do something like:

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: in unix $ for normal users and # is for root.

EDIT: From version 0.8.3 you can call:

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

and

login(user, pass, callback) {
   this.set_prompt('#');
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top