diff options
author | euxane | 2024-11-24 17:17:02 +0100 |
---|---|---|
committer | euxane | 2024-11-24 17:17:02 +0100 |
commit | a348e8732aa03e75b632983949469e2fcbc5a849 (patch) | |
tree | b13a1d145473120a4c1416c06574e3ffb8ff361e | |
parent | b9f75cc1014a90e7af3aea76b197d40a043bf513 (diff) | |
download | tickwatch-a348e8732aa03e75b632983949469e2fcbc5a849.tar.gz |
file: add module
-rw-r--r-- | file.nim | 35 |
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 | |||
5 | import std/paths | ||
6 | import std/strutils | ||
7 | |||
8 | |||
9 | type | ||
10 | FileValueMonitor = ref object | ||
11 | path: Path | ||
12 | |||
13 | FileChangeMonitor = ref object | ||
14 | path: Path | ||
15 | lastValue: int | ||
16 | |||
17 | proc readValue(path: Path): int = | ||
18 | readFile(path.string).strip.parseInt | ||
19 | |||
20 | proc initFileValueMonitor*(path: Path): FileValueMonitor = | ||
21 | FileValueMonitor(path: path) | ||
22 | |||
23 | proc readValue*(mon: FileValueMonitor): int = | ||
24 | mon.path.readValue() | ||
25 | |||
26 | proc initFileChangeMonitor*(path: Path): FileChangeMonitor = | ||
27 | FileChangeMonitor( | ||
28 | path: path, | ||
29 | lastValue: path.readValue(), | ||
30 | ) | ||
31 | |||
32 | proc readValue*(mon: FileChangeMonitor): int = | ||
33 | let newValue = mon.path.readValue() | ||
34 | result = abs(mon.lastValue - newValue) | ||
35 | mon.lastValue = newValue | ||