69 lines
1.6 KiB
C++
69 lines
1.6 KiB
C++
// src/screen.h — VT100-style screen model.
|
|
//
|
|
// A fixed-size grid of cells. Each cell holds up to four bytes of UTF-8 so
|
|
// multi-byte characters round-trip, plus the colour and attribute state it
|
|
// was drawn with.
|
|
|
|
#ifndef GASSYTERM_SRC_SCREEN_H_
|
|
#define GASSYTERM_SRC_SCREEN_H_
|
|
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
namespace gassyterm {
|
|
|
|
constexpr int kDefaultWidth = 80;
|
|
constexpr int kDefaultHeight = 24;
|
|
|
|
// Cell attribute bits.
|
|
enum CellFlag : uint8_t {
|
|
kBold = 1 << 0,
|
|
kDim = 1 << 1,
|
|
kItalic = 1 << 2,
|
|
kUnderline = 1 << 3,
|
|
kInverse = 1 << 4,
|
|
};
|
|
|
|
struct Cell {
|
|
char text[4] = {' ', 0, 0, 0};
|
|
uint8_t len = 1;
|
|
int16_t fg = -1; // -1 means "terminal default", otherwise a palette index.
|
|
int16_t bg = -1;
|
|
uint8_t flags = 0;
|
|
|
|
bool isBlank() const {
|
|
return len == 1 && text[0] == ' ' && fg < 0 && bg < 0 && flags == 0;
|
|
}
|
|
};
|
|
|
|
class Screen {
|
|
public:
|
|
Screen(int width, int height) { resize(width, height); }
|
|
|
|
void resize(int width, int height);
|
|
int width() const { return width_; }
|
|
int height() const { return height_; }
|
|
|
|
Cell& at(int x, int y) { return cells_[index(x, y)]; }
|
|
const Cell& at(int x, int y) const { return cells_[index(x, y)]; }
|
|
|
|
void clear();
|
|
void clearRow(int y);
|
|
void scrollUp(int top, int bottom, int n);
|
|
void scrollDown(int top, int bottom, int n);
|
|
|
|
private:
|
|
size_t index(int x, int y) const {
|
|
return static_cast<size_t>(y) * static_cast<size_t>(width_) +
|
|
static_cast<size_t>(x);
|
|
}
|
|
|
|
int width_ = kDefaultWidth;
|
|
int height_ = kDefaultHeight;
|
|
std::vector<Cell> cells_;
|
|
};
|
|
|
|
} // namespace gassyterm
|
|
|
|
#endif // GASSYTERM_SRC_SCREEN_H_
|