From ee9c18c118eae28f0a299bb8900df74e66e776d4 Mon Sep 17 00:00:00 2001 From: asynts Date: Mon, 5 Oct 2020 14:59:50 +0200 Subject: [PATCH] FileManager: Use new format functions. --- Applications/FileManager/DirectoryView.cpp | 40 +++++++------------ Applications/FileManager/FileUtils.cpp | 26 ++++++------ Applications/FileManager/PropertiesDialog.cpp | 16 ++++---- Applications/FileManager/main.cpp | 18 ++++----- 4 files changed, 44 insertions(+), 56 deletions(-) diff --git a/Applications/FileManager/DirectoryView.cpp b/Applications/FileManager/DirectoryView.cpp index 9fcd7cbd4e..45b3eeea96 100644 --- a/Applications/FileManager/DirectoryView.cpp +++ b/Applications/FileManager/DirectoryView.cpp @@ -97,7 +97,7 @@ void DirectoryView::handle_activation(const GUI::ModelIndex& index) { if (!index.is_valid()) return; - dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr()); + dbgln("on activation: {},{}, this={:p}, m_model={:p}", index.row(), index.column(), this, m_model.ptr()); auto& node = this->node(index); auto path = node.full_path(); @@ -162,7 +162,7 @@ void DirectoryView::setup_model() m_model->on_error = [this](int, const char* error_string) { auto failed_path = m_model->root_path(); - auto error_message = String::format("Could not read %s:\n%s", failed_path.characters(), error_string); + auto error_message = String::formatted("Could not read {}:\n{}", failed_path, error_string); m_error_label->set_text(error_message); set_active_widget(m_error_label); @@ -359,7 +359,7 @@ void DirectoryView::set_status_message(const StringView& message) void DirectoryView::open_parent_directory() { - auto path = String::format("%s/..", model().root_path().characters()); + auto path = String::formatted("{}/..", model().root_path()); model().set_root_path(path); } @@ -387,10 +387,9 @@ void DirectoryView::update_statusbar() { size_t total_size = model().node({}).total_size; if (current_view().selection().is_empty()) { - set_status_message(String::format("%d item%s (%s)", + set_status_message(String::formatted("{} item(s) ({})", model().row_count(), - model().row_count() != 1 ? "s" : "", - human_readable_size(total_size).characters())); + human_readable_size(total_size))); return; } @@ -485,14 +484,11 @@ void DirectoryView::setup_actions() m_mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) { String value; if (GUI::InputBox::show(value, window(), "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) { - auto new_dir_path = LexicalPath::canonicalized_path( - String::format("%s/%s", - path().characters(), - value.characters())); + auto new_dir_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", path(), value)); int rc = mkdir(new_dir_path.characters(), 0777); if (rc < 0) { auto saved_errno = errno; - GUI::MessageBox::show(window(), String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(window(), String::formatted("mkdir(\"{}\") failed: {}", new_dir_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); } } }); @@ -500,25 +496,22 @@ void DirectoryView::setup_actions() m_touch_action = GUI::Action::create("New file...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) { String value; if (GUI::InputBox::show(value, window(), "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) { - auto new_file_path = LexicalPath::canonicalized_path( - String::format("%s/%s", - path().characters(), - value.characters())); + auto new_file_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", path(), value)); struct stat st; int rc = stat(new_file_path.characters(), &st); if ((rc < 0 && errno != ENOENT)) { auto saved_errno = errno; - GUI::MessageBox::show(window(), String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(window(), String::formatted("stat(\"{}\") failed: {}", new_file_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); return; } if (rc == 0) { - GUI::MessageBox::show(window(), String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(window(), String::formatted("{}: Already exists", new_file_path), "Error", GUI::MessageBox::Type::Error); return; } int fd = creat(new_file_path.characters(), 0666); if (fd < 0) { auto saved_errno = errno; - GUI::MessageBox::show(window(), String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(window(), String::format("creat(\"{}\") failed: {}", new_file_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error); return; } rc = close(fd); @@ -555,7 +548,7 @@ void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEve return; auto urls = event.mime_data().urls(); if (urls.is_empty()) { - dbg() << "No files to drop"; + dbgln("No files to drop"); return; } @@ -566,17 +559,12 @@ void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEve for (auto& url_to_copy : urls) { if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path()) continue; - auto new_path = String::format("%s/%s", - target_node.full_path().characters(), - LexicalPath(url_to_copy.path()).basename().characters()); - + auto new_path = String::formatted("{}/{}", target_node.full_path(), LexicalPath(url_to_copy.path()).basename()); if (url_to_copy.path() == new_path) continue; if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) { - auto error_message = String::format("Could not copy %s into %s.", - url_to_copy.to_string().characters(), - new_path.characters()); + auto error_message = String::formatted("Could not copy {} into {}.", url_to_copy.to_string(), new_path); GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error); } } diff --git a/Applications/FileManager/FileUtils.cpp b/Applications/FileManager/FileUtils.cpp index 665fcbb0b2..86408c21b1 100644 --- a/Applications/FileManager/FileUtils.cpp +++ b/Applications/FileManager/FileUtils.cpp @@ -42,9 +42,9 @@ void delete_paths(const Vector& paths, bool should_confirm, GUI::Window* { String message; if (paths.size() == 1) { - message = String::format("Really delete %s?", LexicalPath(paths[0]).basename().characters()); + message = String::formatted("Really delete {}?", LexicalPath(paths[0]).basename()); } else { - message = String::format("Really delete %d files?", paths.size()); + message = String::formatted("Really delete {} files?", paths.size()); } if (should_confirm) { @@ -61,7 +61,7 @@ void delete_paths(const Vector& paths, bool should_confirm, GUI::Window* struct stat st; if (lstat(path.characters(), &st)) { GUI::MessageBox::show(parent_window, - String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)), + String::formatted("lstat({}) failed: {}", path, strerror(errno)), "Delete failed", GUI::MessageBox::Type::Error); break; @@ -73,7 +73,7 @@ void delete_paths(const Vector& paths, bool should_confirm, GUI::Window* if (error) { GUI::MessageBox::show(parent_window, - String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)), + String::formatted("Failed to delete directory \"{}\": {}", error_path, strerror(error)), "Delete failed", GUI::MessageBox::Type::Error); break; @@ -81,7 +81,7 @@ void delete_paths(const Vector& paths, bool should_confirm, GUI::Window* } else if (unlink(path.characters()) < 0) { int saved_errno = errno; GUI::MessageBox::show(parent_window, - String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)), + String::formatted("unlink(\"{}\") failed: {}", path, strerror(saved_errno)), "Delete failed", GUI::MessageBox::Type::Error); break; @@ -98,7 +98,7 @@ int delete_directory(String directory, String& file_that_caused_error) } while (iterator.has_next()) { - auto file_to_delete = String::format("%s/%s", directory.characters(), iterator.next_path().characters()); + auto file_to_delete = String::formatted("{}/{}", directory, iterator.next_path()); struct stat st; if (lstat(file_to_delete.characters(), &st)) { @@ -165,8 +165,8 @@ bool copy_directory(const String& src_path, const String& dst_path, const struct while (di.has_next()) { String filename = di.next_path(); bool is_copied = copy_file_or_directory( - String::format("%s/%s", src_path.characters(), filename.characters()), - String::format("%s/%s", dst_path.characters(), filename.characters())); + String::formatted("{}/{}", src_path, filename), + String::formatted("{}/{}", dst_path, filename)); if (!is_copied) { return false; } @@ -188,7 +188,7 @@ bool copy_file(const String& dst_path, const struct stat& src_stat, Core::File& if (errno != EISDIR) { return false; } - auto dst_dir_path = String::format("%s/%s", dst_path.characters(), LexicalPath(source.filename()).basename().characters()); + auto dst_dir_path = String::formatted("{}/{}", dst_path, LexicalPath(source.filename()).basename()); dst_fd = creat(dst_dir_path.characters(), 0666); if (dst_fd < 0) { return false; @@ -244,17 +244,17 @@ String get_duplicate_name(const String& path, int duplicate_count) StringBuilder duplicated_name; duplicated_name.append('/'); for (size_t i = 0; i < lexical_path.parts().size() - 1; ++i) { - duplicated_name.appendf("%s/", lexical_path.parts()[i].characters()); + duplicated_name.appendff("{}/", lexical_path.parts()[i]); } - auto prev_duplicate_tag = String::format("(%d)", duplicate_count); + auto prev_duplicate_tag = String::formatted("({})", duplicate_count); auto title = lexical_path.title(); if (title.ends_with(prev_duplicate_tag)) { // remove the previous duplicate tag "(n)" so we can add a new tag. title = title.substring(0, title.length() - prev_duplicate_tag.length()); } - duplicated_name.appendf("%s (%d)", lexical_path.title().characters(), duplicate_count); + duplicated_name.appendff("{} ({})", lexical_path.title(), duplicate_count); if (!lexical_path.extension().is_empty()) { - duplicated_name.appendf(".%s", lexical_path.extension().characters()); + duplicated_name.appendff(".{}", lexical_path.extension()); } return duplicated_name.build(); } diff --git a/Applications/FileManager/PropertiesDialog.cpp b/Applications/FileManager/PropertiesDialog.cpp index c972316e51..04bf4851f1 100644 --- a/Applications/FileManager/PropertiesDialog.cpp +++ b/Applications/FileManager/PropertiesDialog.cpp @@ -127,9 +127,9 @@ PropertiesDialog::PropertiesDialog(const String& path, bool disable_rename, Wind } } - properties.append({ "Size:", String::format("%zu bytes", st.st_size) }); - properties.append({ "Owner:", String::format("%s (%lu)", owner_name.characters(), st.st_uid) }); - properties.append({ "Group:", String::format("%s (%lu)", group_name.characters(), st.st_gid) }); + properties.append({ "Size:", String::formatted("{} bytes", st.st_size) }); + properties.append({ "Owner:", String::formatted("{} ({})", owner_name, st.st_uid) }); + properties.append({ "Group:", String::formatted("{} ({})", group_name, st.st_gid) }); properties.append({ "Created at:", GUI::FileSystemModel::timestamp_string(st.st_ctime) }); properties.append({ "Last modified:", GUI::FileSystemModel::timestamp_string(st.st_mtime) }); @@ -172,7 +172,7 @@ void PropertiesDialog::update() { auto bitmap = GUI::FileIconProvider::icon_for_path(m_name, m_mode).bitmap_for_size(32); m_icon->set_bitmap(bitmap); - set_title(String::format("%s - Properties", m_name.characters())); + set_title(String::formatted("{} - Properties", m_name)); } void PropertiesDialog::permission_changed(mode_t mask, bool set) @@ -189,7 +189,7 @@ void PropertiesDialog::permission_changed(mode_t mask, bool set) String PropertiesDialog::make_full_path(const String& name) { - return String::format("%s/%s", m_parent_path.characters(), name.characters()); + return String::formatted("{}/{}", m_parent_path, name); } bool PropertiesDialog::apply_changes() @@ -199,12 +199,12 @@ bool PropertiesDialog::apply_changes() String new_file = make_full_path(new_name).characters(); if (GUI::FilePicker::file_exists(new_file)) { - GUI::MessageBox::show(this, String::format("A file \"%s\" already exists!", new_name.characters()), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(this, String::formatted("A file \"{}\" already exists!", new_name), "Error", GUI::MessageBox::Type::Error); return false; } if (rename(make_full_path(m_name).characters(), new_file.characters())) { - GUI::MessageBox::show(this, String::format("Could not rename file: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(this, String::formatted("Could not rename file: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error); return false; } @@ -215,7 +215,7 @@ bool PropertiesDialog::apply_changes() if (m_permissions_dirty) { if (chmod(make_full_path(m_name).characters(), m_mode)) { - GUI::MessageBox::show(this, String::format("Could not update permissions: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(this, String::formatted("Could not update permissions: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error); return false; } diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 85b3143eee..cb0d07c45c 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -333,7 +333,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio StringBuilder copy_text; for (auto& path : paths) { auto url = URL::create_with_file_protocol(path); - copy_text.appendf("%s\n", url.to_string().characters()); + copy_text.appendff("{}\n", url); } GUI::Clipboard::the().set_data(copy_text.build().bytes(), "text/uri-list"); }, @@ -370,12 +370,12 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio auto do_paste = [&](const GUI::Action& action) { auto data_and_type = GUI::Clipboard::the().data_and_type(); if (data_and_type.mime_type != "text/uri-list") { - dbg() << "Cannot paste clipboard type " << data_and_type.mime_type; + dbgln("Cannot paste clipboard type {}", data_and_type.mime_type); return; } auto copied_lines = String::copy(data_and_type.data).split('\n'); if (copied_lines.is_empty()) { - dbg() << "No files to paste"; + dbgln("No files to paste"); return; } @@ -390,13 +390,13 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio continue; URL url = uri_as_string; if (!url.is_valid() || url.protocol() != "file") { - dbg() << "Cannot paste URI " << uri_as_string; + dbgln("Cannot paste URI {}", uri_as_string); continue; } - auto new_path = String::format("%s/%s", target_directory.characters(), url.basename().characters()); + auto new_path = String::formatted("{}/{}", target_directory, url.basename()); if (!FileUtils::copy_file_or_directory(url.path(), new_path)) { - auto error_message = String::format("Could not paste %s.", url.path().characters()); + auto error_message = String::formatted("Could not paste {}.", url.path()); GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error); } else { refresh_tree_view(); @@ -521,7 +521,7 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio window->set_icon(bitmap); location_textbox.set_icon(bitmap); - window->set_title(String::format("%s - File Manager", new_path.characters())); + window->set_title(String::formatted("{} - File Manager", new_path)); location_textbox.set_text(new_path); if (!is_reacting_to_tree_view_selection_change) { @@ -615,9 +615,9 @@ int run_in_windowed_mode(RefPtr config, String initial_locatio directory_view.launch(URL::create_with_file_protocol(full_path), launcher_handler); }); if (default_file_handler->details().launcher_type == Desktop::Launcher::LauncherType::Application) - file_open_action->set_text(String::format("Run %s", file_open_action->text().characters())); + file_open_action->set_text(String::formatted("Run {}", file_open_action->text())); else - file_open_action->set_text(String::format("Open in %s", file_open_action->text().characters())); + file_open_action->set_text(String::formatted("Open in {}", file_open_action->text())); file_context_menu_action_default_action = file_open_action;