WIP: File browser, needs bug fixes

This commit is contained in:
ailurux
2024-05-06 12:36:16 +10:00
parent fb3d6a7b86
commit eeb3f2d406
5 changed files with 132 additions and 19 deletions
+18 -13
View File
@@ -17,12 +17,13 @@ namespace database {
FileIterator::FileIterator(std::string filepath)
: original_path_(filepath),
current_()
current_(),
offset_(-1)
{
auto lock = drivers::acquire_spi();
const TCHAR* next_path = static_cast<const TCHAR*>(filepath.c_str());
FRESULT res = f_opendir(&dir_, next_path);
const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str());
FRESULT res = f_opendir(&dir_, path);
if (res != FR_OK) {
ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str());
}
@@ -42,36 +43,40 @@ auto FileIterator::next() -> void {
}
auto FileIterator::prev() -> void {
iterate(true);
if (offset_ == 0) {
current_.reset();
return;
}
f_rewinddir(&dir_);
auto new_offset = offset_-1;
offset_ = -1;
for (int i = 0; i < new_offset; i++) {
iterate(false);
}
}
auto FileIterator::iterate(bool reverse) -> bool {
FILINFO info;
if (reverse) {
f_rewinddir(&dir_);
}
{
auto lock = drivers::acquire_spi();
auto res = f_readdir(&dir_, &info);
if (res != FR_OK) {
ESP_LOGI(kTag, "AAAAAAAAAAAAAAAAAAA");
ESP_LOGI(kTag, "%d", res);
ESP_LOGE(kTag, "Error reading directory. Error: %d", res);
return false;
}
}
if (info.fname[0] == 0) {
// End of directory
// Set value to nil
current_.reset();
ESP_LOGI(kTag, "End of dir");
} else {
// Update current value
ESP_LOGI(kTag, "File: %s", info.fname);
offset_++;
current_ = FileEntry{
.isHidden = (info.fattrib & AM_HID) > 0,
.isDirectory = (info.fattrib & AM_DIR) > 0,
.isTrack = false, // TODO
.filepath = original_path_ + info.fname,
.filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname,
};
}
+1
View File
@@ -36,6 +36,7 @@ class FileIterator {
std::string original_path_;
std::optional<FileEntry> current_;
int offset_;
auto iterate(bool reverse = false) -> bool;
};
+30 -6
View File
@@ -88,11 +88,11 @@ static auto fs_iterate(lua_State* state) -> int {
return 1;
}
// static auto db_iterator_clone(lua_State* state) -> int {
// database::Iterator* it = db_check_iterator(state, 1);
// push_iterator(state, *it);
// return 1;
// }
static auto fs_iterator_clone(lua_State* state) -> int {
database::FileIterator* it = check_file_iterator(state, 1);
push_iterator(state, *it);
return 1;
}
static auto fs_iterator_gc(lua_State* state) -> int {
database::FileIterator* it = check_file_iterator(state, 1);
@@ -102,7 +102,7 @@ static auto fs_iterator_gc(lua_State* state) -> int {
static const struct luaL_Reg kFileIteratorFuncs[] = {{"next", fs_iterate},
{"prev", fs_iterate_prev},
// {"clone", db_iterator_clone},
{"clone", fs_iterator_clone},
{"__call", fs_iterate},
{"__gc", fs_iterator_gc},
{NULL, NULL}};
@@ -114,7 +114,31 @@ static auto file_entry_path(lua_State* state) -> int {
return 1;
}
static auto file_entry_is_dir(lua_State* state) -> int {
LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>(
luaL_checkudata(state, 1, kFileEntryMetatable));
lua_pushboolean(state, data->isDirectory);
return 1;
}
static auto file_entry_is_hidden(lua_State* state) -> int {
LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>(
luaL_checkudata(state, 1, kFileEntryMetatable));
lua_pushboolean(state, data->isHidden);
return 1;
}
static auto file_entry_is_track(lua_State* state) -> int {
LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>(
luaL_checkudata(state, 1, kFileEntryMetatable));
lua_pushboolean(state, data->isTrack);
return 1;
}
static const struct luaL_Reg kFileEntryFuncs[] = {{"filepath", file_entry_path},
{"is_directory", file_entry_is_dir},
{"is_hidden", file_entry_is_hidden},
{"is_track", file_entry_is_track},
{"__tostring", file_entry_path},
{NULL, NULL}};