Browse Source

Mostly working search dialog

Tyler Hallada 7 years ago
parent
commit
bf6e27edf3
2 changed files with 96 additions and 87 deletions
  1. 6 6
      notes.md
  2. 90 81
      search-pane2

+ 6 - 6
notes.md

@@ -1,11 +1,5 @@
1 1
 Things I still need to do:
2 2
 
3
-* Add numbers to the result list and allow pressing the number to open result.
4
-* Add descriptions and possibly the url or domain part of the url to each item
5
-  in the list.
6
-  - I might need to use something other than the blessed list to accomplish
7
-    this...
8
-* Find more universal default color scheme.
9 3
 * Make it installable.
10 4
   - It might be easier to replace the python part with node then distribute as
11 5
     something you can `npm install -g`
@@ -25,3 +19,9 @@ DONE
25 19
   - Is a modal the best option for the search form?
26 20
 * Search result caching.
27 21
 * Return to search form from results list.
22
+* Add numbers to the result list and allow pressing the number to open result.
23
+* Add descriptions and possibly the url or domain part of the url to each item
24
+  in the list.
25
+  - I might need to use something other than the blessed list to accomplish
26
+    this...
27
+* Find more universal default color scheme.

+ 90 - 81
search-pane2

@@ -31,11 +31,9 @@ var form = blessed.form({
31 31
     border: {
32 32
         type: 'line',
33 33
         fg: 'white',
34
-        bg: 'grey'
35 34
     },
36 35
     width: '95%',
37 36
     height: 7,
38
-	bg: 'grey',
39 37
 	content: 'Search: '
40 38
 });
41 39
 
@@ -51,16 +49,7 @@ var input = blessed.textbox({
51 49
     },
52 50
     top: 0,
53 51
     left: 10,
54
-    right: 2,
55
-	style: {
56
-		bg: 'darkblue',
57
-		focus: {
58
-			bg: 'darkblue'
59
-		},
60
-		hover: {
61
-			bg: 'darkblue'
62
-		}
63
-	}
52
+    right: 2
64 53
 });
65 54
 
