Advertisement
HangMan23

Edit.lua

Jan 22nd, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.75 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local keyboard = require("keyboard")
  3. local shell = require("shell")
  4. local term = require("term") -- TODO use tty and cursor position instead of global area and gpu
  5. local text = require("text")
  6. local string = require("string")
  7. local unicode = require("unicode")
  8.  
  9. if not term.isAvailable() then
  10.   return
  11. end
  12. local gpu = term.gpu()
  13. args, options = shell.parse(...)
  14. if #args == 0 then
  15.   io.write("Usage: edit <filename>")
  16.   return
  17. end
  18.  
  19. local filename = shell.resolve(args[1])
  20. local file_parentpath = fs.path(filename)
  21.  
  22. if fs.exists(file_parentpath) and not fs.isDirectory(file_parentpath) then
  23.   io.stderr:write(string.format("Not a directory: %s\n", file_parentpath))
  24.   return 1
  25. end
  26.  
  27. local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly()
  28.  
  29. if fs.isDirectory(filename) then
  30.   io.stderr:write("file is a directory\n")
  31.   return 1
  32. elseif not fs.exists(filename) and readonly then
  33.   io.stderr:write("file system is read only\n")
  34.   return 1
  35. end
  36.  
  37. local function loadConfig()
  38.   -- Try to load user settings.
  39.   local env = {}
  40.   local config = loadfile("/etc/edit.cfg", nil, env)
  41.   if config then
  42.     pcall(config)
  43.   end
  44.   -- Fill in defaults.
  45.   env.keybinds = env.keybinds or {
  46.     left = {{"left"}},
  47.     right = {{"right"}},
  48.     up = {{"up"}},
  49.     down = {{"down"}},
  50.     home = {{"home"}},
  51.     eol = {{"end"}},
  52.     pageUp = {{"pageUp"}},
  53.     pageDown = {{"pageDown"}},
  54.  
  55.     backspace = {{"back"}},
  56.     delete = {{"delete"}},
  57.     deleteLine = {{"control", "delete"}, {"shift", "delete"}},
  58.     newline = {{"enter"}},
  59.  
  60.     save = {{"control", "s"}},
  61.  
  62.     close = {{"control", "w"}},
  63.     find = {{"control", "f"}},
  64.     findnext = {{"control", "g"}, {"control", "n"}, {"f3"}}
  65.   }
  66.   -- Generate config file if it didn't exist.
  67.   if not config then
  68.     local root = fs.get("/")
  69.     if root and not root.isReadOnly() then
  70.       fs.makeDirectory("/etc")
  71.       local f = io.open("/etc/edit.cfg", "w")
  72.       if f then
  73.         local serialization = require("serialization")
  74.         for k, v in pairs(env) do
  75.           f:write(k.."="..tostring(serialization.serialize(v, math.huge)).."\n")
  76.         end
  77.         f:close()
  78.       end
  79.     end
  80.   end
  81.   return env
  82. end
  83.  
  84. term.clear()
  85.  
  86. sas = io.open(args[1], "r")
  87. if sas == nil then sas = io.open(args[1], "w") sas:close() end
  88. if sas:read() == "--setEditColor" then
  89.  
  90.   Bcolor = tonumber(string.sub(sas:read(), -8))
  91.   Fcolor = tonumber(string.sub(sas:read(), -8))
  92.  
  93.   if type(Bcolor) == "number" then
  94.     gpu.setBackground(Bcolor)
  95.     ferror1 = false
  96.   elseif Bcolor == nil then
  97.     ferror1 = true
  98.   end
  99.   if type(Fcolor) == "number" then
  100.     gpu.setForeground(Fcolor)
  101.     ferror1 = false
  102.   elseif Fcolor == nil then
  103.     ferror1 = true
  104.   sas:close()
  105.   end
  106.  
  107. else
  108.  
  109.   local file = io.open("/edit.cfg")
  110.   local DefaultB = tonumber(file:read())
  111.   local DefaultF = tonumber(file:read())
  112.   file:close()
  113.  
  114.   gpu.setBackground(DefaultB)
  115.   gpu.setForeground(DefaultF)
  116.  
  117. end
  118.  
  119. local res1, res2 = gpu.getResolution()
  120. gpu.fill(1, 1, res1, res2, " ")
  121. term.setCursorBlink(true)
  122.  
  123. local running = true
  124. local buffer = {}
  125. local scrollX, scrollY = 0, 0
  126. local config = loadConfig()
  127.  
  128. local getKeyBindHandler -- forward declaration for refind()
  129.  
  130. local function helpStatusText()
  131.   local function prettifyKeybind(label, command)
  132.     local keybind = type(config.keybinds) == "table" and config.keybinds[command]
  133.     if type(keybind) ~= "table" or type(keybind[1]) ~= "table" then return "" end
  134.     local alt, control, shift, key
  135.     for _, value in ipairs(keybind[1]) do
  136.       if value == "alt" then alt = true
  137.       elseif value == "control" then control = true
  138.       elseif value == "shift" then shift = true
  139.       else key = value end
  140.     end
  141.     if not key then return "" end
  142.     return label .. ": [" ..
  143.            (control and "Ctrl+" or "") ..
  144.            (alt and "Alt+" or "") ..
  145.            (shift and "Shift+" or "") ..
  146.            unicode.upper(key) ..
  147.            "] "
  148.   end
  149.   return prettifyKeybind("Save", "save") ..
  150.          prettifyKeybind("Close", "close") ..
  151.          prettifyKeybind("Find", "find")
  152. end
  153.  
  154. -------------------------------------------------------------------------------
  155.  
  156. local function setStatus(value)
  157.   local x, y, w, h = term.getGlobalArea()
  158.   value = unicode.wlen(value) > w - 10 and unicode.wtrunc(value, w - 9) or value
  159.   value = text.padRight(value, w - 10)
  160.   gpu.set(x, y + h - 1, value)
  161. end
  162.  
  163. local function getArea()
  164.   local x, y, w, h = term.getGlobalArea()
  165.   return x, y, w, h - 1
  166. end
  167.  
  168. local function removePrefix(line, length)
  169.   if length >= unicode.wlen(line) then
  170.     return ""
  171.   else
  172.     local prefix = unicode.wtrunc(line, length + 1)
  173.     local suffix = unicode.sub(line, unicode.len(prefix) + 1)
  174.     length = length - unicode.wlen(prefix)
  175.     if length > 0 then
  176.       suffix = (" "):rep(unicode.charWidth(suffix) - length) .. unicode.sub(suffix, 2)
  177.     end
  178.     return suffix
  179.   end
  180. end
  181.  
  182. local function lengthToChars(line, length)
  183.   if length > unicode.wlen(line) then
  184.     return unicode.len(line) + 1
  185.   else
  186.     local prefix = unicode.wtrunc(line, length)
  187.     return unicode.len(prefix) + 1
  188.   end
  189. end
  190.  
  191.  
  192. local function isWideAtPosition(line, x)
  193.   local index = lengthToChars(line, x)
  194.   if index > unicode.len(line) then
  195.     return false, false
  196.   end
  197.   local prefix = unicode.sub(line, 1, index)
  198.   local char = unicode.sub(line, index, index)
  199.   --isWide, isRight
  200.   return unicode.isWide(char), unicode.wlen(prefix) == x
  201. end
  202.  
  203. local function drawLine(x, y, w, h, lineNr)
  204.   local yLocal = lineNr - scrollY
  205.   if yLocal > 0 and yLocal <= h then
  206.     local str = removePrefix(buffer[lineNr] or "", scrollX)
  207.     str = unicode.wlen(str) > w and unicode.wtrunc(str, w + 1) or str
  208.     str = text.padRight(str, w)
  209.     gpu.set(x, y - 1 + lineNr - scrollY, str)
  210.   end
  211. end
  212.  
  213. local function getCursor()
  214.   local cx, cy = term.getCursor()
  215.   return cx + scrollX, cy + scrollY
  216. end
  217.  
  218. local function line()
  219.   local cbx, cby = getCursor()
  220.   return buffer[cby]
  221. end
  222.  
  223. local function getNormalizedCursor()
  224.   local cbx, cby = getCursor()
  225.   local wide, right = isWideAtPosition(buffer[cby], cbx)
  226.   if wide and right then
  227.     cbx = cbx - 1
  228.   end
  229.   return cbx, cby
  230. end
  231.  
  232. local function setCursor(nbx, nby)
  233.   local x, y, w, h = getArea()
  234.   nby = math.max(1, math.min(#buffer, nby))
  235.  
  236.   local ncy = nby - scrollY
  237.   if ncy > h then
  238.     term.setCursorBlink(false)
  239.     local sy = nby - h
  240.     local dy = math.abs(scrollY - sy)
  241.     scrollY = sy
  242.     if h > dy then
  243.       gpu.copy(x, y + dy, w, h - dy, 0, -dy)
  244.     end
  245.     for lineNr = nby - (math.min(dy, h) - 1), nby do
  246.       drawLine(x, y, w, h, lineNr)
  247.     end
  248.   elseif ncy < 1 then
  249.     term.setCursorBlink(false)
  250.     local sy = nby - 1
  251.     local dy = math.abs(scrollY - sy)
  252.     scrollY = sy
  253.     if h > dy then
  254.       gpu.copy(x, y, w, h - dy, 0, dy)
  255.     end
  256.     for lineNr = nby, nby + (math.min(dy, h) - 1) do
  257.       drawLine(x, y, w, h, lineNr)
  258.     end
  259.   end
  260.   term.setCursor(term.getCursor(), nby - scrollY)
  261.  
  262.   nbx = math.max(1, math.min(unicode.wlen(line()) + 1, nbx))
  263.   local wide, right = isWideAtPosition(line(), nbx)
  264.   local ncx = nbx - scrollX
  265.   if ncx > w or (ncx + 1 > w and wide and not right) then
  266.     term.setCursorBlink(false)
  267.     scrollX = nbx - w + ((wide and not right) and 1 or 0)
  268.     for lineNr = 1 + scrollY, math.min(h + scrollY, #buffer) do
  269.       drawLine(x, y, w, h, lineNr)
  270.     end
  271.   elseif ncx < 1 or (ncx - 1 < 1 and wide and right) then
  272.     term.setCursorBlink(false)
  273.     scrollX = nbx - 1 - ((wide and right) and 1 or 0)
  274.     for lineNr = 1 + scrollY, math.min(h + scrollY, #buffer) do
  275.       drawLine(x, y, w, h, lineNr)
  276.     end
  277.   end
  278.   term.setCursor(nbx - scrollX, nby - scrollY)
  279.   --update with term lib
  280.   nbx, nby = getCursor()
  281.   gpu.set(x + w - 10, y + h, text.padLeft(string.format("%d,%d", nby, nbx), 10))
  282. end
  283.  
  284. local function highlight(bx, by, length, enabled)
  285.   local x, y, w, h = getArea()
  286.   local cx, cy = bx - scrollX, by - scrollY
  287.   cx = math.max(1, math.min(w, cx))
  288.   cy = math.max(1, math.min(h, cy))
  289.   length = math.max(1, math.min(w - cx, length))
  290.  
  291.   local fg, fgp = gpu.getForeground()
  292.   local bg, bgp = gpu.getBackground()
  293.   if enabled then
  294.     gpu.setForeground(bg, bgp)
  295.     gpu.setBackground(fg, fgp)
  296.   end
  297.   local indexFrom = lengthToChars(buffer[by], bx)
  298.   local value = unicode.sub(buffer[by], indexFrom)
  299.   if unicode.wlen(value) > length then
  300.     value = unicode.wtrunc(value, length + 1)
  301.   end
  302.   gpu.set(x - 1 + cx, y - 1 + cy, value)
  303.   if enabled then
  304.     gpu.setForeground(fg, fgp)
  305.     gpu.setBackground(bg, bgp)
  306.   end
  307. end
  308.  
  309. local function home()
  310.   local cbx, cby = getCursor()
  311.   setCursor(1, cby)
  312. end
  313.  
  314. local function ende()
  315.   local cbx, cby = getCursor()
  316.   setCursor(unicode.wlen(line()) + 1, cby)
  317. end
  318.  
  319. local function left()
  320.   local cbx, cby = getNormalizedCursor()
  321.   if cbx > 1 then
  322.     local wideTarget, rightTarget = isWideAtPosition(line(), cbx - 1)
  323.     if wideTarget and rightTarget then
  324.       setCursor(cbx - 2, cby)
  325.     else
  326.       setCursor(cbx - 1, cby)
  327.     end
  328.     return true -- for backspace
  329.   elseif cby > 1 then
  330.     setCursor(cbx, cby - 1)
  331.     ende()
  332.     return true -- again, for backspace
  333.   end
  334. end
  335.  
  336. local function right(n)
  337.   n = n or 1
  338.   local cbx, cby = getNormalizedCursor()
  339.   local be = unicode.wlen(line()) + 1
  340.   local wide, right = isWideAtPosition(line(), cbx + n)
  341.   if wide and right then
  342.     n = n + 1
  343.   end
  344.   if cbx + n <= be then
  345.     setCursor(cbx + n, cby)
  346.   elseif cby < #buffer then
  347.     setCursor(1, cby + 1)
  348.   end
  349. end
  350.  
  351. local function up(n)
  352.   n = n or 1
  353.   local cbx, cby = getCursor()
  354.   if cby > 1 then
  355.     setCursor(cbx, cby - n)
  356.   end
  357. end
  358.  
  359. local function down(n)
  360.   n = n or 1
  361.   local cbx, cby = getCursor()
  362.   if cby < #buffer then
  363.     setCursor(cbx, cby + n)
  364.   end
  365. end
  366.  
  367. local function delete(fullRow)
  368.   local cx, cy = term.getCursor()
  369.   local cbx, cby = getCursor()
  370.   local x, y, w, h = getArea()
  371.   local function deleteRow(row)
  372.     local content = table.remove(buffer, row)
  373.     local rcy = cy + (row - cby)
  374.     if rcy <= h then
  375.       gpu.copy(x, y + rcy, w, h - rcy, 0, -1)
  376.       drawLine(x, y, w, h, row + (h - rcy))
  377.     end
  378.     return content
  379.   end
  380.   if fullRow then
  381.     term.setCursorBlink(false)
  382.     if #buffer > 1 then
  383.       deleteRow(cby)
  384.     else
  385.       buffer[cby] = ""
  386.       gpu.fill(x, y - 1 + cy, w, 1, " ")
  387.     end
  388.     setCursor(1, cby)
  389.   elseif cbx <= unicode.wlen(line()) then
  390.     term.setCursorBlink(false)
  391.     local index = lengthToChars(line(), cbx)
  392.     buffer[cby] = unicode.sub(line(), 1, index - 1) ..
  393.                   unicode.sub(line(), index + 1)
  394.     drawLine(x, y, w, h, cby)
  395.   elseif cby < #buffer then
  396.     term.setCursorBlink(false)
  397.     local append = deleteRow(cby + 1)
  398.     buffer[cby] = buffer[cby] .. append
  399.     drawLine(x, y, w, h, cby)
  400.   else
  401.     return
  402.   end
  403.   setStatus(helpStatusText())
  404. end
  405.  
  406. local function insert(value)
  407.   if not value or unicode.len(value) < 1 then
  408.     return
  409.   end
  410.   term.setCursorBlink(false)
  411.   local cx, cy = term.getCursor()
  412.   local cbx, cby = getCursor()
  413.   local x, y, w, h = getArea()
  414.   local index = lengthToChars(line(), cbx)
  415.   buffer[cby] = unicode.sub(line(), 1, index - 1) ..
  416.                 value ..
  417.                 unicode.sub(line(), index)
  418.   drawLine(x, y, w, h, cby)
  419.   right(unicode.wlen(value))
  420.   setStatus(helpStatusText())
  421. end
  422.  
  423. local function enter()
  424.   term.setCursorBlink(false)
  425.   local cx, cy = term.getCursor()
  426.   local cbx, cby = getCursor()
  427.   local x, y, w, h = getArea()
  428.   local index = lengthToChars(line(), cbx)
  429.   table.insert(buffer, cby + 1, unicode.sub(buffer[cby], index))
  430.   buffer[cby] = unicode.sub(buffer[cby], 1, index - 1)
  431.   drawLine(x, y, w, h, cby)
  432.   if cy < h then
  433.     if cy < h - 1 then
  434.       gpu.copy(x, y + cy, w, h - (cy + 1), 0, 1)
  435.     end
  436.     drawLine(x, y, w, h, cby + 1)
  437.   end
  438.   setCursor(1, cby + 1)
  439.   setStatus(helpStatusText())
  440. end
  441.  
  442. local findText = ""
  443.  
  444. local function find()
  445.   local x, y, w, h = getArea()
  446.   local cx, cy = term.getCursor()
  447.   local cbx, cby = getCursor()
  448.   local ibx, iby = cbx, cby
  449.   while running do
  450.     if unicode.len(findText) > 0 then
  451.       local sx, sy
  452.       for syo = 1, #buffer do -- iterate lines with wraparound
  453.         sy = (iby + syo - 1 + #buffer - 1) % #buffer + 1
  454.         sx = string.find(buffer[sy], findText, syo == 1 and ibx or 1, true)
  455.         if sx and (sx >= ibx or syo > 1) then
  456.           break
  457.         end
  458.       end
  459.       if not sx then -- special case for single matches
  460.         sy = iby
  461.         sx = string.find(buffer[sy], findText, nil, true)
  462.       end
  463.       if sx then
  464.         sx = unicode.wlen(string.sub(buffer[sy], 1, sx - 1)) + 1
  465.         cbx, cby = sx, sy
  466.         setCursor(cbx, cby)
  467.         highlight(cbx, cby, unicode.wlen(findText), true)
  468.       end
  469.     end
  470.     term.setCursor(7 + unicode.wlen(findText), h + 1)
  471.     setStatus("Find: " .. findText)
  472.  
  473.     local _, address, char, code = term.pull("key_down")
  474.     if address == term.keyboard() then
  475.       local handler, name = getKeyBindHandler(code)
  476.       highlight(cbx, cby, unicode.wlen(findText), false)
  477.       if name == "newline" then
  478.         break
  479.       elseif name == "close" then
  480.         handler()
  481.       elseif name == "backspace" then
  482.         findText = unicode.sub(findText, 1, -2)
  483.       elseif name == "find" or name == "findnext" then
  484.         ibx = cbx + 1
  485.         iby = cby
  486.       elseif not keyboard.isControl(char) then
  487.         findText = findText .. unicode.char(char)
  488.       end
  489.     end
  490.   end
  491.   setCursor(cbx, cby)
  492.   setStatus(helpStatusText())
  493. end
  494.  
  495. -------------------------------------------------------------------------------
  496.  
  497. local keyBindHandlers = {
  498.   left = left,
  499.   right = right,
  500.   up = up,
  501.   down = down,
  502.   home = home,
  503.   eol = ende,
  504.   pageUp = function()
  505.     local x, y, w, h = getArea()
  506.     up(h - 1)
  507.   end,
  508.   pageDown = function()
  509.     local x, y, w, h = getArea()
  510.     down(h - 1)
  511.   end,
  512.  
  513.   backspace = function()
  514.     if not readonly and left() then
  515.       delete()
  516.     end
  517.   end,
  518.   delete = function()
  519.     if not readonly then
  520.       delete()
  521.     end
  522.   end,
  523.   deleteLine = function()
  524.     if not readonly then
  525.       delete(true)
  526.     end
  527.   end,
  528.   newline = function()
  529.     if not readonly then
  530.       enter()
  531.     end
  532.   end,
  533.  
  534.   save = function()
  535.     if readonly then return end
  536.     local new = not fs.exists(filename)
  537.     local backup
  538.     if not new then
  539.       backup = filename .. "~"
  540.       for i = 1, math.huge do
  541.         if not fs.exists(backup) then
  542.           break
  543.         end
  544.         backup = filename .. "~" .. i
  545.       end
  546.       fs.copy(filename, backup)
  547.     end
  548.     if not fs.exists(file_parentpath) then
  549.       fs.makeDirectory(file_parentpath)
  550.     end
  551.     local f, reason = io.open(filename, "w")
  552.     if f then
  553.       local chars, firstLine = 0, true
  554.       for _, line in ipairs(buffer) do
  555.         if not firstLine then
  556.           line = "\n" .. line
  557.         end
  558.         firstLine = false
  559.         f:write(line)
  560.         chars = chars + unicode.len(line)
  561.       end
  562.       f:close()
  563.       local format
  564.       if new then
  565.         format = [["%s" [New] %dL,%dC written]]
  566.       else
  567.         format = [["%s" %dL,%dC written]]
  568.       end
  569.       setStatus(string.format(format, fs.name(filename), #buffer, chars))
  570.     else
  571.       setStatus(reason)
  572.     end
  573.     if not new then
  574.       fs.remove(backup)
  575.     end
  576.   end,
  577.   close = function()
  578.     -- TODO ask to save if changed
  579.     running = false
  580.     gpu.setBackground(0x000000)
  581.     gpu.setForeground(0xffffff)
  582.   end,
  583.   find = function()
  584.     findText = ""
  585.     find()
  586.   end,
  587.   findnext = find
  588. }
  589.  
  590. getKeyBindHandler = function(code)
  591.   if type(config.keybinds) ~= "table" then return end
  592.   -- Look for matches, prefer more 'precise' keybinds, e.g. prefer
  593.   -- ctrl+del over del.
  594.   local result, resultName, resultWeight = nil, nil, 0
  595.   for command, keybinds in pairs(config.keybinds) do
  596.     if type(keybinds) == "table" and keyBindHandlers[command] then
  597.       for _, keybind in ipairs(keybinds) do
  598.         if type(keybind) == "table" then
  599.           local alt, control, shift, key = false, false, false
  600.           for _, value in ipairs(keybind) do
  601.             if value == "alt" then alt = true
  602.             elseif value == "control" then control = true
  603.             elseif value == "shift" then shift = true
  604.             else key = value end
  605.           end
  606.           local keyboardAddress = term.keyboard()
  607.           if (alt     == not not keyboard.isAltDown(keyboardAddress)) and
  608.              (control == not not keyboard.isControlDown(keyboardAddress)) and
  609.              (shift   == not not keyboard.isShiftDown(keyboardAddress)) and
  610.              code == keyboard.keys[key] and
  611.              #keybind > resultWeight
  612.           then
  613.             resultWeight = #keybind
  614.             resultName = command
  615.             result = keyBindHandlers[command]
  616.           end
  617.         end
  618.       end
  619.     end
  620.   end
  621.   return result, resultName
  622. end
  623.  
  624. -------------------------------------------------------------------------------
  625.  
  626. local function onKeyDown(char, code)
  627.   local handler = getKeyBindHandler(code)
  628.   if handler then
  629.     handler()
  630.   elseif readonly and code == keyboard.keys.q then
  631.     running = false
  632.   elseif not readonly then
  633.     if not keyboard.isControl(char) then
  634.       insert(unicode.char(char))
  635.     elseif unicode.char(char) == "\t" then
  636.       insert("  ")
  637.     end
  638.   end
  639. end
  640.  
  641. local function onClipboard(value)
  642.   value = value:gsub("\r\n", "\n")
  643.   local cbx, cby = getCursor()
  644.   local start = 1
  645.   local l = value:find("\n", 1, true)
  646.   if l then
  647.     repeat
  648.       local line = string.sub(value, start, l - 1)
  649.       line = text.detab(line, 2)
  650.       insert(line)
  651.       enter()
  652.       start = l + 1
  653.       l = value:find("\n", start, true)
  654.     until not l
  655.   end
  656.   insert(string.sub(value, start))
  657. end
  658.  
  659. local function onClick(x, y)
  660.   setCursor(x + scrollX, y + scrollY)
  661. end
  662.  
  663. local function onScroll(direction)
  664.   local cbx, cby = getCursor()
  665.   setCursor(cbx, cby - direction * 12)
  666. end
  667.  
  668. -------------------------------------------------------------------------------
  669.  
  670. do
  671.   local f = io.open(filename)
  672.   if f then
  673.     local x, y, w, h = getArea()
  674.     local chars = 0
  675.     for line in f:lines() do
  676.       table.insert(buffer, line)
  677.       chars = chars + unicode.len(line)
  678.       if #buffer <= h then
  679.         drawLine(x, y, w, h, #buffer)
  680.       end
  681.     end
  682.     f:close()
  683.     if #buffer == 0 then
  684.       table.insert(buffer, "")
  685.     end
  686.     local format
  687.     if readonly then
  688.       format = [["%s" [readonly] %dL,%dC]]
  689.     else
  690.       format = [["%s" %dL,%dC]]
  691.     end
  692.     setStatus(string.format(format, fs.name(filename), #buffer, chars))
  693.   else
  694.     table.insert(buffer, "")
  695.     setStatus(string.format([["%s" [New File] ]], fs.name(filename)))
  696.   end
  697.   setCursor(1, 1)
  698. end
  699.  
  700. while running do
  701.   _, nado = gpu.maxResolution()
  702.   if ferror1 and ferror1 ~= nil then gpu.setForeground(0xff0000) gpu.set(47, nado, "Не удалось определить введённый цвет") gpu.setForeground(0xffffff) end
  703.   local event, address, arg1, arg2, arg3 = term.pull()
  704.   if address == term.keyboard() or address == term.screen() then
  705.     local blink = true
  706.     if event == "key_down" then
  707.       onKeyDown(arg1, arg2)
  708.     elseif event == "clipboard" and not readonly then
  709.       onClipboard(arg1)
  710.     elseif event == "touch" or event == "drag" then
  711.       local x, y, w, h = getArea()
  712.       arg1 = arg1 - x + 1
  713.       arg2 = arg2 - y + 1
  714.       if arg1 >= 1 and arg2 >= 1 and arg1 <= w and arg2 <= h then
  715.         onClick(arg1, arg2)
  716.       end
  717.     elseif event == "scroll" then
  718.       onScroll(arg3)
  719.     else
  720.       blink = false
  721.     end
  722.     if blink then
  723.       term.setCursorBlink(true)
  724.     end
  725.   end
  726. end
  727.                                              
  728. term.clear()
  729. term.setCursorBlink(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement