2
0
mirror of https://github.com/koreader/koreader synced 2024-11-11 19:11:14 +00:00
koreader/filesearcher.lua

264 lines
6.8 KiB
Lua
Raw Normal View History

2012-02-18 12:35:03 +00:00
require "rendertext"
require "keys"
require "graphics"
require "font"
2012-02-18 12:35:03 +00:00
FileSearcher = {
2012-02-18 23:21:50 +00:00
-- title height
title_H = 45,
2012-02-18 12:35:03 +00:00
-- spacing between lines
spacing = 40,
2012-02-18 23:21:50 +00:00
-- foot height
foot_H = 27,
2012-02-18 12:35:03 +00:00
-- state buffer
dirs = {},
files = {},
result = {},
2012-02-19 06:41:44 +00:00
items = 0,
page = 0,
current = 1,
2012-02-18 23:21:50 +00:00
oldcurrent = 1,
2012-02-18 12:35:03 +00:00
}
function FileSearcher:readDir()
self.dirs = {self.path}
self.files = {}
while #self.dirs ~= 0 do
new_dirs = {}
-- handle each dir
for __, d in pairs(self.dirs) do
-- handle files in d
for f in lfs.dir(d) do
2012-03-28 16:09:26 +00:00
local file_type = string.lower(string.match(f, ".+%.([^.]+)") or "")
if lfs.attributes(d.."/"..f, "mode") == "directory"
and f ~= "." and f~= ".." and not string.match(f, "^%.[^.]") then
table.insert(new_dirs, d.."/"..f)
2012-03-28 16:09:26 +00:00
elseif file_type == "djvu" or file_type == "pdf"
or file_type == "xps" or file_type == "cbz"
or file_type == "epub" or file_type == "txt"
or file_type == "rtf" or file_type == "htm"
or file_type == "fb2" or file_type == "chm" then
file_entry = {dir=d, name=f,}
table.insert(self.files, file_entry)
--print("file:"..d.."/"..f)
end
end
end
self.dirs = new_dirs
end
end
function FileSearcher:setPath(newPath)
self.path = newPath
self:readDir()
self.items = #self.files
--@TODO check none found 19.02 2012
if self.items == 0 then
return nil
end
self.page = 1
self.current = 1
return true
end
function FileSearcher:setSearchResult(keywords)
2012-02-19 06:41:44 +00:00
self.result = {}
if keywords == " " then -- one space to show all files
self.result = self.files
else
for __,f in pairs(self.files) do
if string.find(string.lower(f.name), keywords) then
table.insert(self.result, f)
end
end
end
self.items = #self.result
self.page = 1
self.current = 1
end
2012-02-19 06:41:44 +00:00
function FileSearcher:init(search_path)
if search_path then
self:setPath(search_path)
else
self:setPath("/mnt/us/documents")
end
2012-02-18 12:35:03 +00:00
end
2012-02-18 23:21:50 +00:00
function FileSearcher:choose(ypos, height, keywords)
local perpage = math.floor(height / self.spacing) - 2
2012-02-18 12:35:03 +00:00
local pagedirty = true
local markerdirty = false
local prevItem = function ()
if self.current == 1 then
if self.page > 1 then
self.current = perpage
self.page = self.page - 1
pagedirty = true
end
else
self.current = self.current - 1
markerdirty = true
end
end
local nextItem = function ()
if self.current == perpage then
if self.page < (self.items / perpage) then
self.current = 1
self.page = self.page + 1
pagedirty = true
end
else
if self.page ~= math.floor(self.items / perpage) + 1
or self.current + (self.page-1)*perpage < self.items then
self.current = self.current + 1
markerdirty = true
end
end
end
-- if given keywords, set new result according to keywords.
-- Otherwise, display the previous search result.
if keywords then
self:setSearchResult(keywords)
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
while true do
local cface, cfhash = Font:getFaceAndHash(22)
local tface, tfhash = Font:getFaceAndHash(25, Font.tfont)
local fface, ffhash = Font:getFaceAndHash(16, Font.ffont)
2012-02-18 12:35:03 +00:00
if pagedirty then
2012-02-19 06:41:44 +00:00
markerdirty = true
2012-02-18 12:35:03 +00:00
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
2012-02-18 23:21:50 +00:00
-- draw menu title
renderUtf8Text(fb.bb, 30, ypos + self.title_H, tface, tfhash,
2012-02-19 06:41:44 +00:00
"Search Result for: "..keywords, true)
2012-02-18 23:21:50 +00:00
-- draw results
2012-02-18 12:35:03 +00:00
local c
2012-02-19 06:41:44 +00:00
if self.items == 0 then -- nothing found
y = ypos + self.title_H + self.spacing * 2
renderUtf8Text(fb.bb, 20, y, cface, cfhash,
"Sorry, no match found.", true)
renderUtf8Text(fb.bb, 20, y + self.spacing, cface, cfhash,
2012-02-19 06:41:44 +00:00
"Please try a different keyword.", true)
markerdirty = false
else -- found something, draw it
for c = 1, perpage do
local i = (self.page - 1) * perpage + c
2012-02-19 06:41:44 +00:00
if i <= self.items then
y = ypos + self.title_H + (self.spacing * c)
renderUtf8Text(fb.bb, 50, y, cface, cfhash,
2012-02-19 06:41:44 +00:00
self.result[i].name, true)
end
2012-02-18 12:35:03 +00:00
end
end
-- draw footer
2012-02-18 23:21:50 +00:00
y = ypos + self.title_H + (self.spacing * perpage) + self.foot_H
x = (fb.bb:getWidth() / 2) - 50
all_page = (math.floor(self.items / perpage)+1)
renderUtf8Text(fb.bb, x, y, fface, ffhash,
"Page "..self.page.." of "..all_page, true)
2012-02-18 12:35:03 +00:00
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
if markerdirty then
if not pagedirty then
if self.oldcurrent > 0 then
2012-02-18 23:21:50 +00:00
y = ypos + self.title_H + (self.spacing * self.oldcurrent) + 10
fb.bb:paintRect(30, y, fb.bb:getWidth() - 60, 3, 0)
fb:refresh(1, 30, y, fb.bb:getWidth() - 60, 3)
2012-02-18 12:35:03 +00:00
end
end
2012-02-18 23:21:50 +00:00
-- draw new marker line
y = ypos + self.title_H + (self.spacing * self.current) + 10
fb.bb:paintRect(30, y, fb.bb:getWidth() - 60, 3, 15)
2012-02-18 12:35:03 +00:00
if not pagedirty then
2012-02-18 23:21:50 +00:00
fb:refresh(1, 30, y, fb.bb:getWidth() - 60, 3)
2012-02-18 12:35:03 +00:00
end
self.oldcurrent = self.current
markerdirty = false
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
if pagedirty then
fb:refresh(0, 0, ypos, fb.bb:getWidth(), height)
pagedirty = false
end
2012-02-18 23:21:50 +00:00
2012-02-18 12:35:03 +00:00
local ev = input.waitForEvent()
ev.code = adjustKeyEvents(ev)
2012-02-18 12:35:03 +00:00
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_FW_UP then
2012-02-18 12:35:03 +00:00
prevItem()
elseif ev.code == KEY_FW_DOWN then
nextItem()
elseif ev.code == KEY_PGFWD then
if self.page < (self.items / perpage) then
if self.current + self.page*perpage > self.items then
self.current = self.items - self.page*perpage
end
self.page = self.page + 1
pagedirty = true
else
self.current = self.items - (self.page-1)*perpage
markerdirty = true
end
elseif ev.code == KEY_PGBCK then
if self.page > 1 then
self.page = self.page - 1
pagedirty = true
else
self.current = 1
markerdirty = true
end
elseif ev.code == KEY_S then
2012-02-19 06:41:44 +00:00
old_keywords = keywords
keywords = InputBox:input(height-100, 100, "Search:", old_keywords)
if keywords then
self:setSearchResult(keywords)
else
keywords = old_keywords
end
pagedirty = true
2012-02-28 14:51:37 +00:00
elseif ev.code == KEY_F then -- invoke fontchooser menu
fonts_menu = SelectMenu:new{
menu_title = "Fonts Menu",
item_array = Font.fonts,
2012-02-28 14:51:37 +00:00
}
2012-02-28 15:05:26 +00:00
local re = fonts_menu:choose(0, height)
if re then
Font.cfont = Font.fonts[re]
Font:update()
2012-02-28 15:05:26 +00:00
end
2012-02-28 14:51:37 +00:00
pagedirty = true
2012-02-18 12:35:03 +00:00
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
file_entry = self.result[perpage*(self.page-1)+self.current]
2012-03-05 01:46:16 +00:00
file_full_path = file_entry.dir .. "/" .. file_entry.name
-- rotation mode might be changed while reading, so
-- record height_percent here
local height_percent = height/fb.bb:getHeight()
2012-03-05 01:46:16 +00:00
openFile(file_full_path)
--reset height and item index if screen has been rotated
local old_perpage = perpage
height = math.floor(fb.bb:getHeight()*height_percent)
perpage = math.floor(height / self.spacing) - 2
self.current = (old_perpage * (self.page - 1) +
self.current) % perpage
self.page = math.floor(self.items / perpage) + 1
pagedirty = true
2012-03-08 06:24:30 +00:00
elseif ev.code == KEY_BACK or ev.code == KEY_HOME then
2012-02-18 12:35:03 +00:00
return nil
end
end
end
end