mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-22 03:00:43 -06:00
use enumNames macro
This commit is contained in:
parent
a6120b3723
commit
055701f867
1 changed files with 16 additions and 9 deletions
|
@ -1,5 +1,5 @@
|
||||||
import std/[
|
import std/[
|
||||||
enumutils, os, strutils, terminal, sequtils
|
macros, os, sequtils, strutils, terminal,
|
||||||
]
|
]
|
||||||
import ./[styles, colors]
|
import ./[styles, colors]
|
||||||
|
|
||||||
|
@ -22,7 +22,10 @@ proc checkColorSupport(): BbMode =
|
||||||
|
|
||||||
let bbMode* = checkColorSupport()
|
let bbMode* = checkColorSupport()
|
||||||
|
|
||||||
func normStyle(style: string): string = style.replace("_","").capitalizeAscii()
|
func firstCapital(s: string): string = s.toLowerAscii().capitalizeAscii()
|
||||||
|
func normalizeStyle(style: string): string = style.replace("_","").capitalizeAscii()
|
||||||
|
func isHex(s: string): bool = (s.startswith "#") and (s.len == 7)
|
||||||
|
|
||||||
func toCode(style: BbStyle): string = $ord(style)
|
func toCode(style: BbStyle): string = $ord(style)
|
||||||
func toCode(abbr: BbStyleAbbr): string = abbr.toStyle().toCode()
|
func toCode(abbr: BbStyleAbbr): string = abbr.toStyle().toCode()
|
||||||
func toCode(color: ColorXterm): string = "38;5;" & $ord(color)
|
func toCode(color: ColorXterm): string = "38;5;" & $ord(color)
|
||||||
|
@ -32,12 +35,16 @@ func toBgCode(c: ColorRgb): string = "48:2;" & $c
|
||||||
func toCode(c: Color256): string = "38;5;" & $c
|
func toCode(c: Color256): string = "38;5;" & $c
|
||||||
func toBgCode(c: Color256): string = "48;5;" & $c
|
func toBgCode(c: Color256): string = "48;5;" & $c
|
||||||
|
|
||||||
const ColorXTermNames = ColorXterm.items().toSeq().mapIt(($it).toLowerAscii().capitalizeAscii())
|
macro enumNames(a: typed): untyped =
|
||||||
const BbStyleNames = BbStyle.items().toSeq().mapIt(($it).toLowerAscii().capitalizeAscii())
|
## unexported macro copied from std/enumutils
|
||||||
const ColorDigitStrings = (1..255).toSeq().mapIt($it)
|
result = newNimNode(nnkBracket)
|
||||||
|
for ai in a.getType[1][1..^1]:
|
||||||
|
assert ai.kind == nnkSym
|
||||||
|
result.add newLit ai.strVal
|
||||||
|
|
||||||
func isHex(s: string): bool =
|
const ColorXTermNames = enumNames(ColorXterm).mapIt(firstCapital(it))
|
||||||
(s.startswith "#") and (s.len == 7)
|
const BbStyleNames = enumNames(BbStyle).mapIt(firstCapital(it))
|
||||||
|
const ColorDigitStrings = (1..255).toSeq().mapIt($it)
|
||||||
|
|
||||||
# TODO: write non-fallible parseStyle(s) procedure
|
# TODO: write non-fallible parseStyle(s) procedure
|
||||||
proc toAnsiCode*(s: string): string =
|
proc toAnsiCode*(s: string): string =
|
||||||
|
@ -53,7 +60,7 @@ proc toAnsiCode*(s: string): string =
|
||||||
else:
|
else:
|
||||||
styles = s.splitWhitespace()
|
styles = s.splitWhitespace()
|
||||||
for style in styles:
|
for style in styles:
|
||||||
let normalizedStyle = style.normStyle
|
let normalizedStyle = normalizeStyle(style)
|
||||||
|
|
||||||
if normalizedStyle in ["B", "I", "U"]:
|
if normalizedStyle in ["B", "I", "U"]:
|
||||||
codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode()
|
codes.add parseEnum[BbStyleAbbr](normalizedStyle).toCode()
|
||||||
|
@ -72,7 +79,7 @@ proc toAnsiCode*(s: string): string =
|
||||||
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
when defined(debugBB): echo "unknown style: " & normalizedStyle
|
||||||
|
|
||||||
if bbMode == On and bgStyle != "":
|
if bbMode == On and bgStyle != "":
|
||||||
let normalizedBgStyle = bgStyle.normStyle
|
let normalizedBgStyle = normalizeStyle(bgStyle)
|
||||||
if normalizedBgStyle in ColorXtermNames:
|
if normalizedBgStyle in ColorXtermNames:
|
||||||
codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode()
|
codes.add parseEnum[ColorXTerm](normalizedBgStyle).toBgCode()
|
||||||
elif normalizedBgStyle.isHex():
|
elif normalizedBgStyle.isHex():
|
||||||
|
|
Loading…
Reference in a new issue