Skip to content
Open
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
80 changes: 70 additions & 10 deletions todo.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global module: false */

module.version = 0.5;
module.version = 0.92;
module.users = {};
module.DB_FILENAME = "todo.json";

Expand Down Expand Up @@ -33,11 +33,13 @@ module.cmd_todo = function cmd_todo(e) {
if (e.args) {
list.push(e.args);
this.saveUsers();
e.notice("Ok.");
// e.notice("Ok.");
} else if (!list.length) {
e.reply("Nothing on your todo list.");
} else {
e.reply(list.join("; "));
e.reply(list.map(function addIndex(todo, index) {
return "[{0}] {1}".format(index, todo);
}).join(" "));
}

return true;
Expand All @@ -49,20 +51,78 @@ module.cmd_tododel = function cmd_tododel(e) {
e.reply(this.cmd_tododel.help);
return true;
}
var index = e.args >>> 0;

var list = this.getList(e.nick);

if (index >= list.length) {
e.reply("You only have {0} things in your todo list.".format(list.length));
if (/^[0-9,]+$/.test(e.args)) {
var indexes = e.args.split(",").map(Number);

var notDeleted = 0;
for (var i = 0; i < indexes.length; i++) {
if (indexes[i] >= list.length) {
notDeleted++;
} else {
list.splice(indexes[i], 1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know about half the codebase is missing braces, but could we please move towards using braces everywhere? Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that not adding braces was the intended convention in this codebase. Will change these.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see how you were got that impression. Indeed, that used to be the convention in the codebase. I still haven't gotten around to fixing it all up though.

}
}

var deleted = indexes.length - notDeleted;
if (deleted != 0) {
this.saveUsers();
// e.notice("Ok, deleted {0} todo{1}.".format(deleted, deleted == 1 ? "" : "s"));
}
if (notDeleted > 0) {
e.reply("You only have {0} todo{1}.".format(list.length, list.length == 1 ? "" : "s"));
}
} else {
list.splice(index, 1);
e.notice("Ok.");
}
var filteredList = list.map(function (s, i) {
return {
elem: s,
index: i
};
}).filter(function (todo) {
return todo.elem.startsWith(e.args);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks confusing. How about something like:

var filteredList = list.filter(function (s) {
    return s.startsWith(e.args);
});

I believe I've shimmed the String.startsWith() method in aucgbot-utils.js, so this should be fine.


switch (filteredList.length) {
case 0:
e.reply('No todos starting with "{0}".'.format(e.args));
break;
case 1:
list.splice(list.indexOf(filteredList[0].index), 1);
this.saveUsers();
// e.notice("Ok, deleted \"{0}\".".format(filteredList[0].elem));
break;
default:
e.reply("Did you mean: {0}".format(filteredList.map(function (todo) {
return "[{0}] {1}".format(todo.index, todo.elem);
}).join(" ")));
}
}
return true;
};
module.cmd_tododel.help = "Delete an item from your todo list. Usage: tododel <index>";
module.cmd_tododel.help = "Delete an item from your todo list. Usage: tododel <comma separated indexes | beginning of todo>";

module.cmd_todoins = function cmd_todoins(e) {
var match = /^([0-9]+) (.+)$/.exec(e.args);
if (!match) {
e.reply(this.cmd_todoins.help);
return true;
}
var list = this.getList(e.nick);
var index = match[1] >>> 0, todo = match[2];

if (index >= list.length) {
e.reply("You only have {0} todo{1}.".format(list.length, list.length == 1 ? "" : "s"));
} else {
list.splice(index, 0, todo);
this.saveUsers();
// e.notice("Ok.");
}
return true;
}

module.cmd_todoins.help = "Inserts a todo before the specified index. Usage: todoins <index> <todo>";

try { module.loadUsers(); } catch (ex) {
println("Error while loading todo lists from disk: ", ex);
Expand Down