Browse Source

Better padding, caching, return to form from list

Tyler Hallada 7 years ago
parent
commit
2ac80d45f7
3 changed files with 62 additions and 14 deletions
  1. 5 5
      notes.md
  2. 28 3
      results.py
  3. 29 6
      search-pane2

+ 5 - 5
notes.md

@@ -1,9 +1,5 @@
1 1
 Things I still need to do:
2 2
 
3
-* Better padding so search can be used in very small terminal windows.
4
-  - The search input scrolls when text reaches half the width...
5
-  - Is a modal the best option for the search form?
6
-* Return to search form from results list.
7 3
 * Add numbers to the result list and allow pressing the number to open result.
8 4
 * Add descriptions and possibly the url or domain part of the url to each item
9 5
   in the list.
@@ -14,7 +10,6 @@ Things I still need to do:
14 10
   - It might be easier to replace the python part with node then distribute as
15 11
     something you can `npm install -g`
16 12
 * Read-line input. I should be able to up-arrow to get my last search.
17
-* Search result caching.
18 13
 * Settings file or panel for configuring colors and keys.
19 14
 * Question mark should open keyboard shortcut info.
20 15
 * Autocomplete? Maybe too much.
@@ -25,3 +20,8 @@ Things I still need to do:
25 20
 DONE
26 21
 * Figure out why there is no visible cursor in w3m when result is opened.
27 22
   - Screen.spawn() should be showing the cursor but it's not. Tmux issue?
23
+* Better padding so search can be used in very small terminal windows.
24
+  - The search input scrolls when text reaches half the width...
25
+  - Is a modal the best option for the search form?
26
+* Search result caching.
27
+* Return to search form from results list.

+ 28 - 3
results.py

@@ -1,10 +1,35 @@
1 1
 import argparse
2 2
 import json
3
+import os
3 4
 
5
+from beaker.cache import CacheManager
6
+from beaker.util import parse_cache_config_options
4 7
 from ddg import search
5 8
 
6
-parser = argparse.ArgumentParser(description='Search duckduckgo and return JSON results')
9
+os.makedirs('cache', exist_ok=True)
10
+os.makedirs('cache/data', exist_ok=True)
11
+os.makedirs('cache/lock', exist_ok=True)
12
+
13
+cache_opts = {
14
+    'cache.type': 'file',
15
+    'cache.data_dir': 'cache/data',
16
+    'cache.lock_dir': 'cache/lock'
17
+}
18
+
19
+cache = CacheManager(**parse_cache_config_options(cache_opts))
20
+search_result_cache = cache.get_cache('search-results')
21
+
22
+parser = argparse.ArgumentParser(
23
+    description='Search duckduckgo and return JSON results'
24
+)
7 25
 parser.add_argument('query', metavar='Q', type=str,
8
-                   help='The query to search on duckduckgo')
26
+                    help='The query to search on duckduckgo')
9 27
 args = parser.parse_args()
10
-print(json.dumps(list(search(args.query, max_results=10))))
28
+
29
+
30
+def get_results():
31
+    return list(search(args.query, max_results=10))
32
+
33
+results = search_result_cache.get(key=args.query, createfunc=get_results)
34
+
35
+print(json.dumps(results))

+ 29 - 6
search-pane2

@@ -3,6 +3,7 @@ var blessed = require('blessed'),
3 3
     PythonShell = require('python-shell'),
4 4
 	screen = blessed.screen({
5 5
         smartCSR: true,
6
+        autoPadding: true
6 7
     }),
7 8
     searchResults;
8 9
 
@@ -32,8 +33,8 @@ var form = blessed.form({
32 33
         fg: 'white',
33 34
         bg: 'grey'
34 35
     },
35
-	width: 'shrink',
36
-	height: 5,
36
+    width: '95%',
37
+    height: 7,
37 38
 	bg: 'grey',
38 39
 	content: 'Search: '
39 40
 });
@@ -50,6 +51,7 @@ var input = blessed.textbox({
50 51
     },
51 52
     top: 0,
52 53
     left: 10,
54
+    right: 2,
53 55
 	style: {
54 56
 		bg: 'darkblue',
55 57
 		focus: {
@@ -69,7 +71,7 @@ var submit = blessed.button({
69 71
 		left: 1,
70 72
 		right: 1
71 73
 	},
72
-	left: 10,
74
+	right: 12,
73 75
 	top: 2,
74 76
 	name: 'submit',
75 77
 	content: 'submit',
@@ -94,7 +96,7 @@ var cancel = blessed.button({
94 96
 		left: 1,
95 97
 		right: 1
96 98
 	},
97
-	left: 20,
99
+	right: 2,
98 100
 	top: 2,
99 101
 	name: 'cancel',
100 102
 	content: 'cancel',
@@ -150,7 +152,8 @@ var results = blessed.listtable({
150 152
         bg: 'grey'
151 153
     },
152 154
     bg: 'grey',
153
-    height: 'shrink'
155
+    height: 'shrink',
156
+    width: '95%'
154 157
 });
155 158
 
156 159
 var resultsList = blessed.list({
@@ -173,7 +176,8 @@ var resultsList = blessed.list({
173 176
     selectedBg: 'darkred',
174 177
     selectedFg: 'yellow',
175 178
     bg: 'grey',
176
-    scollbar: true
179
+    scollbar: true,
180
+    width: '95%'
177 181
 });
178 182
 
179 183
 submit.on('press', function() {
@@ -219,12 +223,31 @@ resultsList.on('select', function(data) {
219 223
     screen.leave();
220 224
     var browser = screen.spawn('w3m', [searchResults[i].link]);
221 225
     browser.on('exit', function() {
226
+        screen.program.hideCursor();
222 227
         resultsList.show();
223 228
         resultsList.focus();
224 229
         screen.render();
225 230
     });
226 231
 });
227 232
 
233
+resultsList.key('left', function() {
234
+    resultsList.hide();
235
+    form.show();
236
+    input.focus();
237
+});
238
+
239
+cancel.key('right', function() {
240
+    form.hide();
241
+    resultsList.show();
242
+    resultsList.focus();
243
+});
244
+
245
+submit.key('right', function() {
246
+    form.hide();
247
+    resultsList.show();
248
+    resultsList.focus();
249
+});
250
+
228 251
 screen.key('q', function() {
229 252
     cleanExit();
230 253
 });