Files
cli-horizontal-rule/src/hr.nim

45 lines
1.3 KiB
Nim

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 [<heading>] [options]
Options:
-c, --color <color> Color to make the rule.
-d, --double-rule Draw as ═══════════
-C, --char <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["<heading>"]:
let heading = $args["<heading>"]
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)