mirror of
https://github.com/LDDestroier/CC/
synced 2025-04-27 21:13:10 +00:00
Added CTRL+S and Q, alt+scrolling
gotta add those selections later
This commit is contained in:
parent
2e289f23bc
commit
b3eca8588c
126
eldit.lua
126
eldit.lua
@ -1,10 +1,12 @@
|
|||||||
-- eldit
|
-- Eldit (still being made)
|
||||||
-- by lddestroier
|
-- by LDDestroier
|
||||||
-- wget https://raw.githubusercontent.com/LDDestroier/CC/master/eldit.lua
|
-- wget https://raw.githubusercontent.com/LDDestroier/CC/master/eldit.lua
|
||||||
|
|
||||||
local scr_x, scr_y = term.getSize()
|
local scr_x, scr_y = term.getSize()
|
||||||
|
local tArg = {...}
|
||||||
|
|
||||||
local eldit = {}
|
local eldit = {}
|
||||||
|
eldit.filename = tArg[1]
|
||||||
eldit.buffer = {{}}
|
eldit.buffer = {{}}
|
||||||
eldit.scrollX = 0
|
eldit.scrollX = 0
|
||||||
eldit.scrollY = 0
|
eldit.scrollY = 0
|
||||||
@ -48,15 +50,40 @@ local explode = function(div, str, replstr, includeDiv)
|
|||||||
return arr
|
return arr
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local readFile = function(path)
|
||||||
|
if fs.exists(path) then
|
||||||
|
local file = fs.open(path, "r")
|
||||||
|
local contents = file.readAll()
|
||||||
|
file.close()
|
||||||
|
return contents
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local writeFile = function(path, contents)
|
||||||
|
if fs.isReadOnly(path) or fs.isDir(path) then
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
local file = fs.open(path, "w")
|
||||||
|
file.write(contents)
|
||||||
|
file.close()
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
prompt = function(prebuffer)
|
prompt = function(prebuffer)
|
||||||
local keysDown = {}
|
local keysDown = {}
|
||||||
local miceDown = {}
|
local miceDown = {}
|
||||||
|
local defaultBarLife = 10
|
||||||
|
local barmsg = "Started Eldit."
|
||||||
|
local barlife = defaultBarLife
|
||||||
if type(prebuffer) == "string" then
|
if type(prebuffer) == "string" then
|
||||||
for i = 1, #prebuffer do
|
for i = 1, #prebuffer do
|
||||||
if prebuffer:sub(i,i) == "\n" then
|
if prebuffer:sub(i,i) == "\n" then
|
||||||
eldit.buffer[#eldit.buffer + 1] = {}
|
eldit.buffer[#eldit.buffer + 1] = {}
|
||||||
else
|
else
|
||||||
eldit.buffer[#eldit.buffer] = prebuffer:sub(i,i)
|
eldit.buffer[#eldit.buffer][#eldit.buffer[#eldit.buffer] + 1] = prebuffer:sub(i,i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif type(prebuffer) == "table" then
|
elseif type(prebuffer) == "table" then
|
||||||
@ -122,10 +149,14 @@ prompt = function(prebuffer)
|
|||||||
term.setCursorPos(eldit.size.x, eldit.size.y + eldit.size.height - 1)
|
term.setCursorPos(eldit.size.x, eldit.size.y + eldit.size.height - 1)
|
||||||
term.setBackgroundColor(colors.gray)
|
term.setBackgroundColor(colors.gray)
|
||||||
eClearLine()
|
eClearLine()
|
||||||
|
if barlife > 0 then
|
||||||
|
term.write(barmsg)
|
||||||
|
else
|
||||||
for id,cur in pairs(eldit.cursors) do
|
for id,cur in pairs(eldit.cursors) do
|
||||||
term.write("(" .. cur.x .. "," .. cur.y .. ") ")
|
term.write("(" .. cur.x .. "," .. cur.y .. ") ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local scrollToCursor = function()
|
local scrollToCursor = function()
|
||||||
local lowCur, highCur = eldit.cursors[1], eldit.cursors[1]
|
local lowCur, highCur = eldit.cursors[1], eldit.cursors[1]
|
||||||
@ -154,11 +185,20 @@ prompt = function(prebuffer)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local adjustScroll = function(amount)
|
local getMaximumWidth = function()
|
||||||
|
local maxX = 0
|
||||||
|
for y = 1, #eldit.buffer do
|
||||||
|
maxX = math.max(maxX, #eldit.buffer[y])
|
||||||
|
end
|
||||||
|
return maxX
|
||||||
|
end
|
||||||
|
|
||||||
|
local adjustScroll = function(modx, mody)
|
||||||
|
if mody then
|
||||||
eldit.scrollY = math.min(
|
eldit.scrollY = math.min(
|
||||||
math.max(
|
math.max(
|
||||||
0,
|
0,
|
||||||
eldit.scrollY + amount
|
eldit.scrollY + mody
|
||||||
),
|
),
|
||||||
math.max(
|
math.max(
|
||||||
0,
|
0,
|
||||||
@ -166,6 +206,19 @@ prompt = function(prebuffer)
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
if modx then
|
||||||
|
eldit.scrollX = math.min(
|
||||||
|
math.max(
|
||||||
|
0,
|
||||||
|
eldit.scrollX + modx
|
||||||
|
),
|
||||||
|
math.max(
|
||||||
|
0,
|
||||||
|
getMaximumWidth() - eldit.size.width + 1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local removeRedundantCursors = function()
|
local removeRedundantCursors = function()
|
||||||
local xes = {}
|
local xes = {}
|
||||||
@ -332,8 +385,22 @@ prompt = function(prebuffer)
|
|||||||
scrollToCursor()
|
scrollToCursor()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
saveFile = function()
|
||||||
|
local compiled = ""
|
||||||
|
for y = 1, #eldit.buffer do
|
||||||
|
compiled = compiled .. table.concat(eldit.buffer[y])
|
||||||
|
if y < #eldit.buffer then
|
||||||
|
compiled = compiled .. "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
writeFile(eldit.filename, compiled)
|
||||||
|
barmsg = "Saved to '" .. eldit.filename .. "'."
|
||||||
|
barlife = defaultBarLife
|
||||||
|
end
|
||||||
|
|
||||||
local evt
|
local evt
|
||||||
local tID = os.startTimer(0.5)
|
local tID = os.startTimer(0.5)
|
||||||
|
local bartID = os.startTimer(0.1)
|
||||||
local doRender = true
|
local doRender = true
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
@ -347,12 +414,35 @@ prompt = function(prebuffer)
|
|||||||
end
|
end
|
||||||
isCursorBlink = not isCursorBlink
|
isCursorBlink = not isCursorBlink
|
||||||
doRender = true
|
doRender = true
|
||||||
|
elseif evt[2] == bartID then
|
||||||
|
bartID = os.startTimer(0.1)
|
||||||
|
barlife = math.max(0, barlife - 1)
|
||||||
end
|
end
|
||||||
elseif evt[1] == "char" or evt[1] == "paste" then
|
elseif evt[1] == "char" or evt[1] == "paste" then
|
||||||
placeText(evt[2])
|
placeText(evt[2])
|
||||||
doRender = true
|
doRender = true
|
||||||
elseif evt[1] == "key" then
|
elseif evt[1] == "key" then
|
||||||
keysDown[evt[2]] = true
|
keysDown[evt[2]] = true
|
||||||
|
if keysDown[keys.leftCtrl] or keysDown[keys.leftCtrl] then
|
||||||
|
|
||||||
|
if evt[2] == keys.backspace then
|
||||||
|
deleteText("word", "backward")
|
||||||
|
doRender, isCursorBlink = true, false
|
||||||
|
|
||||||
|
elseif evt[2] == keys.delete then
|
||||||
|
deleteText("word", "forward")
|
||||||
|
doRender, isCursorBlink = true, false
|
||||||
|
|
||||||
|
elseif evt[2] == keys.q then
|
||||||
|
return "exit"
|
||||||
|
|
||||||
|
elseif evt[2] == keys.s then
|
||||||
|
saveFile()
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
if evt[2] == keys.insert then
|
if evt[2] == keys.insert then
|
||||||
isInsert = not isInsert
|
isInsert = not isInsert
|
||||||
doRender, isCursorBlink = true, true
|
doRender, isCursorBlink = true, true
|
||||||
@ -386,11 +476,11 @@ prompt = function(prebuffer)
|
|||||||
doRender = true
|
doRender = true
|
||||||
|
|
||||||
elseif evt[2] == keys.backspace then
|
elseif evt[2] == keys.backspace then
|
||||||
deleteText(keysDown[keys.leftCtrl] and "word" or "single", "backward")
|
deleteText("single", "backward")
|
||||||
doRender, isCursorBlink = true, false
|
doRender, isCursorBlink = true, false
|
||||||
|
|
||||||
elseif evt[2] == keys.delete then
|
elseif evt[2] == keys.delete then
|
||||||
deleteText(keysDown[keys.leftCtrl] and "word" or "single", keysDown[keys.leftCtrl] and "forward")
|
deleteText("single", nil)
|
||||||
doRender, isCursorBlink = true, false
|
doRender, isCursorBlink = true, false
|
||||||
|
|
||||||
elseif evt[2] == keys.left then
|
elseif evt[2] == keys.left then
|
||||||
@ -410,6 +500,8 @@ prompt = function(prebuffer)
|
|||||||
doRender, isCursorBlink = true, true
|
doRender, isCursorBlink = true, true
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
elseif evt[1] == "key_up" then
|
elseif evt[1] == "key_up" then
|
||||||
keysDown[evt[2]] = nil
|
keysDown[evt[2]] = nil
|
||||||
elseif evt[1] == "mouse_click" then
|
elseif evt[1] == "mouse_click" then
|
||||||
@ -436,7 +528,11 @@ prompt = function(prebuffer)
|
|||||||
miceDown[evt[2]] = nil
|
miceDown[evt[2]] = nil
|
||||||
elseif evt[1] == "mouse_scroll" then
|
elseif evt[1] == "mouse_scroll" then
|
||||||
local amount = (keysDown[keys.leftCtrl] and eldit.size.height or 1) * evt[2]
|
local amount = (keysDown[keys.leftCtrl] and eldit.size.height or 1) * evt[2]
|
||||||
adjustScroll(amount)
|
if keysDown[keys.leftAlt] then
|
||||||
|
adjustScroll(amount, 0)
|
||||||
|
else
|
||||||
|
adjustScroll(0, amount)
|
||||||
|
end
|
||||||
doRender = true
|
doRender = true
|
||||||
end
|
end
|
||||||
if doRender then
|
if doRender then
|
||||||
@ -446,4 +542,16 @@ prompt = function(prebuffer)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
prompt()
|
if not eldit.filename then
|
||||||
|
print("eldit [filename]")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local contents = readFile(eldit.filename)
|
||||||
|
|
||||||
|
local result = {prompt(contents)}
|
||||||
|
if result[1] == "exit" then
|
||||||
|
term.setBackgroundColor(colors.black)
|
||||||
|
term.scroll(1)
|
||||||
|
term.setCursorPos(1, scr_y)
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user