{ "name" : "WakaTime" , "tag" : [ "%SCRIPT", "var utils = {};", "utils.isValidApiKey = function(key) {", " if (!key) return false;", " var re = new RegExp('^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$', 'i');", " return re.test(key);", " };", "utils.urlToPath = function(path) {", " path = decodeURIComponent(path.toString());", " if (path.indexOf('file://') == 0)", " path = path.substring(7);", " return path;", "};", "", "var VERSION = '1.0.0';", "var EDITOR_VERSION = app.getVersion();", "var FREQUENCY = 2;", "var LAST_FILE = '';", "var LAST_TIME = null;", "var WAKATIME_CLI = null; // defined in event handler scope", "var TRIGGERS = {", " 2: '?txs-start',", " 64: '?save-file',", "};", "", "", "function initialize() {", " debug('Initializing WakaTime v' + VERSION + '...');", "}", "", "function onSave() {", " var currentFile = app.getCurrentFileName();", " if (currentFile) {", " currentFile = app.getAbsoluteFilePath(currentFile);", " sendHeartbeat(currentFile, true);", " }", "}", "", "function sendHeartbeat(file, isWrite) {", " debug('Sending heartbeat for file:' + file);", " file = utils.urlToPath(file);", " var args = [", " 'wakatime',", " '--verbose',", " '--entity',", " '\\"' + file + '\\"',", " '--plugin',", " '\\"texstudio/' + EDITOR_VERSION + ' texstudio-wakatime/' + VERSION + '\\"',", " ];", " if (isWrite)", " args.push('--write');", " if (typeof cursor !== 'undefined') {", " args.push('--lineno');", " args.push(cursor.lineNumber() + 1);", " var cursorpos = cursorPosition();", " if (cursorpos != null) {", " args.push('--cursorpos');", " args.push(cursorpos);", " }", " }", " ", " // run wakatime-cli in separate process", " system(args.join(' '));", "}", "", "function cursorPosition() {", " if (typeof editor === 'undefined') {", " return null;", " }", " ", " ", " var lines = editor.document().textLines();", " var cursorpos = 0;", " var lineno = cursor.lineNumber();", " var currentLine = 0;", " while (currentLine <= lineno) {", " if (currentLine == lineno) {", " cursorpos += cursor.columnNumber();", " } else {", " cursorpos += lines[currentLine].length + 1;", " }", " currentLine += 1;", " }", " return cursorpos;", "}", "", "var trigger = TRIGGERS[triggerId];", "switch (trigger) {", " case '?txs-start':", " initialize();", " break;", " case '?save-file':", " onSave();", " break;", "}" ], "description" : [ "" ], "abbrev" : "" , "trigger" : "?txs-start|?save-file" , "menu" : "Scripts" , "shortcut" : "Ctrl+Shift+F12" }