aboutsummaryrefslogtreecommitdiff
path: root/file.nim
blob: 83c2560a2eebc8ffe0de9083080d6d8b787e5899 (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
# tickwatch
# Author: Euxane TRAN-GIRARD
# Licence: EUPL-1.2

import std/paths
import std/strutils


type
  FileValueMonitor = ref object
    path: Path

  FileChangeMonitor = ref object
    path: Path
    lastValue: int

proc readValue(path: Path): int =
  readFile(path.string).strip.parseInt

proc initFileValueMonitor*(path: Path): FileValueMonitor =
  FileValueMonitor(path: path)

proc readValue*(mon: FileValueMonitor): int =
  mon.path.readValue()

proc initFileChangeMonitor*(path: Path): FileChangeMonitor =
  FileChangeMonitor(
    path: path,
    lastValue: path.readValue(),
  )

proc readValue*(mon: FileChangeMonitor): int =
  let newValue = mon.path.readValue()
  result = abs(mon.lastValue - newValue)
  mon.lastValue = newValue