Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --КОНСТАНТЫ--
- local APIs_DESTINATION = "MineCodeAPIs/"
- local PERMANENT_INDENT = 1
- --ЗАГРУЗКА СТОРОННИХ БИБЛИОТЕК, ПЕРВОНАЧАЛЬНАЯ ИНИЦИАЛИЗАЦИЯ ПРОГРАММЫ--
- --os.loadAPI(apis_path .. "graphics")
- os.loadAPI(APIs_DESTINATION .. "SyntaxHighlight")
- --if (os.version() ~= "CraftOS 1.6") then error("Require " .. os.version() .. ".") end
- if not term.isColor() or turtle or pocket then error("This program will run only on advanced computer.") end
- term.clear()
- --ПЕРЕМЕННЫЕ--
- local sizeOfLineCounter = 1
- local termWidth, termHeight = term.getSize()
- local xAbsoluteCursor, yAbsoluteCursor = 4, 1
- local xPrevDeltaCursor, yPrevDeltaCursor = 0, 0
- local xDeltaCursor, yDeltaCursor = 0, 0
- local linesArray = {}
- local editedLine = nil
- local GUIWindow = window.create(term.current(), 1, 1, termWidth-1, termHeight-1)
- GUIWindow.clear()
- local LineCounterWindow = window.create(term.current(), 1, 3, sizeOfLineCounter, termHeight-2)
- LineCounterWindow.clear()
- local WorkspaceWindow = window.create(term.current(), sizeOfLineCounter+1, 3, termWidth-1, termHeight-2)
- WorkspaceWindow.setCursorPos(xAbsoluteCursor, yAbsoluteCursor)
- WorkspaceWindow.clear()
- local workspaceWidth, workspaceHeight = WorkspaceWindow.getSize()
- --РАБОТА С ФАЙЛАМИ--
- local function openFile(filename)
- for readedLine in io.lines(filename) do
- table.insert(linesArray, readedLine)
- end
- editedLine = linesArray[1]
- end
- local function saveFile(filename)
- local _file = fs.open(filename, "w")
- for i = 1, #linesArray do
- _file.writeLine(linesArray[i])
- end
- _file.close()
- end
- --ОТРИСОВКА--
- local function draw_gui()
- end
- local function draw_line(wWindow, iLine)
- WorkspaceWindow.setBackgroundColor(colors.white)
- WorkspaceWindow.setTextColor(colors.black)
- WorkspaceWindow.setCursorPos(PERMANENT_INDENT, iLine)
- local _tmpLine = linesArray[yDeltaCursor+iLine]:sub(xDeltaCursor+1, -1)
- WorkspaceWindow.write(" " .. _tmpLine .. string.rep(" ", workspaceWidth-1))
- SyntaxHighlight.syntaxString(WorkspaceWindow, iLine, PERMANENT_INDENT+1, _tmpLine)
- end
- local function drawLineCounter()
- LineCounterWindow.setBackgroundColor(colors.lightGray)
- LineCounterWindow.setTextColor(colors.white)
- LineCounterWindow.clear()
- for i = 1, #linesArray-yDeltaCursor do
- LineCounterWindow.setCursorPos(1, i)
- LineCounterWindow.write(yDeltaCursor+i .. string.rep(" ", sizeOfLineCounter))
- end
- end
- --draw_workspace() redraw all
- --draw_workspace([startRedrawPos]) redraw startRedrawPos to workspaceHeight lines
- --draw_workspace([startRedrawPos, [endRedrawPos]]) redraw startRedrawPos to endRedrawPos lines
- local function draw_workspace(startRedrawPos, endRedrawPos)
- local prev_xCur, prev_yCur = WorkspaceWindow.getCursorPos()
- WorkspaceWindow.setCursorBlink(false)
- local prevSoLC = sizeOfLineCounter
- sizeOfLineCounter = #tostring(#linesArray)
- if prevSoLC ~= sizeOfLineCounter then
- WorkspaceWindow = window.create(term.current(), sizeOfLineCounter+1, 3, termWidth-1-sizeOfLineCounter, termHeight-2)
- workspaceWidth, workspaceHeight = WorkspaceWindow.getSize()
- end
- for i = startRedrawPos or 1, endRedrawPos or workspaceHeight do
- if linesArray[yDeltaCursor+i] then
- draw_line(WorkspaceWindow, i)
- else
- WorkspaceWindow.setCursorPos(PERMANENT_INDENT, i)
- write(string.rep(" ", workspaceWidth))
- end
- end
- if prevSoLC ~= sizeOfLineCounter then
- LineCounterWindow = window.create(term.current(), 1, 3, sizeOfLineCounter, termHeight-2)
- draw_workspace()
- end
- drawLineCounter()
- WorkspaceWindow.setBackgroundColor(colors.white)
- WorkspaceWindow.setTextColor(colors.black)
- WorkspaceWindow.setCursorPos(prev_xCur, prev_yCur)
- WorkspaceWindow.setCursorBlink(true)
- end
- local function range(bottom, top, value, step)
- if value + step > top then
- return top
- elseif value + step < bottom then
- return bottom
- end
- return value + step
- end
- --РАБОТА С КУРСОРАМИ--
- local function checkEndLine()
- return xAbsoluteCursor + 1 > #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1
- end
- local function checkStartLine()
- return xAbsoluteCursor - 1 < PERMANENT_INDENT + 1
- end
- local function checkTopLinesArray()
- return yAbsoluteCursor - 1 < 1
- end
- local function checkBottomLinesArray()
- return yAbsoluteCursor + 1 > #linesArray
- end
- local function calculateCursors(xRelCur, yRelCur)
- WorkspaceWindow.setCursorPos(xRelCur, yRelCur)
- xPrevDeltaCursor = xDeltaCursor
- yPrevDeltaCursor = yDeltaCursor
- xDeltaCursor = xAbsoluteCursor - xRelCur
- yDeltaCursor = yAbsoluteCursor - yRelCur
- end
- local function moveCursorsToStartLine()
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- xAbsoluteCursor = PERMANENT_INDENT + 1
- xRelativeCursor = PERMANENT_INDENT + 1
- calculateCursors(xRelativeCursor, yRelativeCursor)
- end
- local function moveCursorsToEndLine()
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- xAbsoluteCursor = #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1
- xRelativeCursor = math.min(#linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1, workspaceWidth)
- calculateCursors(xRelativeCursor, yRelativeCursor)
- end
- local function moveCursorsUpAndDown(step)
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- yAbsoluteCursor = range(1, #linesArray, yAbsoluteCursor, step)
- yRelativeCursor = range(1, math.min(workspaceHeight, #linesArray), yRelativeCursor, step)
- if checkEndLine() then
- xAbsoluteCursor = #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1
- xRelativeCursor = #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1
- end
- calculateCursors(xRelativeCursor, yRelativeCursor)
- end
- local function moveCursorsRightAndLeft(step)
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- if step < 0 and checkStartLine() and yAbsoluteCursor > 1 then
- xAbsoluteCursor = #linesArray[yAbsoluteCursor - 1] + PERMANENT_INDENT + 2
- xRelativeCursor = math.min(#linesArray[yAbsoluteCursor - 1] + PERMANENT_INDENT + 2, workspaceWidth)
- yAbsoluteCursor = yAbsoluteCursor - 1
- yRelativeCursor = math.max(yRelativeCursor - 1, 1)
- end
- if step > 0 and checkEndLine() and yAbsoluteCursor < #linesArray then
- xAbsoluteCursor = PERMANENT_INDENT
- xRelativeCursor = PERMANENT_INDENT
- yAbsoluteCursor = yAbsoluteCursor + 1
- yRelativeCursor = math.min(yRelativeCursor + 1, workspaceHeight)
- end
- xAbsoluteCursor = range(PERMANENT_INDENT + 1, #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1, xAbsoluteCursor, step)
- xRelativeCursor = range(PERMANENT_INDENT + 1, math.min(workspaceWidth, #linesArray[yAbsoluteCursor] + PERMANENT_INDENT + 1), xRelativeCursor, step)
- calculateCursors(xRelativeCursor, yRelativeCursor)
- end
- --РЕДАКТИРОВАНЕ ТЕКСТА--
- local function removeChar()
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- if xAbsoluteCursor > PERMANENT_INDENT + 1 then
- linesArray[yAbsoluteCursor] = linesArray[yAbsoluteCursor]:sub(1, xAbsoluteCursor-(PERMANENT_INDENT + 2)) .. linesArray[yAbsoluteCursor]:sub(xAbsoluteCursor-PERMANENT_INDENT, -1)
- xAbsoluteCursor = xAbsoluteCursor - 1
- xRelativeCursor = xRelativeCursor - 1
- draw_workspace(yRelativeCursor, yRelativeCursor)
- elseif yAbsoluteCursor > 1 then
- local _temp = #linesArray[yAbsoluteCursor - 1]
- linesArray[yAbsoluteCursor - 1] = linesArray[yAbsoluteCursor - 1] .. linesArray[yAbsoluteCursor]:sub(xAbsoluteCursor-PERMANENT_INDENT, -1)
- table.remove(linesArray, yAbsoluteCursor)
- yAbsoluteCursor = yAbsoluteCursor - 1
- yRelativeCursor = math.max(yRelativeCursor - 1, 1)
- xAbsoluteCursor = _temp + PERMANENT_INDENT + 1
- xRelativeCursor = math.min(_temp + PERMANENT_INDENT + 1, workspaceWidth)
- draw_workspace(yRelativeCursor, workspaceHeight)
- end
- calculateCursors(xRelativeCursor, yRelativeCursor)
- end
- local function writeChar(char)
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- linesArray[yAbsoluteCursor] = linesArray[yAbsoluteCursor]:sub(1, xAbsoluteCursor-(PERMANENT_INDENT + 1)) .. char .. linesArray[yAbsoluteCursor]:sub(xAbsoluteCursor-PERMANENT_INDENT, -1)
- draw_workspace(yRelativeCursor, yRelativeCursor)
- end
- local function newLine()
- local xRelativeCursor, yRelativeCursor = WorkspaceWindow.getCursorPos()
- table.insert(linesArray, yAbsoluteCursor + 1, linesArray[yAbsoluteCursor]:sub(xAbsoluteCursor-PERMANENT_INDENT, -1))
- linesArray[yAbsoluteCursor] = linesArray[yAbsoluteCursor]:sub(1, xAbsoluteCursor-(PERMANENT_INDENT + 1))
- yAbsoluteCursor = range(1, #linesArray, yAbsoluteCursor, 1)
- yRelativeCursor = range(1, math.min(workspaceHeight, #linesArray), yRelativeCursor, 1)
- xAbsoluteCursor = PERMANENT_INDENT + 1
- xRelativeCursor = PERMANENT_INDENT + 1
- calculateCursors(xRelativeCursor, yRelativeCursor)
- draw_workspace(yRelativeCursor-1, workspaceHeight)
- end
- --openFile("edt")
- openFile("test")
- draw_workspace()
- --ГЛАВНЫЙ ЦИКЛ ПРОГРАММЫ--
- while true do
- if xDeltaCursor ~= xPrevDeltaCursor or yDeltaCursor ~= yPrevDeltaCursor then
- draw_workspace()
- end
- --GUIWindow.clearLine()
- --GUIWindow.setCursorPos(1, 1)
- --GUIWindow.write("xDC: " .. xDeltaCursor .. ". yDC: " .. yDeltaCursor .. ". xpDC: " .. xPrevDeltaCursor .. ". ypDC: " .. yPrevDeltaCursor)
- local event, par1, par2, par3, par4 = os.pullEvent()
- if event == "key" then
- if par1 == keys.down then
- moveCursorsUpAndDown(1)
- elseif par1 == keys.up then
- moveCursorsUpAndDown(-1)
- elseif par1 == keys.left then
- moveCursorsRightAndLeft(-1)
- elseif par1 == keys.right then
- moveCursorsRightAndLeft(1)
- elseif par1 == keys.home then
- moveCursorsToStartLine()
- elseif par1 == keys["end"] then
- moveCursorsToEndLine()
- elseif par1 == keys.backspace then
- removeChar()
- elseif par1 == keys.enter then
- newLine()
- elseif par1 == keys.insert then
- saveFile("test")
- end
- end
- if event == "char" then
- writeChar(par1)
- moveCursorsRightAndLeft(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement