Operating system for OpenComputers
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

198 行
4.8KB

  1. local vtansi = {}
  2. vtansi.sequences = {
  3. [28] = "\n", -- newline
  4. [200] = "\27[A", -- up
  5. [201] = "\27[5~", -- page up
  6. [203] = "\27[D", -- left
  7. [205] = "\27[C", -- right
  8. [208] = "\27[B", -- down
  9. [209] = "\27[6~" -- page down
  10. }
  11. function vtansi.vtemu(gpu) -- table -- function -- takes GPU component proxy *gpu* and returns a function to write to it in a manner like an ANSI terminal
  12. local colours = {0x0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00B6FF,0xFFFFFF}
  13. local mx, my = gpu.maxResolution()
  14. local cx, cy = 1, 1
  15. local pc = " "
  16. local lc = ""
  17. local mode = 0 -- 0 normal, 1 escape, 2 command
  18. local lw = true
  19. local sx, sy = 1,1
  20. local cs = ""
  21. local bg, fg = 0, 0xFFFFFF
  22. -- setup
  23. gpu.setResolution(mx,my)
  24. gpu.fill(1,1,mx,my," ")
  25. local function checkCursor()
  26. if cx > mx and lw then
  27. cx, cy = 1, cy+1
  28. end
  29. if cy > my then
  30. gpu.copy(1,2,mx,my-1,0,-1)
  31. gpu.fill(1,my,mx,1," ")
  32. cy=my
  33. end
  34. if cy < 1 then cy = 1 end
  35. if cx < 1 then cx = 1 end
  36. end
  37. local function termwrite(s)
  38. local wb = ""
  39. local lb, ec = nil, nil
  40. local function flushwb()
  41. while wb:len() > 0 do
  42. checkCursor()
  43. local wl = wb:sub(1,mx-cx+1)
  44. wb = wb:sub(wl:len()+1)
  45. gpu.set(cx, cy, wl)
  46. cx = cx + wl:len()
  47. end
  48. end
  49. local rs = ""
  50. s=s:gsub("\8","\27[D")
  51. pc = gpu.get(cx,cy)
  52. gpu.setForeground(fg)
  53. gpu.setBackground(bg)
  54. gpu.set(cx,cy,pc)
  55. for cc in s:gmatch(".") do
  56. if mode == 0 then
  57. if cc == "\n" then
  58. flushwb()
  59. cx,cy = 1, cy+1
  60. elseif cc == "\t" then
  61. wb=wb..(" "):rep(8*((cx+9)//8))
  62. elseif cc == "\27" then
  63. flushwb()
  64. mode = 1
  65. else
  66. wb = wb .. cc
  67. end
  68. elseif mode == 1 then
  69. if cc == "[" then
  70. mode = 2
  71. else
  72. mode = 0
  73. end
  74. elseif mode == 2 then
  75. if cc:match("[%d;]") then
  76. cs = cs .. cc
  77. else
  78. mode = 0
  79. local tA = {}
  80. for s in cs:gmatch("%d+") do
  81. tA[#tA+1] = tonumber(s)
  82. end
  83. if cc == "H" then
  84. cx, cy = math.min(mx,tA[1] or 1), math.min(my,tA[2] or 1)
  85. elseif cc == "A" then
  86. cy = cy - (tA[1] or 1)
  87. elseif cc == "B" then
  88. cy = cy + (tA[1] or 1)
  89. elseif cc == "C" then
  90. cx = cx + (tA[1] or 1)
  91. elseif cc == "D" then
  92. cx = cx - (tA[1] or 1)
  93. elseif cc == "s" then
  94. sx, sy = cx, cy
  95. elseif cc == "u" then
  96. cx, cy = sx, sy
  97. elseif cc == "n" and tA[1] == 6 then
  98. rs = string.format("%s\27[%d;%dR",rs,cx,cy)
  99. dprint(string.format("reporting %d;%d as current cursor position",cx,cy))
  100. elseif cc == "K" and tA[1] == 1 then
  101. gpu.fill(1,cy,cx,1," ")
  102. elseif cc == "K" and tA[1] == 2 then
  103. gpu.fill(cx,cy,mx,1," ")
  104. elseif cc == "K" then
  105. gpu.fill(1,cy,mx,1," ")
  106. elseif cc == "J" and tA[1] == 1 then
  107. gpu.fill(1,1,mx,cy," ")
  108. elseif cc == "J" and tA[1] == 2 then
  109. gpu.fill(1,1,mx,my," ")
  110. cx, cy = 1, 1
  111. elseif cc == "J" then
  112. gpu.fill(1,cy,mx,my," ")
  113. elseif cc == "m" then
  114. for _,num in ipairs(tA) do
  115. if num == 0 then
  116. fg,bg,ec,lb = 0xFFFFFF,0,false,true
  117. elseif num == 7 then
  118. local nfg,nbg = bg, fg
  119. fg, bg = nfg, nbg
  120. elseif num > 29 and num < 38 then
  121. fg = colours[num-29]
  122. elseif num > 39 and num < 48 then
  123. bg = colours[num-39]
  124. elseif num == 100 or num == 8 then -- disable local echo
  125. ec = false
  126. elseif num == 101 then -- disable line mode
  127. lb = false
  128. end
  129. end
  130. gpu.setForeground(fg)
  131. gpu.setBackground(bg)
  132. end
  133. cs = ""
  134. checkCursor()
  135. end
  136. end
  137. end
  138. flushwb()
  139. checkCursor()
  140. pc = gpu.get(cx,cy)
  141. gpu.setForeground(bg)
  142. gpu.setBackground(fg)
  143. gpu.set(cx,cy,pc)
  144. gpu.setForeground(fg)
  145. gpu.setBackground(bg)
  146. return rs, lb, ec
  147. end
  148. return termwrite
  149. end
  150. function vtansi.vtsession(gpua,scra) -- string string -- table -- creates a process to handle the GPU and screen address combination *gpua*/*scra*. Returns read, write and "close" functions.
  151. local gpu = component.proxy(gpua)
  152. gpu.bind(scra)
  153. local write = vtansi.vtemu(gpu)
  154. local kba = {}
  155. for k,v in ipairs(component.invoke(scra,"getKeyboards")) do
  156. kba[v]=true
  157. end
  158. local buf, lbuf, echo = "", false, false
  159. os.spawn(function()
  160. while true do
  161. local ty,ka,ch,kc = coroutine.yield()
  162. if ty == "key_down" and kba[ka] then
  163. local outs
  164. if ch > 0 then
  165. outs = string.char(ch)
  166. end
  167. outs = vtansi.sequences[kc] or outs
  168. if outs then
  169. if echo then write(outs) end
  170. buf=buf..outs
  171. end
  172. end
  173. end
  174. end,string.format("ttyd[%s:%s]",gpua:sub(1,8),scra:sub(1,8)))
  175. local function bread(n)
  176. coroutine.yield()
  177. local r = buf
  178. buf = ""
  179. return r
  180. end
  181. local function bwrite(d)
  182. local ba, lb, ec = write(d)
  183. buf = buf .. ba
  184. if lb ~= nil then
  185. lbuf = lb
  186. end
  187. if ec ~= nil then
  188. echo = ec
  189. end
  190. end
  191. return bread, bwrite, function() io.write("\27[2J\27[H") end
  192. end
  193. return vtansi