mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 11:00:45 -06:00
Compare commits
4 commits
7385a93110
...
56bb2bb7c1
Author | SHA1 | Date | |
---|---|---|---|
56bb2bb7c1 | |||
c8c66648cc | |||
9462d99d95 | |||
d66e2ed5e2 |
4 changed files with 52 additions and 40 deletions
|
@ -4,8 +4,7 @@
|
||||||
use BB style markup to add color to strings using VT100 escape codes
|
use BB style markup to add color to strings using VT100 escape codes
|
||||||
]##
|
]##
|
||||||
|
|
||||||
# TODO:
|
{.push raises:[].}
|
||||||
#{.push raises:[].}
|
|
||||||
|
|
||||||
import std/[
|
import std/[
|
||||||
macros, os, sequtils, strformat, strutils, terminal
|
macros, os, sequtils, strformat, strutils, terminal
|
||||||
|
@ -72,7 +71,41 @@ const ColorXTermNames = enumNames(ColorXterm).mapIt(firstCapital(it))
|
||||||
const BbStyleNames = enumNames(BbStyle).mapIt(firstCapital(it))
|
const BbStyleNames = enumNames(BbStyle).mapIt(firstCapital(it))
|
||||||
const ColorDigitStrings = (1..255).toSeq().mapIt($it)
|
const ColorDigitStrings = (1..255).toSeq().mapIt($it)
|
||||||
|
|
||||||
# TODO: write non-fallible parseStyle(s) procedure
|
|
||||||
|
template parseUnsafe(body: untyped): untyped =
|
||||||
|
try: body
|
||||||
|
except: discard
|
||||||
|
|
||||||
|
proc parseStyle(codes: var seq[string], style: string) =
|
||||||
|
var style = normalizeStyle(style)
|
||||||
|
|
||||||
|
if style in ["B", "I", "U"]:
|
||||||
|
parseUnsafe: codes.add parseEnum[BbStyleAbbr](style).toCode()
|
||||||
|
elif style in BbStyleNames:
|
||||||
|
parseUnsafe: codes.add parseEnum[BbStyle](style).toCode()
|
||||||
|
|
||||||
|
if not (bbMode == On): return
|
||||||
|
|
||||||
|
if style in ColorXtermNames:
|
||||||
|
parseUnsafe: codes.add parseEnum[ColorXterm](style).toCode()
|
||||||
|
elif style.isHex():
|
||||||
|
codes.add style.hexToRgb.toCode()
|
||||||
|
elif style in ColorDigitStrings:
|
||||||
|
parseUnsafe: codes.add parseInt(style).toCode()
|
||||||
|
else:
|
||||||
|
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
||||||
|
|
||||||
|
func parseBgStyle(codes: var seq[string], style: string) =
|
||||||
|
var style = normalizeStyle(style)
|
||||||
|
if style in ColorXtermNames:
|
||||||
|
parseUnsafe: codes.add parseEnum[ColorXTerm](style).toBgCode()
|
||||||
|
elif style.isHex():
|
||||||
|
codes.add style.hexToRgb().toBgCode()
|
||||||
|
elif style in ColorDigitStrings:
|
||||||
|
parseUnsafe: codes.add parseInt(style).toBgCode()
|
||||||
|
else:
|
||||||
|
when defined(debugBB): echo "unknown bg style: " & style
|
||||||
|
|
||||||
proc toAnsiCode*(s: string): string =
|
proc toAnsiCode*(s: string): string =
|
||||||
if bbMode == Off: return
|
if bbMode == Off: return
|
||||||
var
|
var
|
||||||
|
@ -86,33 +119,10 @@ proc toAnsiCode*(s: string): string =
|
||||||
else:
|
else:
|
||||||
styles = s.splitWhitespace()
|
styles = s.splitWhitespace()
|
||||||
for style in styles:
|
for style in styles:
|
||||||
let normalizedStyle = normalizeStyle(style)
|
parseStyle codes, style
|
||||||
if normalizedStyle in ["B", "I", "U"]:
|
|
||||||
codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode()
|
|
||||||
elif normalizedStyle in BbStyleNames:
|
|
||||||
codes.add parseEnum[BbStyle](normalizedStyle).toCode()
|
|
||||||
|
|
||||||
if not (bbMode == On): continue
|
|
||||||
|
|
||||||
if normalizedStyle in ColorXtermNames:
|
|
||||||
codes.add parseEnum[ColorXterm](normalizedStyle).toCode()
|
|
||||||
elif normalizedStyle.isHex():
|
|
||||||
codes.add normalizedStyle.hexToRgb.toCode()
|
|
||||||
elif normalizedStyle in ColorDigitStrings:
|
|
||||||
codes.add parseInt(normalizedStyle).toCode()
|
|
||||||
else:
|
|
||||||
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
|
||||||
|
|
||||||
if bbMode == On and bgStyle != "":
|
if bbMode == On and bgStyle != "":
|
||||||
let normalizedBgStyle = normalizeStyle(bgStyle)
|
parseBgStyle codes, bgStyle
|
||||||
if normalizedBgStyle in ColorXtermNames:
|
|
||||||
codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode()
|
|
||||||
elif normalizedBgStyle.isHex():
|
|
||||||
codes.add normalizedBgStyle.hexToRgb().toBgCode()
|
|
||||||
elif normalizedBgStyle in ColorDigitStrings:
|
|
||||||
codes.add parseInt(normalizedBgStyle).toBgCode()
|
|
||||||
else:
|
|
||||||
when defined(debugBB): echo "unknown bg style: " & normalizedBgStyle
|
|
||||||
|
|
||||||
if codes.len > 0:
|
if codes.len > 0:
|
||||||
result.add "\e["
|
result.add "\e["
|
||||||
|
@ -325,7 +335,7 @@ proc `&`*(x: BbString, y: BbString): Bbstring =
|
||||||
proc bbEscape*(s: string): string {.inline.} =
|
proc bbEscape*(s: string): string {.inline.} =
|
||||||
s.replace("[", "[[").replace("\\", "\\\\")
|
s.replace("[", "[[").replace("\\", "\\\\")
|
||||||
|
|
||||||
proc bbEcho*(args: varargs[string, `$`]) {.sideEffect.} =
|
proc bbEcho*(args: varargs[string, `$`]) {.raises: [IOError]} =
|
||||||
for x in args:
|
for x in args:
|
||||||
stdout.write(x.bb)
|
stdout.write(x.bb)
|
||||||
stdout.write('\n')
|
stdout.write('\n')
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
|
@ -19,7 +19,7 @@ import ./bbansi
|
||||||
template tryImport(x, body) =
|
template tryImport(x, body) =
|
||||||
when not (compiles do: import x): body else: import x
|
when not (compiles do: import x): body else: import x
|
||||||
tryImport pkg/cligen:
|
tryImport pkg/cligen:
|
||||||
{.fatal: "hwylterm/cli requires cligen>=1.7.5".}
|
{.fatal: "hwylterm/cligen requires cligen>=1.7.5".}
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
|
## hwylterm/logging
|
||||||
|
##
|
||||||
|
## wrapper around std/logging to provide a fancy console logger
|
||||||
|
|
||||||
import std/[logging, strutils]
|
import std/[logging, strutils]
|
||||||
export logging
|
export logging
|
||||||
|
|
||||||
import ./bbansi
|
import ./bbansi
|
||||||
|
|
||||||
var
|
var
|
||||||
|
@ -19,10 +22,10 @@ Level* = enum ## \
|
||||||
]#
|
]#
|
||||||
|
|
||||||
type
|
type
|
||||||
FancyConsoleLogger* = ref object of Logger
|
HwylConsoleLogger* = ref object of Logger
|
||||||
## A logger that writes log messages to the console.
|
## A logger that writes log messages to the console.
|
||||||
##
|
##
|
||||||
## Create a new ``FancyConsoleLogger`` with the `newFancyConsoleLogger proc
|
## Create a new ``HwylConsoleLogger`` with the `newHwylConsoleLogger proc
|
||||||
## <#newConsoleLogger>`_.
|
## <#newConsoleLogger>`_.
|
||||||
##
|
##
|
||||||
useStderr*: bool ## If true, writes to stderr; otherwise, writes to stdout
|
useStderr*: bool ## If true, writes to stderr; otherwise, writes to stdout
|
||||||
|
@ -50,11 +53,11 @@ proc genFmtStr(
|
||||||
): string =
|
): string =
|
||||||
var parts: seq[string]
|
var parts: seq[string]
|
||||||
if fmtPrefix != "": parts.add fmtPrefix
|
if fmtPrefix != "": parts.add fmtPrefix
|
||||||
parts.add $LevelNames[level].bb(levelStyle)
|
parts.add $LevelNames[level].alignLeft(6).bb(levelStyle)
|
||||||
return parts.join(fmtSep) & fmtSuffix
|
return parts.join(fmtSep) & fmtSuffix
|
||||||
|
|
||||||
|
|
||||||
proc newFancyConsoleLogger*(
|
proc newHwylConsoleLogger*(
|
||||||
levelThreshold = lvlAll,
|
levelThreshold = lvlAll,
|
||||||
fmtPrefix = "",
|
fmtPrefix = "",
|
||||||
fmtSep = "|",
|
fmtSep = "|",
|
||||||
|
@ -67,8 +70,8 @@ proc newFancyConsoleLogger*(
|
||||||
warnStyle = "bold yellow",
|
warnStyle = "bold yellow",
|
||||||
errorStyle = "bold red",
|
errorStyle = "bold red",
|
||||||
fatalStyle = "bold red"
|
fatalStyle = "bold red"
|
||||||
): FancyConsoleLogger =
|
): HwylConsoleLogger =
|
||||||
## Creates a new `FancyConsoleLogger<#ConsoleLogger>`_.
|
## Creates a new `HwylConsoleLogger<#HwylConsoleLogger>`_.
|
||||||
new result
|
new result
|
||||||
## log needs to be gcsafe so we pregenerate the log formats when making the handler
|
## log needs to be gcsafe so we pregenerate the log formats when making the handler
|
||||||
let fmtStrs: array[Level, string] = [
|
let fmtStrs: array[Level, string] = [
|
||||||
|
@ -89,8 +92,8 @@ proc newFancyConsoleLogger*(
|
||||||
result.fmtStrs = fmtStrs
|
result.fmtStrs = fmtStrs
|
||||||
|
|
||||||
|
|
||||||
method log*(logger: FancyConsoleLogger, level: Level, args: varargs[string, `$`]) {.gcsafe.} =
|
method log*(logger: HwylConsoleLogger, level: Level, args: varargs[string, `$`]) {.gcsafe.} =
|
||||||
## Logs to the console with the given `FancyConsoleLogger<#ConsoleLogger>`_ only.
|
## Logs to the console with the given `HwylConsoleLogger<#HwylConsoleLogger>`_ only.
|
||||||
##
|
##
|
||||||
## This method ignores the list of registered handlers.
|
## This method ignores the list of registered handlers.
|
||||||
##
|
##
|
||||||
|
|
Loading…
Reference in a new issue