diff options
author | euxane | 2024-11-30 09:58:49 +0100 |
---|---|---|
committer | euxane | 2024-11-30 09:58:49 +0100 |
commit | 903306030f59554ff6fd73146fd30c2330504358 (patch) | |
tree | 2c6063901e45b576100d6c312a68f453ef7b9dc7 | |
parent | dc2ec09d6d29913d696f0ea9dc5d035542c792e8 (diff) | |
download | tickwatch-903306030f59554ff6fd73146fd30c2330504358.tar.gz |
cli: add option for timestamp line header format
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | logger.nim | 17 | ||||
-rw-r--r-- | main.nim | 13 | ||||
-rw-r--r-- | 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 | |||
3 | 3 | ||
4 | ## Next release | 4 | ## Next release |
5 | * Fixed issue emitting pings when the system's IP address changed during exec | 5 | * Fixed issue emitting pings when the system's IP address changed during exec |
6 | * Added `--timestamp=datetime|unix|none` param to set line header format | ||
6 | 7 | ||
7 | ## v2.0 (2024-11-25) | 8 | ## v2.0 (2024-11-25) |
8 | * Fused the `--min` and `--max` display options into `--range=MIN:MAX` | 9 | * Fused the `--min` and `--max` display options into `--range=MIN:MAX` |
@@ -40,9 +40,22 @@ proc puts(f: File, s: string) = | |||
40 | proc puts(f: File, r: Rune) = | 40 | proc puts(f: File, r: Rune) = |
41 | f.puts r.toUTF8 | 41 | f.puts r.toUTF8 |
42 | 42 | ||
43 | proc loop*(probe: (timeout: Duration) -> int, getSymbol: (int) -> Rune) = | 43 | func formatTimestampDateTime*(dt: DateTime): string = |
44 | dt.format(TIMESTAMP_FORMAT) | ||
45 | |||
46 | func formatTimestampUnix*(dt: DateTime): string = | ||
47 | ($dt.toTime.toUnix) & " " | ||
48 | |||
49 | func formatTimestampNone*(dt: DateTime): string = | ||
50 | "" | ||
51 | |||
52 | proc loop*( | ||
53 | probe: (timeout: Duration) -> int, | ||
54 | getSymbol: (int) -> Rune, | ||
55 | timestampHeader: (DateTime) -> string, | ||
56 | ) = | ||
44 | while true: | 57 | while true: |
45 | stdout.puts now().format(TIMESTAMP_FORMAT) | 58 | stdout.puts timestampHeader(now()) |
46 | 59 | ||
47 | for tick in 0..<60: | 60 | for tick in 0..<60: |
48 | let t = now() | 61 | let t = now() |
@@ -37,6 +37,7 @@ proc main() = | |||
37 | var | 37 | var |
38 | scale: Scale = log2 | 38 | scale: Scale = log2 |
39 | symbols = UNICODE_SYMBOLS | 39 | symbols = UNICODE_SYMBOLS |
40 | timestampHeader = formatTimestampDateTime | ||
40 | (min, max) = (0, 1000) | 41 | (min, max) = (0, 1000) |
41 | probe: (timeout: Duration) -> int | 42 | probe: (timeout: Duration) -> int |
42 | 43 | ||
@@ -66,6 +67,12 @@ proc main() = | |||
66 | of "unicode", "utf8", "utf-8": symbols = UNICODE_SYMBOLS | 67 | of "unicode", "utf8", "utf-8": symbols = UNICODE_SYMBOLS |
67 | of "numeric", "ascii": symbols = NUMERIC_SYMBOLS | 68 | of "numeric", "ascii": symbols = NUMERIC_SYMBOLS |
68 | 69 | ||
70 | of "timestamp": | ||
71 | case val: | ||
72 | of "datetime", "date-time": timestampHeader = formatTimestampDateTime | ||
73 | of "unix", "epoch": timestampHeader = formatTimestampUnix | ||
74 | of "none", "false": timestampHeader = formatTimestampNone | ||
75 | |||
69 | of "range": | 76 | of "range": |
70 | let parts = val.split(':', 1) | 77 | let parts = val.split(':', 1) |
71 | if parts.len != 2: raise newException(ValueError, "Invalid range") | 78 | if parts.len != 2: raise newException(ValueError, "Invalid range") |
@@ -87,7 +94,11 @@ proc main() = | |||
87 | if probe == nil: | 94 | if probe == nil: |
88 | raise newException(ValueError, "Missing monitor argument") | 95 | raise newException(ValueError, "Missing monitor argument") |
89 | 96 | ||
90 | loop(probe, (val: int) => symbols.indicator(min, max, val, scale)) | 97 | loop( |
98 | probe, | ||
99 | (val: int) => symbols.indicator(min, max, val, scale), | ||
100 | timestampHeader, | ||
101 | ) | ||
91 | 102 | ||
92 | 103 | ||
93 | when not defined(test): | 104 | when not defined(test): |
@@ -36,6 +36,8 @@ Display options: | |||
36 | Choices: log2 (default), log10, ln, linear | 36 | Choices: log2 (default), log10, ln, linear |
37 | --symbols=CHOICE Set output symbol set | 37 | --symbols=CHOICE Set output symbol set |
38 | Choices: unicode (default), numeric | 38 | Choices: unicode (default), numeric |
39 | --timestamp=CHOICE Set line header timestamp format | ||
40 | Choices: datetime (default), unix, none | ||
39 | 41 | ||
40 | Other options: | 42 | Other options: |
41 | --help, -h Display this help message | 43 | --help, -h Display this help message |