about summary refs log tree commit diff stats
path: root/src/display/client.nim
blob: 54fca3c1d8f57727285ce20057692c8c51298791 (plain) (blame)
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
import options
import os
import selectors
import streams
import tables
import terminal

when defined(posix):
  import posix

import bindings/quickjs
import buffer/container
import css/sheet
import config/config
import display/pager
import html/dom
import html/htmlparser
import io/lineedit
import io/loader
import io/request
import io/term
import js/javascript
import types/cookie
import types/url
import utils/twtstr

type
  Client* = ref ClientObj
  ClientObj* = object
    attrs: TermAttributes
    feednext: bool
    s: string
    errormessage: string
    userstyle: CSSStylesheet
    loader: FileLoader
    console {.jsget.}: Console
    pager {.jsget.}: Pager
    line {.jsget.}: LineEdit
    config: Config
    jsrt: JSRuntime
    jsctx: JSContext
    timeoutid: int
    timeouts: Table[int, tuple[handler: (proc()), fdi: int]]
    intervals: Table[int, tuple[handler: (proc()), fdi: int, tofree: JSValue]]
    timeout_fdis: Table[int, int]
    interval_fdis: Table[int, int]

  Console = ref object
    err: Stream
    pager: Pager
    container: Container
    prev: Container
    ibuf: string
    tty: File

proc readChar(console: Console): char =
  if console.ibuf == "":
    return console.tty.readChar()
  result = console.ibuf[0]
  console.ibuf = console.ibuf.substr(1)

proc `=destroy`(client: var ClientObj) =
  if client.jsctx != nil:
    free(client.jsctx)
  if client.jsrt != nil:
    free(client.jsrt)

proc doRequest(client: Client, req: Request): Response {.jsfunc.} =
  client.loader.doRequest(req)

proc interruptHandler(rt: JSRuntime, opaque: pointer): int {.cdecl.} =
  let client = cast[Client](opaque)
  try:
    let c = client.console.tty.readChar()
    if c == char(3): #C-c
      client.console.ibuf = ""
      return 1
    else:
      client.console.ibuf &= c
  except IOError:
    discard
  return 0

proc evalJS(client: Client, src, filename: string): JSObject =
  unblockStdin(client.console.tty.getFileHandle())
  result = client.jsctx.eval(src, filename, JS_EVAL_TYPE_GLOBAL)
  restoreStdin(client.console.tty.getFileHandle())

proc evalJSFree(client: Client, src, filename: string) =
  free(client.evalJS(src, filename))

proc command0(client: Client, src: string, filename = "<command>", silence = false) =
  let ret = client.evalJS(src, filename)
  if ret.isException():
    client.jsctx.writeException(client.console.err)
  else:
    if not silence:
      let str = ret.toString()
      if str.issome:
        client.console.err.write(str.get & '\n')
        client.console.err.flush()
  free(ret)

proc command(client: Client, src: string) =
  restoreStdin(client.console.tty.getFileHandle())
  client.command0(src)
  client.console.container.cursorLastLine()

proc quit(client: Client, code = 0) {.jsfunc.} =
  if stdout.isatty():
    print(HVP(getTermAttributes(stdout).height, 1))
    print('\n')
    print(EL())
    stdout.showCursor()
  when defined(posix):
    assert kill(client.loader.process, cint(SIGTERM)) == 0
  quit(code)

proc feedNext(client: Client) {.jsfunc.} =
  client.feednext = true

