Skip to content

Commit

Permalink
feat(project): dynamic word wrapping
Browse files Browse the repository at this point in the history
Use term-size to get the column width of the terminal to wrap long lines of text

Close #1
  • Loading branch information
drawnepicenter committed Nov 30, 2016
1 parent b1cbb71 commit 5dfe660
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
11 changes: 8 additions & 3 deletions bin/cmds/ddg.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ exports.handler = function (argv) {
Results: []
};
var body = JSON.parse(response.body);
if (body === blank) {
if (body.Abstract === '' && body.Answer === '' && body.meta !== null) {
console.log('Only metadata was returned:');
console.log(JSON.stringify(body.meta, null, 2));
process.exit(0);
}
if (JSON.stringify(body) === JSON.stringify(blank)) {
console.log('DuckDuckGo found no results.');
process.exit(0);
}
Expand Down Expand Up @@ -116,7 +121,7 @@ exports.handler = function (argv) {
themes.label(theme, 'right', 'Entity', body.Entity);
themes.label(theme, 'right', 'Source', body.AbstractSource);
themes.label(theme, 'right', 'URL', body.AbstractURL);
themes.label(theme, 'right', 'Text', body.AbstractText);
themes.label(theme, 'right', 'Text', tools.wrapStr(body.AbstractText, true, true));
if (body.Results.length > 0) {
themes.label(theme, 'down', 'Primary Results');
for (var i = 0; i <= body.Results - 1; i++) {
Expand Down Expand Up @@ -160,7 +165,7 @@ exports.handler = function (argv) {
themes.label(theme, 'down', 'Related');
for (var _i2 = 0; _i2 <= rcont.length - 1; _i2++) {
var rhash = rcont[_i2];
console.log(rhash.Text + '\n' + rhash.FirstURL);
console.log(tools.wrapStr(rhash.Text, true, true) + '\n' + rhash.FirstURL);
tofile[['relatedText' + _i2]] = rhash.Text;
tofile[['relatedUrl' + _i2]] = rhash.FirstURL;
}
Expand Down
2 changes: 1 addition & 1 deletion bin/cmds/helpers/wa.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function subPod(pn, subpod, tofile) {
themes.label(theme, 'right', 'Title', meta.title);
tf[['pod' + pn]][['subpod' + i]].title = meta.title;
}
themes.label(theme, 'right', 'Text', tools.wrapStr(tools.arrToStr(sp.plaintext)));
themes.label(theme, 'right', 'Text', tools.wrapStr(tools.arrToStr(sp.plaintext), true, true));
tf[['pod' + pn]][['subpod' + i]].text = tools.arrToStr(sp.plaintext);
themes.label(theme, 'right', 'Image', sp.img[0].$.src);
tf[['pod' + pn]][['subpod' + i]].image = sp.img[0].$.src;
Expand Down
2 changes: 1 addition & 1 deletion bin/cmds/wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports.handler = function (argv) {
var pageID = body.query.pageids[0];
var page = body.query.pages[pageID];
var plain = page.extract.trim();
var wrapped = tools.wrapStr(plain, 80, false);
var wrapped = tools.wrapStr(plain, true, true);
themes.label(theme, 'down', 'Summary', wrapped);
tofile.summary = plain;
if (argv.o) tools.outFile(argv.o, argv.f, tofile);
Expand Down
6 changes: 3 additions & 3 deletions bin/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ exports.stripHTML = function (string) {
/**
* Wraps blocks of text
* @param {string} str Long string
* @param {number} col Number of columns
* @param {boolean} hard true, soft false
* @param {boolean} wwrap true, column wrap false
* @return {string} ANSI-wrapped string
*/
exports.wrapStr = function (str, col, hard) {
exports.wrapStr = function (str, hard, wwrap) {
var termsize = ts();
wrap(str, termsize.columns, hard);
return wrap(str, termsize.columns, hard, wwrap);
};

/**
Expand Down
11 changes: 8 additions & 3 deletions src/cmds/ddg.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ exports.handler = (argv) => {
Results: [],
}
const body = JSON.parse(response.body)
if (body === blank) {
if (body.Abstract === '' && body.Answer === '' && body.meta !== null) {
console.log('Only metadata was returned:')
console.log(JSON.stringify(body.meta, null, 2))
process.exit(0)
}
if (JSON.stringify(body) === JSON.stringify(blank)) {
console.log('DuckDuckGo found no results.')
process.exit(0)
}
Expand Down Expand Up @@ -114,7 +119,7 @@ exports.handler = (argv) => {
themes.label(theme, 'right', 'Entity', body.Entity)
themes.label(theme, 'right', 'Source', body.AbstractSource)
themes.label(theme, 'right', 'URL', body.AbstractURL)
themes.label(theme, 'right', 'Text', body.AbstractText)
themes.label(theme, 'right', 'Text', tools.wrapStr(body.AbstractText, true, true))
if (body.Results.length > 0) {
themes.label(theme, 'down', 'Primary Results')
for (let i = 0; i <= body.Results - 1; i++) {
Expand Down Expand Up @@ -158,7 +163,7 @@ exports.handler = (argv) => {
themes.label(theme, 'down', 'Related')
for (let i = 0; i <= rcont.length - 1; i++) {
const rhash = rcont[i]
console.log(`${rhash.Text}\n${rhash.FirstURL}`)
console.log(`${tools.wrapStr(rhash.Text, true, true)}\n${rhash.FirstURL}`)
tofile[[`relatedText${i}`]] = rhash.Text
tofile[[`relatedUrl${i}`]] = rhash.FirstURL
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/helpers/wa.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function subPod(pn, subpod, tofile) {
themes.label(theme, 'right', 'Title', meta.title)
tf[[`pod${pn}`]][[`subpod${i}`]].title = meta.title
}
themes.label(theme, 'right', 'Text', tools.wrapStr(tools.arrToStr(sp.plaintext)))
themes.label(theme, 'right', 'Text', tools.wrapStr(tools.arrToStr(sp.plaintext), true, true))
tf[[`pod${pn}`]][[`subpod${i}`]].text = tools.arrToStr(sp.plaintext)
themes.label(theme, 'right', 'Image', sp.img[0].$.src)
tf[[`pod${pn}`]][[`subpod${i}`]].image = sp.img[0].$.src
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ exports.handler = (argv) => {
const pageID = body.query.pageids[0]
const page = body.query.pages[pageID]
const plain = page.extract.trim()
const wrapped = tools.wrapStr(plain, 80, false)
const wrapped = tools.wrapStr(plain, true, true)
themes.label(theme, 'down', 'Summary', wrapped)
tofile.summary = plain
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
Expand Down
6 changes: 3 additions & 3 deletions src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ exports.stripHTML = (string) => string.replace(/(<([^>]+)>)/ig, '')
/**
* Wraps blocks of text
* @param {string} str Long string
* @param {number} col Number of columns
* @param {boolean} hard true, soft false
* @param {boolean} wwrap true, column wrap false
* @return {string} ANSI-wrapped string
*/
exports.wrapStr = (str, col, hard) => {
exports.wrapStr = (str, hard, wwrap) => {
const termsize = ts()
wrap(str, termsize.columns, hard)
return wrap(str, termsize.columns, hard, wwrap)
}

/**
Expand Down

0 comments on commit 5dfe660

Please sign in to comment.