Finished getting favorites working
This commit is contained in:
parent
d90f8ff97d
commit
02fa37e645
126
main.lua
126
main.lua
|
@ -23,7 +23,10 @@ function string.split(str, sep)
|
||||||
out = string.sub(str, start, point - 1)
|
out = string.sub(str, start, point - 1)
|
||||||
end
|
end
|
||||||
table.insert(t, out)
|
table.insert(t, out)
|
||||||
start = point and point + 1 or break
|
start = point and point + 1 or nil
|
||||||
|
if not start then
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
@ -131,14 +134,13 @@ function mainloop()
|
||||||
elseif key == 'f' then
|
elseif key == 'f' then
|
||||||
forward()
|
forward()
|
||||||
elseif key == 'a' then
|
elseif key == 'a' then
|
||||||
local title
|
add_favorite()
|
||||||
repeat
|
|
||||||
io.write('Enter title for favorite: ')
|
|
||||||
title = io.read()
|
|
||||||
until title
|
|
||||||
table.insert(session.favorites,{display = title, link = session.history.items[session.history.loc]})
|
|
||||||
elseif key == 'l' then
|
elseif key == 'l' then
|
||||||
show_favorites()
|
show_favorites()
|
||||||
|
elseif key == 'u' then
|
||||||
|
update_favorite()
|
||||||
|
elseif key == 'r' then
|
||||||
|
remove_favorite()
|
||||||
elseif key == 'g' then
|
elseif key == 'g' then
|
||||||
io.write('Enter gopher url > ')
|
io.write('Enter gopher url > ')
|
||||||
local url = io.read()
|
local url = io.read()
|
||||||
|
@ -148,36 +150,96 @@ function mainloop()
|
||||||
print('\n')
|
print('\n')
|
||||||
end
|
end
|
||||||
elseif key == 'v' then
|
elseif key == 'v' then
|
||||||
local linkid
|
visit_link()
|
||||||
local favorite
|
|
||||||
repeat
|
|
||||||
favorite = false
|
|
||||||
io.write('Enter link id (0 to cancel) > ')
|
|
||||||
local inp = io.read()
|
|
||||||
linkid = tonumber(inp)
|
|
||||||
if not linkid and string.sub(inp, 1, 1) == 'f' then
|
|
||||||
linkid = tonumber(string.sub(inp, 2, 2))
|
|
||||||
favorite = true
|
|
||||||
end
|
|
||||||
until linkid
|
|
||||||
local linkurl
|
|
||||||
if favorite then
|
|
||||||
linkurl = session.favorites[linkid].link
|
|
||||||
else
|
|
||||||
linkurl = session.current_links[linkid]
|
|
||||||
end
|
|
||||||
if linkurl then
|
|
||||||
table.truncate(session.history.items, session.history.loc + 1)
|
|
||||||
go_to_url(linkurl, true)
|
|
||||||
else
|
|
||||||
local out = string.format('Link id %d does not exist...', linkid)
|
|
||||||
print_gopher_error(out)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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): ')
|
||||||
|
title = io.read()
|
||||||
|
until title
|
||||||
|
if title == '!' then return print('Cancelled') end
|
||||||
|
table.insert(session.favorites,{display = title, link = session.history.items[session.history.loc]})
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function update_favorite()
|
||||||
|
local title
|
||||||
|
io.write('Enter the favorite id (! to cancel) > ')
|
||||||
|
local favid = io.read()
|
||||||
|
if favid == '!' then return print('Cancelled') end
|
||||||
|
local id = tonumber(favid) or tonumber(string.sub(favid,2))
|
||||||
|
if id then
|
||||||
|
local item = session.favorites[id]
|
||||||
|
if item then
|
||||||
|
print(string.format('Old title: %s', item.display))
|
||||||
|
while title ~= '' do
|
||||||
|
io.write('Enter new title (! to cancel) > ')
|
||||||
|
title = io.read()
|
||||||
|
if title == '!' then return print('Cancelled') end
|
||||||
|
end
|
||||||
|
item.display = title
|
||||||
|
print('Favorite has been updated')
|
||||||
|
else
|
||||||
|
print('Invalid favorite id')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print('Invalid favorite id')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function remove_favorite()
|
||||||
|
io.write('Enter the favorite id (! to cancel) > ')
|
||||||
|
local favid = io.read()
|
||||||
|
if favid == '!' then return print('Cancelled') end
|
||||||
|
local id = tonumber(favid) or tonumber(string.sub(favid,2))
|
||||||
|
if id then
|
||||||
|
local item = session.favorites[id]
|
||||||
|
if item then
|
||||||
|
table.remove(session.favorites, id)
|
||||||
|
print('Favorite has been removed')
|
||||||
|
else
|
||||||
|
print('Invalid favorite id')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print('Invalid favorite id')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function visit_link()
|
||||||
|
local linkid
|
||||||
|
local favorite
|
||||||
|
repeat
|
||||||
|
favorite = false
|
||||||
|
io.write('Enter link id (0 to cancel) > ')
|
||||||
|
local inp = io.read()
|
||||||
|
linkid = tonumber(inp)
|
||||||
|
if not linkid and string.sub(inp, 1, 1) == 'f' then
|
||||||
|
linkid = tonumber(string.sub(inp, 2, 2))
|
||||||
|
favorite = true
|
||||||
|
end
|
||||||
|
until linkid
|
||||||
|
local linkurl
|
||||||
|
if favorite then
|
||||||
|
linkurl = session.favorites[linkid].link
|
||||||
|
else
|
||||||
|
linkurl = session.current_links[linkid]
|
||||||
|
end
|
||||||
|
if linkurl then
|
||||||
|
table.truncate(session.history.items, session.history.loc + 1)
|
||||||
|
go_to_url(linkurl, true)
|
||||||
|
else
|
||||||
|
local out = string.format('Link id %d does not exist...', linkid)
|
||||||
|
print_gopher_error(out)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function back()
|
function back()
|
||||||
if session.history.loc > 1 and #session.history.items > 1 then
|
if session.history.loc > 1 and #session.history.items > 1 then
|
||||||
session.history.loc = session.history.loc - 1
|
session.history.loc = session.history.loc - 1
|
||||||
|
|
Loading…
Reference in New Issue