Skip to content

Commit

Permalink
Basic functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Jan 29, 2012
1 parent f3b7c89 commit 3f4d1ca
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 8 deletions.
1 change: 0 additions & 1 deletion data/config.json

This file was deleted.

1 change: 1 addition & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ app.use(flatiron.plugins.cli, {
app.cmd(/version/, commands.version);
app.cmd(/ls/, commands.list);
app.cmd(/check (.+)/, commands.check);
app.cmd(/undo (.+)/, commands.undo);
app.cmd(/rm (.+)/, commands.delete);
app.cmd(/(.+)/, commands.add);
105 changes: 98 additions & 7 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ var commands = module.exports;

/**
* Print alias.
*
* @api private
*/
var print = console.log;
commands.print = console.log;

/**
* The application.
Expand All @@ -31,6 +33,13 @@ var app = require('./app');
*/
var storage = require('./storage');

/**
* Formatter.
*
* @type {Object}
*/
var formatter = require('./formatter');

/**
* Prints current version.
*
Expand All @@ -40,18 +49,100 @@ commands.version = function() {
print(require('../package.json').version);
};

/**
* Lists todo items.
*
* @api public
*/
commands.list = function() {

var out = [];
storage.get('items', function(err, items) {
items || (items = []);
for (var i = -1, len = items.length; ++i < len;) {
if (!app.argv.all && items[i].done) continue;
out.push(formatter.format(items[i], i + 1));
}

out.push('') && out.unshift('');
out.map(function(line) {
commands.print(line);
});
});
};

commands.check = function(item) {

/**
* Marks an item as done.
*
* @param {String} Number.
* @api public
*/
commands.check = function(num) {
num = +num - 1;
commands.toggle(num, true);
};

commands.delete = function(item) {

/**
* Undo a check for item.
*
* @param {String} Number.
* @api public
*/
commands.undo = function(num) {
num = +num - 1;
commands.toggle(num, false);
};

/**
* Toggles an item state.
*
* @param {Number} Item index.
* @param {Boolean} State.
* @api private
*/
commands.toggle = function(num, state) {
storage.get('items', 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.save(function(err) {
if (err) throw err;
});
});
});
};

/**
* Deletes an item.
*
* @param {String} Todo item number.
* @api public
*/
commands.delete = function(num) {
num = +num - 1;
storage.get('items', function(err, items) {
items || (items = []);
items = items.splice(num, 1);
storage.save(function(err) {
if (err) throw err;
});
});
};

/**
* Adds new item to the todo list.
*
* @param {String} Item description.
* @api public
*/
commands.add = function(item) {
console.log(item);
storage.get('items', function(err, items) {
items || (items = []);
items.push({ text: item, done: false });
storage.set('items', items, function(err) {
storage.save(function(err) {
if (err) throw err;
});
});
});
};
35 changes: 35 additions & 0 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
* todo - Todos in the CLI like what.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* Module dependencies.
*/
var color = require('color');

/**
* Formatter namespace.
*
* @type {Object}
*/
var formatter = module.exports;

/**
* Formats an item.
*
* @param {Object} Todo item.
* @param {Number} Item number.
* @returns {String}
* @api public
*/
formatter.format = function(item, num) {
var state = item.done ? '√'.green : '✖'.red ;

return ' '
+ '#' + num + ' '
+ state + ' '
+ item.text;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, "dependencies": {
"flatiron": "~0.1.7"
, "storr": "0.0.1"
, "color": "0.4.1"
}
, "devDependencies": {
"mocha": "0.3.3"
Expand Down

0 comments on commit 3f4d1ca

Please sign in to comment.