Support for partner file rename/delete

This commit is contained in:
Kyle Reed 2023-05-01 09:25:32 -07:00
parent bf4ed416bd
commit 2cba96ff36
4 changed files with 139 additions and 106 deletions

View File

@ -41,6 +41,22 @@ std::string truncate(const std::filesystem::path& path, size_t max_length = 25)
return name.length() <= max_length ? name : name.substr(0, max_length); return name.length() <= max_length ? name : name.substr(0, max_length);
} }
// Gets a human readable size string.
std::string get_pretty_size(uint32_t file_size) {
static const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
size_t suffix_index = 0;
while (file_size >= 1024) {
file_size /= 1024;
suffix_index++;
}
if (suffix_index > 4)
suffix_index = 4;
return to_string_dec_uint(file_size) + suffix[suffix_index];
}
// Case insensitive path equality on underlying "native" string. // Case insensitive path equality on underlying "native" string.
bool iequal( bool iequal(
const std::filesystem::path& lhs, const std::filesystem::path& lhs,
@ -67,7 +83,7 @@ void insert_sorted(std::vector<fileman_entry>& entries, fileman_entry&& entry) {
[](const fileman_entry& lhs, const fileman_entry& rhs) { [](const fileman_entry& lhs, const fileman_entry& rhs) {
if (lhs.is_directory && !rhs.is_directory) if (lhs.is_directory && !rhs.is_directory)
return true; return true;
else if (rhs.is_directory) else if (!lhs.is_directory && rhs.is_directory)
return false; return false;
else else
return lhs.path < rhs.path; return lhs.path < rhs.path;
@ -76,6 +92,41 @@ void insert_sorted(std::vector<fileman_entry>& entries, fileman_entry&& entry) {
entries.insert(it, std::move(entry)); entries.insert(it, std::move(entry));
} }
// Returns the partner file path or an empty path if no partner is found.
std::filesystem::path get_partner_file(std::filesystem::path path) {
const auto txt_path = std::filesystem::path{ u".TXT" };
const auto c16_path = std::filesystem::path{ u".C16" };
auto ext = path.extension();
if (iequal(ext, txt_path))
ext = c16_path;
else if (iequal(ext, c16_path))
ext = txt_path;
else
return { };
path.replace_extension(ext);
return file_exists(path) ? path : std::filesystem::path{ };
}
// Modal prompt to update the partner file.
// Returns true if user wants to update the partner file too.
bool partner_file_prompt(NavigationView& nav, const std::filesystem::path& partner) {
bool result = false;
if (partner.empty())
return result;
nav.push<ModalMessageView>(
"Partner File Exists",
partner.filename().string() + "\nUpdate this file too?",
YESNO,
[&result](bool choice) { result = choice; }
);
return result;
}
} }
namespace ui { namespace ui {
@ -102,11 +153,11 @@ void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_p
// Add "parent" directory if not at the root. // Add "parent" directory if not at the root.
if (!dir_path.empty()) if (!dir_path.empty())
entry_list.insert(entry_list.begin(), { u"..", 0, true }); entry_list.insert(entry_list.begin(), { parent_dir_path, 0, true });
} }
std::filesystem::path FileManBaseView::get_selected_full_path() const { std::filesystem::path FileManBaseView::get_selected_full_path() const {
if (get_selected_entry().path == std::filesystem::path(u"..")) if (get_selected_entry().path == parent_dir_path)
return current_path.parent_path(); return current_path.parent_path();
return current_path / get_selected_entry().path; return current_path / get_selected_entry().path;
@ -160,7 +211,6 @@ void FileManBaseView::focus() {
} }
void FileManBaseView::refresh_list() { void FileManBaseView::refresh_list() {
// TODO: stash previous selected so scroll isn't reset.
if (on_refresh_widgets) if (on_refresh_widgets)
on_refresh_widgets(false); on_refresh_widgets(false);
@ -181,19 +231,8 @@ void FileManBaseView::refresh_list() {
}); });
} else { } else {
auto file_size = entry.size;
size_t suffix_index = 0;
while (file_size >= 1024) {
file_size /= 1024;
suffix_index++;
}
if (suffix_index > 4)
suffix_index = 4;
std::string size_str = to_string_dec_uint(file_size) + suffix[suffix_index];
const auto& assoc = get_assoc(entry.path.extension()); const auto& assoc = get_assoc(entry.path.extension());
auto size_str = get_pretty_size(entry.size);
menu_view.add_item({ menu_view.add_item({
entry_name + std::string(21 - entry_name.length(), ' ') + size_str, entry_name + std::string(21 - entry_name.length(), ' ') + size_str,
@ -267,7 +306,7 @@ FileLoadView::FileLoadView(
refresh_list(); refresh_list();
on_select_entry = [&nav, this]() { on_select_entry = [this]() {
if (get_selected_entry().is_directory) { if (get_selected_entry().is_directory) {
load_directory_contents(get_selected_full_path()); load_directory_contents(get_selected_full_path());
refresh_list(); refresh_list();
@ -279,67 +318,71 @@ FileLoadView::FileLoadView(
}; };
} }
void FileManagerView::on_rename(NavigationView& nav) { void FileManagerView::on_rename() {
auto& entry = get_selected_entry(); // Don't allow rename of ".."
if (get_selected_entry().path == parent_dir_path)
return;
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) { auto& entry = get_selected_entry();
std::string destination_path = current_path.string(); auto name = entry.path.filename().string();
if (destination_path.back() != '/') auto orig_ext = entry.path.extension();
destination_path += '/';
destination_path = destination_path + buffer; uint32_t cursor_pos = (uint32_t)name.length();
rename_file(get_selected_full_path(), destination_path); if (auto pos = name.find_last_of("."); pos != name.npos)
load_directory_contents(current_path); cursor_pos = pos;
refresh_list();
}); text_prompt(nav_, name, cursor_pos, max_filename_length,
[this, &entry, &orig_ext](std::string& renamed) {
auto renamed_path = std::filesystem::path{ renamed };
rename_file(get_selected_full_path(), current_path / renamed_path);
if (iequal(renamed_path.extension(), orig_ext)) {
auto partner = get_partner_file(current_path / entry.path);
if (partner_file_prompt(nav_, partner)) {
auto new_name = renamed_path.replace_extension(partner.extension());
rename_file(partner, current_path / new_name);
}
}
load_directory_contents(current_path);
refresh_list();
});
} }
/*void FileManagerView::on_refactor(NavigationView& nav) { void FileManagerView::on_delete() {
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) { // Don't allow delete of ".."
if (get_selected_entry().path == parent_dir_path)
return;
std::string destination_path = current_path.string(); auto& entry = get_selected_entry();
if (destination_path.back() != '/')//if the path is not ended with '/', add '/' auto name = entry.path.filename().string();
destination_path += '/'; nav_.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
[this, &entry](bool choice) {
if (choice) {
delete_file(get_selected_full_path());
auto selected_path = get_selected_full_path(); auto partner = get_partner_file(current_path / entry.path);
auto extension = selected_path.extension().string(); if (partner_file_prompt(nav_, partner))
delete_file(partner);
if(extension.empty()){// Is Dir load_directory_contents(current_path);
destination_path = destination_path + buffer; refresh_list();
extension_buffer = ""; }
}else{//is File
destination_path = destination_path + buffer + extension_buffer;
}
rename_file(get_selected_full_path(), destination_path); //rename the selected file
if (!extension.empty() && selected_path.string().back() != '/' && extension.substr(1) == "C16") { //substr(1) is for ignore the dot
// Rename its partner ( C16 <-> TXT ) file.
auto partner_file_path = selected_path.string().substr(0, selected_path.string().size() - 4) + ".TXT";
destination_path = destination_path.substr(0, destination_path.size() - 4) + ".TXT";
rename_file(partner_file_path, destination_path);
} else if (!extension.empty() && selected_path.string().back() != '/' && extension.substr(1) == "TXT") {
// If the file user choose is a TXT file.
auto partner_file_path = selected_path.string().substr(0, selected_path.string().size() - 4) + ".C16";
destination_path = destination_path.substr(0, destination_path.size() - 4) + ".C16";
rename_file(partner_file_path, destination_path);
} }
);
}
void FileManagerView::on_new_dir() {
std::string name;
text_prompt(nav_, name, max_filename_length, [this](std::string& dir_name) {
make_new_directory(current_path / dir_name);
load_directory_contents(current_path); load_directory_contents(current_path);
refresh_list(); refresh_list();
}); });
}*/
void FileManagerView::on_delete() {
delete_file(get_selected_full_path());
load_directory_contents(current_path);
refresh_list();
} }
void FileManagerView::refresh_widgets(const bool v) { void FileManagerView::refresh_widgets(const bool v) {
button_rename.hidden(v); button_rename.hidden(v);
//button_refactor.hidden(v);
button_delete.hidden(v); button_delete.hidden(v);
button_new_dir.hidden(v); button_new_dir.hidden(v);
set_dirty(); set_dirty();
@ -384,40 +427,16 @@ FileManagerView::FileManagerView(
button_rename.focus(); button_rename.focus();
}; };
button_rename.on_select = [this, &nav](Button&) { button_rename.on_select = [this](Button&) {
on_rename(nav); on_rename();
}; };
/*button_refactor.on_select = [this, &nav](Button&) { button_delete.on_select = [this](Button&) {
name_buffer = entry_list[menu_view.highlighted_index()].path.filename().string().substr(0, max_filename_length); on_delete();
size_t pos = name_buffer.find_last_of(".");
if (pos != std::string::npos) {
extension_buffer = name_buffer.substr(pos);
name_buffer = name_buffer.substr(0, pos);
}
on_refactor(nav);
};*/
button_delete.on_select = [this, &nav](Button&) {
auto name = get_selected_entry().path.filename().string();
nav.push<ModalMessageView>("Delete", "Delete " + name + "\nAre you sure?", YESNO,
[this](bool choice) {
if (choice)
on_delete();
}
);
}; };
button_new_dir.on_select = [this, &nav](Button&) { button_new_dir.on_select = [this](Button&) {
name_buffer.clear(); on_new_dir();
text_prompt(nav, name_buffer, max_filename_length, [this](std::string& buffer) {
make_new_directory(current_path / buffer);
load_directory_contents(current_path);
refresh_list();
});
}; };
} }
} }

View File

@ -52,15 +52,13 @@ public:
std::string title() const override { return "Fileman"; }; std::string title() const override { return "Fileman"; };
protected: protected:
static constexpr size_t max_filename_length = 64 - 2; // Necessary? static constexpr size_t max_filename_length = 64; // Necessary?
struct file_assoc_t { struct file_assoc_t {
std::filesystem::path extension; std::filesystem::path extension;
const Bitmap* icon; const Bitmap* icon;
ui::Color color; ui::Color color;
}; };
const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" };
const std::vector<file_assoc_t> file_types = { const std::vector<file_assoc_t> file_types = {
{ u".TXT", &bitmap_icon_file_text, ui::Color::white() }, { u".TXT", &bitmap_icon_file_text, ui::Color::white() },
@ -82,6 +80,7 @@ protected:
std::function<void(bool)> on_refresh_widgets { nullptr }; std::function<void(bool)> on_refresh_widgets { nullptr };
std::vector<fileman_entry> entry_list { }; std::vector<fileman_entry> entry_list { };
const std::filesystem::path parent_dir_path { u".." };
std::filesystem::path current_path { u"" }; std::filesystem::path current_path { u"" };
std::filesystem::path extension_filter { u"" }; std::filesystem::path extension_filter { u"" };
@ -148,9 +147,9 @@ private:
std::string extension_buffer { }; std::string extension_buffer { };
void refresh_widgets(const bool v); void refresh_widgets(const bool v);
void on_rename(NavigationView& nav); void on_rename();
//void on_refactor(NavigationView& nav);
void on_delete(); void on_delete();
void on_new_dir();
Labels labels { Labels labels {
{ { 0, 26 * 8 }, "Created ", Color::light_grey() } { { 0, 26 * 8 }, "Created ", Color::light_grey() }
@ -166,7 +165,7 @@ private:
"Rename" "Rename"
}; };
Button button_copy { /*Button button_copy {
{ 10 * 8, 29 * 8, 10 * 8, 32 }, { 10 * 8, 29 * 8, 10 * 8, 32 },
"Copy" "Copy"
}; };
@ -174,7 +173,7 @@ private:
Button button_move { Button button_move {
{ 10 * 8, 29 * 8, 10 * 8, 32 }, { 10 * 8, 29 * 8, 10 * 8, 32 },
"Move" "Move"
}; };*/
Button button_delete { Button button_delete {
{ 21 * 8, 29 * 8, 9 * 8, 32 }, { 21 * 8, 29 * 8, 9 * 8, 32 },
@ -190,7 +189,6 @@ private:
{ 0 * 8, 34 * 8, 14 * 8, 32 }, { 0 * 8, 34 * 8, 14 * 8, 32 },
"New Dir" "New Dir"
}; };
}; };
} /* namespace ui */ } /* namespace ui */

