Advertisement
9551

CC notepad +

Jul 2nd, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.54 KB | None | 0 0
  1. local b
  2. local updaterate
  3. local alive = true
  4. local function tdlMain(arg)
  5.     if not fs.exists("button") then
  6.         shell.run("pastebin get LTDZZZEJ button")
  7.     end
  8.     if not fs.exists("dsa") then
  9.         shell.run("pastebin get J9mhuXuf dsa")
  10.     end
  11.     if not fs.exists("startup") then
  12.         local tempfile = fs.open("startup", "w")
  13.         tempfile.writeLine('shell.run("npp")')
  14.         tempfile.close()
  15.     end
  16.     b = require("button").terminal
  17.     local data = require("dsa")
  18.     local items = {}
  19.     local startsize = {term.getSize()}
  20.     local maxitems = startsize[2] - 5
  21.     updaterate = 0.1
  22.     if not fs.exists("npp.data") then
  23.         fs.open("npp.data", "w").close()
  24.     end
  25.     local function centerWrite(text, customY)
  26.         local width = term.getSize()
  27.         term.setCursorPos(math.ceil((width / 2) - (text:len() / 2)), customY)
  28.         term.write(text)
  29.     end
  30.     term.setCursorPos(1, 1)
  31.     local function tdl()
  32.         term.setTextColor(colors.white)
  33.         centerWrite("CC notepad+", 1)
  34.         term.setTextColor(colors.gray)
  35.         centerWrite(string.rep("-", startsize[1] + 1), 2)
  36.         term.setTextColor(colors.cyan)
  37.     end
  38.     while true do
  39.         term.setBackgroundColor(colors.black)
  40.         term.clear()
  41.         tdl()
  42.         if click ~= nil then
  43.             for i = 1, maxitems do
  44.                 items[i] = data.getLine("npp.data", i)
  45.                 if items[i] == "" then
  46.                     items[i] = nil
  47.                     break
  48.                 end
  49.             end
  50.             do
  51.                 local width = term.getSize()
  52.                 local text = ">create items<"
  53.                 if b.button(1, click, math.ceil((width / 2) - (text:len() / 2)), 3, text) then
  54.                     if #items <= maxitems - 1 then
  55.                         centerWrite("make new item", 4)
  56.                         centerWrite("please enter name", 5)
  57.                         centerWrite("max length: " .. width, 6)
  58.                         local count = 1
  59.                         for i = 1, #items do
  60.                             count = count + 1
  61.                         end
  62.                         repeat
  63.                             term.setCursorPos(1, 7)
  64.                             items[count] = read()
  65.                             if #items[count] >= width then
  66.                                 term.setCursorPos(1, 7)
  67.                                 term.write("your item is too long" .. string.rep(" ", width - 21))
  68.                                 sleep(1.5)
  69.                                 term.clearLine()
  70.                             end
  71.                         until (#items[count] <= width - 1 and #items[count] > 0) or items[count] == "__exit"
  72.                         if items[count] == "__exit" then
  73.                             items[count] = string.rep("-", width + 1)
  74.                         end
  75.                         data.writeLine("npp.data", count, items[count])
  76.                     else
  77.                         centerWrite("you reached", 4)
  78.                         centerWrite("max item count", 5)
  79.                         sleep(1.5)
  80.                     end
  81.                 end
  82.                 term.setTextColor(colors.white)
  83.             end
  84.             for k, v in ipairs(items) do
  85.                 local width = term.getSize()
  86.                 local text = data.getLine("npp.data", k)
  87.                 if text:gsub(string.sub(text, 2), "") == "\127" then
  88.                     term.setTextColor(colors.orange)
  89.                 end
  90.                 local texts = text:gsub("\127", "")
  91.                 if b.button(1, click, math.ceil((width / 2) - (string.len(texts) / 2)), k + 4, texts) then
  92.                     items[k] = nil
  93.                     items = data.ser(items)
  94.                     fs.open("npp.data", "w").close()
  95.                     for i = 1, #items do
  96.                         data.writeLine("npp.data", i, items[i])
  97.                     end
  98.                 end
  99.                 term.setTextColor(colors.white)
  100.             end
  101.             for k, v in ipairs(items) do
  102.                 local width = term.getSize()
  103.                 local text = data.getLine("npp.data", k)
  104.                 if text:gsub(string.sub(text, 2), "") == "\127" then
  105.                     term.setTextColor(colors.orange)
  106.                 end
  107.                 local texts = text:gsub("\127", "")
  108.                 if b.button(2, click, math.ceil((width / 2) - (string.len(texts) / 2)), k + 4, texts) then
  109.                     if text:gsub(string.sub(text, 2), "") ~= "\127" then
  110.                         data.writeLine("npp.data", k, "\127" .. text)
  111.                     else
  112.                         data.writeLine("npp.data", k, text:gsub("\127", ""))
  113.                     end
  114.                 end
  115.                 term.setTextColor(colors.white)
  116.             end
  117.             local scale = {term.getSize()}
  118.             term.setTextColor(colors.red)
  119.             if b.button(1, click, 1, scale[2], ">exit<") then
  120.                 term.clear()
  121.                 term.setCursorPos(1, 1)
  122.                 alive = false
  123.                 break
  124.             end
  125.             term.setTextColor(colors.yellow)
  126.             if b.button(1, click, scale[1] - 11, scale[2], ">update npp<") then
  127.                 shell.run("rename npp npp.old")
  128.                 shell.run("pastebin get yU5gzMZ1 npp")
  129.                 shell.run("rm npp.old")
  130.                 shell.run("npp")
  131.                 break
  132.             end
  133.             if b.button(1, click, scale[1] - 4, 1, ">\16\b<") then
  134.                 shell.run("edit npp.data")
  135.             end
  136.             if b.button(2, click, scale[1] - 4, 1, ">\16\b<") then
  137.                 term.clear()
  138.                 tdl()
  139.                 local run, err = loadfile("npp.data", "bt", _ENV)
  140.                 if not err then
  141.                     ok, err = pcall(run)
  142.                 end
  143.                 if not ok then
  144.                     local width = term.getSize()
  145.                     for i = 3, width - 1 do
  146.                         term.setCursorPos(term.getCursorPos(), i)
  147.                         term.clearLine()
  148.                     end
  149.                     tdl()
  150.                     centerWrite("your code has crashed", 4)
  151.                     centerWrite("reason:", 5)
  152.                     term.setCursorPos(1, 6)
  153.                     print(err or "unknown")
  154.                     sleep(1.5)
  155.                 end
  156.                 sleep(1)
  157.             end
  158.         end
  159.         sleep(0.1)
  160.     end
  161. end
  162. local updatec = function()
  163.     while alive do
  164.         if b then click = b.timetouch(updaterate)
  165.         else sleep(1) end
  166.     end
  167. end
  168. local arg = ...
  169. if arg == "update" then
  170.     shell.run("rename npp npp.old")
  171.     shell.run("pastebin get yU5gzMZ1 npp")
  172.     shell.run("rm npp.old")
  173.     shell.run("npp")
  174. end
  175. local ok, err = pcall(parallel.waitForAll,tdlMain,updatec)
  176. if not ok then
  177.     term.clear()
  178.     term.setCursorPos(1, 1)
  179.     if string.sub(err, 9) ~= "invalid pattern capture" then
  180.         print(
  181.             "CC notepad+ has crashed\nreason:\n" ..
  182.                 err .. "\nhold Q for 1 second to start again\n or press anything else\nto exit"
  183.         )
  184.         local _, event = os.pullEvent("char")
  185.         if event == "q" then
  186.             sleep(1)
  187.             local _, event = os.pullEvent("char")
  188.             if event == "q" then
  189.                 shell.run("npp")
  190.             end
  191.         end
  192.     else
  193.         print("data storage corruted\nmaking copy\nand deleting")
  194.         shell.run("copy npp.data npp.data." .. tostring(math.random(1, 100000)))
  195.         shell.run("rm npp.data")
  196.         sleep(2)
  197.         shell.run("npp")
  198.     end
  199. end
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement