Operating system for OpenComputers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

106 lignes
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
  47. if cc == "s" then -- save cursor
  48. sx, sy = cx, cy
  49. mode = "n"
  50. elseif cc == "u" then -- restore cursor
  51. cx, cy = sx, sy
  52. mode = "n"
  53. elseif cc == "H" then -- cursor home or to
  54. local tx, ty = cs:match("(.-);(.-)")
  55. tx, ty = tx or "1", ty or "1"
  56. cx, cy = tonumber(tx), tonumber(ty)
  57. mode = "n"
  58. elseif cc == "A" then -- cursor up
  59. cy = cy - (tonumber(cs) or 1)
  60. mode = "n"
  61. elseif cc == "B" then -- cursor down
  62. cy = cy + (tonumber(cs) or 1)
  63. mode = "n"
  64. elseif cc == "C" then -- cursor right
  65. cx = cx + (tonumber(cs) or 1)
  66. mode = "n"
  67. elseif cc == "D" then -- cursor left
  68. cx = cx - (tonumber(cs) or 1)
  69. mode = "n"
  70. elseif cc == "h" and lc == "7" then -- enable line wrap
  71. lw = true
  72. elseif cc == "l" and lc == "7" then -- disable line wrap
  73. lw = false
  74. end
  75. cs = cs .. cc
  76. end
  77. if cx > mx and lw then
  78. cx, cy = 1, cy+1
  79. end
  80. if cy > my then
  81. gpu.copy(1,2,mx,my-1,0,-1)
  82. gpu.fill(1,my,mx,1," ")
  83. cy=my
  84. end
  85. if cy < 1 then cy = 1 end
  86. if cx < 1 then cx = 1 end
  87. lc = cc
  88. end
  89. pc = gpu.get(cx,cy)
  90. gpu.setForeground(0)
  91. gpu.setBackground(0xFFFFFF)
  92. gpu.set(cx,cy,pc)
  93. gpu.setForeground(0xFFFFFF)
  94. gpu.setBackground(0)
  95. end
  96. return termwrite
  97. end