diff --git a/src/cb/CMakeLists.txt b/src/cb/CMakeLists.txt index fa9f752e6..9d89b0076 100644 --- a/src/cb/CMakeLists.txt +++ b/src/cb/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(cb src/locales/tr_tr.cpp src/locales/de_de.cpp src/locales/fr_fr.cpp + src/utils/editors.cpp src/utils/utils.cpp src/utils/formatting.cpp src/utils/files.cpp diff --git a/src/cb/src/actions/config.cpp b/src/cb/src/actions/config.cpp index 722f42d63..16d222295 100644 --- a/src/cb/src/actions/config.cpp +++ b/src/cb/src/actions/config.cpp @@ -15,6 +15,64 @@ #include "../clipboard.hpp" namespace PerformAction { -void config() {} +void config() { + // display the configuration for CB + stopIndicator(); + fprintf(stderr, "%s", formatColors("[info]┏━━[inverse] ").data()); + fprintf(stderr, "%s", cb_config_message().data()); + fprintf(stderr, "%s", formatColors(" [noinverse][info]━").data()); + int columns = thisTerminalSize().columns - ((columnLength(clipboard_name_message) + 1)); + for (int i = 0; i < columns; i++) + fprintf(stderr, "━"); + fprintf(stderr, "%s", formatColors("┓[blank]\n").data()); + + // Clipbord editor + fprintf(stderr, formatColors("[info]%s┃ Content editor: [help]%s[blank]\n").data(), generatedEndbar().data(), findUsableEditor() ? findUsableEditor().value().data() : "None"); + + // Max history size + fprintf(stderr, formatColors("[info]%s┃ Max history size: [help]%s[blank]\n").data(), generatedEndbar().data(), !maximumHistorySize.empty() ? maximumHistorySize.data() : "unlimited"); + + // Locale + fprintf(stderr, formatColors("[info]%s┃ Locale: [help]%s[blank]\n").data(), generatedEndbar().data(), !locale.empty() ? locale.data() : "default"); + + // Temporary directory + fprintf(stderr, formatColors("[info]%s┃ Temporary directory: [help]%s[blank]\n").data(), generatedEndbar().data(), global_path.temporary.string().data()); + + // Persistent directory + fprintf(stderr, formatColors("[info]%s┃ Persistent directory: [help]%s[blank]\n").data(), generatedEndbar().data(), global_path.persistent.string().data()); + + // Custom persistent clipboards + fprintf(stderr, + formatColors("[info]%s┃ Custom persistent clipboards: [help]%s[blank]\n").data(), + generatedEndbar().data(), + getenv("CLIPBOARD_CUSTOM_PERSISTENT") ? getenv("CLIPBOARD_CUSTOM_PERSISTENT") : "none"); + + // Audio + fprintf(stderr, formatColors("[info]%s┃ Audio effects: [help]%s[blank]\n").data(), generatedEndbar().data(), envVarIsTrue("CLIPBOARD_NOAUDIO") ? "disabled" : "enabled"); + + // GUI clipboard integration + fprintf(stderr, formatColors("[info]%s┃ GUI clipboard integration: [help]%s[blank]\n").data(), generatedEndbar().data(), envVarIsTrue("CLIPBOARD_NOGUI") ? "disabled" : "enabled"); + + // Remote clipboard integration + fprintf(stderr, formatColors("[info]%s┃ Remote clipboard integration: [help]%s[blank]\n").data(), generatedEndbar().data(), envVarIsTrue("CLIPBOARD_NOREMOTE") ? "disabled" : "enabled"); + + // Progress bar + fprintf(stderr, formatColors("[info]%s┃ Progress bar: [help]%s[blank]\n").data(), generatedEndbar().data(), progress_silent ? "disabled" : "enabled"); + + // Silent output + fprintf(stderr, formatColors("[info]%s┃ Silent output: [help]%s[blank]\n").data(), generatedEndbar().data(), output_silent ? "enabled" : "disabled"); + + // Theme + fprintf(stderr, formatColors("[info]%s┃ Color theme: [help]%s[blank]\n").data(), generatedEndbar().data(), getenv("CLIPBOARD_THEME") ? getenv("CLIPBOARD_THEME") : "default"); + + // Color output + fprintf(stderr, formatColors("[info]%s┃ Color output: [help]%s[blank]\n").data(), generatedEndbar().data(), no_color ? "disabled" : "enabled"); + + fprintf(stderr, "%s", formatColors("[info]┗").data()); + int cols = thisTerminalSize().columns; + for (int i = 0; i < cols - 2; i++) + fprintf(stderr, "━"); + fprintf(stderr, "%s", formatColors("┛[blank]\n").data()); +} } // namespace PerformAction \ No newline at end of file diff --git a/src/cb/src/actions/edit.cpp b/src/cb/src/actions/edit.cpp index 07e4ac516..e7419aee2 100644 --- a/src/cb/src/actions/edit.cpp +++ b/src/cb/src/actions/edit.cpp @@ -25,34 +25,7 @@ void edit() { std::ofstream temp(path.data.raw); } - auto preferredEditor = []() -> std::optional { - if (!copying.items.empty()) return copying.items.at(0).string(); - if (auto editor = getenv("CLIPBOARD_EDITOR"); editor != nullptr) return editor; - if (auto editor = getenv("EDITOR"); editor != nullptr) return editor; - if (auto editor = getenv("VISUAL"); editor != nullptr) return editor; - return std::nullopt; - }; - - auto fallbackEditor = []() -> std::optional { - constexpr std::array fallbacks {"nano", "vim", "nvim", "micro", "code", "gedit", "vi", "notepad.exe", "notepad++.exe", "wordpad.exe", "word.exe"}; - - std::string pathContent(getenv("PATH")); - std::vector paths; - - // split paths by : or ; (: for posix, ; for windows) - auto strings = regexSplit(pathContent, std::regex("[:;]")); - std::transform(strings.begin(), strings.end(), std::back_inserter(paths), [](const std::string& path) { return fs::path(path); }); - - for (const auto& path : paths) - for (const auto& fallback : fallbacks) - if (fs::exists(path / fallback)) return fallback; - - return std::nullopt; - }; - - auto editor = preferredEditor(); - - if (!editor) editor = fallbackEditor(); + auto editor = findUsableEditor(); if (!editor) error_exit("%s", formatColors("[error][inverse] ✘ [noinverse] CB couldn't find a suitable editor to use. [help]⬤ Try setting the CLIPBOARD_EDITOR environment variable.[blank]\n")); diff --git a/src/cb/src/clipboard.hpp b/src/cb/src/clipboard.hpp index 81dbd0b26..31c5ef555 100644 --- a/src/cb/src/clipboard.hpp +++ b/src/cb/src/clipboard.hpp @@ -450,11 +450,13 @@ extern Message one_clipboard_success_message; extern Message many_clipboards_success_message; extern Message clipboard_name_message; extern Message internal_error_message; +extern Message cb_config_message; extern ClipboardContent getGUIClipboard(const std::string& requested_mime); extern void writeToGUIClipboard(const ClipboardContent& clipboard); extern const bool GUIClipboardSupportsCut; extern bool playAsyncSoundEffect(const std::valarray& samples); +extern std::optional findUsableEditor(); namespace PerformAction { void copyItem(const fs::path& f, const bool use_regular_copy = copying.use_safe_copy); diff --git a/src/cb/src/locales/en_us.cpp b/src/cb/src/locales/en_us.cpp index 805c37633..b440412fc 100644 --- a/src/cb/src/locales/en_us.cpp +++ b/src/cb/src/locales/en_us.cpp @@ -118,4 +118,5 @@ Message one_clipboard_success_message = "[success][inverse] ✔ [noinverse] %s o Message many_clipboards_success_message = "[success][inverse] ✔ [noinverse] %s %lu clipboards[blank]\n"; Message clipboard_name_message = "[info][bold]Info for clipboard [help] %s[nobold]"; Message internal_error_message = "[error][inverse] ✘ [noinverse] Internal error: %s\n┃ This might be a bug, or you might be lacking " - "permissions on this system.[blank]\n"; \ No newline at end of file + "permissions on this system.[blank]\n"; +Message cb_config_message = "[info][bold]CB configuration[nobold]"; \ No newline at end of file diff --git a/src/cb/src/utils/editors.cpp b/src/cb/src/utils/editors.cpp new file mode 100644 index 000000000..d2e019266 --- /dev/null +++ b/src/cb/src/utils/editors.cpp @@ -0,0 +1,48 @@ +/* The Clipboard Project - Cut, copy, and paste anything, anytime, anywhere, all from the terminal. + Copyright (C) 2023 Jackson Huff and other contributors on GitHub.com + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see .*/ +#include "../clipboard.hpp" + +std::optional findUsableEditor() { + auto preferredEditor = []() -> std::optional { + if (!copying.items.empty()) return copying.items.at(0).string(); + if (auto editor = getenv("CLIPBOARD_EDITOR"); editor != nullptr) return editor; + if (auto editor = getenv("EDITOR"); editor != nullptr) return editor; + if (auto editor = getenv("VISUAL"); editor != nullptr) return editor; + return std::nullopt; + }; + + auto fallbackEditor = []() -> std::optional { + constexpr std::array fallbacks {"nano", "vim", "nvim", "micro", "code", "gedit", "vi", "notepad.exe", "notepad++.exe", "wordpad.exe", "word.exe"}; + + std::string pathContent(getenv("PATH")); + std::vector paths; + + // split paths by : or ; (: for posix, ; for windows) + auto strings = regexSplit(pathContent, std::regex("[:;]")); + std::transform(strings.begin(), strings.end(), std::back_inserter(paths), [](const std::string& path) { return fs::path(path); }); + + for (const auto& path : paths) + for (const auto& fallback : fallbacks) + if (fs::exists(path / fallback)) return fallback; + + return std::nullopt; + }; + + auto editor = preferredEditor(); + + if (!editor) editor = fallbackEditor(); + + return editor; +} \ No newline at end of file