View File

@ -197,6 +197,13 @@ std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::
return directory_list; return directory_list;
} }
bool file_exists(const std::filesystem::path& file_path) {
FILINFO filinfo;
auto fr = f_stat(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
return fr == FR_OK;
}
uint32_t delete_file(const std::filesystem::path& file_path) { uint32_t delete_file(const std::filesystem::path& file_path) {
return f_unlink(reinterpret_cast<const TCHAR*>(file_path.c_str())); return f_unlink(reinterpret_cast<const TCHAR*>(file_path.c_str()));
} }
@ -251,12 +258,11 @@ std::string filesystem_error::what() const {
} }
path path::parent_path() const { path path::parent_path() const {
const auto t = filename().native(); const auto index = _s.find_last_of(preferred_separator);
const auto index = t.find_last_of(preferred_separator); if( index == _s.npos ) {
if( index == t.npos ) { return { }; // NB: Deviation from STL.
return *this;
} else { } else {
return t.substr(0, index); return _s.substr(0, index);
} }
} }
@ -318,6 +324,12 @@ bool operator>(const path& lhs, const path& rhs) {
return lhs.native() > rhs.native(); return lhs.native() > rhs.native();
} }
path operator+(const path& lhs, const path& rhs) {
path result = lhs;
result += rhs;
return result;
}
path operator/(const path& lhs, const path& rhs) { path operator/(const path& lhs, const path& rhs) {
path result = lhs; path result = lhs;
result /= rhs; result /= rhs;

View File

@ -168,6 +168,7 @@ private:
bool operator==(const path& lhs, const path& rhs); bool operator==(const path& lhs, const path& rhs);
bool operator<(const path& lhs, const path& rhs); bool operator<(const path& lhs, const path& rhs);
bool operator>(const path& lhs, const path& rhs); bool operator>(const path& lhs, const path& rhs);
path operator+(const path& lhs, const path& rhs);
path operator/(const path& lhs, const path& rhs); path operator/(const path& lhs, const path& rhs);
using file_status = BYTE; using file_status = BYTE;
@ -248,6 +249,7 @@ struct FATTimestamp {
uint16_t FAT_time; uint16_t FAT_time;
}; };
bool file_exists(const std::filesystem::path& file_path);
uint32_t delete_file(const std::filesystem::path& file_path); uint32_t delete_file(const std::filesystem::path& file_path);
uint32_t rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name); uint32_t rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name);
FATTimestamp file_created_date(const std::filesystem::path& file_path); FATTimestamp file_created_date(const std::filesystem::path& file_path);
@ -255,6 +257,8 @@ uint32_t make_new_directory(const std::filesystem::path& dir_path);
std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory, const std::filesystem::path& extension); std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory, const std::filesystem::path& extension);
std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::path& directory); std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::path& directory);
/* Gets an auto incrementing filename. */
std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_stem_pattern); std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_stem_pattern);
/* Values added to FatFs FRESULT enum, values outside the FRESULT data type */ /* Values added to FatFs FRESULT enum, values outside the FRESULT data type */