about summary refs log tree commit diff stats
path: root/adapter/protocol/gmifetch.c
Commit message (Expand)AuthorAgeFilesLines
* gmifetch: clean up globalsbptato2024-02-161-30/+21
* gmifetch: set port separatelybptato2024-02-161-17/+15
* gmifetch: clean up URL handlingbptato2024-02-161-120/+91
* adapter/: re-structurebptato2023-12-121-0/+688
0 move around more modules' href='/ahoang/chawan/commit/src/render/rendertext.nim?id=c1b8338045716b25d664c0b8dd91eac0cb76480e'>c1b83380 ^
eb16da9f ^

6b0b7ccf ^


3a12afa7 ^
3a12afa7 ^


4e0fd8c7 ^
1e858c87 ^
4e0fd8c7 ^
f0de405c ^
b1e13dc2 ^
3a12afa7 ^
4e0fd8c7 ^
3a12afa7 ^

4e0fd8c7 ^

26e8968a ^

4e0fd8c7 ^

















3a12afa7 ^

1e858c87 ^
647089a9 ^
3a12afa7 ^

b1e13dc2 ^
647089a9 ^
b086e346 ^
1e858c87 ^
4e0fd8c7 ^


670e5dd9 ^
4e0fd8c7 ^


bee3b59b ^
f0de405c ^



b1e13dc2 ^
1e858c87 ^







79e6de78 ^
1e858c87 ^


f0de405c ^
65b0b48f ^
1e858c87 ^

b1e13dc2 ^
1e858c87 ^


eb16da9f ^

1e858c87 ^
b1e13dc2 ^
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 unicode

import types/cell
import utils/twtstr

import chakasu/charset
import chakasu/decoderstream

type StreamRenderer* = object
  ansiparser: AnsiCodeParser
  format: Format
  af: bool
  stream: Stream
  decoder: DecoderStream
  charsets: seq[Charset]
  newline: bool
  w: int

proc newStreamRenderer*(stream: Stream, charsets: seq[Charset]): StreamRenderer =
  result.format = newFormat()
  result.ansiparser.state = PARSE_DONE
  for i in countdown(charsets.high, 0):
    result.charsets.add(charsets[i])
  if charsets.len == 0:
    result.charsets = @[DefaultCharset]
  let cs = result.charsets.pop()
  let em = if charsets.len > 0:
    DECODER_ERROR_MODE_FATAL
  else:
    DECODER_ERROR_MODE_REPLACEMENT
  result.stream = stream
  result.decoder = newDecoderStream(stream, cs, errormode = em)

proc rewind(renderer: var StreamRenderer) =
  renderer.stream.setPosition(0)
  let cs = renderer.charsets.pop()
  let em = if renderer.charsets.len > 0:
    DECODER_ERROR_MODE_FATAL
  else:
    DECODER_ERROR_MODE_REPLACEMENT
  renderer.decoder = newDecoderStream(renderer.stream, cs, errormode = em)
  renderer.format = newFormat()
  renderer.ansiparser.state = PARSE_DONE

proc renderStream*(grid: var FlexibleGrid, renderer: var StreamRenderer, len: int) =
  if len == 0: return
  template add_format() =
    if renderer.af:
      renderer.af = false
      grid[grid.high].addFormat(renderer.w, renderer.format)

  if grid.len == 0: grid.addLine()
  var buf = newSeq[Rune](len * 4)
  var n: int
  while true:
    n = renderer.decoder.readData(addr buf[0], buf.len * sizeof(buf[0]))
    if renderer.decoder.failed and renderer.charsets.len > 0:
      renderer.rewind()
      continue
    break
  for i in 0 ..< n div sizeof(buf[0]):
    if renderer.newline:
      # avoid newline at end of stream
      grid.addLine()
      renderer.newline = false
      renderer.w = 0
    let r = buf[i]
    if r.isAscii():
      let c = cast[char](r)
      if renderer.ansiparser.state != PARSE_DONE:
        let cancel = renderer.ansiparser.parseAnsiCode(renderer.format, c)
        if not cancel:
          if renderer.ansiparser.state == PARSE_DONE:
            renderer.af = true
          continue
      case c
      of '\n':
        add_format
        renderer.newline = true
      of '\r': discard
      of '\e':
        renderer.ansiparser.reset()
        continue
      else:
        add_format
        grid[^1].str &= c
    else:
      add_format
      grid[^1].str &= r
    renderer.w += r.twidth(renderer.w)