-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat(finder): Add clipboard manager #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fareesh
wants to merge
13
commits into
hyprwm:main
Choose a base branch
from
fareesh:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+359
−25
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6560068
Add clipboard manager & clipboard finder
fareesh 65af8e8
Rework clipboard finder
fareesh 9b37f4c
Replace ghetto algorithm with consistent fuzzy search
fareesh 395dcd2
Pull upstream
fareesh c2b67e0
Change from prefix to launch option
fareesh 1c34c1b
refactor(clipboard): overhaul manager and finder, add flexible provid…
fareesh 687f912
Merge branch 'main' of github.com:hyprwm/hyprlauncher
fareesh a3e2458
Pull upstream
fareesh 5d2f2b4
fix notation, casing, DRY provider-switching, esoteric string split, …
fareesh 4b56f7f
Fix clipboard deletion, bonkers string split, etc.
fareesh 277a19b
Pull upstream
fareesh 4910539
Pull upstream
fareesh d937828
Merge branch 'hyprwm:main' into main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| #include "ClipboardManager.hpp" | ||
| #include <hyprutils/os/Process.hpp> | ||
| #include <iterator> | ||
| #include "../helpers/Log.hpp" | ||
| #include <ranges> | ||
| #include <sstream> | ||
|
|
||
| using namespace Hyprutils::OS; | ||
|
|
||
| CClipboardManager::CClipboardManager(const SClipboardConfig& config) : m_Config(config) {} | ||
|
|
||
| static void executeCommand(const std::string& command, const std::string& arg) { | ||
| if (command.empty()) | ||
| return; | ||
|
|
||
| bool hasPlaceholder = command.find('{') != std::string::npos; | ||
|
|
||
| if (hasPlaceholder) { | ||
| std::string finalCommand; | ||
| try { | ||
| finalCommand = std::vformat(command, std::make_format_args("\"$1\"")); | ||
| } catch (const std::format_error& e) { | ||
| Debug::log(ERR, "Invalid format string in command: {}. Error: {}", command, e.what()); | ||
| return; | ||
| } | ||
| CProcess proc("/bin/sh", {"-c", finalCommand, "-", arg}); | ||
| proc.runAsync(); | ||
| } else if (!arg.empty()) { | ||
| std::string shellCommand = "echo -n \"$1\" | " + command; | ||
| CProcess proc("/bin/sh", {"-c", shellCommand, "-", arg}); | ||
| proc.runAsync(); | ||
| } else { | ||
| CProcess proc("/bin/sh", {"-c", command}); | ||
| proc.runAsync(); | ||
| } | ||
| } | ||
|
|
||
| std::vector<SClipboardHistoryItem> CClipboardManager::getHistory() { | ||
| std::istringstream iss(m_Config.listCmd); | ||
| std::vector<std::string> parts{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}}; | ||
|
|
||
| if (parts.empty()) { | ||
| Debug::log(ERR, "Empty listCmd"); | ||
| return {}; | ||
| } | ||
|
|
||
| const std::string& binary = parts.front(); | ||
| std::vector<std::string> args(parts.begin() + 1, parts.end()); | ||
| CProcess proc(binary, args); | ||
| proc.runSync(); | ||
| const std::string output = proc.stdOut(); | ||
| const std::string errOut = proc.stdErr(); | ||
|
|
||
| if (!errOut.empty()) | ||
| Debug::log(WARN, "listCmd stderr: {}", errOut); | ||
|
|
||
| const int exitCode = proc.exitCode(); | ||
| if (exitCode != 0) { | ||
| Debug::log(ERR, "listCmd exited with code {}: {}", exitCode, m_Config.listCmd); | ||
| return {}; | ||
| } | ||
| std::vector<SClipboardHistoryItem> history; | ||
| for (auto&& line_view : std::ranges::views::split(output, '\n')) { | ||
| std::string line{line_view.begin(), line_view.end()}; | ||
| if (line.empty()) | ||
| continue; | ||
|
|
||
| const auto tabPos = line.find('\t'); | ||
| std::string display = (tabPos != std::string::npos) ? line.substr(tabPos + 1) : line; | ||
| history.emplace_back(line, std::string(display)); | ||
| } | ||
| return history; | ||
| } | ||
|
|
||
| void CClipboardManager::copyItem(const std::string& originalLine) { | ||
| executeCommand(m_Config.copyCmd, originalLine); | ||
| } | ||
|
|
||
| void CClipboardManager::deleteItem(const std::string& item) { | ||
| executeCommand(m_Config.deleteCmd, item); | ||
| } | ||
|
|
||
| SClipboardConfig CClipboardManager::getConfig() const { | ||
| return m_Config; | ||
| } | ||
This file contains hidden or 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,24 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| #include "../config/ConfigManager.hpp" | ||
|
|
||
| struct SClipboardHistoryItem { | ||
| std::string originalLine; | ||
| std::string displayLine; | ||
| }; | ||
|
|
||
| class CClipboardManager { | ||
| public: | ||
| CClipboardManager(const SClipboardConfig& config); | ||
|
|
||
| SClipboardConfig getConfig() const; | ||
| std::vector<SClipboardHistoryItem> getHistory(); | ||
|
|
||
| void copyItem(const std::string& item); | ||
| void deleteItem(const std::string& item); | ||
|
|
||
| private: | ||
| SClipboardConfig m_Config; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| #include "ClipboardEntry.hpp" | ||
| #include "../../helpers/Log.hpp" | ||
|
|
||
| CClipboardEntry::CClipboardEntry(const SClipboardHistoryItem& item, CClipboardManager* manager) : | ||
| m_Content(item.displayLine), m_OriginalLine(item.originalLine), m_Manager(manager) { | ||
| const auto tabPos = m_OriginalLine.find('\t'); | ||
| m_ID = (tabPos != std::string::npos) ? m_OriginalLine.substr(0, tabPos) : m_OriginalLine; | ||
| } | ||
|
|
||
| const std::string& CClipboardEntry::fuzzable() { | ||
| return m_OriginalLine; | ||
| } | ||
|
|
||
| eFinderTypes CClipboardEntry::type() { | ||
| return FINDER_CLIPBOARD; | ||
| } | ||
|
|
||
| void CClipboardEntry::run() { | ||
| Debug::log(LOG, "Running clipboard entry: copying and pasting"); | ||
| m_Manager->copyItem(m_ID); | ||
| } | ||
|
|
||
| void CClipboardEntry::remove() { | ||
| Debug::log(LOG, "Removing clipboard entry from history"); | ||
| m_Manager->deleteItem(m_OriginalLine); | ||
| } |
This file contains hidden or 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,20 @@ | ||
| #pragma once | ||
|
|
||
| #include "../IFinder.hpp" | ||
| #include "../../clipboard/ClipboardManager.hpp" | ||
|
|
||
| class CClipboardEntry : public IFinderResult { | ||
| public: | ||
| CClipboardEntry(const SClipboardHistoryItem& item, CClipboardManager* manager); | ||
| virtual ~CClipboardEntry() = default; | ||
|
|
||
| virtual const std::string& fuzzable(); | ||
| virtual eFinderTypes type(); | ||
| virtual void run(); | ||
| virtual void remove(); | ||
|
|
||
| std::string m_Content; | ||
| std::string m_OriginalLine; | ||
| std::string m_ID; | ||
| CClipboardManager* m_Manager; | ||
| }; |
This file contains hidden or 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,58 @@ | ||
| #include "ClipboardFinder.hpp" | ||
| #include "ClipboardEntry.hpp" | ||
| #include "../../helpers/Log.hpp" | ||
| #include "../../config/ConfigManager.hpp" | ||
| #include "../Fuzzy.hpp" | ||
|
|
||
| CClipboardFinder::CClipboardFinder() = default; | ||
|
|
||
| void CClipboardFinder::onConfigReload() { | ||
| Debug::log(LOG, "ClipboardFinder reloading config."); | ||
| m_ClipboardManager = makeUnique<CClipboardManager>(g_configManager->m_clipboardConfig); | ||
| } | ||
|
|
||
| std::vector<SFinderResult> CClipboardFinder::getResultsForQuery(const std::string& query) { | ||
| if (!m_ClipboardManager) { | ||
| Debug::log(ERR, "Clipboard manager not initialized. Cannot get history."); | ||
| return {}; | ||
| } | ||
|
|
||
| const auto HISTORY = m_ClipboardManager->getHistory(); | ||
| Debug::log(TRACE, "Fetched history has {} items.", HISTORY.size()); | ||
| std::vector<SFinderResult> results; | ||
|
|
||
| if (query.empty()) { | ||
| const size_t limit = std::min((size_t)MAX_RESULTS_PER_FINDER, HISTORY.size()); | ||
| results.reserve(limit); | ||
| for (size_t i = 0; i < limit; ++i) { | ||
| const auto& item = HISTORY[i]; | ||
| results.emplace_back(SFinderResult{ | ||
| .label = item.displayLine, | ||
| .result = makeShared<CClipboardEntry>(item, m_ClipboardManager.get()), | ||
| }); | ||
| } | ||
| } else { | ||
| std::vector<CSharedPointer<IFinderResult>> allHistoryItems; | ||
| allHistoryItems.reserve(HISTORY.size()); | ||
| for (const auto& item : HISTORY) { | ||
| allHistoryItems.emplace_back(makeShared<CClipboardEntry>(item, m_ClipboardManager.get())); | ||
| } | ||
|
|
||
| auto fuzzed = Fuzzy::getNResults(allHistoryItems, query, MAX_RESULTS_PER_FINDER); | ||
| results.reserve(fuzzed.size()); | ||
|
|
||
| for (const auto& f : fuzzed) { | ||
| const auto entry = reinterpretPointerCast<CClipboardEntry>(f); | ||
| if (!entry) | ||
| continue; | ||
|
|
||
| results.emplace_back(SFinderResult{ | ||
| .label = entry->m_Content, | ||
| .result = f, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| Debug::log(TRACE, "Returning {} filtered results.", results.size()); | ||
| return results; | ||
| } |
This file contains hidden or 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,18 @@ | ||
| #pragma once | ||
|
|
||
| #include "../IFinder.hpp" | ||
| #include "../../clipboard/ClipboardManager.hpp" | ||
|
|
||
| class CClipboardFinder : public IFinder { | ||
| public: | ||
| virtual ~CClipboardFinder() = default; | ||
| CClipboardFinder(); | ||
|
|
||
| virtual std::vector<SFinderResult> getResultsForQuery(const std::string& query); | ||
| void onConfigReload(); | ||
|
|
||
| private: | ||
| UP<CClipboardManager> m_ClipboardManager; | ||
| }; | ||
|
|
||
| inline UP<CClipboardFinder> g_clipboardFinder; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what the heck is going on here?