finished search feature!

pull/2/merge
Qingping Hou 12 years ago
parent f9804a1c92
commit 060ad5cb01

@ -190,8 +190,14 @@ function FileChooser:choose(ypos, height)
elseif ev.code == KEY_S then elseif ev.code == KEY_S then
-- invoke search input -- invoke search input
keywords = InputBox:input(height-100, 100, "Search:") keywords = InputBox:input(height-100, 100, "Search:")
if keywords then if keywords then -- display search result according to keywords
-- 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() FileSearcher:init()
file = FileSearcher:choose(ypos, height, keywords) file = FileSearcher:choose(ypos, height, keywords)
if file then if file then

@ -19,18 +19,13 @@ FileSearcher = {
-- foot height -- foot height
foot_H = 27, foot_H = 27,
x_input = 50,
-- state buffer -- state buffer
dirs = {}, dirs = {},
files = {}, files = {},
result = {}, result = {},
fonts = {"sans", "cjk", "mono", items = 0,
"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique", page = 0,
"Helvetica", "Helvetica-Oblique", "Helvetica-BoldOblique", current = 1,
"Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",},
items = 14,
page = 1,
current = 2,
oldcurrent = 1, oldcurrent = 1,
} }
@ -71,16 +66,27 @@ function FileSearcher:setPath(newPath)
end end
function FileSearcher:setSearchResult(keywords) 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.items = #self.result
self.page = 1 self.page = 1
self.current = 1 self.current = 1
end end
function FileSearcher:init(keywords) function FileSearcher:init(search_path)
self:setPath("/home/dave/documents/kindle/backup/documents") if search_path then
self:setSearchResult(keywords) self:setPath(search_path)
--@TODO check this 17.02 2012 else
self:setPath("/mnt/us/documents")
end
end end
@ -118,23 +124,34 @@ function FileSearcher:choose(ypos, height, keywords)
end end
end end
self:setSearchResult(keywords)
while true do while true do
if pagedirty then if pagedirty then
markerdirty = true
fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0) fb.bb:paintRect(0, ypos, fb.bb:getWidth(), height, 0)
-- draw menu title -- draw menu title
renderUtf8Text(fb.bb, 30, ypos + self.title_H, self.tface, self.tfhash, renderUtf8Text(fb.bb, 30, ypos + self.title_H, self.tface, self.tfhash,
"Search Result for"..keywords, true) "Search Result for: "..keywords, true)
-- draw results -- draw results
local c local c
for c = 1, perpage do if self.items == 0 then -- nothing found
local i = (self.page - 1) * perpage + c y = ypos + self.title_H + self.spacing * 2
if i <= self.items then renderUtf8Text(fb.bb, 20, y, self.face, self.fhash,
y = ypos + self.title_H + (self.spacing * c) "Sorry, your keyword did not match any documents.", true)
renderUtf8Text(fb.bb, 50, y, self.face, self.fhash, renderUtf8Text(fb.bb, 20, y + self.spacing, self.face, self.fhash,
self.result[i].name, true) "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
end end
@ -144,7 +161,6 @@ function FileSearcher:choose(ypos, height, keywords)
all_page = (math.floor(self.items / perpage)+1) all_page = (math.floor(self.items / perpage)+1)
renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash, renderUtf8Text(fb.bb, x, y, self.sface, self.sfhash,
"Page "..self.page.." of "..all_page, true) "Page "..self.page.." of "..all_page, true)
markerdirty = true
end end
if markerdirty then if markerdirty then
@ -196,7 +212,14 @@ function FileSearcher:choose(ypos, height, keywords)
markerdirty = true markerdirty = true
end end
elseif ev.code == KEY_S then 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 elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
-- return full file path -- return full file path
file_entry = self.files[perpage*(self.page-1)+self.current] file_entry = self.files[perpage*(self.page-1)+self.current]

@ -4,84 +4,92 @@ require "graphics"
InputBox = { InputBox = {
-- Class vars: -- 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_x = 145,
input_start_y = nil, input_start_y = nil,
input_cur_x = nil, input_cur_x = nil, -- points to the start of next input pos
input_bg = 1, input_bg = 1,
input_string = "", input_string = "",
-- state buffer
dirs = nil, -- font for displaying input content
files = nil, face = freetype.newBuiltinFace("mono", 25),
items = 0, fhash = "m25",
path = "", fheight = 25,
page = 1, fwidth = 16,
current = 1,
oldcurrent = 0,
} }
function InputBox:setPath(newPath) function InputBox:setDefaultInput(text)
self.path = newPath self.input_string = ""
self:readdir() self:addString(text)
self.items = #self.dirs + #self.files --renderUtf8Text(fb.bb, self.input_start_x, self.input_start_y,
if self.items == 0 then --self.face, self.fhash, text, true)
return nil --self.input_cur_x = self.input_start_x + (string.len(text) * self.fwidth)
--self.input_string = text
end
function InputBox:addString(str)
for i = 1, #str do
self:addChar(str:sub(i,i))
end end
self.page = 1
self.current = 1
return true
end end
function InputBox:addChar(text) function InputBox:addChar(char)
renderUtf8Text(fb.bb, self.input_cur_x, self.input_start_y, renderUtf8Text(fb.bb, self.input_cur_x, self.input_start_y, self.face, self.fhash,
self.face, self.fhash, text, true) char, true)
fb:refresh(1, self.input_cur_x, self.input_start_y-19, 16, self.fheight) fb:refresh(1, self.input_cur_x, self.input_start_y-19, self.fwidth, self.fheight)
self.input_cur_x = self.input_cur_x + 16 self.input_cur_x = self.input_cur_x + self.fwidth
self.input_string = self.input_string .. text self.input_string = self.input_string .. char
end end
function InputBox:delChar() function InputBox:delChar()
if self.input_start_x == self.input_cur_x then if self.input_start_x == self.input_cur_x then
return return
end 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 --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.bb:paintRect(self.input_cur_x, self.input_start_y-19,
fb:refresh(1, self.input_cur_x, self.input_start_y-19, 16, self.fheight) 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) self.input_string = self.input_string:sub(0,-2)
end 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 local pagedirty = true
-- do some initilization
self.input_start_y = ypos + 35 self.input_start_y = ypos + 35
self.input_cur_x = self.input_start_x 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 while true do
if pagedirty then if pagedirty then
w = fb.bb:getWidth() - 40 w = fb.bb:getWidth() - 40
h = height - 45 h = height - 45
-- draw input border self:drawBox(ypos, w, h, title)
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
fb:refresh(1, 20, ypos, w, h) fb:refresh(1, 20, ypos, w, h)
pagedirty = false pagedirty = false
end end

Loading…
Cancel
Save