about summary refs log tree commit diff stats
path: root/src/render/rendertext.nim
blob: d9cc4dfc1bba1e9c0bf90e261d80c1a0493eedd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streams

import buffer/cell
import utils/twtstr

proc renderPlainText*(text: string): FlexibleGrid =
  var format = newFormat()
  template add_format() =
    if af:
      af = false
      result[result.high].addFormat(result[^1].str.len, format)

  result.addLine()
  const tabwidth = 8
  var spaces = 0
  var i = 0
  var af = false
  while i < text.len:
    case text[i]
    of '\n':
      add_format
      result.addLine()
    of '\r': discard
    of '\t':
      add_format
      for i in 0 ..< tabwidth:
        result[^1].str &= ' '
        spaces = 0
    of ' ':
      add_format
      result[^1].str &= ' '
      inc spaces
      if spaces == 8:
        spaces = 0
    of '\e':
      i = format.parseAnsiCode(text, i)
      af = true
    elif text[i].isControlChar():
      add_format
      result[^1].str &= '^' & text[i].getControlLetter()
    else:
      add_format
      result[^1].str &= text[i]
    inc i

  if result.len > 1 and result[^1].str.len == 0 and result[^1].formats.len == 0:
    discard result.pop()

proc renderStream*(grid: var FlexibleGrid, stream: Stream, len: int) =
  var format = newFormat()
  template add_format() =
    if af:
      af = false
      grid[grid.high].addFormat(grid[^1].str.len, format)

  if grid.len == 0: grid.addLine()
  const tabwidth = 8
  var spaces = 0
  var af = false
  var i = 0
  while i < len:
    let c = stream.readChar()
    case c
    of '\n':
      add_format
      grid.addLine()
    of '\r': discard
    of '\t':
      add_format
      for i in 0 ..< tabwidth - spaces:
        grid[^1].str &= ' '
        spaces = 0
    of ' ':
      add_format
      grid[^1].str &= c
      inc spaces
      if spaces == 8:
        spaces = 0
    of '\e':
      format.parseAnsiCode(stream)
      af = true
    elif c.isControlChar():
      add_format
      grid[^1].str &= '^' & c.getControlLetter()
    else:
      add_format
      grid[^1].str &= c
    inc i

  #if grid.len > 1 and grid[^1].str.len == 0 and grid[^1].formats.len == 0:
  #  discard grid.pop()