Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic functionality.

  • Loading branch information...
commit 3f4d1ca0476aac12a8f7b4933a59a7b89194de0f 1 parent f3b7c89
@vesln authored
View
1  data/config.json
@@ -1 +0,0 @@
-{}
View
1  lib/app.js
@@ -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);
View
105 lib/commands.js
@@ -14,8 +14,10 @@ var commands = module.exports;
/**
* Print alias.
+ *
+ * @api private
*/
-var print = console.log;
+commands.print = console.log;
/**
* The application.
@@ -32,6 +34,13 @@ var app = require('./app');
var storage = require('./storage');
/**
+ * Formatter.
+ *
+ * @type {Object}
+ */
+var formatter = require('./formatter');
+
+/**
* Prints current version.
*
* @api public
@@ -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;
+ });
+ });
+ });
};
View
35 lib/formatter.js
@@ -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;
+};
View
1  package.json
@@ -7,6 +7,7 @@
, "dependencies": {
"flatiron": "~0.1.7"
, "storr": "0.0.1"
+ , "color": "0.4.1"
}
, "devDependencies": {
"mocha": "0.3.3"
Please sign in to comment.
Something went wrong with that request. Please try again.