Skip to content
Browse files

name changed to how2

1 parent d254da1 commit ef353523c4a1633d600e933277eaf1831cafafcb @santinic committed
Showing with 60 additions and 50 deletions.
  1. 0 bin/{howto → how2}
  2. +25 −23 lib/howto.js
  3. +1 −1 lib/index.js
  4. +3 −17 lib/ui.js
  5. +17 −1 lib/utils.js
  6. +14 −8 package.json
View
0 bin/howto → bin/how2
File renamed without changes.
View
48 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 = {
View
2 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);
View
20 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
};
View
18 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
};
View
22 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",

0 comments on commit ef35352

Please sign in to comment.
Something went wrong with that request. Please try again.