Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cfname = "NEW"
- local title = "\\___/ File: "..cfname
- local clipboard = {}
- local lastname = nil
- local lastlines = nil
- local lines = {" "}
- local firstpoint = nil
- local lastpoint = nil
- local cursor = {1, 1}
- local blink = true
- local scroll = {1, 1}
- local curtab = " "
- local function drawMenu()
- local menu = {"| \\__",
- "| SAVE |",
- "| NEW |",
- "| LOAD |",
- "| PRINT |",
- "+-------+"}
- for y=1,#menu do
- local line = menu[y]
- term.setCursorPos(1, y+1)
- term.write(line)
- end
- end
- local function drawLines()
- local oldx, oldy = term.getCursorPos()
- for y=scroll[2],#lines do
- local line = lines[y]
- for x=scroll[1],string.len(line) do
- local ch = string.sub(line, x, x)
- local color = colors.white
- if firstpoint ~= nil then
- if firstpoint[2] < y and lastpoint[2] > y then color = colors.yellow end
- if firstpoint[2] == y and firstpoint[1] <= x then color = colors.yellow end
- if lastpoint[2] == y and lastpoint[1] >= x then color = colors.yellow end
- end
- term.setCursorPos((x)-scroll[1]+1, (y+2)-scroll[2])
- term.setTextColor(color)
- term.write(ch)
- term.setTextColor(colors.white)
- end
- end
- term.setCursorPos(cursor[1]-scroll[1]+1, ((cursor[2])+2)-scroll[2])
- if blink then term.write("_") end
- term.setCursorPos(oldx, oldy)
- end
- local function type(ch)
- if firstpoint == nil then
- local line = lines[cursor[2]]
- local bef = string.sub(line, 1, cursor[1]-1)
- local aft = string.sub(line, cursor[1], -1)
- local nline = ""
- if bef ~= nil then nline = bef end
- nline = nline..ch
- if aft ~= nil then nline = nline..aft end
- lines[cursor[2]] = nline
- cursor[1] = cursor[1] + 1
- else
- local lns = {}
- for i=1,#lines do
- local line = lines[i]
- if i == firstpoint[2] then
- lns[#lns + 1] = string.sub(line, firstpoint[1], -1)..ch
- elseif i == lastpoint[2] then
- lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
- elseif i > firstpoint[2] and i < lastpoint[2] then
- else
- lns[#lns + 1] = line
- end
- end
- lines = lns
- firstpoint = nil
- lastpoint = nil
- cursor[1] = cursor[1] + 1
- end
- end
- local function backspace()
- if firstpoint == nil then
- local line = lines[cursor[2]]
- if line == "" or line == nil then
- local lns = {}
- for i=1,#lines do
- local line = lines[i]
- if i ~= cursor[2] then
- lns[#lns + 1] = line
- end
- end
- cursor[2] = cursor[2] - 1
- else
- local bef = string.sub(line, 1, cursor[1]-1)
- local aft = string.sub(line, cursor[1]+1, -1)
- local nline = ""
- if bef ~= nil then nline = bef end
- if aft ~= nil then nline = nline..aft end
- lines[cursor[2]] = nline
- cursor[1] = cursor[1] - 1
- end
- else
- local lns = {}
- for i=1,#lines do
- local line = lines[i]
- if i == firstpoint[2] then
- lns[#lns + 1] = string.sub(line, firstpoint[1], -1)
- elseif i == lastpoint[2] then
- lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
- elseif i > firstpoint[2] and i < lastpoint[2] then
- else
- lns[#lns + 1] = line
- end
- end
- lines = lns
- firstpoint = nil
- lastpoint = nil
- cursor[1] = cursor[1] - 1
- end
- end
- local tabamt = " "
- local function tab()
- type(tabamt)
- curtab = curtab..tabamt
- end
- local function untab()
- local line = lines[cursor[2]]
- local sub = string.sub(line, cursor[1]-(string.len(tabat)), cursor[1])
- if sub ~= nil then
- if string.gsub(sub, " ", "") == "" then
- local bef = string.sub(line, 1, cursor[1]-(string.len(tabat)-1))
- local aft = string.sub(line, cursor[1], -1)
- local nline = ""
- if bef ~= nil then nline = bef end
- if aft ~= nil then nline = nline..aft end
- lines[cursor[2]] = nline
- if string.len(curtab) > string.len(tabamt) then
- curtab = string.sub(curtab, 1, string.len(curtab)-string.len(tabamt))
- end
- if curtab == "" then curtab = " " end
- end
- end
- end
- local function enter()
- local lns = {}
- local done = false
- if firstpoint == nil then
- for i=1,#lines do
- local line = lines[i]
- lns[#lns + 1] = line
- if i == cursor[2] and not done then
- lns[#lns + 1] = curtab
- cursor = {string.len(curtab), i+1}
- done = true
- end
- end
- else
- for i=1,#lines do
- local line = lines[i]
- if i == firstpoint[2] then
- lns[#lns + 1] = string.sub(line, 1, firstpoint[1])
- local nstr = " "
- local sub = string.sub(line, firstpoint[1], -1)
- if sub ~= nil and sub ~= "" then nstr = sub end
- lns[#lns + 1] = curtab
- cursor = {string.len(curtab), i+1}
- elseif i == lastpoint[2] then
- lns[#lns + 1] = string.sub(line, 1, lastpoint[1])
- elseif i > firstpoint[2] and i < lastpoint[2] then
- else
- lns[#lns + 1] = line
- end
- end
- firstpoint = nil
- lastpoint = nil
- end
- lines = lns
- end
- local function copy()
- local lns = {}
- for y=firstpoint[2],lastpoint[2] do
- local line = lines[y]
- if y == firstpoint[2] then
- line = string.sub(line, firstpoint[1], -1)
- elseif y == lastpoint[2] then
- line = string.sub(line, 1, lastpoint[1])
- else
- lns[#lns + 1] = line
- end
- end
- clipboard = lns
- end
- local function paste()
- local lns = {}
- for y=1,#lines do
- local line = lines[y]
- if y == cursor[2] then
- for i=1,#clipboard do
- if i == 1 then
- line = line..clipboard[i]
- else
- line = clipboard[i]
- end
- lns[#lns + 1] = line
- end
- else
- lns[#lns + 1] = line
- end
- end
- lastlines = lines
- lines = lns
- end
- local function undo()
- if lastlines ~= nil then
- local templines = lines
- lines = lastlines
- lastlines = templines
- if lastname ~= nil then
- cfname = lastname
- title = string.sub(title, 1, 5).." File: "..cfname
- lastname = nil
- end
- end
- end
- local timer = os.startTimer(0)
- local interval = 0.1
- local function save()
- term.setCursorPos(1, 1)
- local line = ""
- local mx, my = term.getSize()
- for i=1,mx do line = line.." " end
- term.write(line)
- term.setCursorPos(1, 1)
- local msg = "Enter Dir: "
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- local input = read()
- while input == "" or input == nil do
- term.setCursorPos(1, 1)
- term.write(line)
- term.setCursorPos(1, 1)
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- input = read()
- end
- if string.gsub(input, " ", "") == "" then else
- if fs.exists(input) then fs.delete(input) end
- local file = fs.open(input, "a")
- for i=1,#lines do
- local line = lines[i]
- file.writeLine(line)
- end
- file.close()
- cfname = input
- title = string.sub(title, 1, 5).." File: "..cfname
- end
- timer = os.startTimer(interval)
- title = "\\___/ File: "..cfname
- end
- local function load()
- term.setCursorPos(1, 1)
- local line = ""
- local mx, my = term.getSize()
- for i=1,mx do line = line.." " end
- term.write(line)
- term.setCursorPos(1, 1)
- local msg = "Enter Dir: "
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- local input = read()
- while input == "" or input == nil do
- term.setCursorPos(1, 1)
- term.write(line)
- term.setCursorPos(1, 1)
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- input = read()
- end
- if string.gsub(input, " ", "") == "" then else
- if fs.exists(input) then
- local file = fs.open(input, "r")
- local ln = file.readLine()
- local lns = {}
- while ln ~= nil do
- lns[#lns + 1] = ln
- ln = file.readLine()
- end
- file.close()
- lines = lns
- cfname = input
- title = string.sub(title, 1, 5).." File: "..cfname
- end
- end
- timer = os.startTimer(interval)
- title = "\\___/ File: "..cfname
- end
- local function new()
- title = "\\___/ File: NEW"
- clipboard = {}
- lastname = cfname
- lastlines = lines
- cfname = "NEW"
- lines = {" "}
- firstpoint = nil
- lastpoint = nil
- cursor = {1, 1}
- blink = true
- end
- local function round (x)
- if x >= 0 then
- return math.floor (x + 0.5)
- end -- if positive
- return math.ceil (x - 0.5)
- end
- local function print()
- term.setCursorPos(1, 1)
- local line = ""
- local mx, my = term.getSize()
- for i=1,mx do line = line.." " end
- term.write(line)
- term.setCursorPos(1, 1)
- local msg = "Enter title: "
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- while input == "" or input == nil do
- term.setCursorPos(1, 1)
- term.write(line)
- term.setCursorPos(1, 1)
- term.write(msg)
- term.setCursorPos(string.len(msg), 1)
- input = read()
- end
- tSides = {"left","right","bottom","top","front","back"}
- local printer = nil
- for i=1,#tSides do
- local side = tSides[i]
- if peripheral.getType(side) == "printer" and printer == nil then
- printer = peripheral.wrap(side)
- end
- end
- if printer ~= nil then
- printer.newPage()
- local px, py = printer.getPageSize()
- local pages = round(#lines / py)
- if printer.getInkLevel() >= pages then
- if printer.getPaperLevel() >= pages then
- local cur = 0
- for i=1,#lines do
- cur = cur + 1
- if cur > py then
- printer.endPage()
- printer.newPage()
- printer.setPageTitle(input)
- end
- local line = lines[i]
- while string.len(line) > px do
- local ln = string.sub(line, 1, px)
- printer.write(ln)
- cur = cur + 1
- if cur > py then
- printer.endPage()
- printer.newPage()
- printer.setPageTitle(input)
- end
- line = string.sub(line, px+1, -1)
- if line == nil then line = "" end
- end
- printer.write(line)
- end
- printer.endPage()
- end
- end
- end
- timer = os.startTimer(interval)
- title = "\\___/ File: "..input
- end
- local function checkCurs()
- local mx, my = term.getSize()
- if cursor[1] < scroll[1] then scroll[1] = scroll[1] - 1 end
- if cursor[1] > scroll[1]+mx then scroll[1] = scroll[1] + 1 end
- if cursor[2] < scroll[2] then scroll[2] = scroll[2] - 1 end
- if cursor[2] > scroll[2]+my then scroll[2] = scroll[2] + 1 end
- if cursor[2] < 1 then cursor[2] = 1 end
- if cursor[2] > #lines then cursor[2] = #lines end
- if cursor[1] < 1 then cursor[1] = 1 end
- if cursor[1] > string.len(lines[cursor[2]]) then cursor[1] = string.len(lines[cursor[2]]) end
- if scroll[1] < 1 then scroll[1] = 1 end
- if scroll[1] > cursor[1] then scroll[1] = cursor[1] end
- if scroll[2] < 1 then scroll[2] = 1 end
- if scroll[2] > #lines then scroll[2] = #lines end
- end
- local showtab = false
- local on = true
- local dochar = true
- local ticks = 0
- while on do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "timer" and p1 == timer then
- checkCurs()
- term.clear()
- term.setCursorPos(1, 1)
- term.write(title)
- drawLines()
- if showtab then
- drawMenu()
- end
- ticks = ticks + 1
- if ticks >= 3 then
- if blink then blink = false else blink = true end
- ticks = 0
- end
- timer = os.startTimer(interval)
- elseif event == "key" then
- local key = p1
- if key == 29 then
- on = false
- elseif key == 15 then
- tab()
- elseif key == 41 then
- untab()
- elseif key == 54 then
- dochar = false
- elseif key == 28 then
- enter()
- elseif key == 14 then
- backspace()
- elseif key == 157 then
- if showtab then
- showtab = false
- title = "\\___/"..string.sub(title, 6, -1)
- else
- showtab = true
- title = " ____"..string.sub(title, 6, -1)
- end
- elseif key == 200 then --up
- local x = string.len(lines[cursor[2]])
- cursor[2] = cursor[2] - 1
- checkCurs()
- if x > string.len(lines[cursor[2]]) then x = string.len(lines[cursor[2]]) end
- cursor[1] = x
- elseif key == 203 then --left
- cursor[1] = cursor[1] - 1
- elseif key == 208 then --down
- local x = string.len(lines[cursor[2]])
- cursor[2] = cursor[2] + 1
- checkCurs()
- if x > string.len(lines[cursor[2]]) then x = string.len(lines[cursor[2]]) end
- cursor[1] = x
- elseif key == 205 then --right
- cursor[1] = cursor[1] + 1
- end
- elseif event == "char" then
- local char = p1
- if dochar then
- type(char)
- elseif not showtab then
- char = string.lower(char)
- if char == "c" then copy() end
- if char == "v" then paste() end
- if char == "p" then print() end
- if char == "s" then save() end
- if char == "l" then load() end
- if char == "n" then new() end
- if char == "y" or char == "u" then undo() end
- end
- dochar = true
- elseif event == "mouse_click" then
- local button = p1
- local y = p3
- local x = p2
- if y == 1 and x >= 1 and x <= 5 then
- if showtab then
- showtab = false
- title = "\\___/"..string.sub(title, 6, -1)
- else
- showtab = true
- title = " ____"..string.sub(title, 6, -1)
- end
- elseif showtab then
- local cy = y - 2
- if cy == 1 then --save
- save()
- showtab = false
- elseif cy == 2 then --new
- new()
- showtab = false
- elseif cy == 3 then --load
- load()
- showtab = false
- elseif cy == 4 then --print
- print()
- showtab = false
- end
- else
- if button == 2 then
- firstpoint = {cursor[1], cursor[2]}
- lastpoint = {x, y}
- elseif button == 3 then
- firstpoint = nil
- lastpoint = nil
- else
- cursor[1] = x
- cursor[2] = y
- end
- end
- elseif event == "mouse_drag" then
- local x = p1
- local y = p2
- local firstpoint = {cursor[1], cursor[2]}
- local lastpoint = {x, y}
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement