Minor Improvements v0.05

This commit is contained in:
asciibene 2022-04-08 05:28:46 -04:00
parent 0d67113c37
commit a8d346dd70
1 changed files with 40 additions and 23 deletions

View File

@ -1,9 +1,9 @@
--Vi Inspired Textual Editor === VITE --Vi Inspired Textual Editor === VITE
-- Version alpha 0.02 -- Version alpha 0.02
filename="./dummy.txt" filename="./dummy.txt"
VER="v0.02" VER="v0.05"
nc=require("nocurses") nc=require("nocurses")
dofile("box.lua")
function at(s,p) function at(s,p)
return string.sub(s,p,p) return string.sub(s,p,p)
@ -12,8 +12,7 @@ end
----------------------------end small funcs ----------------------------end small funcs
function start() function start()
nc.setunderline(true) nc.setunderline(true)
print("VITE === Vi Inspired Textual Editor === "..VER) print("VITE === Vi Inspired Text Editor === "..VER)
nc.setunderline(false) nc.setunderline(false)
currline=-1 currline=-1
currfile=-1 currfile=-1
@ -21,24 +20,26 @@ function start()
cmd() cmd()
--after closing --after closing
if currfile ~= -1 then if currfile ~= -1 then
print("closing vite...bye") print("Closing VITE...bye!")
currfile:close() currfile:close()
else
print("Closing VITE...bye!")
end end
end end
-- list of Commands -- list of Commands
-- ---------------- -- ----------------
-- * = newfile -- * = newfile
-- > = appendtext -- > = appendtext
-- : = commands -- : = main commands
-- -- = = Display pending lines
-- ? = Display Help
function cmd() function cmd()
while inp~=":q" do
-- main loop
if currline== -1 then if currline== -1 then
print("No file opened") print("No file opened")
elseif currline~=-1 then
print(currline..":")
end end
while true do
---------------------- main loop
if currline ~= -1 then print(currline..":") end -- InfoLine TM
nc.setfontcolor("YELLOW") nc.setfontcolor("YELLOW")
inp = io.input():read("*l") inp = io.input():read("*l")
nc.setfontcolor("WHITE") nc.setfontcolor("WHITE")
@ -51,7 +52,9 @@ function cmd()
print("New File:"..newfn.."\n") print("New File:"..newfn.."\n")
currline=1 currline=1
elseif at(inp,1) == ">" and string.len(inp)==1 then elseif at(inp,1) == ">" and string.len(inp)==1 then
print("line [#"..currline.."]:") -- print("line [#"..currline.."]:")
table.insert(lines_tbl,currline,"")
currline=currline+1
-- if inp has > symbol, then its a literal line to write to file -- if inp has > symbol, then its a literal line to write to file
elseif at(inp,1) == ">" and string.len(inp)>1 then elseif at(inp,1) == ">" and string.len(inp)>1 then
table.insert(lines_tbl,currline,string.sub(inp,2)) table.insert(lines_tbl,currline,string.sub(inp,2))
@ -62,15 +65,22 @@ function cmd()
write_file() write_file()
elseif at(inp,1) == "=" and string.len(inp)==1 then elseif at(inp,1) == "=" and string.len(inp)==1 then
display_text() display_text()
elseif string.match(inp,"[%d*][%>][%g*]") and string.len(inp)>2 then elseif string.match(inp,"%d+%>%g+") and string.len(inp)>3 then
-- FIXME FIXME FIXME ^^^^^^
lpos=string.match(inp,"%d")
add_line(lpos,str) add_line(lpos,str)
elseif at(inp,1)=="x" and at(inp,2)=="!" then elseif at(inp,1)=="x" and at(inp,2)=="!" then
break break
elseif at(inp,1)=="?" and #inp==1 then
print("Help")
elseif at(inp,1)==":" and at(inp,2)=="q" then
break
else else
-- catchall --
nc.setfontcolor("RED") nc.setfontcolor("RED")
print("Unknown Command!") print("Unknown Command!")
nc.setfontcolor("WHITE") nc.setfontcolor("WHITE")
-- end of command def ========== -- end of command defs =========================
end end
end end
@ -91,7 +101,7 @@ function write_file() -- flush data to the file":w"
if currfile~=-1 then if currfile~=-1 then
currfile:write(table.concat(lines_tbl,"\n").."\n") currfile:write(table.concat(lines_tbl,"\n").."\n")
print("Flushed buffer unto "..newfn) print("Flushed buffer unto "..newfn)
-- TODO Show filesize -- TODO Show filesize XXX
else else
print("No file to write to !") print("No file to write to !")
end end
@ -101,14 +111,20 @@ function new_file(fn) --also load
if currfile==-1 then if currfile==-1 then
currfile = io.open(fn,"w+") currfile = io.open(fn,"w+")
elseif currfile~=-1 then elseif currfile~=-1 then
currfile:close() nc.setfontcolor("RED")
-- XXX Make it ask for confirm print("Confirm Close file?")
-- XXX make it display whats going on nc.setfontcolor("WHITE")
currfile = io.open(fn,"w+") if nc.getch()=="y" or nc.getch()=="Y" then
currfile:close()
currfile = io.open(fn,"w+")
elseif nc.getch()=="n" then
anxietyattack=true
pleb=true
end
end end
end end
function display_file() function display_file() -- Displays file proper
currfile:seek("set") currfile:seek("set")
local lnum = 1 local lnum = 1
for line in currfile:lines() do for line in currfile:lines() do
@ -118,18 +134,19 @@ function display_file()
end end
print("=================================\n\n") print("=================================\n\n")
end end
function display_text() -- prints lines_tbl
function display_text() -- prints lines_tbl (bef flush)
currfile:seek("set") currfile:seek("set")
local line local line
nc.clrscr() nc.clrscr()
nc.setfontcolor("CYAN") nc.setfontcolor("CYAN")
for k,ln in pairs(lines_tbl) do for k,ln in pairs(lines_tbl) do
nc.gotoxy(1,k) -- nc.gotoxy(1,k)
print(k..":"..ln) print(k..":"..ln)
end end
print("=================================\n\n") print("=================================\n\n")
nc.setfontcolor("WHITE") nc.setfontcolor("WHITE")
end end
start() start()