diff options
author | bptato <nincsnevem662@gmail.com> | 2021-01-22 15:35:56 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2021-01-22 15:35:56 +0100 |
commit | 94d681b3935a3f9105dc60320230fa9657cbd7b5 (patch) | |
tree | 3eb9189d01f3627927bc25b53fca1da20ae9b1e2 /twtstr.nim | |
parent | 5d6af7f57a89239554a9cd51fe60f8227d7ce549 (diff) | |
download | chawan-94d681b3935a3f9105dc60320230fa9657cbd7b5.tar.gz |
something broke again
Diffstat (limited to 'twtstr.nim')
-rw-r--r-- | twtstr.nim | 79 |
1 files changed, 36 insertions, 43 deletions
diff --git a/twtstr.nim b/twtstr.nim index 50f0e4da..85035a61 100644 --- a/twtstr.nim +++ b/twtstr.nim @@ -2,59 +2,52 @@ import terminal import strutils import unicode -type ustring* = seq[Rune] - -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 +const runeSpace* = " ".runeAt(0) func ansiStyle*(str: string, style: Style): string = - return ansiStyleCode(style) & str + return ansiStyleCode(style) & str & "\e[0m" func ansiFgColor*(str: string, color: ForegroundColor): string = - return ansiForegroundColorCode(color) & str + return ansiForegroundColorCode(color) & str & ansiResetCode 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: - result.setLen(max - 1) - result[max - 2] = '$' + return str.substr(0, max - 2) & "$" + return str -func fitValueToSize*(str: ustring, size: int): ustring = - if str.len < size: - return str & ' '.repeat(size - str.len).toRunes() +func fitValueToSize*(str: string, size: int): string = + if str.runeLen < size: + return str & ' '.repeat(size - str.runeLen) return str.maxString(size) -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) +func buttonFmt*(str: string): string = + return "[".ansiFgColor(fgRed) & str.ansiFgColor(fgRed).ansiReset() & "]".ansiFgColor(fgRed).ansiReset() + +func buttonRaw*(str: string): string = + return "[" & str & "]" + +func remove*(str: string, c: string): string = + let rem = c.toRunes()[0] + for rune in str.runes: + if rem != rune: + result &= $rune + +func isControlChar*(c: char): bool = + return int(c) <= 0x1F or int(c) == 0x7F + +func getControlChar*(c: char): char = + if int(c) >= int('a'): + return char(int(c) - int('a') + 1) + elif c == '?': + return char(127) + assert(false) + +func getControlLetter*(c: char): char = + if int(c) <= 0x1F: + return char(int(c) + int('A') - 1) + elif c == '\x7F': + return '?' + assert(false) |