Skip to content

Commit

Permalink
update tools: code page switching for windows, and format tools code …
Browse files Browse the repository at this point in the history
…with `clang-format -i ` (#721)
  • Loading branch information
fxliang authored Sep 28, 2023
1 parent e849f28 commit d03a881
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 101 deletions.
17 changes: 17 additions & 0 deletions tools/codepage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CODEPAGE_H_
#define CODEPAGE_H_

#ifdef _WIN32
#include <windows.h>
inline unsigned int SetConsoleOutputCodePage(unsigned int codepage = CP_UTF8) {
unsigned int cp = GetConsoleOutputCP();
SetConsoleOutputCP(codepage);
return cp;
}
#else
inline unsigned int SetConsoleOutputCodePage(unsigned int codepage = 65001) {
return 0;
}
#endif /* _WIN32 */

#endif /* CODEPAGE_H_ */
67 changes: 37 additions & 30 deletions tools/rime_api_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@
#include <stdlib.h>
#include <string.h>
#include <rime_api.h>
#include "codepage.h"

void print_status(RimeStatus *status) {
printf("schema: %s / %s\n",
status->schema_id, status->schema_name);
void print_status(RimeStatus* status) {
printf("schema: %s / %s\n", status->schema_id, status->schema_name);
printf("status: ");
if (status->is_disabled) printf("disabled ");
if (status->is_composing) printf("composing ");
if (status->is_ascii_mode) printf("ascii ");
if (status->is_full_shape) printf("full_shape ");
if (status->is_simplified) printf("simplified ");
if (status->is_disabled)
printf("disabled ");
if (status->is_composing)
printf("composing ");
if (status->is_ascii_mode)
printf("ascii ");
if (status->is_full_shape)
printf("full_shape ");
if (status->is_simplified)
printf("simplified ");
printf("\n");
}

void print_composition(RimeComposition *composition) {
const char *preedit = composition->preedit;
if (!preedit) return;
void print_composition(RimeComposition* composition) {
const char* preedit = composition->preedit;
if (!preedit)
return;
size_t len = strlen(preedit);
size_t start = composition->sel_start;
size_t end = composition->sel_end;
Expand All @@ -36,31 +42,28 @@ void print_composition(RimeComposition *composition) {
putchar(']');
}
}
if (i == cursor) putchar('|');
if (i == cursor)
putchar('|');
if (i < len)
putchar(preedit[i]);
putchar(preedit[i]);
}
printf("\n");
}

void print_menu(RimeMenu *menu) {
if (menu->num_candidates == 0) return;
printf("page: %d%c (of size %d)\n",
menu->page_no + 1,
menu->is_last_page ? '$' : ' ',
menu->page_size);
void print_menu(RimeMenu* menu) {
if (menu->num_candidates == 0)
return;
printf("page: %d%c (of size %d)\n", menu->page_no + 1,
menu->is_last_page ? '$' : ' ', menu->page_size);
for (int i = 0; i < menu->num_candidates; ++i) {
bool highlighted = i == menu->highlighted_candidate_index;
printf("%d. %c%s%c%s\n",
i + 1,
highlighted ? '[' : ' ',
menu->candidates[i].text,
highlighted ? ']' : ' ',
printf("%d. %c%s%c%s\n", i + 1, highlighted ? '[' : ' ',
menu->candidates[i].text, highlighted ? ']' : ' ',
menu->candidates[i].comment ? menu->candidates[i].comment : "");
}
}

void print_context(RimeContext *context) {
void print_context(RimeContext* context) {
if (context->composition.length > 0) {
print_composition(&context->composition);
print_menu(&context->menu);
Expand Down Expand Up @@ -99,8 +102,8 @@ bool execute_special_command(const char* line, RimeSessionId session_id) {
if (rime->get_schema_list(&list)) {
printf("schema list:\n");
for (size_t i = 0; i < list.size; ++i) {
printf("%lu. %s [%s]\n", (i + 1),
list.list[i].name, list.list[i].schema_id);
printf("%lu. %s [%s]\n", (i + 1), list.list[i].name,
list.list[i].schema_id);
}
rime->free_schema_list(&list);
}
Expand Down Expand Up @@ -175,12 +178,14 @@ void on_message(void* context_object,
const char* state_label =
rime->get_state_label(session_id, option_name, state);
if (state_label) {
printf("updated option: %s = %d // %s\n", option_name, state, state_label);
printf("updated option: %s = %d // %s\n", option_name, state,
state_label);
}
}
}

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
unsigned int codepage = SetConsoleOutputCodePage();
RimeApi* rime = rime_get_api();

RIME_STRUCT(RimeTraits, traits);
Expand All @@ -199,13 +204,14 @@ int main(int argc, char *argv[]) {
RimeSessionId session_id = rime->create_session();
if (!session_id) {
fprintf(stderr, "Error creating rime session.\n");
SetConsoleOutputCodePage(codepage);
return 1;
}

const int kMaxLength = 99;
char line[kMaxLength + 1] = {0};
while (fgets(line, kMaxLength, stdin) != NULL) {
for (char *p = line; *p; ++p) {
for (char* p = line; *p; ++p) {
if (*p == '\r' || *p == '\n') {
*p = '\0';
break;
Expand All @@ -226,5 +232,6 @@ int main(int argc, char *argv[]) {

rime->finalize();

SetConsoleOutputCodePage(codepage);
return 0;
}
45 changes: 22 additions & 23 deletions tools/rime_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,46 @@
#include <rime/dict/dictionary.h>
#include <rime/dict/dict_compiler.h>
#include <rime/lever/deployment_tasks.h>
#include "codepage.h"

using namespace rime;

class RimeConsole {
public:
RimeConsole() : interactive_(false),
engine_(Engine::Create()) {
conn_ = engine_->sink().connect(
[this](const string& x) { OnCommit(x); });
}
~RimeConsole() {
conn_.disconnect();
RimeConsole() : interactive_(false), engine_(Engine::Create()) {
conn_ = engine_->sink().connect([this](const string& x) { OnCommit(x); });
}
~RimeConsole() { conn_.disconnect(); }

void OnCommit(const string &commit_text) {
void OnCommit(const string& commit_text) {
if (interactive_) {
std::cout << "commit : [" << commit_text << "]" << std::endl;
}
else {
} else {
std::cout << commit_text << std::endl;
}
}

void PrintComposition(const Context *ctx) {
void PrintComposition(const Context* ctx) {
if (!ctx || !ctx->IsComposing())
return;
std::cout << "input : [" << ctx->input() << "]" << std::endl;
const Composition &comp = ctx->composition();
const Composition& comp = ctx->composition();
if (comp.empty())
return;
std::cout << "comp. : [" << comp.GetDebugText() << "]" << std::endl;
const Segment &current(comp.back());
const Segment& current(comp.back());
if (!current.menu)
return;
int page_size = engine_->active_engine()->schema()->page_size();
int page_no = current.selected_index / page_size;
the<Page> page(current.menu->CreatePage(page_size, page_no));
if (!page)
return;
std::cout << "page_no: " << page_no
<< ", index: " << current.selected_index << std::endl;
std::cout << "page_no: " << page_no << ", index: " << current.selected_index
<< std::endl;
int i = 0;
for (const an<Candidate> &cand : page->candidates) {
std::cout << "cand. " << (++i % 10) << ": [";
for (const an<Candidate>& cand : page->candidates) {
std::cout << "cand. " << (++i % 10) << ": [";
std::cout << cand->text();
std::cout << "]";
if (!cand->comment().empty())
Expand All @@ -72,20 +68,19 @@ class RimeConsole {
}
}

void ProcessLine(const string &line) {
void ProcessLine(const string& line) {
KeySequence keys;
if (!keys.Parse(line)) {
LOG(ERROR) << "error parsing input: '" << line << "'";
return;
}
for (const KeyEvent &key : keys) {
for (const KeyEvent& key : keys) {
engine_->ProcessKey(key);
}
Context *ctx = engine_->active_engine()->context();
Context* ctx = engine_->active_engine()->context();
if (interactive_) {
PrintComposition(ctx);
}
else {
} else {
if (ctx && ctx->IsComposing()) {
ctx->Commit();
}
Expand All @@ -102,7 +97,8 @@ class RimeConsole {
};

// program entry
int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
unsigned int codepage = SetConsoleOutputCodePage();
// initialize la Rime
SetupLogging("rime.console");
LoadModules(kDefaultModules);
Expand All @@ -111,12 +107,14 @@ int main(int argc, char *argv[]) {
InstallationUpdate installation;
if (!installation.Run(&deployer)) {
std::cerr << "failed to initialize installation." << std::endl;
SetConsoleOutputCodePage(codepage);
return 1;
}
std::cerr << "initializing...";
WorkspaceUpdate workspace_update;
if (!workspace_update.Run(&deployer)) {
std::cerr << "failure!" << std::endl;
SetConsoleOutputCodePage(codepage);
return 1;
}
std::cerr << "ready." << std::endl;
Expand All @@ -132,5 +130,6 @@ int main(int argc, char *argv[]) {
std::getline(std::cin, line);
console.ProcessLine(line);
}
SetConsoleOutputCodePage(codepage);
return 0;
}
Loading

0 comments on commit d03a881

Please sign in to comment.