about summary refs log tree commit diff stats
path: root/main.nim
blob: db4b0f6d4a17b8631e500fb0d52b61786bb42688 (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
import httpClient
import uri
import os

import fusion/htmlparser
import fusion/htmlparser/xmltree

import display
import termattrs
import buffer
import twtio
import config

proc loadRemotePage*(url: string): string =
  return newHttpClient().getContent(url)

proc loadLocalPage*(url: string): string =
  return readFile(url)

proc loadPageUri(uri: Uri, currentcontent: XmlNode): XmlNode =
  var moduri = uri
  moduri.anchor = ""
  if uri.scheme == "" and uri.path == "" and uri.anchor != "" and currentcontent != nil:
    return currentcontent
  elif uri.scheme == "" or uri.scheme == "file":
    return parseHtml(loadLocalPage($moduri))
  else:
    return parseHtml(loadRemotePage($moduri))

var buffers: seq[Buffer]

proc main*() =
  if paramCount() != 1:
    eprint "Invalid parameters. Usage:\ntwt <url>"
    quit(1)
  if not readConfig("config"):
    eprint "Failed to read keymap, falling back to default"
  let attrs = getTermAttributes()
  let buffer = newBuffer(attrs)
  let uri = parseUri(paramStr(1))
  buffers.add(buffer)
  buffer.setLocation(uri)
  buffer.htmlSource = loadPageUri(uri, buffer.htmlSource)
  buffer.renderHtml()
  var lastUri = uri
  while displayPage(attrs, buffer):
    statusMsg("Loading...", buffer.height)
    var newUri = buffer.document.location
    lastUri.anchor = ""
    newUri.anchor = ""
    if $lastUri != $newUri:
      buffer.clearBuffer()
      buffer.htmlSource = loadPageUri(buffer.document.location, buffer.htmlSource)
      buffer.renderHtml()
    lastUri = newUri

#waitFor loadPage("https://lite.duckduckgo.com/lite/?q=hello%20world")
main()