Escape paths in file dialog

This commit is contained in:
Nikita Lisitsa 2023-01-05 16:08:47 +03:00
parent 361ef9fd7a
commit 1b8339ed5d

View file

@ -98,6 +98,23 @@ namespace psemek::ui
gfx::texture_2d file_;
};
std::string replace(std::string str, std::string const & match, std::string const & replacement)
{
for (std::size_t i = 0; (i = str.find(match, i)) != std::string::npos;)
{
str.replace(i, match.size(), replacement);
i += replacement.size();
}
return str;
}
std::string escape(std::string str)
{
str = replace(std::move(str), "[", "\\[");
str = replace(std::move(str), "]", "\\]");
return str;
}
}
std::shared_ptr<window> make_file_dialog(file_dialog_options const & options)
@ -217,9 +234,9 @@ namespace psemek::ui
for (auto const & component : new_path)
{
if (component.string() != "/")
path_str += "[link:" + util::to_string(++index) + "]" + component.string() + "[/link]/";
path_str += "[link:" + util::to_string(++index) + "]" + escape(component.string()) + "[/link]/";
else
path_str += "[link:" + util::to_string(++index) + "]" + component.string() + "[/link]";
path_str += "[link:" + util::to_string(++index) + "]" + escape(component.string()) + "[/link]";
}
current_path_label->set_tagged_text(std::move(path_str));
@ -265,7 +282,7 @@ namespace psemek::ui
contents_str += "[image:file]";
contents_str += "[link:" + util::to_string(index++) + "]";
contents_str += entry.path.filename().string() + "[/link]\n";
contents_str += escape(entry.path.filename().string()) + "[/link]\n";
}
directory_view->set_tagged_text(std::move(contents_str));
directory_view_scroller->set_position(scroller::direction::vertical, 0.f, false);