Skip to content

Commit 21322b0

Browse files
committed
feat(terminal,main):1.8.17, 给终端添加设置欢迎辞的接口,并修改main,使程序终端的欢迎辞中包含程序名
1 parent df185a2 commit 21322b0

File tree

14 files changed

+67
-33
lines changed

14 files changed

+67
-33
lines changed

examples/terminal/tcp_rpc/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ int main(int argc, char **argv)
5151
SetScopeExitAction([sp_loop] { delete sp_loop; });
5252

5353
Terminal term;
54+
term.setWelcomeText("Welcome to Terminal TcpRPC demo! \r\n");
5455
TcpRpc rpc(sp_loop, &term);
5556
if (!rpc.initialize(bind_addr)) {
5657
std::cout << "Error: rpc init fail" << std::endl;

examples/terminal/telnetd/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ int main(int argc, char **argv)
5151
SetScopeExitAction([sp_loop] { delete sp_loop; });
5252

5353
Terminal term;
54+
term.setWelcomeText("Welcome to Terminal Telnet demo! \r\n");
5455
Telnetd telnetd(sp_loop, &term);
5556
if (!telnetd.initialize(bind_addr)) {
5657
std::cout << "Error: telnetd init fail" << std::endl;

modules/main/context_imp.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void ContextImp::fillDefaultConfig(Json &cfg) const
104104
cfg["thread_pool"] = R"({"min":0, "max":1})"_json;
105105
}
106106

107-
bool ContextImp::initialize(const Json &cfg)
107+
bool ContextImp::initialize(const char* proc_name, const Json &cfg)
108108
{
109109
if (util::json::HasObjectField(cfg, "loop")) {
110110
auto &js_loop = cfg["loop"];
@@ -120,6 +120,29 @@ bool ContextImp::initialize(const Json &cfg)
120120
if (!initThreadPool(js_thread_pool))
121121
return false;
122122

123+
{
124+
std::ostringstream oss;
125+
oss <<
126+
"\r\n"
127+
"Welcome to '" << proc_name << "' main terminal!\r\n"
128+
"\r\n"
129+
"This program is based on cpp-tbox which designed by Hevake Lee.\r\n"
130+
"Repository: https://github.com/cpp-main/cpp-tbox\r\n"
131+
"\r\n"
132+
R"( .============. )""\r\n"
133+
R"( // M A K E / \ )""\r\n"
134+
R"( // C++ DEV / \ )""\r\n"
135+
R"( // E A S Y / \/ \ )""\r\n"
136+
R"( ++ ----------. \/\ . )""\r\n"
137+
R"( \\ \ \ /\ / )""\r\n"
138+
R"( \\ \ \ / )""\r\n"
139+
R"( \\ \ \ / )""\r\n"
140+
R"( -============' )""\r\n"
141+
"\r\n";
142+
143+
sp_terminal_->setWelcomeText(oss.str());
144+
}
145+
123146
if (util::json::HasObjectField(cfg, "telnetd")) {
124147
auto &js_telnetd = cfg["telnetd"];
125148
initTelnetd(js_telnetd);

modules/main/context_imp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ContextImp : public Context {
3838

3939
void fillDefaultConfig(Json &cfg) const;
4040

41-
bool initialize(const Json &cfg);
41+
bool initialize(const char *proc_name, const Json &cfg);
4242
bool start();
4343
void stop();
4444
void cleanup();

modules/main/log.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ void Log::initShellForSink(log::Sink &log_ch, terminal::TerminalNodes &term, ter
241241
<< " 1: Error\r\n"
242242
<< " 2: Warn\r\n"
243243
<< " 3: Notice\r\n"
244-
<< " 4: Info\r\n"
245-
<< " 5: Debug\r\n"
246-
<< " 6: Trace\r\n"
244+
<< " 4: Important\r\n"
245+
<< " 5: Info\r\n"
246+
<< " 6: Debug\r\n"
247+
<< " 7: Trace\r\n"
247248
;
248249

249250
s.send(oss.str());

modules/main/misc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ __attribute__((weak)) void OnAbnormalExit() { }
8585

8686
void SayHello()
8787
{
88-
LogInfo("=== TBOX MAIN STARTUP ===");
88+
LogInfo("=== CPP-TBOX MAIN STARTUP ===");
8989
LogInfo("App Describe: %s", GetAppDescribe().c_str());
9090

9191
int major, minor, rev, build;

modules/main/run_in_backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ bool Start(int argc, char **argv)
156156
std::this_thread::sleep_for(std::chrono::seconds(1));
157157
};
158158

159-
if (ctx.initialize(js_conf)) {
159+
if (ctx.initialize(argv[0], js_conf)) {
160160
if (apps.initialize(js_conf)) {
161161
if (ctx.start()) { //! 启动所有应用
162162
if (apps.start()) {

modules/main/run_in_frontend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ int Main(int argc, char **argv)
146146
std::this_thread::sleep_for(std::chrono::seconds(1));
147147
};
148148

149-
if (ctx.initialize(js_conf)) {
149+
if (ctx.initialize(argv[0], js_conf)) {
150150
if (apps.initialize(js_conf)) {
151151
if (ctx.start() && apps.start()) { //! 启动所有应用
152152
RunInFrontend(ctx, apps, loop_exit_wait);

modules/terminal/impl/terminal.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ using namespace std;
3535

3636
Terminal::Impl::Impl()
3737
{
38+
welcome_text_ = \
39+
"\r\n"
40+
R"( .============. )""\r\n"
41+
R"( // M A K E / \ )""\r\n"
42+
R"( // C++ DEV / \ )""\r\n"
43+
R"( // E A S Y / \/ \ )""\r\n"
44+
R"( ++ ----------. \/\ . )""\r\n"
45+
R"( \\ \ \ /\ / )""\r\n"
46+
R"( \\ \ \ / )""\r\n"
47+
R"( \\ \ \ / )""\r\n"
48+
R"( -============' )""\r\n"
49+
"\r\n"
50+
"Welcome to cpp-tbox terminal.\r\n";
51+
3852
root_token_ = nodes_.alloc(new DirNode("this is root node"));
3953
}
4054

@@ -94,28 +108,14 @@ void Terminal::Impl::setOptions(const SessionToken &st, uint32_t options)
94108

95109
bool Terminal::Impl::onBegin(const SessionToken &st)
96110
{
97-
const char *welcome_str = \
98-
"\r\n"
99-
R"( .============. )""\r\n"
100-
R"( // M A K E / \ )""\r\n"
101-
R"( // C++ DEV / \ )""\r\n"
102-
R"( // E A S Y / \/ \ )""\r\n"
103-
R"( ++ ----------. \/\ . )""\r\n"
104-
R"( \\ \ \ /\ / )""\r\n"
105-
R"( \\ \ \ / )""\r\n"
106-
R"( \\ \ \ / )""\r\n"
107-
R"( -============' )""\r\n"
108-
"\r\n"
109-
"Welcome to cpp-tbox terminal.\r\n"
110-
"Type 'help' for more information.\r\n"
111-
"\r\n";
112-
113111
auto s = sessions_.at(st);
114112
if (s == nullptr)
115113
return false;
116114

117-
if (!(s->options & kQuietMode))
118-
s->wp_conn->send(st, welcome_str);
115+
if (!(s->options & kQuietMode)) {
116+
s->wp_conn->send(st, welcome_text_);
117+
s->wp_conn->send(st, "Type 'help' for more information.\r\n\r\n");
118+
}
119119

120120
printPrompt(s);
121121

modules/terminal/impl/terminal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ class Terminal::Impl {
5252
NodeToken createFuncNode(const Func &func, const std::string &help);
5353
NodeToken createDirNode(const std::string &help);
5454
bool deleteNode(NodeToken node_token);
55-
NodeToken rootNode() const;
55+
NodeToken rootNode() const { return root_token_; }
5656
NodeToken findNode(const std::string &path) const;
5757
bool mountNode(const NodeToken &parent, const NodeToken &child, const std::string &name);
5858
bool umountNode(const NodeToken &parent, const std::string &name);
5959

60+
public:
61+
void setWelcomeText(const std::string &text) { welcome_text_ = text; }
62+
6063
protected:
6164
void onChar(SessionContext *s, char ch);
6265
void onEnterKey(SessionContext *s);
@@ -92,6 +95,8 @@ class Terminal::Impl {
9295
cabinet::Cabinet<SessionContext> sessions_;
9396
cabinet::Cabinet<Node> nodes_;
9497
NodeToken root_token_;
98+
99+
std::string welcome_text_;
95100
};
96101

97102
}

modules/terminal/impl/terminal_nodes.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ bool Terminal::Impl::deleteNode(NodeToken node_token)
5454
}
5555
}
5656

57-
NodeToken Terminal::Impl::rootNode() const
58-
{
59-
return root_token_;
60-
}
61-
6257
NodeToken Terminal::Impl::findNode(const string &path_str) const
6358
{
6459
Path node_path;

modules/terminal/terminal.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ Terminal::~Terminal()
3636
delete impl_;
3737
}
3838

39+
void Terminal::setWelcomeText(const std::string &welcome_text)
40+
{
41+
impl_->setWelcomeText(welcome_text);
42+
}
43+
3944
SessionToken Terminal::newSession(Connection *wp_conn)
4045
{
4146
return impl_->newSession(wp_conn);

modules/terminal/terminal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class Terminal : public TerminalInteract,
5353
virtual bool mountNode(const NodeToken &parent, const NodeToken &child, const std::string &name) override;
5454
virtual bool umountNode(const NodeToken &parent, const std::string &name) override;
5555

56+
public:
57+
void setWelcomeText(const std::string &text);
58+
5659
private:
5760
class Impl;
5861
Impl *impl_ = nullptr;

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 8
24-
TBOX_VERSION_REVISION := 16
24+
TBOX_VERSION_REVISION := 17

0 commit comments

Comments
 (0)