Operating system for OpenComputers
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.5KB

  1. function vt100emu(gpu) -- takes GPU component proxy *gpu* and returns a function to write to it in a manner like an ANSI terminal
  2. local mx, my = gpu.maxResolution()
  3. local cx, cy = 1, 1
  4. local pc = " "
  5. local lc = ""
  6. local mode = "n"
  7. local lw = true
  8. local sx, sy = 1,1
  9. local cs = ""
  10. -- setup
  11. gpu.setResolution(mx,my)
  12. gpu.fill(1,1,mx,my," ")
  13. function termwrite(s)
  14. s=s:gsub("\8","\27[D")
  15. pc = gpu.get(cx,cy)
  16. gpu.setForeground(0xFFFFFF)
  17. gpu.setBackground(0)
  18. gpu.set(cx,cy,pc)
  19. for i = 1, s:len() do
  20. local cc = s:sub(i,i)
  21. if mode == "n" then
  22. if cc == "\n" then -- line feed
  23. cx, cy = 1, cy+1
  24. elseif cc == "\r" then -- cursor home
  25. cx = 1
  26. elseif cc == "\27" then -- escape
  27. mode = "e"
  28. elseif string.byte(cc) > 31 and string.byte(cc) < 127 then -- printable, I guess
  29. gpu.set(cx, cy, cc)
  30. cx = cx + 1
  31. end
  32. elseif mode == "e" then
  33. if cc == "[" then
  34. mode = "v"
  35. cs = ""
  36. elseif cc == "D" then -- scroll down
  37. gpu.copy(1,2,mx,my-1,0,-1)
  38. gpu.fill(1,my,mx,1," ")
  39. cy=cy+1
  40. mode = "n"
  41. elseif cc == "M" then -- scroll up
  42. gpu.copy(1,1,mx,my-1,0,1)
  43. gpu.fill(1,1,mx,1," ")
  44. mode = "n"
  45. end
  46. elseif mode == "v" then -- save cursor
  47. local n = cs:sub(cs:len(),cs:len())
  48. if n == "" then n = "\1" end
  49. if cc == "s" then
  50. sx, sy = cx, cy
  51. mode = "n"
  52. elseif cc == "u" then -- restore cursor
  53. cx, cy = sx, sy
  54. mode = "n"
  55. elseif cc == "H" then -- cursor home or to
  56. local tx, ty = cs:match("(.-);(.-)")
  57. tx, ty = tx or "1", ty or "1"
  58. cx, cy = tonumber(tx), tonumber(ty)
  59. mode = "n"
  60. elseif cc == "A" then -- cursor up
  61. cy = cy - string.byte(n)
  62. mode = "n"
  63. elseif cc == "B" then -- cursor down
  64. cy = cy + string.byte(n)
  65. mode = "n"
  66. elseif cc == "C" then -- cursor right
  67. cx = cx + string.byte(n)
  68. mode = "n"
  69. elseif cc == "D" then -- cursor left
  70. cx = cx - string.byte(n)
  71. mode = "n"
  72. elseif cc == "h" and lc == "7" then -- enable line wrap
  73. lw = true
  74. elseif cc == "l" and lc == "7" then -- disable line wrap
  75. lw = false
  76. end
  77. cs = cs .. cc
  78. end
  79. if cx > mx and lw then
  80. cx, cy = 1, cy+1
  81. end
  82. if cy > my then
  83. gpu.copy(1,2,mx,my-1,0,-1)
  84. gpu.fill(1,my,mx,1," ")
  85. cy=my
  86. end
  87. if cy < 1 then cy = 1 end
  88. if cx < 1 then cx = 1 end
  89. lc = cc
  90. end
  91. pc = gpu.get(cx,cy)
  92. gpu.setForeground(0)
  93. gpu.setBackground(0xFFFFFF)
  94. gpu.set(cx,cy,pc)
  95. gpu.setForeground(0xFFFFFF)
  96. gpu.setBackground(0)
  97. end
  98. return termwrite
  99. end