From 02fa37e645c7345d8cb6c1383d251988ad336d9f Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Fri, 4 Jan 2019 20:39:32 -0800 Subject: [PATCH] Finished getting favorites working --- main.lua | 126 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 32 deletions(-) diff --git a/main.lua b/main.lua index fdac6c8..6eb142a 100644 --- a/main.lua +++ b/main.lua @@ -23,7 +23,10 @@ function string.split(str, sep) out = string.sub(str, start, point - 1) end 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 return t end @@ -131,14 +134,13 @@ function mainloop() elseif key == 'f' then forward() elseif key == 'a' then - local title - 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]}) + add_favorite() elseif key == 'l' then show_favorites() + elseif key == 'u' then + update_favorite() + elseif key == 'r' then + remove_favorite() elseif key == 'g' then io.write('Enter gopher url > ') local url = io.read() @@ -148,36 +150,96 @@ function mainloop() print('\n') end elseif key == 'v' then - 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 + visit_link() 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() if session.history.loc > 1 and #session.history.items > 1 then session.history.loc = session.history.loc - 1