Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/cmd/addsubtask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

const log = require('../utils/log.js');
const config = require('../utils/config.js');
const { prompt } = require('../utils/prompt.js');

const finish = require('../utils/finish.js');


/**
* This command adds a subtask to an existing task (Pro accounts only)
* @param args [parentTaskIndex, subtaskName...]
* @param env
*/
function action(args, env) {

// Need at least a parent task index
if ( args.length === 0 || args[0].length === 0 ) {
prompt('Parent Task Index:', 'Subtask Name:', _promptFinished);
}
else if ( args[0].length === 1 ) {
// Have parent index but no subtask name
let parentIndex = parseInt(args[0][0]);
if ( isNaN(parentIndex) ) {
log.spinner.error("Parent task index must be a number");
return finish();
}
prompt('Subtask Name:', function(answers) {
for ( let i = 0; i < answers.length; i++ ) {
_process(parentIndex, answers[i][0], i+1, answers.length);
}
});
}
else {
// Have both parent index and subtask name
let parentIndex = parseInt(args[0][0]);
if ( isNaN(parentIndex) ) {
log.spinner.error("Parent task index must be a number");
return finish();
}
let subtaskName = args[0].slice(1).join(' ');
_process(parentIndex, subtaskName);
}

}


/**
* Process the returned prompt answers
* @private
*/
function _promptFinished(answers) {
for ( let i = 0; i < answers.length; i++ ) {
let parentIndex = parseInt(answers[i][0]);
let subtaskName = answers[i][1];
if ( isNaN(parentIndex) ) {
log.spinner.error("Parent task index must be a number");
continue;
}
_process(parentIndex, subtaskName, i+1, answers.length);
}
}


/**
* Process the request to add a subtask
* @private
*/
function _process(parentIndex, subtaskName, count=1, max=1) {
log.spinner.start("Adding Subtask...");
config.user(function(user) {

// Add Subtask
user.tasks.addSubtask(parentIndex, subtaskName, function(err) {
if ( err ) {
if ( err.code === 4040 ) {
log.spinner.error("Subtasks require a Pro account");
}
else if ( err.code === 4050 ) {
log.spinner.error("Invalid parent task (index: " + parentIndex + ")");
}
else if ( err.code === 4060 ) {
log.spinner.error("Subtask nested too deep (max 3 levels)");
}
else if ( err.code === 4070 ) {
log.spinner.error("Cannot add subtask: repeating task in hierarchy");
}
else {
log.spinner.error("Could not add subtask (" + err.msg + ")");
}
}
_processFinished(count, max);
});
});
}

/**
* Request Callback
* @private
*/
function _processFinished(count, max) {
log.spinner.start("Adding Subtask [" + count + "/" + max + "]...");
if ( count === max ) {
log.spinner.success("Subtask(s) Added");
return finish();
}
}



module.exports = {
command: 'addsubtask [args...]',
alias: 'as',
description: 'Add a subtask to an existing task (Pro accounts only)',
action: action
};
5 changes: 5 additions & 0 deletions src/cmd/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ function action(args, env) {
printIndicator('subtask',task);
}

// Display parent task indicator
if (task.hasSubtasks) {
printIndicator('parentTask',task);
}

// Add the Task Name
log.style(task.name+' ', namestyle);

Expand Down
6 changes: 6 additions & 0 deletions src/cmd/lsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ function action(args, env) {
printIndicator('subtask',task);
}

// Display parent task indicator
if (task.hasSubtasks) {
log.style(' ');
printIndicator('parentTask',task);
}

// Print the Task Name
log.style(' ');
log.style(task.name + ' ', priStyle);
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/lsp.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ function action(args, env) {
printIndicator('subtask',task);
}

// Display parent task indicator
if (task.hasSubtasks) {
log.style(' ');
printIndicator('parentTask',task);
}

// Print the Task Name
log.style(' ');
log.style(task.name + ' ', priStyle);
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function displayTask(taskDetails) {
debug(taskDetails)
let index = taskDetails.index;
// eslint-disable-next-line no-unused-vars
const { _list, list_id, location_id, taskseries_id, task_id, _index, name, priority, start, due, completed, isRecurring, recurrenceRuleRaw, isSubtask, estimate, url, tags, notes, ...otherAttributes } = taskDetails.task;
const { _list, list_id, location_id, taskseries_id, task_id, _index, name, priority, start, due, completed, isRecurring, recurrenceRuleRaw, isSubtask, hasSubtasks, estimate, url, tags, notes, ...otherAttributes } = taskDetails.task;

const listName = LIST_MAP.get(list_id) || "Not found";
const locationName = LOCATION_MAP.get(location_id) || "Not found";
Expand All @@ -133,6 +133,8 @@ function displayTask(taskDetails) {

log.style(`Is Subtask: `,styles.index)
log(`${isSubtask}`)
log.style(`Has Subtasks: `,styles.index)
log(`${hasSubtasks}`)
log.style(`Estimate: `,styles.index)
log(humanizeDuration(estimate))
log.style(`Location: `,styles.index)
Expand Down
7 changes: 5 additions & 2 deletions src/utils/printIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ function printIndicator(type,task) {
let iconType = config.get().iconType;

let indicatorStyle = task.isCompleted ? styles.completed : styles[type];
let notesIndicator,urlIndicator,recurringIndicator,subTaskIndicator;
let notesIndicator,urlIndicator,recurringIndicator,subTaskIndicator,parentTaskIndicator;
iconType = iconType || 'text'; // defaults to text if nothing included
switch (iconType) {
case 'emoji':
notesIndicator = '📓';
urlIndicator = '🔗';
recurringIndicator = '🔁';
subTaskIndicator = '⤴️ '
parentTaskIndicator = '📋 '
break;
case 'text':
default:
notesIndicator = '*';
urlIndicator = '+';
recurringIndicator = 'r';
subTaskIndicator = '(s) '
parentTaskIndicator = '(p) '
break;
}
let indicators = {
notes: notesIndicator,
url: urlIndicator,
recurring: recurringIndicator,
subtask: subTaskIndicator
subtask: subTaskIndicator,
parentTask: parentTaskIndicator
}
log.style(indicators[type], indicatorStyle);
}
Expand Down
Loading