about summary refs log tree commit diff stats
path: root/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-01-22 12:54:21 +0100
committerbptato <nincsnevem662@gmail.com>2021-01-22 12:54:21 +0100
commit5d6af7f57a89239554a9cd51fe60f8227d7ce549 (patch)
treecc89b54443b227b4fc2140ee82ceee268d989f9f /twtstr.nim
parent6e1ef74bdca4c629bda2756a6e72279e038195f4 (diff)
downloadchawan-5d6af7f57a89239554a9cd51fe60f8227d7ce549.tar.gz
failed attempt for unicode
Diffstat (limited to 'twtstr.nim')
-rw-r--r--twtstr.nim59
1 files changed, 49 insertions, 10 deletions
diff --git a/twtstr.nim b/twtstr.nim
index 983d58d8..50f0e4da 100644
--- a/twtstr.nim
+++ b/twtstr.nim
@@ -1,21 +1,60 @@
 import terminal
 import strutils
+import unicode
 
-func addAnsiStyle*(str: string, style: Style): string =
-  return ansiStyleCode(style) & str & "\e[0m"
+type ustring* = seq[Rune]
 
-func addAnsiFgColor*(str: string, color: ForegroundColor): string =
-  return ansiForegroundColorCode(color) & str & ansiResetCode
+const runeSpace*: Rune = " ".toRunes()[0]
+const runeNewline*: Rune = "\n".toRunes()[0]
+const runeReturn*: Rune = "\r".toRunes()[0]
+
+func isWhitespace(r: Rune): bool =
+  case r
+  of runeSpace, runeNewline, runeReturn: return true
+  else: return false
+
+func ansiStyle*(str: string, style: Style): string =
+  return ansiStyleCode(style) & str
+
+func ansiFgColor*(str: string, color: ForegroundColor): string =
+  return ansiForegroundColorCode(color) & str
+
+func ansiReset*(str: string): string =
+  return str & ansiResetCode
+
+func ansiStyle*(str: ustring, style: Style): ustring =
+  return ansiStyleCode(style).toRunes() & str
+
+func ansiFgColor*(str: ustring, color: ForegroundColor): ustring =
+  return ansiForegroundColorCode(color).toRunes & str
+
+func ansiReset*(str: ustring): ustring =
+  return str & ansiResetCode.toRunes()
+
+func maxString*(str: ustring, max: int): ustring =
+  result = str
+  if max < str.len:
+    result.setLen(max - 2)
+    result &= "$".toRunes()
 
 func maxString*(str: string, max: int): string =
+  result = str
   if max < str.len:
-    return str.substr(0, max - 2) & "$"
-  return str
+    result.setLen(max - 1)
+    result[max - 2] = '$'
 
-func fitValueToSize*(str: string, size: int): string =
+func fitValueToSize*(str: ustring, size: int): ustring =
   if str.len < size:
-    return str & ' '.repeat(size - str.len)
+    return str & ' '.repeat(size - str.len).toRunes()
   return str.maxString(size)
 
-func buttonStr*(str: string): string =
-  return "[".addAnsiFgColor(fgRed) & str.addAnsiFgColor(fgRed) & "]".addAnsiFgColor(fgRed)
+func buttonFmt*(str: ustring): ustring =
+  return "[".toRunes().ansiFgColor(fgRed).ansiReset() & str.ansiFgColor(fgRed).ansiReset() & "]".ansiFgColor(fgRed).toRunes().ansiReset()
+
+func buttonRaw*(str: ustring): ustring = 
+  return "[".toRunes() & str & "]".toRunes()
+
+func remove*(s: ustring, r: Rune): ustring =
+  for c in s:
+    if c != r:
+      result.add(c)