proc input(client: Client) =
  restoreStdin(client.console.tty.getFileHandle())
  let c = client.console.readChar()
  client.s &= c
  if client.pager.lineedit.isSome:
    let edit = client.pager.lineedit.get
    client.line = edit
    if edit.escNext:
      edit.escNext = false
      if edit.write(client.s):
        client.s = ""
    else:
      let action = getLinedAction(client.config, client.s)
      if action == "":
        if edit.write(client.s):
          client.s = ""
        else:
          client.feedNext = true
      elif not client.feedNext:
        client.evalJSFree(action, "<command>")
      if client.pager.lineedit.isNone:
        client.line = nil
      if not client.feedNext:
        client.pager.updateReadLine()
  else:
    let action = getNormalAction(client.config, client.s)
    client.evalJSFree(action, "<command>")
  if not client.feedNext:
    client.s = ""
  else:
    client.feedNext = false
  if client.pager.container != nil:
    if client.pager.lineedit.isNone:
      client.pager.refreshStatusMsg()
      client.pager.displayStatus()
      client.pager.displayCursor()

proc setTimeout[T: JSObject|string](client: Client, handler: T, timeout = 0): int {.jsfunc.} =
  let id = client.timeoutid
  inc client.timeoutid
  let fdi = client.pager.selector.registerTimer(timeout, true, nil)
  client.timeout_fdis[fdi] = id
  when T is string:
    client.timeouts[id] = ((proc() =
      client.evalJSFree(handler, "setTimeout handler")
    ), fdi)
  else:
    let fun = JS_DupValue(handler.ctx, handler.val)
    client.timeouts[id] = ((proc() =
      let ret = JSObject(ctx: handler.ctx, val: fun).callFunction()
      if ret.isException():
        ret.ctx.writeException(client.console.err)
      JS_FreeValue(ret.ctx, ret.val)
      JS_FreeValue(ret.ctx, fun)
    ), fdi)
  return id

proc setInterval[T: JSObject|string](client: Client, handler: T, interval = 0): int {.jsfunc.} =
  let id = client.timeoutid
  inc client.timeoutid
  let fdi = client.pager.selector.registerTimer(interval, false, nil)
  client.interval_fdis[fdi] = id
  when T is string:
    client.intervals[id] = ((proc() =
      client.evalJSFree(handler, "setInterval handler")
    ), fdi, JS_NULL)
  else:
    let fun = JS_DupValue(handler.ctx, handler.val)
    client.intervals[id] = ((proc() =
      let obj = JSObject(ctx: handler.ctx, val: fun)
      let ret = obj.callFunction()
      if ret.isException():
        ret.ctx.writeException(client.console.err)
      JS_FreeValue(ret.ctx, ret.val)
    ), fdi, fun)
  return id

proc clearTimeout(client: Client, id: int) {.jsfunc.} =
  if id in client.timeouts:
    let timeout = client.timeouts[id]
    client.pager.selector.unregister(timeout.fdi)
    client.timeout_fdis.del(timeout.fdi)
    client.timeouts.del(id)

proc clearInterval(client: Client, id: int) {.jsfunc.} =
  if id in client.intervals:
    let interval = client.intervals[id]
    client.pager.selector.unregister(interval.fdi)
    JS_FreeValue(client.jsctx, interval.tofree)
    client.interval_fdis.del(interval.fdi)
    client.intervals.del(id)

proc inputLoop(client: Client) =
  client.pager.selector.registerHandle(int(client.console.tty.getFileHandle()), {Read}, nil)
  while true:
    let events = client.pager.selector.select(-1)
    for event in events:
      if event.fd == client.console.tty.getFileHandle():
        client.input()
        stdout.flushFile()
      elif event.fd in client.interval_fdis:
        client.intervals[client.interval_fdis[event.fd]].handler()
      elif event.fd in client.timeout_fdis:
        let id = client.timeout_fdis[event.fd]
        let timeout = client.timeouts[id]
        timeout.handler()
        client.clearTimeout(id)
      else:
        let container = client.pager.fdmap[FileHandle(event.fd)]
        if not client.pager.handleEvent(container):
          disableRawMode()
          for msg in client.pager.status:
            eprint msg
          client.quit(1)
    if client.pager.scommand != "":
      client.command(client.pager.scommand)
      client.pager.scommand = ""
    if client.pager.lineedit.isNone and client.pager.redraw or client.pager.container.redraw:
      client.pager.refreshDisplay(client.pager.container)
      client.pager.draw()
      client.pager.redraw = false
      client.pager.container.redraw = false

