-
Notifications
You must be signed in to change notification settings - Fork 563
/
rime_console.cc
135 lines (124 loc) · 3.69 KB
/
rime_console.cc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2011-04-24 GONG Chen <chen.sst@gmail.com>
//
#include <cstring>
#include <iostream>
#include <rime/candidate.h>
#include <rime/common.h>
#include <rime/composition.h>
#include <rime/context.h>
#include <rime/deployer.h>
#include <rime/engine.h>
#include <rime/key_event.h>
#include <rime/menu.h>
#include <rime/schema.h>
#include <rime/setup.h>
#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(); }
void OnCommit(const string& commit_text) {
if (interactive_) {
std::cout << "commit : [" << commit_text << "]" << std::endl;
} else {
std::cout << commit_text << std::endl;
}
}
void PrintComposition(const Context* ctx) {
if (!ctx || !ctx->IsComposing())
return;
std::cout << "input : [" << ctx->input() << "]" << std::endl;
const Composition& comp = ctx->composition();
if (comp.empty())
return;
std::cout << "comp. : [" << comp.GetDebugText() << "]" << std::endl;
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;
int i = 0;
for (const an<Candidate>& cand : page->candidates) {
std::cout << "cand. " << (++i % 10) << ": [";
std::cout << cand->text();
std::cout << "]";
if (!cand->comment().empty())
std::cout << " " << cand->comment();
std::cout << " quality=" << cand->quality();
std::cout << std::endl;
}
}
void ProcessLine(const string& line) {
KeySequence keys;
if (!keys.Parse(line)) {
LOG(ERROR) << "error parsing input: '" << line << "'";
return;
}
for (const KeyEvent& key : keys) {
engine_->ProcessKey(key);
}
Context* ctx = engine_->active_engine()->context();
if (interactive_) {
PrintComposition(ctx);
} else {
if (ctx && ctx->IsComposing()) {
ctx->Commit();
}
}
}
void set_interactive(bool interactive) { interactive_ = interactive; }
bool interactive() const { return interactive_; }
private:
bool interactive_;
the<Engine> engine_;
connection conn_;
};
// program entry
int main(int argc, char* argv[]) {
unsigned int codepage = SetConsoleOutputCodePage();
// initialize la Rime
SetupLogging("rime.console");
LoadModules(kDefaultModules);
Deployer deployer;
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;
RimeConsole console;
// "-i" turns on interactive mode (no commit at the end of line)
bool interactive = argc > 1 && !strcmp(argv[1], "-i");
console.set_interactive(interactive);
// process input
string line;
while (std::cin) {
std::getline(std::cin, line);
console.ProcessLine(line);
}
SetConsoleOutputCodePage(codepage);
return 0;
}