about summary refs log tree commit diff stats
path: root/src/local
diff options
context:
space:
mode:
Diffstat (limited to 'src/local')
-rw-r--r--src/local/pager.nim85
1 files changed, 74 insertions, 11 deletions
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 5dbb8ae0..80a001e4 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -53,8 +53,9 @@ import chagashi/charset
 
 type
   LineMode* = enum
-    NO_LINEMODE, LOCATION, USERNAME, PASSWORD, COMMAND, BUFFER, SEARCH_F,
-    SEARCH_B, ISEARCH_F, ISEARCH_B, GOTO_LINE
+    LOCATION, USERNAME, PASSWORD, COMMAND, BUFFER, SEARCH_F, SEARCH_B,
+    ISEARCH_F, ISEARCH_B, GOTO_LINE,
+    DOWNLOAD = "(Download) Save file to"
 
   # fdin is the original fd; fdout may be the same, or different if mailcap
   # is used.
@@ -79,6 +80,12 @@ type
     outputId: int
     status: uint16
 
+  LineData = ref object of RootObj
+
+  LineDataDownload = ref object of LineData
+    outputId: int
+    stream: Stream
+
   Pager* = ref object
     alertState: PagerAlertState
     alerts: seq[string]
@@ -98,6 +105,7 @@ type
     inputBuffer*: string # currently uninterpreted characters
     iregex: Result[Regex, string]
     isearchpromise: EmptyPromise
+    lineData: LineData
     lineedit*: Option[LineEdit]
     linehist: array[LineMode, LineHistory]
     linemode: LineMode
@@ -206,13 +214,13 @@ proc searchPrev(pager: Pager, n = 1) {.jsfunc.} =
       pager.container.cursorNextMatch(pager.regex.get, wrap, true, n)
     pager.container.markPos()
 
-proc getLineHist(pager: Pager, mode: LineMode): LineHistory =
+proc getLineHist(pager: Pager; mode: LineMode): LineHistory =
   if pager.linehist[mode] == nil:
     pager.linehist[mode] = newLineHistory()
   return pager.linehist[mode]
 
-proc setLineEdit(pager: Pager, prompt: string, mode: LineMode,
-    current = "", hide = false) =
+proc setLineEdit(pager: Pager; prompt: string; mode: LineMode; current = "";
+    hide = false) =
   let hist = pager.getLineHist(mode)
   if pager.term.isatty() and pager.config.input.use_mouse:
     pager.term.disableMouse()
@@ -220,6 +228,9 @@ proc setLineEdit(pager: Pager, prompt: string, mode: LineMode,
   pager.lineedit = some(edit)
   pager.linemode = mode
 
+proc setLineEdit(pager: Pager; mode: LineMode; current = "") =
+  pager.setLineEdit($mode, mode, current)
+
 proc clearLineEdit(pager: Pager) =
   pager.lineedit = none(LineEdit)
   if pager.term.isatty() and pager.config.input.use_mouse:
@@ -1118,6 +1129,23 @@ proc updateReadLineISearch(pager: Pager, linemode: LineMode) =
       pager.isearchpromise = nil
   )
 
+proc saveTo(pager: Pager; data: LineDataDownload; path: string) =
+  if pager.loader.redirectToFile(data.outputId, path):
+    pager.alert("Saving file to " & path)
+    pager.loader.resume(@[data.outputId])
+    data.stream.close()
+    pager.lineData = nil
+  else:
+    pager.ask("Failed to save to path " & path & ". Retry?").then(
+      proc(x: bool) =
+        if x:
+          pager.alert("Failed to save to path " & path)
+          pager.setLineEdit(DOWNLOAD, path)
+        else:
+          data.stream.close()
+          pager.lineData = nil
+    )
+
 proc updateReadLine*(pager: Pager) =
   let lineedit = pager.lineedit.get
   if pager.linemode in {ISEARCH_F, ISEARCH_B}:
@@ -1154,7 +1182,18 @@ proc updateReadLine*(pager: Pager) =
         pager.searchNext()
       of GOTO_LINE:
         pager.container.gotoLine(lineedit.news)
-      else: discard
+      of DOWNLOAD:
+        let data = LineDataDownload(pager.lineData)
+        if fileExists(lineedit.news):
+          pager.ask("Override file " & lineedit.news & "?").then(
+            proc(x: bool) =
+              if x:
+                pager.saveTo(data, lineedit.news)
+              else:
+                pager.setLineEdit(DOWNLOAD, lineedit.news)
+          )
+        pager.saveTo(data, lineedit.news)
+      of ISEARCH_F, ISEARCH_B: discard
     of CANCEL:
       case pager.linemode
       of USERNAME: pager.discardBuffer()
