git.haldean.org e / master buf.nim
master

Tree @master (Download .tar.gz)

buf.nim @masterraw · history · blame

{. experimental: "notnil" .}
import blit
import input
import unicode

type
  Buf* = ref object of RootObj
    buf: string
    blit*: Blit

proc skip(r: Rune): bool =
  if r == Rune('\r'):
    return true
  if r.toUTF8 == "\xEF\xBB\xBF":
    return true
  return false

proc paint(buf: Buf) =
  var
    w = 0
    h = 0
    cur = 0
    last: Rune
  for r in runes(buf.buf):
    if r.skip():
      continue
    if r == Rune('\n'):
      h += 1
      cur = 0
    else:
      cur += 1
      if cur > w:
        w = cur
    last = r
  if last != Rune('\n'):
    h += 1

  buf.blit.resize(Size(w: w, h: h))
  buf.blit.fill(NO_CHAR)

  var p = Pt(x: 0, y: 0)
  for r in runes(buf.buf):
    if r.skip():
      continue
    if r == Rune('\n'):
      p.y += 1
      p.x = 0
    else:
      buf.blit.set(p, (r, 0.uint8))
      p.x += 1

proc newBuf*(): Buf =
  return Buf(blit: newBlit(Size(w: 0, h: 0)))

proc load*(buf: Buf, f: string) =
  buf.buf = readFile(f)
  buf.paint()

proc load*(buf: Buf, f: File) =
  buf.buf = f.readAll()
  buf.paint()

proc set*(buf: Buf, str: string) =
  buf.buf = str
  buf.paint()

method handlekey*(this: Buf, k: Key): bool {. base .} =
  false