Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Also enable multiple lists #6

Closed
wants to merge 4 commits into from

4 participants

@fudanchii

In addition to previous pull request, these commits also enable multiple lists.
Use it like this

todo @work Come to office earlier.
todo @work ls
todo @work ls --all

etc...

@shinuza

Closes #2

@tttp

Hi,

trying

todo @home ls
but it creates a new todo ("@home ls") instead of listing all the todo in the @home context.

Did I miss something obvious?

@vesln
Owner

Hey guys, sorry for the huge delay. All of this will be implemented in the next version. I want to refactor most of the internals of the app. Hopefully I'll find some free time during the holidays. This project is actually something that I use on daily basis, but due to the craziness of the startup world, it never made it to the top of my list until now. I'm looking forward to making it happen as soon as possible.

@vesln vesln closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2012
  1. @fudanchii
  2. @fudanchii

    enable init data dir

    fudanchii authored
Commits on Jan 30, 2012
  1. @fudanchii

    Enable multi list support

    fudanchii authored
  2. @fudanchii

    Remove trailing comma

    fudanchii authored
This page is out of date. Refresh to see the latest.
Showing with 88 additions and 13 deletions.
  1. +4 −1 lib/app.js
  2. +8 −1 lib/cli.js
  3. +74 −9 lib/commands.js
  4. +2 −2 lib/storage.js
View
5 lib/app.js
@@ -26,14 +26,17 @@ app.use(flatiron.plugins.cli, {
'Usage:',
'',
' todo Go shopping. - Adds new item.',
+ ' todo init - Init data dir at $HOME/.todo',
' todo ls. - Lists not finished items.',
' todo ls --all - Lists all items.',
' todo rm 1 - Removes #1 item.',
' todo check 1 - Marks #1 item as done.',
' todo undo 1 - Marks #1 item as not done yet.',
' todo clear - Clears the whole list.',
+ ' todo @home Sleep. - Adds new item to `home` todo list.',
+ ' todo @home <cmd> - Exec command `cmd` for `home` todo list.',
' todo version - Lib version.',
'',
'Author: Veselin Todorov <hi@vesln.com>'
]
-});
+});
View
9 lib/cli.js
@@ -37,5 +37,12 @@ app.cmd(/undo (.+)/, commands.undo);
// Removes a todo item.
app.cmd(/rm (.+)/, commands.delete);
+// Init data dir
+app.cmd(/init/, commands.init);
+
+// Multi list support command
+// list name prefixed with '@'
+app.cmd(/@(\S+) (\w+)(.*)/, commands.operate);
+
// Adds new todo item.
-app.cmd(/(.+)/, commands.add);
+app.cmd(/(.+)/, commands.add);
View
83 lib/commands.js
@@ -41,6 +41,14 @@ var storage = require('./storage');
var formatter = require('./formatter');
/**
+ * To-Do list name
+ * Default to items
+ *
+ * @type {String}
+ */
+var list = 'items';
+
+/**
* Prints current version.
*
* @api public
@@ -56,7 +64,9 @@ commands.version = function() {
*/
commands.list = function() {
var out = [];
- storage.get('items', function(err, items) {
+ var from = '\n '+list+':';
+ console.log(from);
+ storage.get(list, function(err, items) {
items || (items = []);
for (var i = -1, len = items.length; ++i < len;) {
if (!app.argv.all && items[i].done) continue;
@@ -100,11 +110,11 @@ commands.undo = function(num) {
* @api private
*/
commands.toggle = function(num, state) {
- storage.get('items', function(err, items) {
+ storage.get(list, function(err, items) {
items || (items = []);
if (!items[num]) throw new Error('There is no todo item with number ' + num + 1);
items[num].done = state;
- storage.set('items', items, function() {
+ storage.set(list, items, function() {
storage.save(function(err) {
if (err) throw err;
});
@@ -120,10 +130,10 @@ commands.toggle = function(num, state) {
*/
commands.delete = function(num) {
num = +num - 1;
- storage.get('items', function(err, items) {
+ storage.get(list, function(err, items) {
items || (items = []);
items.splice(num, 1);
- storage.set('items', items, function() {
+ storage.set(list, items, function() {
storage.save(function(err) {
if (err) throw err;
});
@@ -138,7 +148,7 @@ commands.delete = function(num) {
* @api public
*/
commands.clear = function(num) {
- storage.set('items', [], function(err, items) {
+ storage.set(list, [], function(err, items) {
storage.save(function(err) {
if (err) throw err;
});
@@ -152,13 +162,68 @@ commands.clear = function(num) {
* @api public
*/
commands.add = function(item) {
- storage.get('items', function(err, items) {
+ storage.get(list, function(err, items) {
items || (items = []);
items.push({ text: item, done: false });
- storage.set('items', items, function(err) {
+ storage.set(list, items, function(err) {
storage.save(function(err) {
if (err) throw err;
});
});
});
-};
+};
+
+/**
+ * Initialize data dir.
+ *
+ * @api public
+ */
+commands.init = function() {
+ var fs = require('fs'),
+ path = require('path');
+ var _root_ = process.env.HOME,
+ dir = path.join(_root_, '.todo');
+
+ (function(dir) {
+ var result = false;
+ fs.mkdir(dir, 0755, function(err) {
+ if (err) {
+ console.log('Can not initialize data dir.');
+ } else {
+ console.log('Data dir ['+dir+'] created.');
+ }
+ });
+ })(dir);
+};
+
+
+/**
+ * Commands lookup table
+ *
+ * @type {Object}
+ */
+var opt_table = {
+ 'ls': commands.list,
+ 'clear': commands.clear,
+ 'check': commands.check,
+ 'undo': commands.undo,
+ 'rm': commands.delete
+};
+
+/**
+ * Do operation to specific list.
+ * Use lookup table.
+ *
+ * @param {String} Todo list name.
+ * @param {String} Command to execute.
+ * @param {String} Command's option/arg.
+ * @api public
+ */
+commands.operate = function (to, cmd, option) {
+ list = to;
+ if (typeof opt_table[cmd] === 'undefined') {
+ commands.add(cmd + option);
+ } else {
+ opt_table[cmd](option);
+ }
+}
View
4 lib/storage.js
@@ -16,11 +16,11 @@ var path = require('path');
*
* @type {String}
*/
-var db = path.join(__dirname, '..', 'data', 'db.json');
+var db = path.join(process.env.HOME, '.todo', 'db.json');
/**
* Storage object.
*
* @type {Object}
*/
-var storage = module.exports = new Storr(db);
+var storage = module.exports = new Storr(db);
Something went wrong with that request. Please try again.