@@ -1163,7 +1202,11 @@ proc updateReadLine*(pager: Pager) =
         pager.discardBuffer()
       of BUFFER: pager.container.readCanceled()
       of COMMAND: pager.commandMode = false
+      of DOWNLOAD:
+        let data = LineDataDownload(pager.lineData)
+        data.stream.close()
       else: discard
+      pager.lineData = nil
   if lineedit.state in {LineEditState.CANCEL, LineEditState.FINISH}:
     if pager.lineedit.get == lineedit:
       pager.clearLineEdit()
@@ -1242,6 +1285,7 @@ type CheckMailcapResult = object
   ostreamOutputId: int
   connect: bool
   ishtml: bool
+  found: bool
 
 # Pipe output of an x-ansioutput mailcap command to the text/x-ansi handler.
 proc ansiDecode(pager: Pager; url: URL; ishtml: var bool; fdin: cint): cint =
@@ -1443,17 +1487,22 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
   if shortContentType == "text/html":
     # We support text/html natively, so it would make little sense to execute
     # mailcap filters for it.
-    return CheckMailcapResult(connect: true, fdout: stream.fd, ishtml: true)
+    return CheckMailcapResult(
+      connect: true,
+      fdout: stream.fd,
+      ishtml: true,
+      found: true
+    )
   if shortContentType == "text/plain":
     # text/plain could potentially be useful. Unfortunately, many mailcaps
     # include a text/plain entry with less by default, so it's probably better
     # to ignore this.
-    return CheckMailcapResult(connect: true, fdout: stream.fd)
+    return CheckMailcapResult(connect: true, fdout: stream.fd, found: true)
   #TODO callback for outpath or something
   let url = container.url
   let entry = pager.mailcap.getMailcapEntry(contentType, "", url)
   if entry == nil:
-    return CheckMailcapResult(connect: true, fdout: stream.fd)
+    return CheckMailcapResult(connect: true, fdout: stream.fd, found: false)
   let tmpdir = pager.tmpdir
   let ext = url.pathname.afterLast('.')
   let tempfile = getTempFile(tmpdir, ext)
@@ -1499,10 +1548,11 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
       connect: true,
       fdout: response.body.fd,
       ostreamOutputId: response.outputId,
-      ishtml: ishtml
+      ishtml: ishtml,
+      found: true
     )
   delEnv("MAILCAP_URL")
