From 0f40bc9ae4be09ff24f79fef992409cdc4c69646 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 19 Dec 2025 11:36:34 -0600 Subject: [PATCH] Initial commit and implementation. --- .gitignore | 2 ++ hr.nimble | 14 ++++++++++++++ src/hr.nim | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 hr.nimble create mode 100644 src/hr.nim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9f3eb0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/hr +.*.sw? diff --git a/hr.nimble b/hr.nimble new file mode 100644 index 0000000..01749a1 --- /dev/null +++ b/hr.nimble @@ -0,0 +1,14 @@ +# Package + +version = "0.1.0" +author = "Jonathan Bernard" +description = "Simple CLI utility to print a horizontal rule with an optional header" +license = "MIT" +srcDir = "src" +bin = @["hr"] + + +# Dependencies + +requires "nim >= 2.2.6" +requires @["cliutils >= 0.11.0", "docopt"] diff --git a/src/hr.nim b/src/hr.nim new file mode 100644 index 0000000..dee84f9 --- /dev/null +++ b/src/hr.nim @@ -0,0 +1,44 @@ +import std/[strutils, terminal, unicode] +import cliutils, docopt +# This is just an example to get you started. A typical binary package +# uses this file as the main entry point of the application. + +const VERSION = "0.1.0" +const USAGE = """Usage: + hr [] [options] + +Options: + + -c, --color Color to make the rule. + -d, --double-rule Draw as ═══════════ + -C, --char Character to use to make the rule +""" + +when isMainModule: + try: + let args = docopt(USAGE, version = VERSION) + + let color = + if args["--color"]: parseEnum[TerminalColors]("c" & $args["--color"]) + else: cDefault + + let ruleChar = + if args["--char"]: $args["--char"] + elif args["--double-rule"]: "═" + else: "─" + + stdout.writeLine(termFmt( + repeat(ruleChar, terminalWidth()), + fg = color, bg = cDefault)) + + if args[""]: + let heading = $args[""] + let padding = max(0, (terminalWidth() - runeLen(heading)) div 2) + stdout.writeLine(termFmt( + repeat(" ", padding) & heading & repeat(" ", padding), + fg = color, bg = cDefault)) + + except: + stderr.writeLine("hf - FATAL: " & getCurrentExceptionMsg()) + stderr.writeLine(getCurrentException().getStackTrace()) + quit(QuitFailure)