VITE/vite.lua

198 lines
5.2 KiB
Lua
Raw Permalink Normal View History

2022-04-07 14:03:54 -04:00
--Vi Inspired Textual Editor === VITE
2022-04-08 18:44:31 -04:00
-- Version 0.05
-- by: ASCII Benefactor
-- April 2022
2022-04-21 14:21:24 -04:00
-------------------------------------
2022-04-08 18:44:31 -04:00
filename="dummy.txt"
2022-04-08 05:28:46 -04:00
VER="v0.05"
2022-04-07 14:03:54 -04:00
nc=require("nocurses")
2022-04-08 05:28:46 -04:00
dofile("box.lua")
2022-04-21 14:21:24 -04:00
-- dofile("func.lua")
2022-04-07 14:03:54 -04:00
2022-04-08 18:44:31 -04:00
-- Small Funcs Start ------------------------------
2022-04-07 14:03:54 -04:00
function at(s,p)
return string.sub(s,p,p)
end
2022-04-21 14:21:24 -04:00
function len(s)
return string.len(s)
end
2022-04-08 18:44:31 -04:00
----------------------------end small funcs ------
2022-04-07 14:03:54 -04:00
function start()
nc.setunderline(true)
2022-04-08 05:28:46 -04:00
print("VITE === Vi Inspired Text Editor === "..VER)
2022-04-07 14:03:54 -04:00
nc.setunderline(false)
currline=-1
currfile=-1
lines_tbl={}
cmd()
--after closing
if currfile ~= -1 then
2022-04-08 05:28:46 -04:00
print("Closing VITE...bye!")
2022-04-07 14:03:54 -04:00
currfile:close()
2022-04-08 05:28:46 -04:00
else
print("Closing VITE...bye!")
2022-04-07 14:03:54 -04:00
end
end
-- list of Commands
-- ----------------
-- * = newfile
-- > = appendtext
2022-04-08 05:28:46 -04:00
-- : = main commands
-- = = Display pending lines
-- ? = Display Help
2022-04-07 14:03:54 -04:00
function cmd()
if currline== -1 then
print("No file opened")
end
2022-04-08 05:28:46 -04:00
while true do
---------------------- main loop
if currline ~= -1 then print(currline..":") end -- InfoLine TM
2022-04-07 14:03:54 -04:00
nc.setfontcolor("YELLOW")
inp = io.input():read("*l")
nc.setfontcolor("WHITE")
2022-04-21 14:21:24 -04:00
-- all commands start with a symbol
2022-04-07 14:03:54 -04:00
if at(inp,1)=="*" and string.len(inp)<=1 then
print("You must enter a valid filename...")
elseif at(inp,1)=="*" and string.len(inp)>1 then
newfn=string.sub(inp,2)
new_file(newfn)
print("New File:"..newfn.."\n")
currline=1
elseif at(inp,1) == ">" and string.len(inp)==1 then
2022-04-08 05:28:46 -04:00
-- print("line [#"..currline.."]:")
table.insert(lines_tbl,currline,"")
currline=currline+1
2022-04-21 14:21:24 -04:00
-- if inp has > symbol, then its a literal line to write to file
2022-04-07 14:03:54 -04:00
elseif at(inp,1) == ">" and string.len(inp)>1 then
table.insert(lines_tbl,currline,string.sub(inp,2))
currline=currline+1
elseif at(inp,1) == ":" and at(inp,2)=="l" and string.len(inp)>2 then
-- TODO MAKE LOAD FUNC TODO
elseif at(inp,1) == ":" and at(inp,2)=="w" then
write_file()
elseif at(inp,1) == "=" and string.len(inp)==1 then
display_text()
2022-04-21 14:21:24 -04:00
elseif at(inp,1)==":" and at(inp,2)=="g" and at(inp,3)=="o" and string.match(inp,"%d+")~= nil then
currline=string.match(inp,"%d+")
elseif at(inp,1)=="x" and #inp==1 then
2022-04-08 05:28:46 -04:00
elseif at(inp,1)=="?" and #inp==1 then
2022-04-08 18:44:31 -04:00
print_help()
2022-04-08 05:28:46 -04:00
elseif at(inp,1)==":" and at(inp,2)=="q" then
break
2022-04-07 14:03:54 -04:00
else
2022-04-08 05:28:46 -04:00
-- catchall --
2022-04-21 14:21:24 -04:00
if inp=="" then
else
nc.setfontcolor("RED")
print("Unknown Command!")
nc.setfontcolor("WHITE")
end
2022-04-08 05:28:46 -04:00
-- end of command defs =========================
2022-04-21 14:21:24 -04:00
end
2022-04-07 14:03:54 -04:00
end
end
function add_line(lpos,str)
local str,lpos
if currfile~=-1 then
table.insert(lines_tbl,lpos,str)
else
print("No file Open!")
end
end
function write_file() -- flush data to the file":w"
if currfile~=-1 then
currfile:write(table.concat(lines_tbl,"\n").."\n")
print("Flushed buffer unto "..newfn)
2022-04-08 05:28:46 -04:00
-- TODO Show filesize XXX
2022-04-07 14:03:54 -04:00
else
print("No file to write to !")
end
end
function new_file(fn) --also load
if currfile==-1 then
currfile = io.open(fn,"w+")
elseif currfile~=-1 then
2022-04-08 05:28:46 -04:00
nc.setfontcolor("RED")
print("Confirm Close file?")
nc.setfontcolor("WHITE")
if nc.getch()=="y" or nc.getch()=="Y" then
currfile:close()
currfile = io.open(fn,"w+")
elseif nc.getch()=="n" then
2022-04-21 14:21:24 -04:00
return 0
2022-04-08 05:28:46 -04:00
end
2022-04-07 14:03:54 -04:00
end
end
2022-04-08 05:28:46 -04:00
function display_file() -- Displays file proper
2022-04-07 14:03:54 -04:00
currfile:seek("set")
local lnum = 1
for line in currfile:lines() do
nc.gotoxy(1,lnum)
print(lnum..":"..line)
lnum=lnum+1
end
end
2022-04-08 05:28:46 -04:00
2022-04-08 18:44:31 -04:00
function print_help()
2022-04-21 14:21:24 -04:00
local k
2022-04-08 18:44:31 -04:00
nc.clrscr()
nc.setfontcolor("GREEN")
print(" VITE commands ")
2022-04-21 14:21:24 -04:00
print("===================================")
print("* new file ")
print("> insert line")
print("= Show lines")
print(": main commands")
print("enter any key to continue..." )
while k == null do
k=nc.getch()
end
nc.clrscr()
print(" Main ':' commands ")
print("===================================")
print(":q quit")
print(":w Write pending files to file")
print("k Show lines")
print(": main commands")
print("enter any key to continue..." )
while k == null do
k=nc.getch()
end
print("End of Help")
2022-04-08 18:44:31 -04:00
nc.setfontcolor("WHITE")
end
2022-04-21 14:21:24 -04:00
function rem_line(lnum)
2022-04-08 18:44:31 -04:00
2022-04-21 14:21:24 -04:00
end
2022-04-08 05:28:46 -04:00
function display_text() -- prints lines_tbl (bef flush)
2022-04-07 14:03:54 -04:00
currfile:seek("set")
local line
nc.clrscr()
nc.setfontcolor("CYAN")
2022-04-21 14:21:24 -04:00
for lnum,lstr in pairs(lines_tbl) do
2022-04-08 05:28:46 -04:00
-- nc.gotoxy(1,k)
2022-04-21 14:21:24 -04:00
print(lnum..":"..lstr)
2022-04-07 14:03:54 -04:00
end
print("=================================\n\n")
nc.setfontcolor("WHITE")
end
2022-04-21 14:21:24 -04:00
-- Program Loop Begin
2022-04-07 14:03:54 -04:00
start()