aboutsummaryrefslogtreecommitdiff
path: root/src/then.nim
blob: be3e813e8f5c7ef115b87ffab3c0f38894428c03 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import std/[times, os, strutils, strbasics, strscans, sets, re, deques], parseopt

const VERSION = "0.1"
const configDir = expandTilde("~/.config/then/")
const defaultConfigFile = configDir & "then.toml"
const defaultCalendarFile = configDir & "calendar.then"

type
  Token = object
    token: string
    precedence: int

# Creates a default configuration file if it doesn't exist
proc createDefaultConfigFile() =
  if not fileExists(defaultConfigFile):
    if not dirExists(configDir):
      createDir(configDir)
    writeFile(defaultConfigFile, """
    [config]
    directory = "~/.config/then/"
    """)

# Creates a default calendar file if it doesn't exist
proc createDefaultCalendarFile() =
  if not fileExists(defaultCalendarFile):
    if not dirExists(configDir):
      createDir(configDir)
    writeFile(defaultCalendarFile, """
    # Add your events here
    """)

proc printHelp(badCmd: string) =
  if badCmd != "":
    echo badCmd & " is not a valid option\n"
  echo """
Usage:
  then [options] [commands]

OPTIONS
  -h, --help     show list of CLI options
  -v, --version  show then version
  --future=DAYS  set future
  --past=DAYS    set past

COMMANDS
  a              print upcoming agenda (default command)
  e              open default calendar file in $EDITOR
  j              print the modified julian day
  """
  quit()

proc printVersion() =
  echo "then " & VERSION
  quit()

proc checkType(cond: string): string =
  if cond.len == 4 and parseInt(cond) != 0: return "year"
  if cond.len in [1, 2] and parseInt(cond) != 0: return "day"
  if cond in DefaultLocale.MMM.toHashSet or cond in DefaultLocale.MMMM.toHashSet: return "month"
  if cond in DefaultLocale.ddd.toHashSet or cond in DefaultLocale.dddd.toHashSet: return "dayofweek"
  if find(cond, re"\d{1,2}:\d{2}") > -1: return "time"
  if find(cond, re"\dw") > -1: return "firstweek"
  if find(cond, re"\dl") > -1: return "lastweek"
  if find(cond, re"J%\d([-+]\d)?") > -1: return "julian"
  return "unknown"

proc checkCond(cond: string, day: DateTime): bool =
  case checkType(cond):
    of "year":
      if parseInt(cond) == day.year: return true
    of "day":
      if parseInt(cond) == parseInt(day.format("d")): return true
      if parseInt(cond) == parseInt(day.format("dd")): return true
    of "month":
      if cond == day.format("MMM"): return true
      if cond == day.format("MMMM"): return true
    of "dayofweek":
      if cond == day.format("ddd"): return true
      if cond == day.format("dddd"): return true
    of "time":
      return true
    of "firstweek":
      discard
# Uh oh we didn't think about this hard enough

    of "unknown":
      echo "UNKNOWN THING ", cond
  return false

proc getConditions(dateStr: string): seq[string] =
  var conditions: seq[string]
  conditions = dateStr.rsplit(' ')
  return conditions

# TODO simplify?? Would it be easier to identify conditions and replace them with TRUE/FALSE then just process the string?

# Shunting yard to parse conditions, parens, and logical operators
# Does NOT parse out individual conditions yet because some individual
# conditions (like 1W) require context of surrounding conditions.
# The logic around parentheses is a little weird because conditions like
# "<(March 15)" contain parentheses that we don't want to throw out
proc parseDateToEvents(dateStr: string): seq[string] =
  var holdingStack = initDeque[Token]()
  var resultStack = initDeque[Token]()
  var newToken: string = ""
  for c in dateStr:
    echo c
    case c:
      of '(':
        if newToken.len == 1:
          if newToken[0] == '>' or newToken[0] == '<':
            newToken.add(c)
          else
            echo "ERROR: newToken exists but we got an open paren?"
        else:
          holdingStack.addLast(Token(token: "(", precedence: 0))
      of ')':
        if newToken.len > 0: # Closing token like <(March 15). Push the token.
          resultStack.addLast(Token(token: newToken, precedence: 0))
          newToken = ""
        else: # Logical closing paren. Pop stack until (.
          while holdingStack.len > 0 and holdingStack.peekLast.token != "(":
            if holdingStack.len == 0:
              echo "UH OH, ERROR MISMATCHED PARENS"
            resultStack.addLast(holdingStack.popLast)
          discard holdingStack.popLast # Pop the (
      of '!':
        holdingStack.addLast(Token(token: "!", precedence: 3))
      of '|':
        while holdingStack.len > 0 and holdingStack.peekLast.precedence > 2:
          resultStack.addLast(holdingStack.popLast)
        holdingStack.addLast(Token(token: "|", precedence: 2))
      of ' ':
        if newToken.len > 1 and newToken[1] == '(' and newToken[newToken.len-1] != ')': # Middle of token like <(March 15).
          newToken.add(c)
        else: # End of new token, implicit &
          # Add newToken to result
          resultStack.addLast(Token(token: newToken, precedence: 0))
          newToken = ""
          # Add implicit & to holding
          while holdingStack.len > 0 and holdingStack.peekLast.precedence > 1:
            resultStack.addLast(holdingStack.popLast)
          holdingStack.addLast(Token(token:"&", precedence: 1))
      of '>','<',':','-','a'..'z','A'..'Z','0'..'9':
        newToken.add(c)
      else:
        echo "Weird character in dateStr: ", c
        discard
  # Push last token
  if newToken.len > 0:
    resultStack.addLast(Token(token: newToken, precedence: 0))
  # Clear the holding stack
  while holdingStack.len > 0:
    var t = holdingStack.popLast
    if t.token != "(":
      resultStack.addLast(t)
  echo resultStack


proc getEvents(cal: seq[string], day: DateTime): seq[string] =
  var events: seq[string]
  for line in cal:
    var (success, dateStr, eventStr) = scanTuple(line, "$*,$*")
    if success:
      dateStr.strip
      eventStr.strip
      discard parseDateToEvents(dateStr)
      #let conditions = getConditions(dateStr)
    #  var match = true
    #  for c in conditions:
    #    c = c.toLower.capitalizeAscii
    #    if checkCond(c, day) == false:
    #      match = false
    #      break
    #  if match:
    #    events.add(eventStr)
    else:
      echo "WARNING: Cannot parse line: ", line
  return events

proc printEvents(cal: seq[string], day: DateTime) =
  let events = getEvents(cal, day)
  if events.len > 0:
    echo day.format("ddd  yyyy MMM dd")
    for e in events:
      echo " - ", e

proc openCal(file: string): seq[string] =
  let f = open(file)
  var cal: seq[string]
  for line in f.lines:
    cal.add(line)
  f.close()
  return cal

proc printCalendar() =
  let cal = openCal(defaultCalendarFile)
  var past = 3
  var future = 7
  let now = now()

  while past > 0:
    let curDay = now - past.days
    printEvents(cal, curDay)
    dec past

  printEvents(cal, now)

  for i in 1..future:
    let curDay = now + i.days
    printEvents(cal, curDay)

proc main() =
  var   past: int = 1
  var future: int = 14
  var  noArg: bool = true
  for kind, key, val in getopt():
    case kind
    of cmdLongOption, cmdShortOption:
      noArg = false
      case key
      of "help", "h":
        printHelp("")
      of "version", "v":
        printVersion()
      else:
        printHelp(key)
    else:
      discard

  if noArg:
    printCalendar()

when isMainModule:
  main()