From 65a5d026e1e9c2d81efbf5669096152582c8608c Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Tue, 16 Dec 2025 15:00:33 -0600 Subject: [PATCH] WIP: Refactor for Nim 2.x --- log_happy.nim | 27 ++++++++++++++++++++------- log_happy.nimble | 3 +-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/log_happy.nim b/log_happy.nim index 2d996ca..d404880 100644 --- a/log_happy.nim +++ b/log_happy.nim @@ -2,8 +2,9 @@ ## ========= ## ## Little tool to extract expected information from log streams. -import json, logging, ncurses, os, osproc, sequtils, streams, strutils, threadpool -import nre except toSeq +import std/[json, logging, os, osproc, sequtils, streams, strutils] +import std/nre except toSeq +import docopt, malebolgia, ncurses import private/ncurses_ext @@ -15,6 +16,16 @@ type pattern: Regex expected, found: bool + SourceKind = enum skStdIn, skFile, skCmd + InSource = object + case kind: SourceKind + of skStdIn: + discard + of skFile: + filename: string + of skCmd: + cmdInvocation: string + let expPattern = re"^-([eE])([^;]+);(.+)$" var msgChannel: Channel[string] @@ -24,15 +35,16 @@ proc exitErr(msg: string): void = quit(QuitFailure) proc readStream(stream: Stream): void = - var line = TaintedString"" + var line: string try: while stream.readLine(line): msgChannel.send(line) except: discard "" when isMainModule: var cmdProc: Process - var inStream: Stream + var source = InSource(kind: skStdIn) var outFile: File + var threadMaster = createMaster() try: let VERSION = "0.2.0" @@ -113,10 +125,10 @@ Expectations JSON definitions follow this format: elif arg.startsWith("-i"): let filename = arg[2..^1] try: - if not existsFile(filename): + if not fileExists(filename): exitErr "no such file (" & filename & ")" - inStream = newFileStream(filename) + source = InSource(kind: skFile, filename: filename) except: exitErr "could not open file (" & filename & "):\t\n" & getCurrentExceptionMsg() @@ -157,7 +169,7 @@ Expectations JSON definitions follow this format: inStream = cmdProc.outputStream open(msgChannel) - spawn readStream(inStream) + threadMaster.spawn readStream(inStream) # Init ncurses let stdscr = initscr() @@ -272,5 +284,6 @@ Expectations JSON definitions follow this format: if not inStream.isNil: close(inStream) if not outFile.isNil: close(outFile) + threadMaster.cancel() close(msgChannel) endwin() diff --git a/log_happy.nimble b/log_happy.nimble index d3ed46c..2ed42c8 100644 --- a/log_happy.nimble +++ b/log_happy.nimble @@ -8,5 +8,4 @@ bin = @["log_happy"] # Dependencies -requires @["nim >= 0.16.1", "docopt >= 0.6.4", "ncurses"] - +requires @["nim >= 0.16.1", "docopt >= 0.6.4", "ncurses", "malebolgia >= 1.3.2"]