Skip to content

Commit

Permalink
Add logic to Config action
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Dec 15, 2023
1 parent f69d3d7 commit 14371ba
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/cb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 59 additions & 1 deletion src/cb/src/actions/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 1 addition & 28 deletions src/cb/src/actions/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,7 @@ void edit() {
std::ofstream temp(path.data.raw);
}

auto preferredEditor = []() -> std::optional<std::string> {
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<std::string> {
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<fs::path> 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"));

Expand Down
2 changes: 2 additions & 0 deletions src/cb/src/clipboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<short>& samples);
extern std::optional<std::string> findUsableEditor();

namespace PerformAction {
void copyItem(const fs::path& f, const bool use_regular_copy = copying.use_safe_copy);
Expand Down
3 changes: 2 additions & 1 deletion src/cb/src/locales/en_us.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
"permissions on this system.[blank]\n";
Message cb_config_message = "[info][bold]CB configuration[nobold]";
48 changes: 48 additions & 0 deletions src/cb/src/utils/editors.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.*/
#include "../clipboard.hpp"

std::optional<std::string> findUsableEditor() {
auto preferredEditor = []() -> std::optional<std::string> {
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<std::string> {
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<fs::path> 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;
}

0 comments on commit 14371ba

Please sign in to comment.