Skip to content

Commit

Permalink
Fix #13: Add option -r to select a random cow (#30)
Browse files Browse the repository at this point in the history
* Fix #13: Add option -r to select a random cow

* Fix indentation
  • Loading branch information
doug2k1 authored and piuccio committed Mar 27, 2018
1 parent 26d3ffd commit 87759cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
9 changes: 5 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ var argv = require("optimist")
"n" : "If it is specified, the given message will not be word-wrapped.",
"W" : "Specifies roughly where the message should be wrapped. The default is equivalent to -W 40 i.e. wrap words at or before the 40th column.",
"f" : "Specifies a cow picture file (''cowfile'') to use. It can be either a path to a cow file or the name of one of cows included in the package.",
"r" : "Select a random cow",
"l" : "List all cowfiles included in this package."
})
.boolean(["b", "d", "g", "p", "s", "t", "w", "y", "n", "h", "l"])
.boolean(["b", "d", "g", "p", "s", "t", "w", "y", "n", "h", "r", "l"])
.argv;

if (argv.l) {
Expand Down Expand Up @@ -64,9 +65,9 @@ function say () {

function listCows () {
require("./index").list(function(err, list) {
if (err) throw new Error(err);
console.log(list.join(" "));
});
if (err) throw new Error(err);
console.log(list.join(" "));
});
}

function showHelp () {
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ exports.think = function (options) {
exports.list = cows.list;

function doIt (options, sayAloud) {
var cow = cows.get(options.f || "default");
var cowFile;

if (options.r) {
var cowsList = cows.listSync();
cowFile = cowsList[Math.floor(Math.random() * cowsList.length)];
} else {
cowFile = options.f || "default";
}

var cow = cows.get(cowFile);
var face = faces(options);
face.thoughts = sayAloud ? "\\" : "o";

Expand Down
19 changes: 13 additions & 6 deletions lib/cows.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ var fs = require("fs");
var replacer = require("./replacer");

var textCache = {};
var cowsPath = path.join(__dirname, "../cows");

function cowNamesFromFiles (files) {
return files.map(function (cow) {
return path.basename(cow, ".cow");
});
}

exports.get = function (cow) {
var text = textCache[cow];
Expand All @@ -25,13 +32,13 @@ exports.get = function (cow) {
}

exports.list = function (callback) {
fs.readdir(path.join(__dirname, "../cows"), function (err, files) {
fs.readdir(cowsPath, function (err, files) {
if (err) return callback(err);

var cows = files.map(function (cow) {
return path.basename(cow, ".cow");
});

return callback(null, cows);
return callback(null, cowNamesFromFiles(files));
});
}

exports.listSync = function () {
return cowNamesFromFiles(fs.readdirSync(cowsPath))
}

0 comments on commit 87759cc

Please sign in to comment.