-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcliphist.cpp
115 lines (102 loc) · 3.45 KB
/
cliphist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// https://devblogs.microsoft.com/oldnewthing/20230302-00/?p=107889
// eg cliphist.exe | fzf -d "\x1F" --with-nth=2 --bind "enter:become(echo {1})" --preview "cliphist view {1}" | lineargs cliphist select {}
#include <stdio.h>
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <cstring>
#include <string>
namespace winrt
{
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::ApplicationModel::DataTransfer;
}
void replace_all(std::wstring& str, std::wstring_view find, std::wstring_view replace)
{
std::wstring::size_type pos = 0;
while ((pos = str.find(find, pos)) != std::wstring::npos)
{
str.replace(pos, find.length(), replace);
}
}
winrt::ClipboardHistoryItem FindClipboardHistoryItem(winrt::ClipboardHistoryItemsResult result, std::wstring_view id)
{
for (winrt::ClipboardHistoryItem item : result.Items())
{
if (item.Id() == id)
return item;
}
return nullptr;
}
winrt::IAsyncAction DumpClipboardHistoryAsync()
{
winrt::ClipboardHistoryItemsResult result = co_await winrt::Clipboard::GetHistoryItemsAsync();
for (winrt::ClipboardHistoryItem item : result.Items())
{
winrt::DataPackageView content = item.Content();
if (content.Contains(winrt::StandardDataFormats::Text()))
{
winrt::hstring text = co_await content.GetTextAsync();
std::wstring text2(text);
replace_all(text2, L"\r\n", L"|");
wprintf(L"%s\x1F %.80s\n", item.Id().c_str(), text2.c_str());
}
else
{
wprintf(L"%s\x1F (no text content)\n", item.Id().c_str());
}
}
}
winrt::IAsyncAction DumpClipboardHistoryItemAsync(std::wstring_view id)
{
winrt::ClipboardHistoryItemsResult result = co_await winrt::Clipboard::GetHistoryItemsAsync();
winrt::ClipboardHistoryItem item = FindClipboardHistoryItem(result, id);
if (!item)
{
fwprintf(stderr, L"Clipboard history item not found.\n");
co_return;
}
winrt::DataPackageView content = item.Content();
if (content.Contains(winrt::StandardDataFormats::Text()))
{
winrt::hstring text = co_await content.GetTextAsync();
wprintf(L"%s", text.c_str());
}
else
{
wprintf(L"(no text content)\n");
}
}
winrt::IAsyncAction SelectClipboardHistoryItemAsync(std::wstring_view id)
{
winrt::ClipboardHistoryItemsResult result = co_await winrt::Clipboard::GetHistoryItemsAsync();
winrt::ClipboardHistoryItem item = FindClipboardHistoryItem(result, id);
if (!item)
{
fwprintf(stderr, L"Clipboard history item not found.\n");
co_return;
}
winrt::Clipboard::SetHistoryItemAsContent(item);
}
int wmain(const int argc, const wchar_t* argv[])
{
winrt::init_apartment(winrt::apartment_type::multi_threaded);
if (!winrt::Clipboard::IsHistoryEnabled())
{
fwprintf(stderr, L"Clipboard history not enabled.\n");
return 1;
}
if (argc <= 1)
DumpClipboardHistoryAsync().get();
else if (_wcsicmp(argv[1], L"view") == 0)
DumpClipboardHistoryItemAsync(argv[2]).get();
else if (_wcsicmp(argv[1], L"select") == 0)
SelectClipboardHistoryItemAsync(argv[2]).get();
else
{
fwprintf(stderr, L"Unknown option.\n");
return 1;
}
return 0;
}