about summary refs log tree commit diff stats
path: root/src/main.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-03-13 13:40:23 +0100
committerbptato <nincsnevem662@gmail.com>2021-03-13 13:40:23 +0100
commit97f19da347b27a4d12f54784fa8bcbf304aa4fea (patch)
treec4bd5c601f0f8774402e4182bde8af6f5bcd5c3b /src/main.nim
parent6084e104d5c3868196c9e2d3748c6627cf983470 (diff)
downloadchawan-97f19da347b27a4d12f54784fa8bcbf304aa4fea.tar.gz
Moved stuff etc
Diffstat (limited to 'src/main.nim')
-rw-r--r--src/main.nim65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main.nim b/src/main.nim
new file mode 100644
index 00000000..bc212ef0
--- /dev/null
+++ b/src/main.nim
@@ -0,0 +1,65 @@
+import httpClient
+import uri
+import os
+import streams
+
+import display
+import termattrs
+import buffer
+import twtio
+import config
+import parser
+
+let clientInstance = newHttpClient()
+proc loadRemotePage*(url: string): string =
+  return clientInstance.getContent(url)
+
+proc loadLocalPage*(url: string): string =
+  return readFile(url)
+
+proc getRemotePage*(url: string): Stream =
+  return clientInstance.get(url).bodyStream
+
+proc getLocalPage*(url: string): Stream =
+  return newFileStream(url, fmRead)
+
+proc getPageUri(uri: Uri): Stream =
+  var moduri = uri
+  moduri.anchor = ""
+  if uri.scheme == "" or uri.scheme == "file":
+    return getLocalPage($moduri)
+  else:
+    return getRemotePage($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.document = parseHtml(getPageUri(uri))
+  buffer.setLocation(uri)
+  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()
+      if uri.scheme == "" and uri.path == "" and uri.anchor != "":
+        discard
+      else:
+        buffer.document = parseHtml(getPageUri(buffer.document.location))
+      buffer.renderHtml()
+    lastUri = newUri
+
+main()
+#parseCSS(newFileStream("default.css", fmRead))