1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00
ton/terminal/terminal.h

101 lines
2.9 KiB
C
Raw Permalink Normal View History

2019-09-07 10:03:22 +00:00
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
2020-02-15 16:03:17 +00:00
Copyright 2017-2020 Telegram Systems LLP
2019-09-07 10:03:22 +00:00
*/
#pragma once
#include "td/actor/actor.h"
#include "td/utils/buffer.h"
#include <functional>
#include <ostream>
namespace td {
class TerminalIOOutputter {
public:
static const size_t BUFFER_SIZE = 128 * 1024;
2020-02-15 16:03:17 +00:00
TerminalIOOutputter(bool is_err)
: buffer_(new char[BUFFER_SIZE])
, is_err_(is_err)
, sb_(std::make_unique<StringBuilder>(td::MutableSlice{buffer_, BUFFER_SIZE}, true)) {
2019-09-07 10:03:22 +00:00
}
TerminalIOOutputter(TerminalIOOutputter &&X) = default;
template <class T>
TerminalIOOutputter &operator<<(const T &other) {
*sb_ << other;
return *this;
}
TerminalIOOutputter &operator<<(std::ostream &(*pManip)(std::ostream &)) {
*sb_ << '\n';
return *this;
}
auto &sb() {
return *sb_;
}
MutableCSlice as_cslice() {
return sb_->as_cslice();
}
bool is_error() const {
return sb_->is_error();
}
void flush();
2019-09-07 10:03:22 +00:00
~TerminalIOOutputter();
private:
char *buffer_;
2020-02-15 16:03:17 +00:00
bool is_err_;
2019-09-07 10:03:22 +00:00
std::unique_ptr<StringBuilder> sb_;
};
class TerminalIO : public actor::Actor {
public:
class Callback {
public:
virtual ~Callback() = default;
virtual void line_cb(td::BufferSlice line) = 0;
//virtual std::vector<std::string> autocomplete_cb(std::string line) = 0;
};
virtual ~TerminalIO() = default;
virtual void update_prompt(std::string new_prompt) = 0;
virtual void update_callback(std::unique_ptr<Callback> callback) = 0;
static void output(std::string line);
static void output(td::Slice slice);
2020-02-15 16:03:17 +00:00
static void output_stderr(std::string line);
static void output_stderr(td::Slice slice);
static void output_stdout(td::Slice line, double max_wait);
2019-09-07 10:03:22 +00:00
static TerminalIOOutputter out() {
2020-02-15 16:03:17 +00:00
return TerminalIOOutputter{false};
}
static TerminalIOOutputter err() {
return TerminalIOOutputter{true};
2019-09-07 10:03:22 +00:00
}
virtual void output_line(std::string line) = 0;
2020-02-15 16:03:17 +00:00
virtual void output_line_stderr(std::string line) = 0;
2019-09-07 10:03:22 +00:00
virtual void set_log_interface() = 0;
2020-02-15 16:03:17 +00:00
virtual bool readline_used() const = 0;
2019-09-07 10:03:22 +00:00
2020-07-10 10:46:16 +00:00
static td::actor::ActorOwn<TerminalIO> create(std::string prompt, bool use_readline, bool no_input,
2019-09-07 10:03:22 +00:00
std::unique_ptr<Callback> callback);
};
} // namespace td