#TODO this is dumb
proc readFile(client: Client, path: string): string {.jsfunc.} =
  try:
    return readFile(path)
  except IOError:
    discard

#TODO ditto
proc writeFile(client: Client, path: string, content: string) {.jsfunc.} =
  writeFile(path, content)

proc launchClient*(client: Client, pages: seq[string], ctype: Option[string], dump: bool) =
  if client.config.startup != "":
    let s = readFile(client.config.startup)
    client.console.err = newFileStream(stderr)
    client.command0(s, client.config.startup, silence = true)
    #TODO
    client.console.err = newStringStream()
    quit()
  client.userstyle = client.config.stylesheet.parseStylesheet()
  if not stdin.isatty:
    client.pager.readPipe(ctype, stdin.getFileHandle())
    stdin.close()
  else:
    client.console.tty = stdin

  for page in pages:
    client.pager.loadURL(page, ctype = ctype)

  if stdout.isatty() and not dump:
    when defined(posix):
      enableRawMode(client.console.tty.getFileHandle())
    client.inputLoop()
  else:
    for msg in client.pager.status:
      eprint msg
    for container in client.pager.containers:
      container.render()
      container.drawBuffer()
    stdout.close()
  client.quit()

proc nimGCStats(client: Client): string {.jsfunc.} =
  return GC_getStatistics()

proc jsGCStats(client: Client): string {.jsfunc.} =
  return client.jsrt.getMemoryUsage()

proc newConsole(pager: Pager): Console =
  new(result)
  var pipefd: array[0..1, cint]
  if pipe(pipefd) == -1:
    raise newException(Defect, "Failed to open console pipe.")
  let url = newURL("javascript:console.show()")
  result.container = pager.readPipe0(some("text/plain"), pipefd[0], option(url))
  pager.registerContainer(result.container)
  discard close(pipefd[0])
  var f: File
  if not open(f, pipefd[1], fmWrite):
    raise newException(Defect, "Failed to open file for console pipe.")
  result.err = newFileStream(f)
  result.pager = pager

proc log(console: Console, ss: varargs[string]) {.jsfunc.} =
  for i in 0..<ss.len:
    console.err.write(ss[i])
    if i != ss.high:
      console.err.write(' ')
  console.err.write('\n')
  console.err.flush()

proc show(console: Console) {.jsfunc.} =
  if console.pager.container != console.container:
    console.prev = console.pager.container
    console.pager.setContainer(console.container)

proc hide(console: Console) {.jsfunc.} =
  if console.pager.container == console.container:
    console.pager.setContainer(console.prev)

proc sleep(client: Client, millis: int) {.jsfunc.} =
  sleep millis

proc newClient*(config: Config): Client =
  new(result)
  result.config = config
  result.attrs = getTermAttributes(stdout)
  result.loader = newFileLoader()
  var tty: File
  if stdin.isatty():
    tty = stdin
  elif stdout.isatty():
    discard open(tty, "/dev/tty", fmRead)
  result.pager = newPager(config, result.attrs, tty)
  result.console = newConsole(result.pager)
  result.console.tty = tty
  let rt = newJSRuntime()
  rt.setInterruptHandler(interruptHandler, cast[pointer](result))
  let ctx = rt.newJSContext()
  result.jsrt = rt
  result.jsctx = ctx
  var global = ctx.getGlobalObject()
  ctx.registerType(Client, asglobal = true)
  global.setOpaque(result)
  ctx.setProperty(global.val, "client", global.val)
  free(global)

  ctx.registerType(Console)

  ctx.addCookieModule()
  ctx.addUrlModule()
  ctx.addDOMModule()
  ctx.addHTMLModule()
  ctx.addRequestModule()
  ctx.addLineEditModule()
  ctx.addPagerModule()