-2

I am creating a SQL shell using jQuery Terminal. I want to add sql syntax highlighter so that when ever user types a query, SQL keywords get highlighted.

There are many jQuery plugins for syntax highlighting but I'm unbale to plugin them in terminal.

Any help is much appreciated. TIA.

SaurabhR
  • 129
  • 3
  • 10

1 Answers1

1

If you want to have syntax highlighting while you type (before you press enter) then it will be hard or even impossible to do, the terminal had formatting while you type but I remove it because there where issues with wrapping long lines.

If you want to have syntax highlight when user type enter then you can use this code:

// mysql keywords
var uppercase = [
    'ACCESSIBLE', 'ADD', 'ALL', 'ALTER', 'ANALYZE', 'AND', 'AS', 'ASC',
    'ASENSITIVE', 'BEFORE', 'BETWEEN', 'BIGINT', 'BINARY', 'BLOB',
    'BOTH', 'BY', 'CALL', 'CASCADE', 'CASE', 'CHANGE', 'CHAR',
    'CHARACTER', 'CHECK', 'COLLATE', 'COLUMN', 'CONDITION',
    'CONSTRAINT', 'CONTINUE', 'CONVERT', 'CREATE', 'CROSS',
    'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER',
    'CURSOR', 'DATABASE', 'DATABASES', 'DAY_HOUR', 'DAY_MICROSECOND',
    'DAY_MINUTE', 'DAY_SECOND', 'DEC', 'DECIMAL', 'DECLARE', 'DEFAULT',
    'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC',
    'DISTINCT', 'DISTINCTROW', 'DIV', 'DOUBLE', 'DROP', 'DUAL', 'EACH',
    'ELSE', 'ELSEIF', 'ENCLOSED', 'ESCAPED', 'EXISTS', 'EXIT',
    'EXPLAIN', 'FALSE', 'FETCH', 'FLOAT', 'FLOAT4', 'FLOAT8', 'FOR',
    'FORCE', 'FOREIGN', 'FROM', 'FULLTEXT', 'GRANT', 'GROUP', 'HAVING',
    'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE', 'HOUR_SECOND',
    'IF', 'IGNORE', 'IN', 'INDEX', 'INFILE', 'INNER', 'INOUT',
    'INSENSITIVE', 'INSERT', 'INT', 'INT1', 'INT2', 'INT3', 'INT4',
    'INT8', 'INTEGER', 'INTERVAL', 'INTO', 'IS', 'ITERATE', 'JOIN',
    'KEY', 'KEYS', 'KILL', 'LEADING', 'LEAVE', 'LEFT', 'LIKE', 'LIMIT',
    'LINEAR', 'LINES', 'LOAD', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCK',
    'LONG', 'LONGBLOB', 'LONGTEXT', 'LOOP', 'LOW_PRIORITY',
    'MASTER_SSL_VERIFY_SERVER_CERT', 'MATCH', 'MEDIUMBLOB', 'MEDIUMINT',
    'MEDIUMTEXT', 'MIDDLEINT', 'MINUTE_MICROSECOND', 'MINUTE_SECOND',
    'MOD', 'MODIFIES', 'NATURAL', 'NOT', 'NO_WRITE_TO_BINLOG', 'NULL',
    'NUMERIC', 'ON', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', 'OR', 'ORDER',
    'OUT', 'OUTER', 'OUTFILE', 'PRECISION', 'PRIMARY', 'PROCEDURE',
    'PURGE', 'RANGE', 'READ', 'READS', 'READ_WRITE', 'REAL',
    'REFERENCES', 'REGEXP', 'RELEASE', 'RENAME', 'REPEAT', 'REPLACE',
    'REQUIRE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'RLIKE',
    'SCHEMA', 'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT', 'SENSITIVE',
    'SEPARATOR', 'SET', 'SHOW', 'SMALLINT', 'SPATIAL', 'SPECIFIC',
    'SQL', 'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING', 'SQL_BIG_RESULT',
    'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT', 'SSL', 'STARTING',
    'STRAIGHT_JOIN', 'TABLE', 'TERMINATED', 'THEN', 'TINYBLOB',
    'TINYINT', 'TINYTEXT', 'TO', 'TRAILING', 'TRIGGER', 'TRUE', 'UNDO',
    'UNION', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'UPDATE', 'USAGE', 'USE',
    'USING', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', 'VALUES',
    'VARBINARY', 'VARCHAR', 'VARCHARACTER', 'VARYING', 'WHEN', 'WHERE',
    'WHILE', 'WITH', 'WRITE', 'XOR', 'YEAR_MONTH', 'ZEROFILL'];
var keywords = uppercase.concat(uppercase.map(function(keyword) {
    return keyword.toLowerCase();
}));
$.terminal.defaults.formatters.push(function(string) {
    return string.split(/((?:\s| )+)/).map(function(string) {
        if (keywords.indexOf(string) != -1) {
            return '[[b;blue;]' + string + ']';
        } else {
            return string;
        }
    }).join('');
});

you need to use /((?:\s| )+)/ regex because echo_command function was escaping the string (replacing spaces by  ) before calling echo, it can be /(\s+)/ in next 0.12 version.

EDIT: from version 1.0.0 it will also highlight while you're typing.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • In devel branch (it will be in 0.12 version) formatters now work with command line so the example will work while you're typing your SQL command. – jcubic Jan 14 '17 at 21:05
  • This is working perfectly as what was needed. I have pulled the code from 0.12 branch. So now my SQL keywords gets highlighted as I type the command. This is really a great plugin. And thanks for your prompt replies. – SaurabhR Jan 21 '17 at 06:23
  • Small question: When will devel branch go into production? I think there are some small issues in the code. E.g. After replacing terminal.js with devel version, a small iframe has started appearing on my terminal UI. – SaurabhR Jan 21 '17 at 07:55
  • @SaurabhR you need to use devel version of CSS, the iframe is hidden by css. I think that I'll publish 0.12 in the end of January. – jcubic Jan 21 '17 at 09:44
  • I used devel CSS and the iframe is gone....But still there are many features which are not working in devel branch. E.g. Ctrl+L takes focus to URL bar E.g. Up/Down arrow keys dosn't show history When will this branch go in production? – SaurabhR Feb 13 '17 at 10:33
  • @SaurabhR if you have older browser or Safari then you need to use [key property polyfill](https://github.com/cvan/keyboardevent-key-polyfill). If your browser don't support key property you should have exception in console (it came from keydown event in cmd plugin so it's not on the terminal). – jcubic Feb 13 '17 at 12:36
  • @SaurabhR that devel version you were using was released, if you notice some bugs you should report them on [github](https://github.com/jcubic/jquery.terminal/issues/new). – jcubic Feb 14 '17 at 08:54
  • I have pulled the latest version...and it works flawlessly. Once again Thanks. – SaurabhR Feb 16 '17 at 10:02
  • The formatter also changes the content which I echo on terminal. i.e. If the output of my sql query contains some SQL keywords, even those get formatted. How to avoid that? – SaurabhR Feb 17 '17 at 09:47
  • @SaurabhR it should work if you pass option `formatters: false` to echo. – jcubic Feb 17 '17 at 13:20