From a348e8732aa03e75b632983949469e2fcbc5a849 Mon Sep 17 00:00:00 2001 From: euxane Date: Sun, 24 Nov 2024 17:17:02 +0100 Subject: file: add module --- file.nim | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 file.nim diff --git a/file.nim b/file.nim new file mode 100644 index 0000000..83c2560 --- /dev/null +++ b/file.nim @@ -0,0 +1,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 -- cgit v1.2.3