mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
finished search feature!
This commit is contained in:
@@ -190,8 +190,14 @@ function FileChooser:choose(ypos, height)
|
||||
elseif ev.code == KEY_S then
|
||||
-- invoke search input
|
||||
keywords = InputBox:input(height-100, 100, "Search:")
|
||||
if keywords then
|
||||
-- display search result according to keywords
|
||||
if keywords then -- display search result according to keywords
|
||||
--[[
|
||||
----------------------------------------------------------------
|
||||
|| uncomment following line and set the correct path if you want
|
||||
|| to test search feature in EMU mode
|
||||
----------------------------------------------------------------
|
||||
--]]
|
||||
--FileSearcher:init("/home/dave/documents/kindle/backup/documents")
|
||||
FileSearcher:init()
|
||||
file = FileSearcher:choose(ypos, height, keywords)
|
||||
if file then
|
||||
|
||||
@@ -19,18 +19,13 @@ FileSearcher = {
|
||||
-- foot height
|
||||
foot_H = 27,
|
||||
|
||||
x_input = 50,
|
||||
-- state buffer
|
||||
dirs = {},
|
||||
files = {},
|
||||
result = {},
|
||||
fonts = {"sans", "cjk", "mono",
|
||||
"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
|
||||
"Helvetica", "Helvetica-Oblique", "Helvetica-BoldOblique",
|
||||
"Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",},
|
||||
items = 14,
|
||||
page = 1,
|
||||
current = 2,
|
||||
items = 0,
|
||||
page = 0,
|
||||
current = 1,
|
||||
oldcurrent = 1,
|
||||
}
|
||||
|
||||
@@ -71,16 +66,27 @@ function FileSearcher:setPath(newPath)
|
||||
end
|
||||
|
||||
function FileSearcher:setSearchResult(keywords)
|
||||
self.result = self.files
|
||||
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
|
||||
|
||||
function FileSearcher:init(keywords)
|
||||
self:setPath("/home/dave/documents/kindle/backup/documents")
|
||||
self:setSearchResult(keywords)
|
||||
--@TODO check this 17.02 2012
|
||||
function FileSearcher:init(search_path)
|
||||
if search_path then
|
||||
self:setPath(search_path)
|
||||
else
|
||||
self:setPath("/mnt/us/documents")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -118,23 +124,34 @@ function FileSearcher:choose(ypos, height, keywords)
|
||||
end
|
||||
end
|
||||
|
||||
self:setSearchResult(keywords)
|
||||
|
||||
while true do
|
||||
if pagedirty then
|
||||
markerdirty = true
|
||||
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
|
||||
|
||||
-- draw menu title
|
||||
renderUtf8Text(fb.bb, 30, ypos + self.title_H, self.tface, self.tfhash,
|
||||
"Search Result for"..keywords, true)
|
||||
"Search Result for: "..keywords, true)
|
||||
|
||||
-- draw results
|
||||
local c
|
||||
for c = 1, perpage do
|
||||
local i = (self.page - 1) * perpage + c
|
||||
if i <= self.items then
|
||||
y = ypos + self.title_H + (self.spacing * c)
|
||||
renderUtf8Text(fb.bb, 50, y, self.face, self.fhash,
|
||||
self.result[i].name, true)
|
||||
if self.items == 0 then -- nothing found
|
||||
y = ypos + self.title_H + self.spacing * 2
|
||||
renderUtf8Text(fb.bb, 20, y, self.face, self.fhash,
|
||||
"Sorry, your keyword did not match any documents.", true)
|
||||
renderUtf8Text(fb.bb, 20, y + self.spacing, self.face, self.fhash,
|
||||
"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
|
||||
if i <= self.items then
|
||||
y = ypos + self.title_H + (self.spacing * c)
|
||||
renderUtf8Text(fb.bb, 50, y, self.face, self.fhash,
|
||||
self.result[i].name, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -144,7 +161,6 @@ function FileSearcher:choose(ypos, height, keywords)
|
||||
all_page = (math.floor(self.items / perpage)+1)
|
||||
renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash,
|
||||
"Page "..self.page.." of "..all_page, true)
|
||||
markerdirty = true
|
||||
end
|
||||
|
||||
if markerdirty then
|
||||
@@ -196,7 +212,14 @@ function FileSearcher:choose(ypos, height, keywords)
|
||||
markerdirty = true
|
||||
end
|
||||
elseif ev.code == KEY_S then
|
||||
input = InputBox:input(height-100, 100)
|
||||
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
|
||||
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
|
||||
-- return full file path
|
||||
file_entry = self.files[perpage*(self.page-1)+self.current]
|
||||
|
||||
108
inputbox.lua
108
inputbox.lua
@@ -4,84 +4,92 @@ require "graphics"
|
||||
|
||||
InputBox = {
|
||||
-- Class vars:
|
||||
|
||||
-- font for displaying input content
|
||||
face = freetype.newBuiltinFace("mono", 25),
|
||||
fhash = "m25",
|
||||
fheight = 25,
|
||||
-- font for input title display
|
||||
tface = freetype.newBuiltinFace("sans", 28),
|
||||
tfhash = "s28",
|
||||
-- spacing between lines
|
||||
spacing = 40,
|
||||
|
||||
input_start_x = 145,
|
||||
input_start_y = nil,
|
||||
input_cur_x = nil,
|
||||
input_cur_x = nil, -- points to the start of next input pos
|
||||
|
||||
input_bg = 1,
|
||||
|
||||
input_string = "",
|
||||
-- state buffer
|
||||
dirs = nil,
|
||||
files = nil,
|
||||
items = 0,
|
||||
path = "",
|
||||
page = 1,
|
||||
current = 1,
|
||||
oldcurrent = 0,
|
||||
|
||||
-- font for displaying input content
|
||||
face = freetype.newBuiltinFace("mono", 25),
|
||||
fhash = "m25",
|
||||
fheight = 25,
|
||||
fwidth = 16,
|
||||
}
|
||||
|
||||
function InputBox:setPath(newPath)
|
||||
self.path = newPath
|
||||
self:readdir()
|
||||
self.items = #self.dirs + #self.files
|
||||
if self.items == 0 then
|
||||
return nil
|
||||
end
|
||||
self.page = 1
|
||||
self.current = 1
|
||||
return true
|
||||
function InputBox:setDefaultInput(text)
|
||||
self.input_string = ""
|
||||
self:addString(text)
|
||||
--renderUtf8Text(fb.bb, self.input_start_x, self.input_start_y,
|
||||
--self.face, self.fhash, text, true)
|
||||
--self.input_cur_x = self.input_start_x + (string.len(text) * self.fwidth)
|
||||
--self.input_string = text
|
||||
end
|
||||
|
||||
function InputBox:addChar(text)
|
||||
renderUtf8Text(fb.bb, self.input_cur_x, self.input_start_y,
|
||||
self.face, self.fhash, text, true)
|
||||
fb:refresh(1, self.input_cur_x, self.input_start_y-19, 16, self.fheight)
|
||||
self.input_cur_x = self.input_cur_x + 16
|
||||
self.input_string = self.input_string .. text
|
||||
function InputBox:addString(str)
|
||||
for i = 1, #str do
|
||||
self:addChar(str:sub(i,i))
|
||||
end
|
||||
end
|
||||
|
||||
function InputBox:addChar(char)
|
||||
renderUtf8Text(fb.bb, self.input_cur_x, self.input_start_y, self.face, self.fhash,
|
||||
char, true)
|
||||
fb:refresh(1, self.input_cur_x, self.input_start_y-19, self.fwidth, self.fheight)
|
||||
self.input_cur_x = self.input_cur_x + self.fwidth
|
||||
self.input_string = self.input_string .. char
|
||||
end
|
||||
|
||||
function InputBox:delChar()
|
||||
if self.input_start_x == self.input_cur_x then
|
||||
return
|
||||
end
|
||||
self.input_cur_x = self.input_cur_x - 16
|
||||
self.input_cur_x = self.input_cur_x - self.fwidth
|
||||
--fill last character with blank rectangle
|
||||
fb.bb:paintRect(self.input_cur_x, self.input_start_y-19, 16, self.fheight, self.input_bg)
|
||||
fb:refresh(1, self.input_cur_x, self.input_start_y-19, 16, self.fheight)
|
||||
fb.bb:paintRect(self.input_cur_x, self.input_start_y-19,
|
||||
self.fwidth, self.fheight, self.input_bg)
|
||||
fb:refresh(1, self.input_cur_x, self.input_start_y-19, self.fwidth, self.fheight)
|
||||
self.input_string = self.input_string:sub(0,-2)
|
||||
end
|
||||
|
||||
function InputBox:input(ypos, height, title)
|
||||
function InputBox:drawBox(ypos, w, h, title)
|
||||
-- draw input border
|
||||
fb.bb:paintRect(20, ypos, w, h, 5)
|
||||
-- draw input slot
|
||||
fb.bb:paintRect(140, ypos + 10, w - 130, h - 20, self.input_bg)
|
||||
-- draw input title
|
||||
renderUtf8Text(fb.bb, 35, self.input_start_y, self.face, self.fhash,
|
||||
title, true)
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
|| d_text default to nil (used to set default text in input slot)
|
||||
--]]
|
||||
function InputBox:input(ypos, height, title, d_text)
|
||||
local pagedirty = true
|
||||
-- do some initilization
|
||||
self.input_start_y = ypos + 35
|
||||
self.input_cur_x = self.input_start_x
|
||||
|
||||
if d_text then -- if specified default text, draw it
|
||||
w = fb.bb:getWidth() - 40
|
||||
h = height - 45
|
||||
self:drawBox(ypos, w, h, title)
|
||||
self:setDefaultInput(d_text)
|
||||
fb:refresh(1, 20, ypos, w, h)
|
||||
pagedirty = false
|
||||
else -- otherwise, leave the draw task to the main loop
|
||||
self.input_string = ""
|
||||
end
|
||||
|
||||
while true do
|
||||
if pagedirty then
|
||||
w = fb.bb:getWidth() - 40
|
||||
h = height - 45
|
||||
-- draw input border
|
||||
fb.bb:paintRect(20, ypos, w, h, 5)
|
||||
-- draw input slot
|
||||
fb.bb:paintRect(140, ypos + 10, w - 130, h - 20, self.input_bg)
|
||||
renderUtf8Text(fb.bb, 35, self.input_start_y, self.face, self.fhash,
|
||||
title, true)
|
||||
markerdirty = true
|
||||
end
|
||||
|
||||
if pagedirty then
|
||||
self:drawBox(ypos, w, h, title)
|
||||
fb:refresh(1, 20, ypos, w, h)
|
||||
pagedirty = false
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user