aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreuxane2024-11-24 17:17:02 +0100
committereuxane2024-11-24 17:17:02 +0100
commita348e8732aa03e75b632983949469e2fcbc5a849 (patch)
treeb13a1d145473120a4c1416c06574e3ffb8ff361e
parentb9f75cc1014a90e7af3aea76b197d40a043bf513 (diff)
downloadtickwatch-a348e8732aa03e75b632983949469e2fcbc5a849.tar.gz
file: add module
-rw-r--r--file.nim35
1 files changed, 35 insertions, 0 deletions
diff --git a/file.nim b/file.nim
new file mode 100644
index 0000000..83c2560
--- /dev/null
+++ b/file.nim
@@ -0,0 +1,35 @@
1# tickwatch
2# Author: Euxane TRAN-GIRARD
3# Licence: EUPL-1.2
4
5import std/paths
6import std/strutils
7
8
9type
10 FileValueMonitor = ref object
11 path: Path
12
13 FileChangeMonitor = ref object
14 path: Path
15 lastValue: int
16
17proc readValue(path: Path): int =
18 readFile(path.string).strip.parseInt
19
20proc initFileValueMonitor*(path: Path): FileValueMonitor =
21 FileValueMonitor(path: path)
22
23proc readValue*(mon: FileValueMonitor): int =
24 mon.path.readValue()
25
26proc initFileChangeMonitor*(path: Path): FileChangeMonitor =
27 FileChangeMonitor(
28 path: path,
29 lastValue: path.readValue(),
30 )
31
32proc readValue*(mon: FileChangeMonitor): int =
33 let newValue = mon.path.readValue()
34 result = abs(mon.lastValue - newValue)
35 mon.lastValue = newValue