Skip to content

Commit 6d5e779

Browse files
committed
add output to console for '-ouput -'
1 parent c797f0d commit 6d5e779

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/main.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using namespace winrt;
44
using namespace winrt::Windows::Foundation;
55
using namespace winrt::Windows::UI::UIAutomation;
66

7-
#define VERSION_STRING "0.1.240505"
7+
#define VERSION_STRING "0.1.240911"
88

99
std::string get_current_time()
1010
{
@@ -27,13 +27,13 @@ class Engine
2727
winrt::com_ptr<IUIAutomationCondition> _condition;
2828
std::string _prebuffer;
2929
std::string _sfilename;
30-
30+
3131
winrt::hstring get_livecaptions()
3232
{
3333
wil::unique_bstr text;
3434
winrt::com_ptr<IUIAutomationElement> window_element;
3535
winrt::com_ptr<IUIAutomationElement> text_element;
36-
36+
3737
try{
3838
auto window = FindWindowW(L"LiveCaptionsDesktopWindow", nullptr);
3939
winrt::check_hresult(_automation->ElementFromHandle(window, window_element.put()));
@@ -43,23 +43,14 @@ class Engine
4343
winrt::check_hresult(text_element->get_CurrentName(text.put()));
4444
return text.get();
4545
}
46+
4647
return winrt::hstring();
4748
}
4849
catch (winrt::hresult_error &e){}
4950
catch (std::exception &e){}
5051
return winrt::hstring();
5152
}
52-
53-
public:
54-
Engine(const std::string &filename) : _sfilename{filename}
55-
{
56-
winrt::init_apartment();
57-
_automation = try_create_instance<IUIAutomation>(guid_of<CUIAutomation>());
58-
winrt::check_hresult(_automation->CreatePropertyCondition(UIA_AutomationIdPropertyId, wil::make_variant_bstr(L"CaptionsTextBlock"), _condition.put()));
59-
}
60-
~Engine() { winrt::uninit_apartment(); }
61-
62-
void save_current_captions(bool includeLastLine = false)
53+
void ostream_captions(std::ostream &os)
6354
{
6455
auto hs_current = get_livecaptions();
6556
if(hs_current.empty()) return;
@@ -87,47 +78,53 @@ class Engine
8778
if (first_new_line < lines.size())
8879
{
8980
// Append new lines to the file and prebuffer
90-
std::ofstream file(_sfilename, std::ios::app);
91-
if (!file.is_open())
92-
{
93-
std::cerr << "[Error]Failed to open file: " << _sfilename << std::endl;
94-
return;
95-
}
96-
97-
98-
file << "[" << get_current_time() << "] " << std::endl;
81+
os << "[" << get_current_time() << "] " << std::endl;
9982

10083
for (size_t i = first_new_line; i < lines.size(); ++i)
10184
{
102-
file << lines[i] << std::endl;
85+
os << lines[i] << std::endl;
10386
}
104-
file.flush();
105-
file.close();
106-
}
87+
}
88+
}
89+
public:
90+
Engine(const std::string &filename) : _sfilename{filename}
91+
{
92+
winrt::init_apartment();
93+
_automation = try_create_instance<IUIAutomation>(guid_of<CUIAutomation>());
94+
winrt::check_hresult(_automation->CreatePropertyCondition(UIA_AutomationIdPropertyId, wil::make_variant_bstr(L"CaptionsTextBlock"), _condition.put()));
10795
}
96+
~Engine() { winrt::uninit_apartment(); }
97+
10898
static bool is_livecaption_running()
10999
{
110100
return FindWindowW(L"LiveCaptionsDesktopWindow", nullptr) != NULL;
111101
}
112-
};
113-
114102

103+
void ouput_captions()
104+
{
105+
if(_sfilename.compare("-")==0)
106+
{
107+
ostream_captions(std::cout);
108+
return;
109+
}
110+
std::ofstream of(_sfilename,std::ios::app);
111+
if (of.is_open())
112+
{
113+
ostream_captions(of);
114+
of.flush();
115+
of.close();
116+
}
117+
}
118+
bool touch_file()
119+
{
120+
if(_sfilename.compare("-")==0) return true;
115121

116-
// void usage()
117-
// {
118-
// std::cerr << "Write all content of LiveCaptions Windows System Program into file, continually.Ctrl-C to exit." << std::endl;
119-
// std::cerr << "Usage: get-livecatpions file" << std::endl;
120-
// std::cerr << "Options:" << std::endl;
121-
// std::cerr << " file filename, to save content of live catpions running." << std::endl;
122-
// exit(1);
123-
// }
124-
bool touch_file(const std::string &filename)
125-
{
126-
std::ofstream file(filename,std::ios::app);
127-
auto ret = file.is_open();
128-
file.close();
129-
return ret;
130-
}
122+
std::ofstream file(_sfilename,std::ios::app);
123+
auto ret = file.is_open();
124+
file.close();
125+
return ret;
126+
}
127+
};
131128

132129
int main(int argc, char *argv[])
133130
{
@@ -168,7 +165,7 @@ int main(int argc, char *argv[])
168165
asio::signal_set signals(io_context, SIGINT, SIGTERM);
169166
signals.async_wait([&](auto, auto){
170167
std::cerr << "ctrl-c to exit." <<std::endl;
171-
eng.save_current_captions();
168+
eng.ouput_captions();
172169
io_context.stop();
173170
});
174171
std::cout<<"Save content into file, every 1 min."<<std::endl;
@@ -186,7 +183,7 @@ int main(int argc, char *argv[])
186183
io_context.stop();
187184
break;
188185
}
189-
if(ulCount++%6==0) eng.save_current_captions();
186+
if(ulCount++%6==0) eng.ouput_captions();
190187
}
191188
co_return;
192189
},

0 commit comments

Comments
 (0)