From 71a87f8f41c191f4144a556f9158f17640cd5ad6 Mon Sep 17 00:00:00 2001 From: Alexander Batischev Date: Wed, 2 Oct 2024 20:56:15 +0300 Subject: Remove some implicit Filepath conversions from {File,Dir}BrowserFormAction --- src/dirbrowserformaction.cpp | 17 +++++------------ src/filebrowserformaction.cpp | 29 +++++++---------------------- test/filepath.cpp | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/dirbrowserformaction.cpp b/src/dirbrowserformaction.cpp index 296a0a4e..92e26175 100644 --- a/src/dirbrowserformaction.cpp +++ b/src/dirbrowserformaction.cpp @@ -62,24 +62,17 @@ bool DirBrowserFormAction::process_operation(Operation op, selection.name, status); files_list.set_position(0); - std::string fn = utils::getcwd(); + const auto fn = utils::getcwd(); update_title(fn); - if (fn.back() != NEWSBEUTER_PATH_SEP) { - fn.push_back(NEWSBEUTER_PATH_SEP); - } - - set_value("filenametext", fn); + const auto fn_with_trailing_slash = fn.join(Filepath{}); + set_value("filenametext", fn_with_trailing_slash.to_locale_string()); do_redraw = true; } break; case file_system::FileType::RegularFile: { - std::string fn = utils::getcwd(); - if (fn.back() != NEWSBEUTER_PATH_SEP) { - fn.push_back(NEWSBEUTER_PATH_SEP); - } - fn.append(selection.name); - set_value("filenametext", fn); + const auto filename = utils::getcwd().join(selection.name); + set_value("filenametext", filename.to_locale_string()); f.set_focus("filename"); } break; diff --git a/src/filebrowserformaction.cpp b/src/filebrowserformaction.cpp index a91f4992..f55debf7 100644 --- a/src/filebrowserformaction.cpp +++ b/src/filebrowserformaction.cpp @@ -61,35 +61,20 @@ bool FileBrowserFormAction::process_operation(Operation op, selection.name, status); files_list.set_position(0); - std::string fn = utils::getcwd(); + auto fn = utils::getcwd(); update_title(fn); - if (fn.back() != NEWSBEUTER_PATH_SEP) { - fn.push_back(NEWSBEUTER_PATH_SEP); + const auto fnstr = Filepath::from_locale_string(f.get("filenametext")).file_name(); + if (fnstr) { + fn.push(*fnstr); } - - const std::string fnstr = - f.get("filenametext"); - const std::string::size_type base = - fnstr.find_last_of(NEWSBEUTER_PATH_SEP); - if (base == std::string::npos) { - fn.append(fnstr); - } else { - fn.append(fnstr, - base + 1, - std::string::npos); - } - set_value("filenametext", fn); + set_value("filenametext", fn.to_locale_string()); do_redraw = true; } break; case file_system::FileType::RegularFile: { - std::string fn = utils::getcwd(); - if (fn.back() != NEWSBEUTER_PATH_SEP) { - fn.push_back(NEWSBEUTER_PATH_SEP); - } - fn.append(selection.name); - set_value("filenametext", fn); + const auto filename = utils::getcwd().join(selection.name); + set_value("filenametext", filename.to_locale_string()); f.set_focus("filename"); } break; diff --git a/test/filepath.cpp b/test/filepath.cpp index 81a26eb4..a0d05ba3 100644 --- a/test/filepath.cpp +++ b/test/filepath.cpp @@ -41,6 +41,14 @@ TEST_CASE("push() adds a new component to the path", "[Filepath]") REQUIRE(dir == Filepath::from_locale_string("/tmp/newsboat/.local/share/cache/cache.db")); } +TEST_CASE("push() still adds a separator to non-empty path if new component is empty", + "[Filepath]") +{ + auto dir = Filepath::from_locale_string("/root"); + dir.push(Filepath::from_locale_string("")); + REQUIRE(dir.display() == "/root/"); +} + TEST_CASE("Can be extended with join()", "[Filepath]") { const auto tmp = Filepath::from_locale_string("/tmp"); @@ -52,6 +60,14 @@ TEST_CASE("Can be extended with join()", "[Filepath]") REQUIRE(subdir == Filepath::from_locale_string("/tmp/newsboat/tests")); } +TEST_CASE("join() still adds a separator to non-empty path if new component is empty", + "[Filepath]") +{ + const auto path = Filepath::from_locale_string("relative path"); + const auto path_with_trailing_slash = path.join(Filepath{}); + REQUIRE(path_with_trailing_slash.display() == "relative path/"); +} + TEST_CASE("Can be copied", "[Filepath]") { auto original = Filepath::from_locale_string("/etc/hosts"); -- cgit v1.2.3