neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
console_client.hpp
Go to the documentation of this file.
1// console_client.hpp
2/*
3 neoGFX Design Studio
4 Copyright(C) 2022 Leigh Johnston
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
24
26{
27 class console_client : public reference_counted<i_console_client>, public neolib::lifetime<>
28 {
29 public:
30 define_declared_event(Output, output, std::string const&)
31 };
32
33 class console : public console_client
34 {
35 public:
36 define_event(Command, command, std::string const&)
37 public:
38 console(std::string const& aName = {}) :
39 iName{ aName }
40 {
41 }
42 public:
43 void start() final
44 {
45 output().trigger(iName + ">");
46 }
47 void resize_window(std::uint16_t aWidth, std::uint16_t aHeight) final
48 {
49 }
50 void input(std::string const& aText) final
51 {
52 destroyed_flag destroyed{ *this };
53
54 if (aText == "\x7F")
55 {
56 output().trigger("\b\x1B[K");
57 if (!iBuffer.empty())
58 iBuffer.pop_back();
59 return;
60 }
61
62 output().trigger(aText);
63 iBuffer += aText;
64 std::size_t next;
65 while (!destroyed && (next = iBuffer.find("\r\n")) != std::string::npos)
66 {
67 auto const nextCommand = iBuffer.substr(0, next);
68 iBuffer = iBuffer.substr(next + 2);
69 if (!nextCommand.empty())
70 command().trigger(nextCommand);
71 if (!destroyed)
72 output().trigger(iName + ">");
73 }
74 }
75 private:
76 std::string iName;
77 std::string iBuffer;
78 };
79}
void resize_window(std::uint16_t aWidth, std::uint16_t aHeight) final
void input(std::string const &aText) final
define_event(Command, command, std::string const &) public
#define define_declared_event(name, declName,...)
Definition event.hpp:195