From 903306030f59554ff6fd73146fd30c2330504358 Mon Sep 17 00:00:00 2001 From: euxane Date: Sat, 30 Nov 2024 09:58:49 +0100 Subject: cli: add option for timestamp line header format --- changelog.md | 1 + logger.nim | 17 +++++++++++++++-- main.nim | 13 ++++++++++++- readme.md | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 3f6aebc..b8af052 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ Changelog ## Next release * Fixed issue emitting pings when the system's IP address changed during exec +* Added `--timestamp=datetime|unix|none` param to set line header format ## v2.0 (2024-11-25) * Fused the `--min` and `--max` display options into `--range=MIN:MAX` diff --git a/logger.nim b/logger.nim index a4d90a0..48aedf2 100644 --- a/logger.nim +++ b/logger.nim @@ -40,9 +40,22 @@ proc puts(f: File, s: string) = proc puts(f: File, r: Rune) = f.puts r.toUTF8 -proc loop*(probe: (timeout: Duration) -> int, getSymbol: (int) -> Rune) = +func formatTimestampDateTime*(dt: DateTime): string = + dt.format(TIMESTAMP_FORMAT) + +func formatTimestampUnix*(dt: DateTime): string = + ($dt.toTime.toUnix) & " " + +func formatTimestampNone*(dt: DateTime): string = + "" + +proc loop*( + probe: (timeout: Duration) -> int, + getSymbol: (int) -> Rune, + timestampHeader: (DateTime) -> string, +) = while true: - stdout.puts now().format(TIMESTAMP_FORMAT) + stdout.puts timestampHeader(now()) for tick in 0..<60: let t = now() diff --git a/main.nim b/main.nim index 3128bac..cca5528 100644 --- a/main.nim +++ b/main.nim @@ -37,6 +37,7 @@ proc main() = var scale: Scale = log2 symbols = UNICODE_SYMBOLS + timestampHeader = formatTimestampDateTime (min, max) = (0, 1000) probe: (timeout: Duration) -> int @@ -66,6 +67,12 @@ proc main() = of "unicode", "utf8", "utf-8": symbols = UNICODE_SYMBOLS of "numeric", "ascii": symbols = NUMERIC_SYMBOLS + of "timestamp": + case val: + of "datetime", "date-time": timestampHeader = formatTimestampDateTime + of "unix", "epoch": timestampHeader = formatTimestampUnix + of "none", "false": timestampHeader = formatTimestampNone + of "range": let parts = val.split(':', 1) if parts.len != 2: raise newException(ValueError, "Invalid range") @@ -87,7 +94,11 @@ proc main() = if probe == nil: raise newException(ValueError, "Missing monitor argument") - loop(probe, (val: int) => symbols.indicator(min, max, val, scale)) + loop( + probe, + (val: int) => symbols.indicator(min, max, val, scale), + timestampHeader, + ) when not defined(test): diff --git a/readme.md b/readme.md index c82ba18..a105b43 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,8 @@ Display options: Choices: log2 (default), log10, ln, linear --symbols=CHOICE Set output symbol set Choices: unicode (default), numeric + --timestamp=CHOICE Set line header timestamp format + Choices: datetime (default), unix, none Other options: --help, -h Display this help message -- cgit v1.2.3