about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-27 21:36:35 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-27 22:34:10 +0100
commit65b0b48f445b6c56016a3c842089ef117a9298bc (patch)
tree3b82319e3ec57c24e2c191d83ecb755275d30933 /src/utils/twtstr.nim
parent4042ec0dd0515ac9d538e763d6b99b256b640eca (diff)
downloadchawan-65b0b48f445b6c56016a3c842089ef117a9298bc.tar.gz
Proper support for tabs
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 91633734..96d4bf2f 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -993,10 +993,6 @@ func width*(r: Rune): int =
     return int(width_table[int(r)])
 {.pop.}
 
-func width*(s: string): int =
-  for r in s.runes():
-    result += width(r)
-
 func width*(s: string, len: int): int =
   var i = 0
   var m = len
@@ -1023,6 +1019,22 @@ func width*(s: seq[Rune], min: int): int =
     result += width(s[i])
     inc i
 
+# Width, but also works with tabs.
+# Needs the column width of the text so far.
+func twidth*(r: Rune, w: int): int =
+  if r != Rune('\t'):
+    return r.width()
+  return ((w div 8) + 1) * 8 - w
+
+func width*(s: string): int =
+  for r in s.runes():
+    result += twidth(r, result)
+
+func twidth*(s: string, w: int): int =
+  result = w
+  for r in s.runes():
+    result += twidth(r, result)
+
 func breaksWord*(r: Rune): bool =
   return not (r.isDigitAscii() or r.width() == 0 or r.isAlpha())