Fix project search and status view for new add directory feature

This commit is contained in:
Francesco Abbate 2020-12-27 18:28:21 +01:00
parent e4fa196b33
commit df74781da8
4 changed files with 20 additions and 9 deletions

View File

@ -73,7 +73,7 @@ command.add(nil, {
["core:find-file"] = function()
local files = {}
for dir, item in core.project_files() do
for dir, item in core.get_project_files() do
if item.type == "file" then
local filename = dir == core.project_dir and item.filename:match('^[/\\](.+)') or dir .. item.filename
table.insert(files, common.home_encode(filename))

View File

@ -155,12 +155,22 @@ local function project_files_iter(state)
return dir.name, dir.files[state.file_index]
end
function core.project_files()
function core.get_project_files()
local state = { dir_index = 1, file_index = 0 }
return project_files_iter, state
end
function core.project_files_number()
local n = 0
for i = 1, #core.project_directories do
n = n + #core.project_directories[i].files
end
return n
end
-- create a directory using mkdir but may need to create the parent
-- directories as well.
local function create_user_directory()

View File

@ -120,9 +120,7 @@ function StatusView:get_items()
style.icon_font, "g",
style.font, style.dim, self.separator2,
#core.docs, style.text, " / ",
-- FIXME: the number of file should be calculated
-- across directories.
#core.project_directories[1].files, " files"
core.project_files_number(), " files"
}
end

View File

@ -49,11 +49,13 @@ function ResultsView:begin_search(text, fn)
self.selected_idx = 0
core.add_thread(function()
for i, file in ipairs(core.project_files) do
local i = 1
for dir_name, file in core.get_project_files() do
if file.type == "file" then
find_all_matches_in_file(self.results, file.filename, fn)
find_all_matches_in_file(self.results, dir_name .. file.filename, fn)
end
self.last_file_idx = i
i = i + 1
end
self.searching = false
self.brightness = 100
@ -162,11 +164,12 @@ function ResultsView:draw()
-- status
local ox, oy = self:get_content_offset()
local x, y = ox + style.padding.x, oy + style.padding.y
local per = self.last_file_idx / #core.project_files
local files_number = core.project_files_number()
local per = self.last_file_idx / files_number
local text
if self.searching then
text = string.format("Searching %d%% (%d of %d files, %d matches) for %q...",
per * 100, self.last_file_idx, #core.project_files,
per * 100, self.last_file_idx, files_number,
#self.results, self.query)
else
text = string.format("Found %d matches for %q",