Save file working and lots of bugs fixed and text changed

This commit is contained in:
sloumdrone 2019-01-04 22:24:40 -08:00
parent 02fa37e645
commit 540091c109
1 changed files with 89 additions and 29 deletions

118
main.lua
View File

@ -1,11 +1,11 @@
#!/usr/bin/env lua
local version = 'v0.3.5'
local version = 'v0.6.0'
local socket = require("socket")
local urlparser = require("socket.url")
local separator = '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
local separator = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
local valid_types = {'0', '1', 'i'}
session = { history={ loc=0, items={} }, current_links = {}, favorites={} }
local save_file = string.format('%s/.stubbfaves', assert(os.getenv('HOME'), 'Unable to get home directory'))
local save_file_location = string.format('%s/.stubbfaves', assert(os.getenv('HOME'), 'Unable to get home directory'))
function string.split(str, sep)
@ -49,6 +49,7 @@ table.has = function(t, item)
return false
end
table.truncate = function(t, i)
while t[i] do
table.remove(t, i)
@ -123,7 +124,6 @@ function mainloop()
print(bar)
-- print('\27[7m (G)o to url, (B)ack, (V)isit link, (Q)uit \27[0m')
local key = string.lower(getch())
io.write('\n')
if key == 'q' then
save_favorites()
os.exit(0)
@ -139,8 +139,12 @@ function mainloop()
show_favorites()
elseif key == 'u' then
update_favorite()
elseif key == 'r' then
elseif key == 'd' then
remove_favorite()
elseif key == 's' then
save_file()
elseif key == 'r' then
refresh()
elseif key == 'g' then
io.write('Enter gopher url > ')
local url = io.read()
@ -156,11 +160,18 @@ function mainloop()
end
function refresh()
if session.history.loc > 0 then
go_to_url(session.history.items[session.history.loc])
end
end
function add_favorite()
if #session.history.items == 0 then return false end
local title
repeat
io.write('Enter title for favorite (! to cancel): ')
io.write('Enter title for bookmark (! to cancel): ')
title = io.read()
until title
if title == '!' then return print('Cancelled') end
@ -170,7 +181,7 @@ end
function update_favorite()
local title
io.write('Enter the favorite id (! to cancel) > ')
io.write('Enter the bookmark id (! to cancel) > ')
local favid = io.read()
if favid == '!' then return print('Cancelled') end
local id = tonumber(favid) or tonumber(string.sub(favid,2))
@ -184,18 +195,18 @@ function update_favorite()
if title == '!' then return print('Cancelled') end
end
item.display = title
print('Favorite has been updated')
print('Bookmark has been updated')
else
print('Invalid favorite id')
print('Invalid bookmark id')
end
else
print('Invalid favorite id')
print('Invalid bookmark id')
end
end
function remove_favorite()
io.write('Enter the favorite id (! to cancel) > ')
io.write('Enter the bookmark id (! to cancel) > ')
local favid = io.read()
if favid == '!' then return print('Cancelled') end
local id = tonumber(favid) or tonumber(string.sub(favid,2))
@ -203,31 +214,38 @@ function remove_favorite()
local item = session.favorites[id]
if item then
table.remove(session.favorites, id)
print('Favorite has been removed')
print('Bookmark has been removed')
else
print('Invalid favorite id')
print('Invalid bookmark id')
end
else
print('Invalid favorite id')
print('Invalid bookmark id')
end
end
function visit_link()
local linkid
local favorite
repeat
favorite = false
io.write('Enter link id (0 to cancel) > ')
io.write('Enter link id (! to cancel) > ')
local inp = io.read()
if inp == '!' then return print('Cancelled') end
linkid = tonumber(inp)
if not linkid and string.sub(inp, 1, 1) == 'f' then
linkid = tonumber(string.sub(inp, 2, 2))
linkid = tonumber(string.sub(inp, 2))
favorite = true
end
until linkid
local linkurl
if favorite then
linkurl = session.favorites[linkid].link
selection = session.favorites[linkid]
if selection then
linkurl = session.favorites[linkid].link
else
return print('Invalid bookmark id')
end
else
linkurl = session.current_links[linkid]
end
@ -256,17 +274,60 @@ function forward()
end
function print_help()
print(separator)
print('\nStubb ' .. version)
print('\nA text based gopher client that\nsupports gophertypes 0, 1, and 7\n(text, gopher map, search).')
print('\nCommands:\n')
print('(G)o to url\n(V)iew link id\n(L)ist favorites\n(A)dd to favorites\n(R)emove from favorites\n(B)ack\n(F)orward\n(S)ave to disk\n(H)elp\n(Q)uit\n')
local helpstring = [[
Stubb is a text based gopher client that only displays and supports gophertypes:
0 (text) and 1 (gopher map). The application is named for the character Stubb
from Moby Dick known for his imaginative patter and good humor.
Commands:
<Navigation>
(G)o to url
(V)iew link (by id)
(B)ack
(F)orward
(R)eload current file
<Bookmarks>
(A)dd bookmark for current page
(L)ist bookmarks
(U)pdate bookmark (by id)
(D)elete bookmark (by id)
<System>
(H)elp
(S)ave file as...
(Q)uit Stubb
]]
print(helpstring)
end
function print_gopher_error(text)
print(separator)
print(text)
print(separator)
end
function save_file()
if not session.filedata then return print('No file available. Save canceled.') end
local homedir = assert(os.getenv('HOME'), 'Unable to find home directory')
io.write('Enter the filename to save as (! to cancel): ' .. homedir .. '/')
local filename = io.read()
if filename == '!' or filename == '' then
return print('Cancelled')
elseif string.match(filename, '^[%.%w_-]+$') then
local filepath = homedir .. '/' .. filename
local f = io.open(filepath, 'w+')
f:write(table.concat(session.filedata,'\n'))
f:close()
print(string.format('File saved as: %s', filepath))
else
print('Invalid filename')
end
end
@ -275,13 +336,12 @@ function go_to_url(u, add2history)
if not url.host or url.scheme ~= 'gopher' then
print(separator)
print(' I n v a l i d u r l')
print(separator)
elseif table.has(valid_types, url.gophertype) then
filedata = request_gopher(url.host, url.port, url.path)
print(separator)
handle_response(filedata, url.gophertype)
session.filedata = request_gopher(url.host, url.port, url.path)
print(separator)
handle_response(session.filedata, url.gophertype)
local o = url.scheme .. '://' .. url.host .. ':' .. url.port .. '/' .. url.gophertype .. url.path
print('\n\n')
print(o)
if add2history then
table.truncate(session.history.items, session.history.loc + 1)
@ -332,7 +392,7 @@ end
function show_favorites()
print(separator)
print('\n >---F A V O R I T E S--->\n\n')
print('\n >---B O O K M A R K S--->\n\n')
for i, v in ipairs(session.favorites) do
print(string.format(' (f%d) %s', i, v.display))
end
@ -341,7 +401,7 @@ end
function save_favorites()
local file = io.open(save_file, 'w+')
local file = io.open(save_file_location, 'w+')
io.output(file)
io.write("session.favorites = {\n")
for _,v in ipairs(session.favorites) do
@ -361,8 +421,8 @@ end
-- run the program
if file_exists(save_file) then
dofile(save_file)
if file_exists(save_file_location) then
dofile(save_file_location)
end
mainloop()
-- end of program