1
0
Fork 0
mirror of https://github.com/marty-oehme/scripts.git synced 2024-12-22 07:58:08 +00:00

Added few log calls and code comments.

This commit is contained in:
Filip Makowski 2017-06-11 21:56:35 +02:00
parent 2887d10710
commit 6948de72ca

View file

@ -41,7 +41,7 @@ QtObject {
// export selected data to Taskwarrior // export selected data to Taskwarrior
script.registerCustomAction("exportToTaskwarrior", "Export selected list as Taskwarrior tasks", "Export to Taskwarrior"); script.registerCustomAction("exportToTaskwarrior", "Export selected list as Taskwarrior tasks", "Export to Taskwarrior");
// create a menu entry "Create meeting note" with a button and a freedesktop theme icon // import selected projects from Taskwarrior
script.registerCustomAction("importFromTaskwarrior", "Import tasks from Taskwarrior as a list", "Import from Taskwarrior"); script.registerCustomAction("importFromTaskwarrior", "Import tasks from Taskwarrior as a list", "Import from Taskwarrior");
} }
@ -77,6 +77,7 @@ QtObject {
var isProjectName = projectRegExp.exec(str); var isProjectName = projectRegExp.exec(str);
if (isProjectName) { if (isProjectName) {
var projectName = isProjectName[2]; var projectName = isProjectName[2];
// Header level is simply the number of "#" characters
var headerLevel = isProjectName[1].length; var headerLevel = isProjectName[1].length;
func(projectName, headerLevel); func(projectName, headerLevel);
return true; return true;
@ -84,6 +85,7 @@ QtObject {
} }
function logIfVerbose(str) { function logIfVerbose(str) {
// Logs only, if `verbose` setting is enabled.
if (verbose) { if (verbose) {
script.log(str); script.log(str);
} }
@ -107,6 +109,7 @@ QtObject {
// We are keeping the project name as a array of strings. We will concatenate them to // We are keeping the project name as a array of strings. We will concatenate them to
// get the final projectName with nesting. // get the final projectName with nesting.
var projectName = []; var projectName = [];
// The reference header level is useful in case selection does not start with single "#".
var referenceHeaderLevel = 0; var referenceHeaderLevel = 0;
// For each line, we are gathering data to properly create tasks. // For each line, we are gathering data to properly create tasks.
@ -115,11 +118,15 @@ QtObject {
logIfVerbose("Detected project name: " + proName); logIfVerbose("Detected project name: " + proName);
logIfVerbose("Detected header level: " + headerLevel); logIfVerbose("Detected header level: " + headerLevel);
// No project detected yet - define initial reference header level.
if (projectName.length === 0) { if (projectName.length === 0) {
referenceHeaderLevel = headerLevel - 1; referenceHeaderLevel = headerLevel - 1;
} }
// Project name on the same or lower hierarchy level means, that we need
// to pop last or multiple project name segments.
if (projectName.length + referenceHeaderLevel >= headerLevel) { if (projectName.length + referenceHeaderLevel >= headerLevel) {
logIfVerbose("Detected similar or lower header level");
var i; var i;
for (i = projectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) { for (i = projectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) {
projectName.pop(); projectName.pop();
@ -162,6 +169,9 @@ QtObject {
case "importFromTaskwarrior": case "importFromTaskwarrior":
// Get selected text to determine the project we want to import from. // Get selected text to determine the project we want to import from.
logIfVerbose("Importing tasks from Taskwarrior.");
// Saving selected text - we will modify it later.
var plainText = getSelectedTextAndSeparateByNewline(); var plainText = getSelectedTextAndSeparateByNewline();
var projectNames = []; var projectNames = [];
@ -183,7 +193,7 @@ QtObject {
var newProjectName = projectNames[projectNames.length - 1].slice(); var newProjectName = projectNames[projectNames.length - 1].slice();
logIfVerbose("Last project name inserted was " + newProjectName.join('.')); logIfVerbose("Last project name inserted was " + newProjectName.join('.'));
if (newProjectName.length + referenceHeaderLevel >= headerLevel) { if (newProjectName.length + referenceHeaderLevel >= headerLevel) {
logIfVerbose("Same header level detected."); logIfVerbose("Detected similar or lower header level");
var i; var i;
for (i = newProjectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) { for (i = newProjectName.length + referenceHeaderLevel - headerLevel + 1; i > 0; i--) {
newProjectName.pop(); newProjectName.pop();
@ -198,6 +208,9 @@ QtObject {
logIfVerbose("Project name detected. Inserted value is " + newProjectName.join('.')) logIfVerbose("Project name detected. Inserted value is " + newProjectName.join('.'))
})) return; })) return;
// We remember lines, that have project names in them. Those will be our anchors after
// which we will insert fetched tasks.
projectNameLines.push(currentLineNo); projectNameLines.push(currentLineNo);
}); });
@ -240,19 +253,25 @@ QtObject {
var taskIds = []; var taskIds = [];
tasksSeparated.forEach( function(task){ tasksSeparated.forEach( function(task){
// Splitting output to be used later
var taskParamsRegexp = /(\d+)[\s*]?(.+)?[\s*]?/i; var taskParamsRegexp = /(\d+)[\s*]?(.+)?[\s*]?/i;
var fetchTaskParams = taskParamsRegexp.exec(task); var fetchTaskParams = taskParamsRegexp.exec(task);
logIfVerbose("Extracted data from task: ID " + fetchTaskParams[1] + " Desc: " + fetchTaskParams[2]); logIfVerbose("Extracted data from task: ID " + fetchTaskParams[1] + " Desc: " + fetchTaskParams[2]);
var taskEntry = "* " + fetchTaskParams[2]; var taskEntry = "* " + fetchTaskParams[2];
logIfVerbose("Inserting \"" + taskEntry + "\" after line " + projectNameLines[currentProjectNumber - 1]);
plainText.splice(projectNameLines[currentProjectNumber - 1], 0, taskEntry); plainText.splice(projectNameLines[currentProjectNumber - 1], 0, taskEntry);
// We are updating line number assigned to detected project names.
var i; var i;
for (i = currentProjectNumber; i < projectNameLines.length; i++) { for (i = currentProjectNumber; i < projectNameLines.length; i++) {
projectNameLines[i] += 1; projectNameLines[i] += 1;
} }
// We gather task IDs in case deleteOnImport is enabled.
taskIds.push(fetchTaskParams[1]); taskIds.push(fetchTaskParams[1]);
}); });
if (deleteOnImport) { if (deleteOnImport) {
logIfVerbose("Deleting tasks " + taskIds.join(','));
script.startDetachedProcess(taskPath, script.startDetachedProcess(taskPath,
[ [
taskIds.join(' '), taskIds.join(' '),
@ -264,6 +283,7 @@ QtObject {
}); });
// Finally, selected text is replaced by the text with insertions.
script.noteTextEditWrite(plainText.join('\n')); script.noteTextEditWrite(plainText.join('\n'));
break; break;