Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --get filepath
- local arg = { ... }
- if #arg == 0 then
- print("Syntax: editor <path>")
- return
- end
- --constants
- local TIMER_INTERVAL = .1 --updates the screen this often (in seconds)
- local X_MIN, Y_MIN = 5, 1 --min values for file display
- local X_MAX, Y_MAX = term.getSize() --max values for file display
- --flags
- local bDebug = true --display debugging info if true
- local bInsert = true --insert text if true, override text if false
- --variables
- local sFilepath = arg[1] --path to the file being editted
- local tFile = { } --table of lines for the file being editted
- local xFile, yFile = 1, 1 --the location of the cursor in the text file
- local xScroll, yScroll = 0, 0 --amount file is scrolled on screen
- local sDebugText = "" --debug text shown if debugging is on
- --load a file from hdd into editor
- function load()
- --error handling file
- if fs.exists(sFilepath) then
- if fs.isDir(sFilepath) then
- print("Cannot read directory as a file.")
- error() --exit program
- else
- --load file
- local file = fs.open(sFilepath, "r")
- while true do
- local line = file.readLine()
- if line == nil then
- break end
- table.insert(tFile, line)
- end
- file.close()
- end
- else
- table.insert(tFile, "")
- end
- end
- --intialization
- function intialize()
- load() --load file
- --setup screen
- term.setCursorPos(X_MIN,Y_MIN)
- term.setCursorBlink(true)
- draw()
- end
- --moves cursor around
- function shiftCursor(xShift,yShift)
- local x,y = term.getCursorPos()
- --handle left/right movement
- if xFile == 1 and xShift < 0 then --if at start of line and moving left, move to end of above line (if exists)
- if yFile ~= 1 then
- xFile = #tFile[yFile-1] + 1
- yFile = yFile - 1
- x = X_MIN + xFile - 1 + xScroll
- y = y - 1
- end
- elseif xFile > #tFile[yFile] and xShift > 0 then --if at end of line and moving right, move to start of next line (if exists)
- if yFile ~= #tFile then
- xFile = 1
- yFile = yFile + 1
- x = X_MIN - xScroll
- y = y + 1
- end
- else --regular movement
- xFile = xFile + xShift
- x = x + xShift
- end
- --handle up/down movement
- if yFile == 1 and yShift < 0 then --if at first line and moving up, move to start of line
- xFile = 1
- x = X_MIN - xScroll
- elseif yFile == #tFile and yShift > 0 then --if at last line and moving down, move to end of line
- xFile = #tFile[yFile] + 1
- x = X_MIN + xFile - 1 - xScroll
- else --regular movement
- yFile = yFile + yShift
- y = y + yShift
- --if the cursor is past the end of the last character after having moved, move it to the last character
- if xFile > #tFile[yFile] then
- xFile = 1 + #tFile[yFile]
- x = X_MIN + #tFile[yFile] - xScroll
- end
- end
- sDebugText = sDebugText .. " MIN/MAX:"..X_MIN..","..X_MAX
- --set cursor pos
- term.setCursorPos(x, y)
- end
- --move cursor to the start of the line
- function shiftCursorHome()
- local x, y = term.getCursorPos()
- xFile = 1
- term.setCursorPos(X_MIN - xScroll, y)
- end
- --move cursor to the end of the line (end of text)
- function shiftCursorEnd()
- local x, y = term.getCursorPos()
- xFile = 1 + #tFile[yFile]
- term.setCursorPos(X_MIN + #tFile[yFile] - xScroll, y)
- end
- --save editor's text to a file
- function save()
- file = fs.open(sFilepath, "w")
- for i,line in ipairs(tFile) do
- file.writeLine(line) end
- file.close()
- end
- function write(sText)
- tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. sText .. string.sub(tFile[yFile], xFile)
- shiftCursor(1,0)
- end
- --display debug info in bottom right
- function drawDebug(sText)
- local x,y = term.getCursorPos()
- term.setCursorPos(X_MAX + 1 - #sText, Y_MAX) --set cursor at bottom rightmost while still showing all text
- term.write(sText)
- term.setCursorPos(x, y)
- end
- --scrolls the screen with the cursor
- function scroll()
- local x,y = term.getCursorPos()
- if y > Y_MAX then
- yScroll = yScroll + (y - Y_MAX)
- y = Y_MAX
- elseif y < Y_MIN then
- yScroll = yScroll - (Y_MIN - y)
- y = Y_MIN
- end
- if x > X_MAX then
- xScroll = xScroll + (x - X_MAX)
- x = X_MAX
- elseif x < X_MIN then
- xScroll = xScroll - (X_MIN - x)
- x = X_MIN
- end
- sDebugText = sDebugText .. " Scroll:"..xScroll..","..yScroll
- term.setCursorPos(x, y)
- end
- --redraws entire screen
- function draw()
- local x,y = term.getCursorPos()
- term.clear()
- --draw line numbers
- for i=Y_MIN,Y_MAX do
- if tFile[i+yScroll] ~= nil then
- term.setCursorPos(1, i)
- if i+yScroll < 100 then term.write(" ") end
- if i+yScroll < 10 then term.write(" ") end
- term.write(i + yScroll)
- end
- end
- --draw line number seperator
- for i=Y_MIN,Y_MAX do
- term.setCursorPos(X_MIN-1, i)
- term.write("|")
- end
- --draw file text
- --TODO stop draw at last visible line for code effeciency
- for i=Y_MIN,Y_MAX do
- if tFile[i+yScroll] ~= nil then
- term.setCursorPos(X_MIN, i)
- term.write(string.sub(tFile[i+yScroll], 1 + xScroll, X_MAX - X_MIN + 1 + xScroll))
- end
- end
- --draw debug
- if bDebug then
- drawDebug(sDebugText) end
- term.setCursorPos(x, y)
- end
- --main loop
- function run()
- while true do
- sDebugText = ""
- --wait for key input
- local e, p1 = os.pullEvent()
- if e == "char" then --handles character input
- write(p1)
- elseif e == "key" then -- handles non-character input
- --DONT USE F2, F10, F11, ALT, ESC
- --functional keys
- if p1 == keys.f1 then -- F1 - menu
- elseif p1 == keys.f3 then -- F3 - save
- save()
- sDebugText = "SAVED"
- elseif p1 == keys.f4 then -- F4 - exit
- term.clear()
- term.setCursorPos(1,1)
- return
- --arrow keys
- elseif p1 == keys.up then
- shiftCursor(0, -1)
- elseif p1 == keys.down then
- shiftCursor(0, 1)
- elseif p1 == keys.left then
- shiftCursor(-1, 0)
- elseif p1 == keys.right then
- shiftCursor(1, 0)
- --backspace, delete
- elseif p1 == keys.backspace then
- if not (yFile == 1 and xFile == 1) then
- if xFile == 1 then --if at the start of a line, concatenate line above with this line
- shiftCursor(0,-1)
- shiftCursorEnd()
- tFile[yFile] = tFile[yFile] .. tFile[yFile+1] --concat lines
- table.remove(tFile, yFile+1) --remove second line
- else --otherwise just delete one character
- shiftCursor(-1,0)
- tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. string.sub(tFile[yFile], xFile+1)
- end
- end
- elseif p1 == keys.delete then --delete
- if not (yFile == #tFile and xFile == #tFile[yFile] + 1) then
- if xFile > #tFile[yFile] then --if cursor is at the end of the line, concatenate with line below
- tFile[yFile] = tFile[yFile] .. tFile[yFile+1] --concat lines
- table.remove(tFile, yFile+1) --remove second line
- else --otherwise just delete one character
- tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) .. string.sub(tFile[yFile], xFile+1)
- end
- end
- --enter, home, end
- elseif p1 == keys.enter then
- if xFile <= #tFile[yFile] then --if cursor is between characters
- table.insert(tFile, yFile+1, string.sub(tFile[yFile], xFile)) --move remainder of characters to a new line
- tFile[yFile] = string.sub(tFile[yFile], 1, xFile-1) --and delete the moved characters from the previous line
- else --cursor at the end of the line
- table.insert(tFile, yFile+1, "")
- end
- shiftCursorHome()
- shiftCursor(0,1)
- elseif p1 == keys.home then
- shiftCursorHome()
- elseif p1 == keys["end"] then
- shiftCursorEnd()
- elseif p1 == keys.home then
- shiftCursorHome()
- end
- end
- local x,y = term.getCursorPos()
- sDebugText = sDebugText .. " File:"..xFile..","..yFile .." Scrn:"..x..","..y
- scroll()
- draw()
- end
- end
- intialize()
- run()
Advertisement
Add Comment
Please, Sign In to add comment