about summary refs log tree commit diff stats
path: root/src/main.nim
blob: 84b57bcf1f71953b9dbb730b1e77b486dfd5e215 (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
import types/dispatcher
let disp = newDispatcher()

import options
import os
import terminal

when defined(profile):
  import nimprof

import config/config
import display/client
import ips/forkserver
import utils/twtstr

let conf = readConfig()
widthtable = makewidthtable(conf.ambiguous_double)
let params = commandLineParams()

proc version(long: static bool = false): string =
  result = "Chawan browser v0.1 "
  when defined(debug):
    result &= "(debug)"
  else:
    result &= "(release)"

proc help(i: int) =
  let s = version() & """

Usage: cha [options] [URL(s) or file(s)...]
Options:
    --                          Interpret all following arguments as URLs
    -d, --dump                  Print page to stdout
    -c, --css <stylesheet>      Pass stylesheet (e.g. -c 'a{color: blue}')
    -o, --opt <config>          Pass config options (e.g. -o 'page.q="QUIT"')
    -T, --type <type>           Specify content mime type
    -M, --monochrome            Alias of -o color-mode='monochrome'
    -V, --visual                Visual startup mode
    -r, --run <script/file>     Run passed script or file
    -h, --help                  Print this usage message
    -v, --version               Print version information"""
  if i == 0:
    echo s
  else:
    eprint s
  quit(i)

var i = 0
var ctype = none(string)
var pages: seq[string]
var dump = false
var visual = false
var escape_all = false
while i < params.len:
  let param = params[i]
  if escape_all: # after --
    pages.add(param)
    inc i
    continue
  case param
  of "-v", "--version":
    echo version(true)
    quit(0)
  of "-M", "--monochrome":
    conf.colormode = some(MONOCHROME)
  of "-V", "--visual":
    visual = true
  of "-T":
    inc i
    if i < params.len:
      ctype = some(params[i])
    else:
      help(1)
  of "-":
    discard # emulate programs that accept - as stdin
  of "-d", "-dump", "--dump":
    dump = true
  of "-c", "--css":
    inc i
    if i < params.len:
      conf.stylesheet &= params[i]
    else:
      help(1)
  of "-o", "--opt":
    inc i
    if i < params.len:
      conf.parseConfig(getCurrentDir(), params[i])
    else:
      help(1)
  of "-h",  "--help":
    help(0)
  of "-r", "--run":
    inc i
    if i < params.len:
      conf.startup = params[i]
      conf.headless = true
      dump = true
    else:
      help(1)
  of "--":
    escape_all = true
  elif param.len > 0:
    if param[0] == '-':
      help(1)
    else:
      pages.add(param)
  inc i

if pages.len == 0 and stdin.isatty():
  if visual:
    pages.add(conf.visualhome)
  else:
    let http = getEnv("HTTP_HOME")
    if http != "": pages.add(http)
    else:
      let www = getEnv("WWW_HOME")
      if www != "": pages.add(www)

if pages.len == 0 and not conf.headless:
  if stdin.isatty:
    help(1)

conf.nmap = constructActionTable(conf.nmap)
conf.lemap = constructActionTable(conf.lemap)
disp.forkserver.loadForkServerConfig(conf)

newClient(conf, disp).launchClient(pages, ctype, dump)