From ef353523c4a1633d600e933277eaf1831cafafcb Mon Sep 17 00:00:00 2001
From: Claudio Santini <vuotomeccanico@gmail.com>
Date: Fri, 19 Feb 2016 21:58:34 +0100
Subject: [PATCH] name changed to how2

---
 bin/how2     |  3 +++
 bin/howto    |  3 ---
 lib/howto.js | 48 +++++++++++++++++++++++++-----------------------
 lib/index.js |  2 +-
 lib/ui.js    | 20 +++-----------------
 lib/utils.js | 18 +++++++++++++++++-
 package.json | 22 ++++++++++++++--------
 7 files changed, 63 insertions(+), 53 deletions(-)
 create mode 100755 bin/how2
 delete mode 100755 bin/howto

diff --git a/bin/how2 b/bin/how2
new file mode 100755
index 0000000..4bd2d67
--- /dev/null
+++ b/bin/how2
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../lib/index');
diff --git a/bin/howto b/bin/howto
deleted file mode 100755
index 4bd2d67..0000000
--- a/bin/howto
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/usr/bin/env node
-
-require('../lib/index');
diff --git a/lib/howto.js b/lib/howto.js
index 117362c..a91984b 100644
--- a/lib/howto.js
+++ b/lib/howto.js
@@ -5,14 +5,18 @@ var assert = require('assert');
 
 var ui = require('./ui');
 var utils = require('./utils');
