--Vi Inspired Textual Editor === VITE -- Version alpha 0.02 filename="./dummy.txt" VER="v0.02" nc=require("nocurses") function at(s,p) return string.sub(s,p,p) end ----------------------------end small funcs function start() nc.setunderline(true) print("VITE === Vi Inspired Textual Editor === "..VER) nc.setunderline(false) currline=-1 currfile=-1 lines_tbl={} cmd() --after closing if currfile ~= -1 then print("closing vite...bye") currfile:close() end end -- list of Commands -- ---------------- -- * = newfile -- > = appendtext -- : = commands -- function cmd() while inp~=":q" do -- main loop if currline== -1 then print("No file opened") elseif currline~=-1 then print(currline..":") end nc.setfontcolor("YELLOW") inp = io.input():read("*l") nc.setfontcolor("WHITE") -- all commands start with a symbol 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 print("line [#"..currline.."]:") -- if inp has > symbol, then its a literal line to write to file 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() elseif string.match(inp,"[%d*][%>][%g*]") and string.len(inp)>2 then add_line(lpos,str) elseif at(inp,1)=="x" and at(inp,2)=="!" then break else nc.setfontcolor("RED") print("Unknown Command!") nc.setfontcolor("WHITE") -- end of command def ========== end 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) -- TODO Show filesize 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 currfile:close() -- XXX Make it ask for confirm -- XXX make it display whats going on currfile = io.open(fn,"w+") end end function display_file() currfile:seek("set") local lnum = 1 for line in currfile:lines() do nc.gotoxy(1,lnum) print(lnum..":"..line) lnum=lnum+1 end print("=================================\n\n") end function display_text() -- prints lines_tbl currfile:seek("set") local line nc.clrscr() nc.setfontcolor("CYAN") for k,ln in pairs(lines_tbl) do nc.gotoxy(1,k) print(k..":"..ln) end print("=================================\n\n") nc.setfontcolor("WHITE") end start()