aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-11-14 15:06:48 +0100
committerbptato <nincsnevem662@gmail.com>2021-11-14 15:06:48 +0100
commitf802b92f0071700e3aacf560d2a90fb47b3baf6b (patch)
tree6fea0afaf7588ef51dad727a9025cc3cf0ba0104
parent60b583516262be8754c6563ed9253b8964bef5d8 (diff)
downloadchawan-f802b92f0071700e3aacf560d2a90fb47b3baf6b.tar.gz
Store pixels per character/row
-rw-r--r--src/io/buffer.nim38
-rw-r--r--src/io/term.nim27
-rw-r--r--src/utils/twtstr.nim20
3 files changed, 44 insertions, 41 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index cfe45b5e..05581551 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -44,8 +44,8 @@ type
func newBuffer*(attrs: TermAttributes): Buffer =
new(result)
- result.width = attrs.termWidth
- result.height = attrs.termHeight - 1
+ result.width = attrs.width
+ result.height = attrs.height - 1
result.attrs = attrs
result.display = newFixedGrid(result.width, result.height)
@@ -325,21 +325,6 @@ proc cursorLineEnd*(buffer: Buffer) =
buffer.fromx = max(buffer.cursorx - buffer.width + 1, 0)
buffer.redraw = buffer.fromx > 0
-#TODO this is sloooooow
-proc cursorRightOverflow(buffer: Buffer) =
- buffer.cursorRight()
- if buffer.cursorx >= buffer.currentLineWidth() - 1 and buffer.cursory < buffer.numLines - 1:
- buffer.cursorDown()
- buffer.cursorLineBegin()
- buffer.refreshDisplay()
-
-proc cursorLeftOverflow(buffer: Buffer) =
- buffer.cursorLeft()
- if buffer.cursorx <= 0 and buffer.cursory > 0:
- buffer.cursorUp()
- buffer.cursorLineEnd()
- buffer.refreshDisplay()
-
proc cursorNextWord*(buffer: Buffer) =
let llen = buffer.currentLineWidth() - 1
if llen >= 0:
@@ -375,6 +360,21 @@ proc cursorPrevWord*(buffer: Buffer) =
buffer.cursorUp()
buffer.cursorLineEnd()
+#TODO this is sloooooow
+#proc cursorRightOverflow(buffer: Buffer) =
+# buffer.cursorRight()
+# if buffer.cursorx >= buffer.currentLineWidth() - 1 and buffer.cursory < buffer.numLines - 1:
+# buffer.cursorDown()
+# buffer.cursorLineBegin()
+# buffer.refreshDisplay()
+#
+#proc cursorLeftOverflow(buffer: Buffer) =
+# buffer.cursorLeft()
+# if buffer.cursorx <= 0 and buffer.cursory > 0:
+# buffer.cursorUp()
+# buffer.cursorLineEnd()
+# buffer.refreshDisplay()
+
proc cursorNextLink*(buffer: Buffer) =
#TODO
#let ocx = buffer.cursorx
@@ -531,8 +531,8 @@ proc refreshTermAttrs*(buffer: Buffer): bool =
let newAttrs = getTermAttributes()
if newAttrs != buffer.attrs:
buffer.attrs = newAttrs
- buffer.width = newAttrs.termWidth
- buffer.height = newAttrs.termHeight
+ buffer.width = newAttrs.width
+ buffer.height = newAttrs.height
return true
return false
diff --git a/src/io/term.nim b/src/io/term.nim
index 5103c153..d60d5746 100644
--- a/src/io/term.nim
+++ b/src/io/term.nim
@@ -1,14 +1,31 @@
import terminal
+when defined(posix):
+ import termios
type
TermAttributes* = object
- termWidth*: int
- termHeight*: int
+ width*: int
+ height*: int
+ ppc*: int
+ ppl*: int
proc getTermAttributes*(): TermAttributes =
- let attrs = TermAttributes(termWidth: terminalWidth(),
- termHeight: terminalHeight())
- return attrs
+ if stdin.isatty():
+ when defined(posix):
+ var win: IOctl_WinSize
+ if ioctl(cint(getOsFileHandle(stdout)), TIOCGWINSZ, addr win) != -1:
+ result.width = int(win.ws_col)
+ result.height = int(win.ws_row)
+ result.ppc = int(win.ws_xpixel) div int(win.ws_col)
+ result.ppl = int(win.ws_ypixel) div int(win.ws_row)
+ return
+ #fail
+ result.width = terminalWidth()
+ result.height = terminalHeight()
+ if result.height == 0:
+ result.height = 24
+ result.ppc = 9
+ result.ppl = 18
proc termGoto*(x: int, y: int) =
setCursorPos(stdout, x, y)
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 27269169..6291df36 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -3,6 +3,7 @@ import strutils
import unicode
import tables
import json
+import bitops
func ansiStyle*(str: string, style: Style): seq[string] =
result &= ansiStyleCode(style)
@@ -67,26 +68,11 @@ func isControlChar*(r: Rune): bool =
of Rune(0x7F): return true
else: return false
-func genControlCharMap*(): string =
- for c in low(char)..high(char):
- if c >= 'a':
- result &= char(int(c) - int('a') + 1)
- elif c == '?':
- result &= char(127)
- else:
- result &= char(0)
-
-const controlCharMap = genControlCharMap()
-
func getControlChar*(c: char): char =
- return controlCharMap[int(c)]
+ return char(bitand(int(c), 0x1f))
func getControlLetter*(c: char): char =
- if int(c) <= 0x1F:
- return char(int(c) + int('A') - 1)
- elif c == '\x7F':
- return '?'
- assert(false)
+ return char(bitor(int(c), 0x40))
func findChar*(str: string, c: char, start: int = 0): int =
var i = start