Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write command #3

Merged
merged 1 commit into from Feb 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/todo
Expand Up @@ -2,12 +2,12 @@

/*!
* todo - Todos in the CLI like what.
*
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* Dispatches the argv to the application.
*/
require('../lib/cli').start();
require('../lib/cli').start();
5 changes: 3 additions & 2 deletions lib/app.js
@@ -1,6 +1,6 @@
/*!
* todo - Todos in the CLI like what.
*
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/
Expand All @@ -13,7 +13,7 @@ var path = require('path');

/**
* The application object.
*
*
* @type {Object}
*/
var app = module.exports = flatiron.app;
Expand All @@ -33,6 +33,7 @@ app.use(flatiron.plugins.cli, {
' todo undo 1 - Marks #1 item as not done yet.',
' todo clear - Clears the whole list.',
' todo version - Lib version.',
' todo write - Write items to file.',
'',
'Author: Veselin Todorov <hi@vesln.com>'
]
Expand Down
9 changes: 6 additions & 3 deletions lib/cli.js
@@ -1,20 +1,20 @@
/*!
* todo - Todos in the CLI like what.
*
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* The application object.
*
*
* @type {Object}
*/
var app = module.exports = require('./app')

/**
* Commands.
*
*
* @type {Object}
*/
var commands = require('./commands');
Expand All @@ -37,5 +37,8 @@ app.cmd(/undo (.+)/, commands.undo);
// Removes a todo item.
app.cmd(/rm (.+)/, commands.delete);

// Writes todo to file.
app.cmd(/write (.+)/, commands.write);

// Adds new todo item.
app.cmd(/(.+)/, commands.add);
68 changes: 52 additions & 16 deletions lib/commands.js
@@ -1,48 +1,53 @@
/*!
* todo - Todos in the CLI like what.
*
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/

/**
* Commands namespace.
*
*
* @type {Object}
*/
var commands = module.exports;

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

/**
* The application.
*
*
* @type {Object}
*/
var app = require('./app');

/**
* Storage. Just an alias to application config.
*
*
* @type {Object}
*/
var storage = require('./storage');

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

/**
* File writing.
*/
var fs = require('fs');

/**
* Prints current version.
*
*
* @api public
*/
commands.version = function() {
Expand All @@ -51,7 +56,7 @@ commands.version = function() {

/**
* Lists todo items.
*
*
* @api public
*/
commands.list = function() {
Expand All @@ -62,7 +67,7 @@ commands.list = function() {
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);
Expand All @@ -72,7 +77,7 @@ commands.list = function() {

/**
* Marks an item as done.
*
*
* @param {String} Number.
* @api public
*/
Expand All @@ -83,7 +88,7 @@ commands.check = function(num) {

/**
* Undo a check for item.
*
*
* @param {String} Number.
* @api public
*/
Expand All @@ -94,7 +99,7 @@ commands.undo = function(num) {

/**
* Toggles an item state.
*
*
* @param {Number} Item index.
* @param {Boolean} State.
* @api private
Expand All @@ -114,7 +119,7 @@ commands.toggle = function(num, state) {

/**
* Deletes an item.
*
*
* @param {String} Todo item number.
* @api public
*/
Expand All @@ -133,7 +138,7 @@ commands.delete = function(num) {

/**
* Clears the whole todo item.
*
*
* @param {String} Todo item number.
* @api public
*/
Expand All @@ -147,7 +152,7 @@ commands.clear = function(num) {

/**
* Adds new item to the todo list.
*
*
* @param {String} Item description.
* @api public
*/
Expand All @@ -161,4 +166,35 @@ commands.add = function(item) {
});
});
});
};

/**
* Prints the todo list to a file.
*
* @param {String} File location/name
* @api public
*/
commands.write = function(filename) {
var data = '';
if (!filename) {
var filename = "~/todo.txt";
}
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) {
console.log(line);
data += line +"\n";
});
fs.writeFile(filename, data, 'utf8', function(err, written) {
if (err) return console.log(err);

Copy link
Contributor

Choose a reason for hiding this comment

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

I would log that write was successful if no error.

});
});
};