-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f69d3d7
commit 14371ba
Showing
6 changed files
with
113 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |