Files
bilicraft-bot/include/TerminalConsole.h
T
2026-11-24 12:43:25 +08:00

41 lines
1.3 KiB
C++

#pragma once
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
namespace Bilicraft
{
// Owns terminal state, never the supplied descriptors. The caller must be the
// sole input reader and keep all descriptors open until destruction. Stop logger
// callbacks before destruction. Control/input methods belong to the main thread;
// WriteLog is safe from other threads. Do not write directly to the terminal
// while split mode is active: use Out(), Err(), or WriteLog().
class TerminalConsole
{
public:
explicit TerminalConsole(int inputFd = 0, int outputFd = 1, int errorFd = 2);
~TerminalConsole() noexcept;
TerminalConsole(const TerminalConsole &) = delete;
TerminalConsole &operator=(const TerminalConsole &) = delete;
// Call after login. Idempotent; unsupported terminals retain plain line
// input (including prompts), with no terminal control sequences generated.
void Start();
bool IsSplit() const;
std::optional<std::string> PollLine();
bool IsEof() const;
void SetInputEnabled(bool enabled);
void Tick();
std::ostream &Out();
std::ostream &Err();
void WriteLog(std::string_view text, bool error = false) noexcept;
private:
struct Impl;
std::unique_ptr<Impl> impl;
};
} // namespace Bilicraft