add print helper

This commit is contained in:
Rasmus Moorats 2023-09-30 16:15:06 +03:00
parent 49a1eac1b6
commit f2ebcdc16e
Signed by: xx
GPG key ID: FE14255A6AE7241C
3 changed files with 19 additions and 7 deletions

View file

@ -1,4 +1,5 @@
import std/[os, posix, posix_utils]
import lib/prnt
proc `<`(t1, t2: Time): bool {.borrow.}
@ -19,5 +20,4 @@ for dir in params:
if latest[1] < tim:
latest = (file.path, tim)
stdout.write latest[0]
stdout.flushFile
prnt latest[0]

11
src/lib/prnt.nim Normal file
View file

@ -0,0 +1,11 @@
import std/terminal
let isTty = stdout.isatty
proc prnt*(what: string) =
## Writes to stdout.
## If the stdout is a tty, appends a newline.
## Otherwise, no newline is inserted.
stdout.write what
if isTty: stdout.write "\n"
stdout.flushFile

View file

@ -1,4 +1,5 @@
import std/[os, random, strutils]
import std/[os, random, strutils, sugar]
import lib/prnt
const selection = Digits + Letters
@ -10,8 +11,8 @@ let
randomize()
# todo: write helper proc that only writes newline if it's a tty
for _ in 0 ..< length:
stdout.write selection.sample
let res = collect:
for _ in 0 ..< length:
selection.sample
stdout.flushFile
prnt cast[string](res)