Skip to content

Commit

Permalink
📦NEW: Display results (#14)
Browse files Browse the repository at this point in the history
📦NEW: Display results (#14)
  • Loading branch information
aneeshkodali authored Nov 24, 2020
2 parents d69bc51 + 51611ee commit d7998b6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
},
"dependencies": {
"axios": "^0.21.0",
"chalk": "^4.1.0",
"cli-meow-help": "^2.0.2",
"cli-welcome": "^2.2.2",
"enquirer": "^2.3.6",
"keypress": "^0.2.1",
"log-update": "^4.0.0",
"meow": "^8.0.0",
"node-cli-handle-error": "^1.0.0",
"ora": "^5.1.0"
},
"devDependencies": {
Expand Down
38 changes: 22 additions & 16 deletions utils/cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const axios = require('axios');
const ora = require('ora');
const results = require('./results');
const handleError = require('node-cli-handle-error');

// base url
const baseUrl = 'https://api.stackexchange.com/2.2/search/advanced';
// default params
const site = 'stackoverflow';
const filter = '!)rTkraPXy17fPqpx7wE5';
const pageSize = 10;

/**
*
* @param encodedString - string to decode
*/
const decodeEntities = encodedString => {
const translate_re = /&(nbsp|amp|quot|lt|gt);/g;
const translate = {
Expand All @@ -17,18 +24,18 @@ const decodeEntities = encodedString => {
gt: '>'
};
return encodedString
.replace(translate_re, function (match, entity) {
.replace(translate_re, (match, entity) => {
return translate[entity];
})
.replace(/&#(\d+);/gi, function (match, numStr) {
.replace(/&#(\d+);/gi, (match, numStr) => {
const num = parseInt(numStr, 10);
return String.fromCharCode(num);
});
};

module.exports = async (question, flags) => {
// spinner
const spinner = ora();
const spinner = ora(`Fetching results for your query...`);

// default params
const order = flags.indexOf(`--asc`) >= 0 ? 'asc' : 'desc';
Expand All @@ -48,9 +55,10 @@ module.exports = async (question, flags) => {
console.log('');
spinner.start();
const { data } = await axios.get(
`${baseUrl}?order=${order}&sort=${sort}&q=${question}&site=${site}&filter=${filter}`
`${baseUrl}?order=${order}&sort=${sort}&q=${question}&pageSize=${pageSize}&site=${site}&filter=${filter}`
);

spinner.succeed();
console.log('');
// decode html characters to regular chars
for (const [key, value] of Object.entries(data['items'])) {
let item = value['body_markdown'];
Expand All @@ -62,19 +70,17 @@ module.exports = async (question, flags) => {
data['items'][key]['body'] = [];

// Uncomment the code below in order to concat the body_markdown array into one string
//let whole_string = '';
//for(substring_key in data['items'][key]['body_markdown']){
// whole_string += data['items'][key]['body_markdown'][substring_key];
//}
//data['items'][key]['body_markdown'] = whole_string
// let whole_string = '';
// for (substring_key in data['items'][key]['body_markdown']) {
// whole_string +=
// data['items'][key]['body_markdown'][substring_key];
// }
// data['items'][key]['body_markdown'] = whole_string;
}

let { items } = data;
console.log(items);

spinner.stop();
results(items, order, sort);
} catch (err) {
spinner.stop();
console.error(`Error: ${err.response.data.error_message}`);
spinner.fail();
handleError(`Something went wrong.`, err);
}
};
59 changes: 59 additions & 0 deletions utils/results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const keypress = require('keypress');
const logUpdate = require('log-update');
const chalk = require('chalk');

/**
*
* @param index - thread number
*/
const formatThread = (index, thread, order, sort) => {
return `${chalk.dim(
`Thread #${index + 1} | Order: ${order} | Sort By: ${sort}`
)}\n\n${chalk.hex(`#14b514`).bold.inverse(` TITLE `)} ${
thread[index].title
}\n\n${chalk.hex(`#14b514`).bold.inverse(` QUESTION `)} ${
thread[index].body
}\n\n${chalk.dim(`Press ESC to exit.`)}`;
};

/**
*
* @param array - That has all the data
*/
const switchResult = (threads, order, sort) => {
let counter = 0;
logUpdate(formatThread(counter, threads, order, sort));

// switch the result back and forth from left and right arrow keys and exits with escape key
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
if (key.name === 'right' && counter !== threads.length - 1) {
counter++;
logUpdate(formatThread(counter, threads, order, sort));
}
if (key.name === 'left' && counter !== 0) {
counter--;
logUpdate(formatThread(counter, threads, order, sort));
}
if (key.name === 'escape') {
process.exit();
}
});

process.stdin.setRawMode(true);
process.stdin.resume();
};

module.exports = (results, order, sort) => {
let basicInfoOfQuestions = [];

results.map(result => {
const infoObj = {
title: result.title,
body: result.body_markdown
};
basicInfoOfQuestions.push(infoObj);
});

switchResult(basicInfoOfQuestions, order, sort);
};

0 comments on commit d7998b6

Please sign in to comment.