ctucx.git: nimstagit

[nimlang] incomplete reimplementation of stagit

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 
import os, times, parsecfg, strutils, json

import types

proc readConfig* (path: string): types.Config =
  if not fileExists(path):
    echo "Error: config-file does not exist!"
    quit(QuitFailure)

  let configFile = loadConfig(path)

  let
    outputDirectory = configFile.getSectionValue("", "outputDirectory", "")
    title           = configFile.getSectionValue("", "title",           "nimstagit")
    description     = configFile.getSectionValue("", "description",     "")
    scanPath        = configFile.getSectionValue("", "scanPath",        "")
    projectsFile    = configFile.getSectionValue("", "projectsFile",    "")
    readmeFiles     = configFile.getSectionValue("", "readmeFiles",     "README, readme, README.md, readme.md")
    renderMarkdown  = configFile.getSectionValue("", "renderMarkdown",  "true").parseBool

  var projectsList: seq[string]

  if scanPath == "":
    echo "Error: config-value 'outputDirectory' has to be set!"
    quit(QuitFailure)

  if scanPath == "":
    echo "Error: config-value 'scanPath' has to be set!"
    quit(QuitFailure)

  if projectsFile != "":
    if not fileExists(projectsFile):
      echo "Error: projectsFile does not exist!"
      quit(QuitFailure)

    for element in readFile(projectsFile).splitLines:
      if element != "": projectsList.add(element)

  else:
    for kind, element in walkDir(scanPath):
      if kind != pcDir: continue
      projectsList.add(splitPath(element).tail)

  result = types.Config(
        outputDirectory:   outputDirectory,
        title:             title,
        description:       description,
        scanPath:          scanPath,
        projectsList:      projectsList,
        readmeFiles:       readmeFiles.split(", "),
        renderMarkdown:    renderMarkdown
      )

proc mergeJson* (a: JsonNode, b: JsonNode): JsonNode =
  result = a
  for key, val in b:
      result[key] = val 

proc nameFromPath* (path: string): string = result = splitPath(path).tail; removeSuffix(result, ".git")

proc relativeTimeFromNow* (time: Time): string =
  let
    timeNow  = now().toTime
    timeDiff = timeNow - time
    weeks    = inWeeks(timeDiff)
    days     = inDays(timeDiff)
    hours    = inHours(timeDiff)
    minutes  = inMinutes(timeDiff)

  if weeks > 0:
      result = $weeks & " week(s)"
  elif days > 0:
      result = $days & " day(s)"
  elif hours > 0:
      result = $hours & " hour(s)"
  elif minutes > 2:
      result = $minutes & " Minutes"
  else:
      result = "now"