+var log = require('./log');
 
 var options = { version: 2.2 };
 var context = new stackexchange(options);
 
 function selectedGoogleItemCallback(links, callback) {
     return function(index) {
+        if(links.length === 0) {
+            log.error('Sorry, I cannot find any reasonable answer for your query.');
+            process.exit(1);
+        }
         var selectedLink = links[index];
-        // console.log(selectedLink)
         var parsedLink = utils.parseStackoverflowQuestionId(selectedLink.link)
         fetchQuestionAnswers(parsedLink, callback);
     };
@@ -28,8 +32,8 @@ function fetchQuestionAnswers(parsedLink, callback) {
     context.questions.answers(questionCriteria, function(err, results){
         if(err) {
             ui.stop();
-            console.error("Cannot fetch answers from Stackoverflow.");
-            console.error(err);
+            log.error("Cannot fetch answers from Stackoverflow.");
+            log.error(err);
             process.exit(1);
         }
         if(results.error_id) {
@@ -46,7 +50,6 @@ function fetchQuestionAnswers(parsedLink, callback) {
         var answers = _.sortBy(results.items, function(answer) {
             return - answer.score;
         });
-        // console.log(results)
         callback(answers);
 
     }, [parsedLink.questionId]);
@@ -57,17 +60,17 @@ function googleError(err, links) {
     var msg = err.toString()
 
     if(msg.indexOf('have detected unusual traffic') !== -1) {
-        console.error(
+        log.error(
             "You are doing to many requests to Google. " +
             "You need to wait a bit before trying again.");
     }
     else if(msg.indexOf('ENOTFOUND') !== -1) {
-        console.error("Cannot connect to Google. Make sure you are connected.");
-        console.error(err);
+        log.error("Cannot connect to Google. Make sure you are connected.");
+        log.error(err);
     }
     else {
-        console.error("Cannot connect to Google.");
-        console.error(err);
+        log.error("Cannot connect to Google.");
+        log.error(err);
     }
     process.exit(1);
 }
@@ -139,24 +142,23 @@ function magic(text, lang) {
     }
 
     function selectedAnswer(titles, answers, index) {
-        var markdown = ui.toEscapedMarkdown(answers[index].body_markdown);
+        var markdown = utils.toEscapedMarkdown(answers[index].body_markdown);
 
         var title = titles[index];
-        console.log(colors.blue(title));
-        console.log(colors.blue(Array(title.length).join('=')));
+        console.log(colors.underline.green(title+'\n'));
+        // console.log(colors.blue(Array(title.length).join('=')));
         console.log(markdown);
 
         // console.log('If you want to choose the questions use -i');
-
-        console.log('Alternative answers:');
-        titles = titles.slice(0, 10).map(function(title, i) {
-            return title;
-        });
-        titles.forEach(function(title, i) {
-            console.log(i+') '+title);
-        })
-        var index = scanfIndex();
-        selectedAnswer(titles, answers, index);
+        // console.log('Alternative answers:');
+        // titles = titles.slice(0, 10).map(function(title, i) {
+        //     return title;
+        // });
+        // titles.forEach(function(title, i) {
+        //     console.log(i+') '+title);
+        // })
+        // var index = scanfIndex();
+        // selectedAnswer(titles, answers, index);
     }
 
     search(text, lang, function(links, titles) {
@@ -164,7 +166,7 @@ function magic(text, lang) {
             selectedAnswer(titles, answers, 0);
         });
         selectItem(0);
-     });
+    });
 }
 
 module.exports = {
diff --git a/lib/index.js b/lib/index.js
index 9f5ce3f..5956931 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -18,7 +18,7 @@ if(argv._.length === 0) {
 
 var text = argv._.join(' ');
 var lang = argv.l;
-var howto = require('./howto');
+var howto = require('./how2');
 
 if(argv.m) {
     howto.magic(text, lang);
diff --git a/lib/ui.js b/lib/ui.js
index 17b285e..d922285 100644
--- a/lib/ui.js
+++ b/lib/ui.js
@@ -1,15 +1,7 @@
 var _ = require('lodash');
 var blessed = require('blessed');
-var marked = require('marked');
-var TerminalRenderer = require('marked-terminal');
-var htmlentities = require('ent');
 
-marked.setOptions({
-    // Define custom renderer
-    renderer: new TerminalRenderer({
-        unescape: true,
-    })
-});
+var utils = require('./utils');
 
 var screen;
 var googleList;
@@ -83,7 +75,7 @@ function showGoogleList(items, callback) {
 }
 
 function makeTitleForAnswer(answer) {
-    var withColors = marked(answer.body_markdown);
+    var withColors = utils.marked(answer.body_markdown);
 
     var lines = withColors.split('\n');
 
@@ -137,7 +129,7 @@ function showAnswers(answers, callback) {
 }
 
 function showAnswer(answer) {
-    var text = toEscapedMarkdown(answer.body_markdown);
+    var text = utils.toEscapedMarkdown(answer.body_markdown);
 
     var answerBox = blessed.box({
         top: 'center',
@@ -173,10 +165,6 @@ function showAnswer(answer) {
     screen.render();
 }
 
-function toEscapedMarkdown(markdown) {
-     return htmlentities.decode(marked(markdown));
-}
-
 function magicSelect(rows) {
     screen = blessed.screen({
         autoPadding: true
@@ -189,7 +177,6 @@ function magicSelect(rows) {
 }
 
 module.exports = {
-    marked: marked,
     start: start,
     stop: function() {
         if(screen) {
@@ -200,6 +187,5 @@ module.exports = {
     showGoogleList: showGoogleList,
     showAnswers: showAnswers,
     showAnswer: showAnswer,
-    toEscapedMarkdown: toEscapedMarkdown,
     magicSelect: magicSelect
 };
diff --git a/lib/utils.js b/lib/utils.js
index a6a8a81..10e35e8 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,4 +1,14 @@
 var _ = require('lodash');
+var htmlentities = require('ent');
+var marked = require('marked');
+var TerminalRenderer = require('marked-terminal');
+
+marked.setOptions({
+    // Define custom renderer
+    renderer: new TerminalRenderer({
+        unescape: true,
+    })
+});
 
 function stripStackOverflow(title) {
     var split = title.split(' - ');
@@ -44,8 +54,14 @@ function isValidGoogleLink(link) {
     return re.exec(link.link) !== null;
 }
 
+function toEscapedMarkdown(markdown) {
+     return htmlentities.decode(marked(markdown));
+}
+
 module.exports = {
     parseStackoverflowQuestionId: parseStackoverflowQuestionId,
     stripStackOverflow: stripStackOverflow,
-    isValidGoogleLink: isValidGoogleLink
+    isValidGoogleLink: isValidGoogleLink,
+    toEscapedMarkdown: toEscapedMarkdown,
+    marked: marked
 };
diff --git a/package.json b/package.json
index 6cdbf0d..761d296 100644
--- a/package.json
+++ b/package.json
@@ -1,31 +1,37 @@
 {
-  "name": "howto",
-  "author": "Claudio Santini",
+  "name": "how2",
+  "author": "Claudio Santini <hireclaudio@gmail.com> (https://www.linkedin.com/in/santinic)",
   "version": "1.0.0",
-  "description": "Uses Google and Stackoverflow to find how to do things",
-  "licence": "NON-MILITARY-SOCIALIST-LICENCE V1",
+  "description": "Uses Google and Stackoverflow to find how to do things on a Unix commmand line",
+  "licence": "NON-MILITARY SOCIALIST LICENCE VERSION 1",
   "keywords": [
+    "how2",
     "howto",
     "man",
     "google",
     "stackoverflow",
-    "stackexchange"
+    "stack overflow",
+    "stackexchange",
+    "stack exchange"
   ],
   "tags": [
+    "how2",
     "howto",
     "man",
     "google",
     "stackoverflow",
-    "stackexchange"
+    "stack overflow",
+    "stackexchange",
+    "stack exchange"
   ],
   "homepage": "https://github.com/santinic/howto",
   "preferGlobal": true,
   "main": "./index.js",
   "bin": {
-    "howto": "./bin/howto"
+    "howto": "./bin/how2"
   },
   "scripts": {
-    "start": "./bin/howto concatenate two files"
+    "start": "./bin/how2 concatenate two files"
   },
   "dependencies": {
     "blessed": "^0.1.81",
