Skip to content

Commit 3c81d13

Browse files
authored
Merge pull request #5 from kikiklang/line-wrapper
Line wrapper
2 parents 7af0aae + bbd25d0 commit 3c81d13

File tree

5 files changed

+82
-17
lines changed

5 files changed

+82
-17
lines changed

cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
'use strict'
33

44
const meow = require('meow')
5-
const updateNotifier = require('update-notifier');
5+
const updateNotifier = require('update-notifier')
66
const help = require('./src/help')
7-
const pkg = require('./package.json');
7+
const pkg = require('./package.json')
88
const launch = require('./index')
99

1010
const cli = meow(help, {
@@ -32,7 +32,7 @@ const cli = meow(help, {
3232
}
3333
})
3434

35-
updateNotifier({pkg}).notify();
35+
updateNotifier({pkg}).notify()
3636

3737
launch.wikipediaCLI(cli.flags)
3838

package-lock.json

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wiclipedia",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "wikipedia articles summaries in your terminal",
55
"repository": "kikiklang/wiclipedia",
66
"homepage": "https://github.com/kikiklang/wiclipedia",
@@ -37,7 +37,6 @@
3737
],
3838
"dependencies": {
3939
"boxen": "^4.2.0",
40-
"clear": "^0.1.0",
4140
"configstore": "^5.0.1",
4241
"iso-639-1": "^2.1.3",
4342
"kleur": "^4.0.0",

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports.storeSearches = (userInput, lang) => {
4343
this.model.set('history', history)
4444
}
4545

46-
exports.clear = () => {
46+
exports.clearHistory = () => {
4747
this.model.set('history', [])
4848
}
4949

src/wiclipedia.js

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
const qoa = require('qoa')
88
const boxen = require('boxen')
9-
const clear = require('clear')
109
const {italic, yellow, red, bold} = require('kleur')
1110

1211
const header = require('./header')
@@ -15,9 +14,14 @@ const prompt = require('./prompt')
1514
const config = require('./config')
1615
const options = require('./boxen-options')
1716

17+
/**
18+
* Check the user's answer picked from the prompt.
19+
* Check specifically if the user decided to quit the program or wanted to make another search
20+
* @param {String} input The picked choice after prompt
21+
*/
1822
function _checkUserAnswers(input) {
1923
if (input.userPick.includes('(Try another search)')) {
20-
clear()
24+
process.stdout.write('\u001Bc') // Clear the console
2125
header.logAppName()
2226
prompt.topicInteractive.menu = []
2327
return _search()
@@ -28,6 +32,11 @@ function _checkUserAnswers(input) {
2832
}
2933
}
3034

35+
/**
36+
* Fill an array with all possibles values for interactive menu based prompt
37+
* @param {Array} topics set of values coming from wikipedia API
38+
* @param {String} promptName Allow to pick the right prompt
39+
*/
3140
function _fillInteractiveTopicsName(topics, promptName) {
3241
topics.forEach(item => {
3342
prompt[promptName].menu.push(item.title)
@@ -36,14 +45,47 @@ function _fillInteractiveTopicsName(topics, promptName) {
3645
prompt[promptName].menu.push(red('(Quit)'))
3746
}
3847

48+
/**
49+
* Log / display the article summaries choosen by the user
50+
* @param {Object} result the response object received from the API call
51+
*/
3952
function _displayArticle(result) {
4053
const articleName = bold(result.title)
4154
const link = yellow(result.url)
55+
const lineLength = process.stdout.columns
4256

4357
console.log(boxen(`${articleName} - ${link}`, options.boxenOptions('blue')))
44-
console.log(result.text)
58+
console.log(lineLength < 85 ? result.text : _lineWrapper(result.text))
4559
}
4660

61+
/**
62+
* Format the logged article summary to avoid long line of text and offer better reading experience
63+
* @param {String} text the summary received from the API call
64+
*/
65+
function _lineWrapper(text) {
66+
let index = 0
67+
return text
68+
.split('')
69+
.map(char => {
70+
if (char === '\n') {
71+
index = 0
72+
}
73+
74+
if (char === ' ' && index > 80) {
75+
char = '\n'
76+
index = 0
77+
}
78+
79+
index++
80+
return char
81+
}).join('')
82+
}
83+
84+
/**
85+
* Display a prompt that helps user to set a language
86+
* Save the answer to a json file
87+
* Display a confirmation to the user
88+
*/
4789
async function _askForlanguage() {
4890
const isLangAlreadySet = await config.checkLang()
4991
if (!isLangAlreadySet) {
@@ -55,13 +97,24 @@ async function _askForlanguage() {
5597
}
5698
}
5799

100+
/**
101+
* Display a prompt that ask the user to choose a topic
102+
* check the stored language config
103+
* call the wikipedia API for a set of topics
104+
*/
58105
async function _askForATopic() {
59106
const input = await qoa.prompt(prompt.topicQuestion)
60107
const lang = await config.checkLang()
61108
const suggestedTopics = await fetch.getSuggestedTopic(input.userSearch, lang)
62109
_fillInteractiveTopicsName(suggestedTopics, 'topicInteractive')
63110
}
64111

112+
/**
113+
* Display a prompt that ask the user to choose between a list of topics
114+
* check the stored language config
115+
* call the wikipedia API to request the choosen article
116+
* save the search
117+
*/
65118
async function _refineTopics() {
66119
const input = await qoa.interactive(prompt.topicInteractive)
67120
await _checkUserAnswers(input)
@@ -73,15 +126,21 @@ async function _refineTopics() {
73126
prompt.topicInteractive.menu = []
74127
}
75128

129+
/**
130+
* Just calling other functions
131+
*/
76132
async function _search() {
77133
await _askForATopic()
78134
await _refineTopics()
79135
}
80136

137+
/**
138+
* Check if the user wants to make another research
139+
*/
81140
async function _searchAgain() {
82141
const input = await qoa.confirm(prompt.topicRedo)
83142
if (input.redo) {
84-
clear()
143+
process.stdout.write('\u001Bc') // Clear the console
85144
await header.logAppName()
86145
await _search()
87146
await _searchAgain()
@@ -92,6 +151,9 @@ async function _searchAgain() {
92151
}
93152
}
94153

154+
/**
155+
* Bootstrap the app
156+
*/
95157
exports.launchProgram = async () => {
96158
await config.model
97159
await header.logAppName()
@@ -100,6 +162,9 @@ exports.launchProgram = async () => {
100162
await _searchAgain()
101163
}
102164

165+
/**
166+
* Allow the user to specify a language for the displayed articles
167+
*/
103168
exports.setLang = async () => {
104169
console.log(italic('full ISO codes list here -> https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes'))
105170
const input = await qoa.prompt(prompt.langQuestion)
@@ -109,12 +174,18 @@ exports.setLang = async () => {
109174
console.log(`you chose: ${name} (${nativeName})`)
110175
}
111176

177+
/**
178+
* Allow the user to delete all previous searches
179+
*/
112180
exports.clearHistory = () => {
113-
config.clear()
181+
config.clearHistory()
114182

115183
console.log('history is clear now')
116184
}
117185

186+
/**
187+
* Allow the user to display all previous searches
188+
*/
118189
exports.displayPreviousSearches = async () => {
119190
const history = await config.getHistory()
120191

0 commit comments

Comments
 (0)