66
77const qoa = require ( 'qoa' )
88const boxen = require ( 'boxen' )
9- const clear = require ( 'clear' )
109const { italic, yellow, red, bold} = require ( 'kleur' )
1110
1211const header = require ( './header' )
@@ -15,9 +14,14 @@ const prompt = require('./prompt')
1514const config = require ( './config' )
1615const 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+ */
1822function _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+ */
3140function _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+ */
3952function _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+ */
4789async 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+ */
58105async 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+ */
65118async 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+ */
76132async function _search ( ) {
77133 await _askForATopic ( )
78134 await _refineTopics ( )
79135}
80136
137+ /**
138+ * Check if the user wants to make another research
139+ */
81140async 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+ */
95157exports . 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+ */
103168exports . 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+ */
112180exports . 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+ */
118189exports . displayPreviousSearches = async ( ) => {
119190 const history = await config . getHistory ( )
120191
0 commit comments