about summary refs log tree commit diff stats
path: root/adapter/tools/mancha.nim
blob: fa18717756efe6e401cebf281931dabfcccede2b (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
import std/os
import std/osproc

proc help(i: int) =
    let s = """
Usage:
mancha [-M path] [[-s] section] -k keyword
mancha [-M path] [[-s] section] name
mancha -l file"""
    if i == 0:
      stdout.write(s & '\n')
    else:
      stderr.write(s & '\n')
    quit(i)

var i = 1
let n = paramCount()

proc getnext(): string =
  inc i
  if i <= n:
    return paramStr(i)
  help(1)
  ""

proc main() =
  let n = paramCount()
  var section = ""
  var local = ""
  var man = ""
  var keyword = ""
  var forceSection = false
  while i <= n:
    let s = paramStr(i)
    if s == "":
      inc i
      continue
    let c = s[0]
    let L = s.len
    if c == '-':
      if s.len != 2:
        help(1)
      case s[1]
      of 'h': help(0)
      of 'M': putEnv("MANPATH", getnext())
      of 's':
        section = getnext()
        forceSection = true
      of 'l': local = getnext()
      of 'k': keyword = getnext()
      else: help(1)
    elif section == "" and (c in {'0'..'9'} or L == 1 and c in {'n', 'l', 'x'}):
      section = s
    elif man == "":
      man = s
    else:
      help(1)
    inc i
  if not forceSection and section != "" and man == "" and keyword == "" and
      local == "":
    man = section
    section = ""
  if not ((local != "") != (man != "") != (keyword != "")):
    help(1)
  if local != "" and section != "":
    help(1)
  let qsec = if section != "": '(' & section & ')' else: ""
  let query = if local != "":
    if local[0] == '~':
      local = expandTilde(local)
    elif local[0] != '/':
      local = (getCurrentDir() / local)
    "man-l:" & local
  elif keyword != "":
    "man-k:" & keyword & qsec
  else:
    "man:" & man & qsec
  var cha = getEnv("MANCHA_CHA")
  if cha == "":
    cha = "cha"
  quit(execCmd(cha & " " & quoteShellPosix(query)))

main()