Skip to content
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

feat: get ascii mode by command line #1329

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,15 @@ void RimeWithWeaselHandler::EndMaintenance() {
m_session_status_map.clear();
}

bool RimeWithWeaselHandler::GetStatus(EatLine eat) {
if (m_active_session) {
_Respond(m_active_session, eat);
return true;
} else {
return false;
}
}

void RimeWithWeaselHandler::SetOption(WeaselSessionId ipc_id,
const std::string& opt,
bool val) {
Expand Down
9 changes: 9 additions & 0 deletions WeaselIPC/WeaselClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ bool ClientImpl::Echo() {
return (serverEcho == session_id);
}

bool ClientImpl::GetStatus() {
LRESULT ret = _SendMessage(WEASEL_IPC_GET_STATUS, 0, session_id);
return ret != 0;
}

bool ClientImpl::GetResponseData(ResponseHandler const& handler) {
if (!handler) {
return false;
Expand Down Expand Up @@ -280,6 +285,10 @@ bool Client::Echo() {
return m_pImpl->Echo();
}

bool Client::GetStatus() {
return m_pImpl->GetStatus();
}

bool Client::GetResponseData(ResponseHandler handler) {
return m_pImpl->GetResponseData(handler);
}
3 changes: 2 additions & 1 deletion WeaselIPC/WeaselClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ClientImpl {
void FocusIn();
void FocusOut();
void TrayCommand(UINT menuId);
bool GetStatus();
bool GetResponseData(ResponseHandler const& handler);

protected:
Expand All @@ -46,4 +47,4 @@ class ClientImpl {
PipeChannel<PipeMessage> channel;
};

} // namespace weasel
} // namespace weasel
15 changes: 15 additions & 0 deletions WeaselIPCServer/WeaselServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ DWORD ServerImpl::OnChangePage(WEASEL_IPC_COMMAND uMsg,
return 0;
}

DWORD ServerImpl::OnGetStatus(WEASEL_IPC_COMMAND uMsg,
DWORD wParam,
DWORD lParam) {
if (m_pRequestHandler) {
auto eat = [this](std::wstring& msg) -> bool {
*channel << msg;
return true;
};
m_pRequestHandler->GetStatus(eat);
return 1;
}
return 0;
}

#define MAP_PIPE_MSG_HANDLE(__msg, __wParam, __lParam) \
{ \
auto lParam = __lParam; \
Expand Down Expand Up @@ -394,6 +408,7 @@ void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp) {
OnHighlightCandidateOnCurrentPage);
PIPE_MSG_HANDLE(WEASEL_IPC_CHANGE_PAGE, OnChangePage);
PIPE_MSG_HANDLE(WEASEL_IPC_TRAY_COMMAND, OnCommand);
PIPE_MSG_HANDLE(WEASEL_IPC_GET_STATUS, OnGetStatus);
END_MAP_PIPE_MSG_HANDLE(result);

resp(result);
Expand Down
1 change: 1 addition & 0 deletions WeaselIPCServer/WeaselServerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ServerImpl : public CWindowImpl<ServerImpl, CWindow, ServerWinTraits>
DWORD wParam,
DWORD lParam);
DWORD OnChangePage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnGetStatus(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);

public:
ServerImpl();
Expand Down
26 changes: 26 additions & 0 deletions WeaselServer/WeaselServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "stdafx.h"
#include "resource.h"
#include "WeaselService.h"
#include <ResponseParser.h>
#include <WeaselIPC.h>
#include <WeaselUI.h>
#include <RimeWithWeasel.h>
Expand All @@ -15,6 +16,7 @@
#include <WinUser.h>
#include <memory>
#include <atlstr.h>
#include <iostream>
#pragma comment(lib, "Shcore.lib")
CAppModule _Module;

Expand Down Expand Up @@ -71,6 +73,30 @@ int WINAPI _tWinMain(HINSTANCE hInstance,
WeaselServerApp::explore(WeaselServerApp::install_dir());
return 0;
}
if (!wcscmp(L"/isascii", lpstrCmdLine)) {
// https://speedyleion.github.io/c/c++/windows/2021/07/11/WinMain-and-stdout.html
if (!GetStdHandle(STD_OUTPUT_HANDLE)) {
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
freopen("CONOUT$", "wb", stdout);
}
}

weasel::Client client;
if (client.Connect()) // try to connect to running server
{
if (client.GetStatus()) {
std::wstring commit;
weasel::Status status;
weasel::ResponseParser parser(&commit, NULL, &status);
bool ok = client.GetResponseData(std::ref(parser));
if (ok) {
int out = status.ascii_mode ? 1 : 0;
std::cout << out << std::endl;
}
}
}
return 0;
}
if (!wcscmp(L"/ascii", lpstrCmdLine) || !wcscmp(L"/nascii", lpstrCmdLine)) {
weasel::Client client;
bool ascii = !wcscmp(L"/ascii", lpstrCmdLine);
Expand Down
1 change: 1 addition & 0 deletions include/RimeWithWeasel.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class RimeWithWeaselHandler : public weasel::RequestHandler {
virtual void UpdateInputPosition(RECT const& rc, WeaselSessionId ipc_id);
virtual void StartMaintenance();
virtual void EndMaintenance();
virtual bool GetStatus(EatLine eat);
virtual void SetOption(WeaselSessionId ipc_id,
const std::string& opt,
bool val);
Expand Down
4 changes: 4 additions & 0 deletions include/WeaselIPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum WEASEL_IPC_COMMAND {
WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE,
WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE,
WEASEL_IPC_CHANGE_PAGE,
WEASEL_IPC_GET_STATUS,
WEASEL_IPC_LAST_COMMAND
};

Expand Down Expand Up @@ -91,6 +92,7 @@ struct RequestHandler {
virtual void EndMaintenance() {}
virtual void SetOption(DWORD session_id, const std::string& opt, bool val) {}
virtual void UpdateColorTheme(BOOL darkMode) {}
virtual bool GetStatus(EatLine eat) { return false; }
};

// 處理server端回應之物件
Expand Down Expand Up @@ -150,6 +152,8 @@ class Client {
void FocusOut();
// 托盤菜單
void TrayCommand(UINT menuId);
// 獲取當前輸入法狀態
bool GetStatus();
// 读取server返回的数据
bool GetResponseData(ResponseHandler handler);

Expand Down