66 55
 var submit = blessed.button({
@@ -127,57 +116,34 @@ var loading = blessed.loading({
127 116
     },
128 117
     border: {
129 118
         type: 'line',
130
-        fg: 'white',
131
-        bg: 'grey'
119
+        fg: 'white'
132 120
     },
133 121
     width: 32,
134 122
     height: 7,
135
-    bg: 'grey',
136 123
 });
137 124
 
138
-var results = blessed.listtable({
125
+var resultsForm = blessed.form({
139 126
     parent: screen,
140 127
     hidden: true,
141
-    top: 'center',
142
-    left: 'center',
143
-    padding: {
144
-        left: 1,
145
-        right: 1,
146
-        top: 1,
147
-        bottom: 1
148
-    },
149
-    border: {
150
-        type: 'line',
151
-        fg: 'white',
152
-        bg: 'grey'
153
-    },
154
-    bg: 'grey',
155
-    height: 'shrink',
156
-    width: '95%'
128
+    input: false,
129
+    keys: true,
130
+    tags: true
157 131
 });
158 132
 
159
-var resultsList = blessed.list({
160
-    parent: screen,
161
-    hidden: true,
133
+var resultsSet = blessed.radioset({
134
+    parent: resultsForm,
135
+    input: false,
162 136
     keys: true,
163
-    top: 'center',
164
-    left: 'center',
165
-    padding: {
166
-        left: 1,
167
-        right: 1,
168
-        top: 1,
169
-        bottom: 1
170
-    },
137
+    tags: true,
138
+    top: 0,
139
+    left: 0,
171 140
     border: {
172 141
         type: 'line',
173
-        fg: 'white',
174
-        bg: 'grey'
142
+        fg: 'white'
175 143
     },
176 144
     selectedBg: 'darkred',
177 145
     selectedFg: 'yellow',
178
-    bg: 'grey',
179 146
     scollbar: true,
180
-    width: '95%'
181 147
 });
182 148
 
183 149
 submit.on('press', function() {
@@ -195,21 +161,49 @@ input.on('submit', function(data) {
195 161
 form.on('submit', function(data) {
196 162
     form.hide();
197 163
     PythonShell.run('results.py', {args: [data.input], mode: 'json',
198
-                                   scriptPath: '/home/thallada/workspace/search-pane/'},
164
+                                   scriptPath: '/home/thallada/workspace/search-pane/',
165
+                                   pythonPath: '/usr/bin/python3'},
199 166
                     function (err, results) {
200 167
         loading.stop();
201 168
         if (err) throw err;
202 169
         searchResults = results[0];
203
-        var list = results[0].map(function(r) {
204
-            return r.title;
205
-        });
206
-        resultsList.setItems(list);
207
-        resultsList.show();
208
-        resultsList.focus();
170
+        resultsForm.show();
171
+        searchResults.forEach(function(r, i) {
172
+            var result = blessed.radiobutton({
173
+                tags: true,
174
+                top: i * 4,
175
+                height: 4,
176
+                padding: {
177
+                    bottom: 1
178
+                },
179
+                text: '{bold}' + (i + 1) + '. ' + r.title + '{/}\n\t{blue-fg}' + r.link + '{/}\n\t' + r.desc
180
+            });
181
+            result.set('index', i);
182
+            resultsSet.append(result);
183
+            result.key('left', backToSearch);
184
+            result.on('check', function() { 
185
+                resultsForm.hide();
186
+                screen.leave();
187
+                var browser = screen.spawn('w3m', [searchResults[this.get('index')].link]);
188
+                var self = this;
189
+                browser.on('exit', function() {
190
+                    screen.program.hideCursor();
191
+                    resultsForm.show();
192
+                    self.focus();
193
+                    screen.render();
194
+                });
195
+            })
196
+            result.key('j', listNext);
197
+            result.key('k', listPrev);
198
+            result.key('up', listNext);
199
+            result.key('down', listPrev);
200
+            result.on('keypress', numberPress);
201
+        })
202
+        resultsSet.children[0].focus(); // first result
209 203
         screen.render();
210 204
     });
211 205
     loading.load('Searching...');
212
-    loading._.icon.style = {bg: 'grey'}; // Hacky, loading icon doesn't support styling via options
206
+    // loading._.icon.style = {bg: 'grey'}; // Hacky, loading icon doesn't support styling via options
213 207
 });
214 208
 
215 209
 form.on('reset', function(data) {
@@ -217,40 +211,55 @@ form.on('reset', function(data) {
217 211
     cleanExit();
218 212
 });
219 213
 
220
-resultsList.on('select', function(data) {
221
-    var i = resultsList.getItemIndex(data);
222
-    resultsList.hide();
223
-    screen.leave();
224
-    var browser = screen.spawn('w3m', [searchResults[i].link]);
225
-    browser.on('exit', function() {
226
-        screen.program.hideCursor();
227
-        resultsList.show();
228
-        resultsList.focus();
229
-        screen.render();
230
-    });
231
-});
232
-
233
-resultsList.key('left', function() {
234
-    resultsList.hide();
214
+var backToSearch = function() {
215
+    screen.saveFocus();
216
+    resultsForm.hide();
235 217
     form.show();
236 218
     input.focus();
237
-});
219
+    screen.render();
220
+}
238 221
 
239
-cancel.key('right', function() {
240
-    form.hide();
241
-    resultsList.show();
242
-    resultsList.focus();
243
-});
222
+var forwardToList = function() {
223
+    if (searchResults) {
224
+        form.hide();
225
+        resultsForm.show();
226
+        screen.restoreFocus();
227
+        screen.render();
228
+    }
229
+}
244 230
 
245
-submit.key('right', function() {
246
-    form.hide();
247
-    resultsList.show();
248
-    resultsList.focus();
249
-});
231
+var numberPress = function(key) {
232
+    numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
233
+    if (numbers.indexOf(key) > -1) {
234
+        resultsSet.children[parseInt(key, 10) - 1].focus();
235
+        resultsSet.children[parseInt(key, 10) - 1].check();
236
+    }
237
+}
238
+
239
+var listNext = function() {
240
+    resultsForm.focusNext();
241
+}
242
+var listPrev = function() {
243
+    resultsForm.focusPrevious();
244
+}
245
+
246
+// resultsSet.key('left', backToSearch);
247
+// resultsForm.key('left', backToSearch);
248
+// resultsSet.on('keypress', numberPress);
249
+// resultsForm.on('keypress', numberPress);
250
+
251
+form.key('right', forwardToList);
252
+input.key('right', forwardToList);
253
+submit.key('right', forwardToList);
254
+cancel.key('right', forwardToList);
250 255
 
251 256
 screen.key('q', function() {
252 257
     cleanExit();
253 258
 });
254 259
 
260
+screen.key('escape', function() {
261
+    cleanExit();
262
+});
263
+
255 264
 screen.render();
256 265
 input.focus();