From f22b2301fae33e0d0b38ec7ae2900e761a97f08c Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Tue, 12 Mar 2013 12:51:00 +0800 Subject: [PATCH] add last documents menu entry --- frontend/settings.lua | 15 +++++++++++++++ reader.lua | 31 ++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/frontend/settings.lua b/frontend/settings.lua index 638484488..7a691dd9c 100644 --- a/frontend/settings.lua +++ b/frontend/settings.lua @@ -15,6 +15,21 @@ function DocSettings:getHistoryPath(fullpath) return "./history/["..basename:gsub("/","#").."] "..filename..".lua" end +function DocSettings:getPathFromHistory(hist_name) + -- 1. select everything included in brackets + local s = string.match(hist_name,"%b[]") + -- 2. crop the bracket-sign from both sides + -- 3. and finally replace decorative signs '#' to dir-char '/' + return string.gsub(string.sub(s,2,-3),"#","/") +end + +function DocSettings:getNameFromHistory(hist_name) + -- at first, search for path length + local s = string.len(string.match(hist_name,"%b[]")) + -- and return the rest of string without 4 last characters (".lua") + return string.sub(hist_name, s+2, -5) +end + function DocSettings:open(docfile) local conf_path = nil if docfile == ".reader" then diff --git a/reader.lua b/reader.lua index e93839a4a..a0e8c9dc6 100755 --- a/reader.lua +++ b/reader.lua @@ -27,6 +27,32 @@ HomeMenu = InputContainer:new{ } function HomeMenu:setUpdateItemTable() + function readHistDir(order_arg, re) + local pipe_out = io.popen("ls "..order_arg.." -1 ./history") + for f in pipe_out:lines() do + table.insert(re, { + dir = DocSettings:getPathFromHistory(f), + name = DocSettings:getNameFromHistory(f), + }) + end + end + + local hist_sub_item_table = {} + local last_files = {} + readHistDir("-c", last_files) + for _,v in pairs(last_files) do + table.insert(hist_sub_item_table, { + text = v.name, + callback = function() + showReader(v.dir .. "/" .. v.name) + end + }) + end + table.insert(self.item_table, { + text = "Last documents", + sub_item_table = hist_sub_item_table, + }) + table.insert(self.item_table, { text = "Exit", callback = function() @@ -36,9 +62,8 @@ function HomeMenu:setUpdateItemTable() end function HomeMenu:onTapShowMenu() - if #self.item_table == 0 then - self:setUpdateItemTable() - end + self.item_table = {} + self:setUpdateItemTable() local home_menu = Menu:new{ title = "Home menu",