-  return CheckMailcapResult(connect: false, fdout: -1)
+  return CheckMailcapResult(connect: false, fdout: -1, found: true)
 
 proc redirectTo(pager: Pager; container: Container; request: Request) =
   pager.gotoURL(request, some(container.url), replace = container,
@@ -1554,6 +1604,19 @@ proc connected(pager: Pager; container: Container; response: Response) =
     container.contentType.get & ";charset=" & $container.charset
   let mailcapRes = pager.checkMailcap(container, istream, response.outputId,
     realContentType)
+  if not mailcapRes.found:
+    pager.setLineEdit(DOWNLOAD,
+      pager.config.external.download_dir &
+      container.url.pathname.afterLast('/'))
+    pager.lineData = LineDataDownload(
+      outputId: response.outputId,
+      stream: istream
+    )
+    pager.deleteContainer(container)
+    pager.redraw = true
+    pager.refreshStatusMsg()
+    dec pager.numload
+    return
   if mailcapRes.connect:
     container.ishtml = mailcapRes.ishtml
     # buffer now actually exists; create a process for it
le='Blame the previous revision' href='/ahoang/Nim/blame/compiler/sem.nim?h=devel&id=c9690864d4cf9945cb5eb9497f7621e56f9bebd0'>^
7063670a2 ^
4447c1b7e ^
7063670a2 ^
eaeed1f84 ^
c7158af75 ^
b731e6ef1 ^
c7158af75 ^

eaeed1f84 ^
4308f3225 ^

92b8fac94 ^
4308f3225 ^
b595fc834 ^


4308f3225 ^
92b8fac94 ^
4308f3225 ^


4447c1b7e ^
4308f3225 ^
eaeed1f84 ^
4b7655fd1 ^


2df9b442c ^
2afadc5c9 ^
adc75d020 ^


2afadc5c9 ^

adc75d020 ^


fa0a327dd ^



af441e607 ^


d912d1837 ^


adc75d020 ^





b3d759ca6 ^

















adc75d020 ^




4196757de ^

adc75d020 ^


e210b049e ^

adc75d020 ^

b3d759ca6 ^
adc75d020 ^

eaeed1f84 ^
14b5d5f26 ^
eaeed1f84 ^

10e23e915 ^
5e15dec17 ^


1dd01e589 ^
5e15dec17 ^
438703f59 ^

3dcf73548 ^



5e15dec17 ^
10e23e915 ^
5e15dec17 ^
eaeed1f84 ^






9e92455a5 ^
2f066395b ^
9e92455a5 ^
2f066395b ^
12bac28d2 ^
af7c92c00 ^
12bac28d2 ^
9e92455a5 ^
f6f5c9e9e ^

89086a8e1 ^
f6f5c9e9e ^
89086a8e1 ^
66a255652 ^








89086a8e1 ^
2dcbc6493 ^
6a9baf3fd ^


5eba93d58 ^



6a9baf3fd ^

5eba93d58 ^
afddae5aa ^
6a9baf3fd ^
afddae5aa ^




fc452787e ^




9fb075557 ^

fc452787e ^










12bac28d2 ^
edab4aaad ^


afddae5aa ^
edab4aaad ^
afddae5aa ^
89086a8e1 ^
71695ab79 ^






754e2ef1d ^


71695ab79 ^


754e2ef1d ^
0c31686fe ^
71695ab79 ^




afddae5aa ^
c1155cd2a ^


92b8fac94 ^
c1155cd2a ^
e25474154 ^
42516c008 ^
6a9baf3fd ^
e5bd3b5b9 ^
c1155cd2a ^

92b8fac94 ^
c1155cd2a ^

92b8fac94 ^
b595fc834 ^

4cda0861f ^
afddae5aa ^
e5bd3b5b9 ^
b64eeeb43 ^
071b1e395 ^
f6f5c9e9e ^

93fa75bb0 ^




69997af8e ^

9e92455a5 ^
93fa75bb0 ^
69997af8e ^
e25474154 ^
a59abdf8e ^
12bac28d2 ^
f7884717c ^
a59abdf8e ^




f6f5c9e9e ^
a59abdf8e ^



2aabae702 ^
2dcbc6493 ^

a59abdf8e ^
f6f5c9e9e ^
a59abdf8e ^

69997af8e ^
93fa75bb0 ^
e25474154 ^
f6f5c9e9e ^

e25474154 ^
4d9a5dc8f ^
92b8fac94 ^
2e5265bef ^
6a9baf3fd ^

6378fbd66 ^
6a9baf3fd ^
f6f5c9e9e ^

e25474154 ^
032599c15 ^


032599c15 ^

b595fc834 ^


92b8fac94 ^
b595fc834 ^
032599c15 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^
032599c15 ^
789ba107c ^







23c3af80f ^
e25474154 ^
55f8ed245 ^




92b8fac94 ^
b0c11d3ef ^

55f8ed245 ^
e25474154 ^
36e25a684 ^

92b8fac94 ^
e25474154 ^
eebee0eff ^
6378fbd66 ^
edab4aaad ^
3051c52f5 ^
8d19a93f1 ^
121519301 ^
9dd753f21 ^
a158053ae ^
42d0911d6 ^
623fd8a8a ^
e25474154 ^
3d1c6de63 ^

e25474154 ^
b731e6ef1 ^
e25474154 ^
b731e6ef1 ^

3d1c6de63 ^
e25474154 ^

36e25a684 ^

244c14db0 ^
e25474154 ^
9e92455a5 ^
5b28d0820 ^
7063670a2 ^
55f8ed245 ^
7063670a2 ^
e25474154 ^


5b28d0820 ^
e25474154 ^
b64eeeb43 ^
cb6ad8cb7 ^

244c14db0 ^
cb6ad8cb7 ^
9e92455a5 ^
623fd8a8a ^


7a2b1a752 ^
623fd8a8a ^


5b28d0820 ^



92b8fac94 ^
5b28d0820 ^
087b8621d ^
b731e6ef1 ^
5b28d0820 ^
92b8fac94 ^
de338526e ^
92b8fac94 ^
b731e6ef1 ^
087b8621d ^
7e31134ff ^

de338526e ^
be6474af6 ^
e25474154 ^
7063670a2 ^
9a6f47ae6 ^

b0c11d3ef ^

92b8fac94 ^
e25474154 ^
a1da1f987 ^

e25474154 ^
623fd8a8a ^
e25474154 ^
091c1b307 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438