2

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
}

1 Answer 1

2

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('#');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.