improved filechooser; enable "single file" mode of kindlepdfviewer

with this mode it's possible to start kindlepdfviewer opening last
viewed file, bypassing filechooser
pull/2/merge
traycold 12 years ago
parent 9fb0fcadd8
commit 04184a5950

@ -33,13 +33,37 @@ FileChooser = {
page = 1, page = 1,
current = 1, current = 1,
oldcurrent = 0, oldcurrent = 0,
exception_message = nil
} }
function getAbsolutePath(aPath)
local abs_path
if not aPath then
abs_path = aPath
elseif aPath:match('^//') then
abs_path = aPath:sub(2)
elseif aPath:match('^/') then
abs_path = aPath
elseif #aPath == 0 then
abs_path = '/'
else
local curr_dir = lfs.currentdir()
abs_path = aPath
if lfs.chdir(aPath) then
abs_path = lfs.currentdir()
lfs.chdir(curr_dir)
end
--print("rel: '"..aPath.."' abs:'"..abs_path.."'")
end
return abs_path
end
function FileChooser:readdir() function FileChooser:readdir()
self.dirs = {} self.dirs = {}
self.files = {} self.files = {}
for f in lfs.dir(self.path) do for f in lfs.dir(self.path) do
if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not string.match(f, "^%.[^.]") then if lfs.attributes(self.path.."/"..f, "mode") == "directory" and f ~= "." and not (f==".." and self.path=="/") and not string.match(f, "^%.[^.]") then
--print(self.path.." -> adding: '"..f.."'")
table.insert(self.dirs, f) table.insert(self.dirs, f)
elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$")then elseif string.match(f, ".+%.[pP][dD][fF]$") or string.match(f, ".+%.[dD][jJ][vV][uU]$")then
table.insert(self.files, f) table.insert(self.files, f)
@ -51,15 +75,22 @@ function FileChooser:readdir()
end end
function FileChooser:setPath(newPath) function FileChooser:setPath(newPath)
self.path = newPath local curr_path = self.path
self:readdir() self.path = getAbsolutePath(newPath)
self.items = #self.dirs + #self.files local readdir_ok, exc = pcall(self.readdir,self)
if self.items == 0 then if(not readdir_ok) then
return nil print("readdir error: "..tostring(exc))
self.exception_message = exc
return self:setPath(curr_path)
else
self.items = #self.dirs + #self.files
if self.items == 0 then
return nil
end
self.page = 1
self.current = 1
return true
end end
self.page = 1
self.current = 1
return true
end end
function FileChooser:updateFont() function FileChooser:updateFont()
@ -74,7 +105,7 @@ function FileChooser:updateFont()
end end
function FileChooser:choose(ypos, height) function FileChooser:choose(ypos, height)
local perpage = math.floor(height / self.spacing) - 1 local perpage = math.floor(height / self.spacing) - 2
local pagedirty = true local pagedirty = true
local markerdirty = false local markerdirty = false
@ -122,8 +153,11 @@ function FileChooser:choose(ypos, height)
renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true) renderUtf8Text(fb.bb, 50, ypos + self.spacing*c, self.face, self.fhash, self.files[i-#self.dirs], true)
end end
end end
renderUtf8Text(fb.bb, 39, ypos + self.spacing * perpage + 32, self.fface, self.ffhash, renderUtf8Text(fb.bb, 5, ypos + self.spacing * perpage + 42, self.fface, self.ffhash,
"Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true) "Page "..self.page.." of "..(math.floor(self.items / perpage)+1), true)
local msg = self.exception_message and self.exception_message:match("[^%:]+:%d+: (.*)") or "Path: "..self.path
self.exception_message = nil
renderUtf8Text(fb.bb, 5, ypos + self.spacing * (perpage+1) + 27, self.fface, self.ffhash, msg, true)
markerdirty = true markerdirty = true
end end
if markerdirty then if markerdirty then
@ -146,7 +180,7 @@ function FileChooser:choose(ypos, height)
end end
local ev = input.waitForEvent() local ev = input.waitForEvent()
print("key code:"..ev.code) --print("key code:"..ev.code)
ev.code = adjustKeyEvents(ev) ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_UP then if ev.code == KEY_FW_UP then

@ -2,5 +2,5 @@
echo unlock > /proc/keypad echo unlock > /proc/keypad
echo unlock > /proc/fiveway echo unlock > /proc/fiveway
cd /mnt/us/kindlepdfviewer/ cd /mnt/us/kindlepdfviewer/
./reader.lua /mnt/us/documents ./reader.lua $1
echo 1 > /proc/eink_fb/update_display echo 1 > /proc/eink_fb/update_display

@ -39,20 +39,21 @@ function openFile(filename)
page_num = DJVUReader.settings:readsetting("last_page") or 1 page_num = DJVUReader.settings:readsetting("last_page") or 1
DJVUReader:goto(tonumber(page_num)) DJVUReader:goto(tonumber(page_num))
DJVUReader:inputloop() DJVUReader:inputloop()
reader_settings:savesetting("lastfile", filename)
end end
elseif file_type == "pdf" then elseif file_type == "pdf" then
if PDFReader:open(filename,"") then -- TODO: query for password if PDFReader:open(filename,"") then -- TODO: query for password
page_num = PDFReader.settings:readsetting("last_page") or 1 page_num = PDFReader.settings:readsetting("last_page") or 1
PDFReader:goto(tonumber(page_num)) PDFReader:goto(tonumber(page_num))
PDFReader:inputloop() PDFReader:inputloop()
reader_settings:savesetting("lastfile", filename)
end end
end end
end end
optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts) function showusage()
if optarg["h"] or ARGV[optind] == nil then print("usage: ./reader.lua [OPTION] ... path")
print("usage: ./reader.lua [OPTION] ... DOCUMENT.PDF") print("Read PDFs and DJVUs on your E-Ink reader")
print("Read PDFs on your E-Ink reader")
print("") print("")
print("-p, --password=PASSWORD set password for reading PDF document") print("-p, --password=PASSWORD set password for reading PDF document")
print("-g, --goto=page start reading on page") print("-g, --goto=page start reading on page")
@ -63,14 +64,21 @@ if optarg["h"] or ARGV[optind] == nil then
print(" \"emu\" (DXG emulation)") print(" \"emu\" (DXG emulation)")
print("-h, --help show this usage help") print("-h, --help show this usage help")
print("") print("")
print("If you give the name of a directory instead of a path, a file") print("If you give the name of a directory instead of a file path, a file")
print("chooser will show up and let you select a PDF file") print("chooser will show up and let you select a PDF|DJVU file")
print("")
print("If you don't pass any path, the last viewed document will be opened")
print("") print("")
print("This software is licensed under the GPLv3.") print("This software is licensed under the GPLv3.")
print("See http://github.com/hwhw/kindlepdfviewer for more info.") print("See http://github.com/hwhw/kindlepdfviewer for more info.")
return return
end end
optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts)
if optarg["h"] then
return showusage()
end
if optarg["d"] == "k3" then if optarg["d"] == "k3" then
-- for now, the only difference is the additional input device -- for now, the only difference is the additional input device
@ -116,9 +124,10 @@ PDFReader:init()
DJVUReader:init() DJVUReader:init()
-- display directory or open file -- display directory or open file
if lfs.attributes(ARGV[optind], "mode") == "directory" then local patharg = ARGV[optind] or reader_settings:readsetting("lastfile")
if patharg and lfs.attributes(patharg, "mode") == "directory" then
local running = true local running = true
FileChooser:setPath(ARGV[optind]) FileChooser:setPath(patharg)
while running do while running do
local file = FileChooser:choose(0,height) local file = FileChooser:choose(0,height)
if file ~= nil then if file ~= nil then
@ -127,8 +136,10 @@ if lfs.attributes(ARGV[optind], "mode") == "directory" then
running = false running = false
end end
end end
elseif patharg then
openFile(patharg, optarg["p"])
else else
openFile(ARGV[optind], optarg["p"]) return showusage()
end end

Loading…
Cancel
Save