123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- #ifndef DOOR_H
- #define DOOR_H
- #include "anyoption.h"
- #include <cstdint>
- #include <ctime>
- #include <fstream>
- #include <functional>
- #include <future>
- #include <iostream>
- #include <list>
- #include <memory>
- #include <ostream>
- #include <vector>
- #include <termios.h>
- #include <unistd.h>
- #define CSI "\x1b["
- #define XKEY_START 0x1000
- #define XKEY_UP_ARROW 0x1001
- #define XKEY_DOWN_ARROW 0x1002
- #define XKEY_RIGHT_ARROW 0x1003
- #define XKEY_LEFT_ARROW 0x1004
- #define XKEY_HOME 0x1010
- #define XKEY_END 0x1011
- #define XKEY_PGUP 0x1012
- #define XKEY_PGDN 0x1023
- #define XKEY_INSERT 0x1024
- #define XKEY_DELETE 0x7f
- #define XKEY_F1 0x1021
- #define XKEY_F2 0x1022
- #define XKEY_F3 0x1023
- #define XKEY_F4 0x1024
- #define XKEY_F5 0x1025
- #define XKEY_F6 0x1026
- #define XKEY_F7 0x1027
- #define XKEY_F8 0x1028
- #define XKEY_F9 0x1029
- #define XKEY_F10 0x102a
- #define XKEY_F11 0x102b
- #define XKEY_F12 0x102c
- #define XKEY_UNKNOWN 0x1111
- #define TIMEOUT -1
- #define HANGUP -2
- #define OUTOFTIME -3
- namespace door {
- extern bool unicode;
- extern bool full_cp437;
- extern bool debug_capture;
- extern std::list<char> pushback;
- void cp437toUnicode(std::string input, std::string &out);
- void cp437toUnicode(const char *input, std::string &out);
- enum class COLOR : std::int8_t {
-
- BLACK,
-
- RED,
-
- GREEN,
-
- BROWN,
-
- YELLOW = 3,
-
- BLUE,
-
- MAGENTA,
-
- CYAN,
-
- WHITE
- };
- enum class ATTR : std::int8_t {
-
- RESET,
-
- BOLD,
-
- BRIGHT = 1,
-
- BLINK = 5,
-
- INVERSE = 7
- };
- class ANSIColor {
-
- COLOR fg;
-
- COLOR bg;
-
-
- unsigned int reset : 1;
-
- unsigned int bold : 1;
-
- unsigned int blink : 1;
-
- unsigned int inverse : 1;
- public:
- ANSIColor();
- ANSIColor(ATTR a);
- ANSIColor(COLOR f);
- ANSIColor(COLOR f, ATTR a);
- ANSIColor(COLOR f, ATTR a1, ATTR a2);
- ANSIColor(COLOR f, COLOR b);
- ANSIColor(COLOR f, COLOR b, ATTR a);
- ANSIColor(COLOR f, COLOR b, ATTR a1, ATTR a2);
- ANSIColor &Attr(ATTR a);
- bool operator==(const ANSIColor &c) const;
- bool operator!=(const ANSIColor &c) const;
- void setFg(COLOR f);
- void setFg(COLOR f, ATTR a);
- void setBg(COLOR b);
-
- COLOR getFg() { return fg; };
-
- COLOR getBg() { return bg; };
- void attr(ATTR a);
- std::string output(void) const;
- std::string debug(void);
- std::string output(ANSIColor &previous) const;
- friend std::ostream &operator<<(std::ostream &os, const ANSIColor &c);
- };
- class Door : public std::ostream, private std::streambuf {
- private:
- std::streamsize xsputn(const char *s, std::streamsize n) override;
- int overflow(int c) override;
-
- std::string doorname;
- void parse_dropfile(const char *filepath);
- void init(void);
- std::time_t startup;
-
- struct termios tio_default;
- signed int getch(void);
- signed int getkey_or_pushback(void);
-
- bool has_dropfile;
- bool debugging;
-
- std::string dropfilename;
-
- vector<std::string> dropfilelines;
-
- ofstream logf;
- void detect_unicode_and_screen(void);
-
- std::promise<void> stop_thread;
-
- int seconds_elapsed;
- void time_thread_run(std::future<void> future);
-
- std::thread time_thread;
- public:
- Door(std::string dname, int argc, char *argv[]);
- Door(Door &) = delete;
- virtual ~Door();
- ofstream &log(void);
-
- AnyOption opt;
-
- std::string debug_buffer;
-
- ANSIColor previous;
-
- bool track;
-
- int cx;
-
- int cy;
-
- int width;
-
- int height;
-
- int inactivity;
-
- std::string username;
-
- std::string handle;
-
- std::string location;
-
- std::string sysop;
-
-
- int node;
-
- atomic<int> time_left;
-
- atomic<int> time_used;
- signed int getkey(void);
- bool haskey(void);
- signed int sleep_key(int secs);
- signed int sleep_ms_key(int msecs);
- std::string input_string(int max);
- int get_one_of(const char *keys);
- };
- class ColorOutput {
- public:
- ColorOutput();
- void reset(void);
-
- ANSIColor c;
-
- int pos;
-
- int len;
- };
- class Render {
-
- std::string text;
- public:
- Render(const std::string txt);
-
- std::vector<ColorOutput> outputs;
- void append(ANSIColor color, int len = 1);
- void output(std::ostream &os);
- };
- typedef std::function<Render(const std::string &)> renderFunction;
- typedef std::function<std::string(void)> updateFunction;
- class Clrscr {
- public:
- Clrscr(void);
- friend std::ostream &operator<<(std::ostream &os, const Clrscr &clr);
- };
- extern Clrscr cls;
- class NewLine {
- public:
- NewLine(void);
- friend std::ostream &operator<<(std::ostream &os, const NewLine &nl);
- };
- extern NewLine nl;
- extern ANSIColor reset;
- enum class Justify { NONE, LEFT, RIGHT, CENTER };
- class Goto {
-
- int x;
-
- int y;
- public:
- Goto(int xpos, int ypos);
-
- Goto(const Goto &) = default;
- void set(int xpos, int ypos);
- friend std::ostream &operator<<(std::ostream &os, const Goto &g);
- };
- extern const char SaveCursor[];
- extern const char RestoreCursor[];
- #ifdef EXPERIMENTAL
- class LineBase {
- public:
- virtual ~LineBase() = default;
- virtual bool update(void) = 0;
-
- };
- class BasicLine {
- protected:
- std::string text;
- bool hasColor;
- ANSIColor color;
-
- renderFunction render;
-
- updateFunction updater;
- public:
- BasicLine(std::string txt);
- BasicLine(std::string txt, ANSIColor c);
- BasicLine(const BasicLine &rhs) = default;
- virtual ~BasicLine() = default;
- bool hasRender(void);
- void setText(std::string txt);
- void setColor(ANSIColor c);
- void setRender(renderFunction rf);
- void setUpdater(updateFunction uf);
- bool update(void);
- friend std::ostream &operator<<(std::ostream &os, const BasicLine &l);
- };
- class MultiLine {
- protected:
- std::vector<std::shared_ptr<BasicLine>> lines;
- public:
- MultiLine();
- void append(std::shared_ptr<BasicLine> bl);
- bool update(void);
- friend std::ostream &operator<<(std::ostream &os, const MultiLine &l);
- };
- #endif
- class Line {
- protected:
-
- std::string text;
-
- bool hasColor;
-
- ANSIColor color;
-
- std::string padding;
-
- ANSIColor paddingColor;
-
- renderFunction render;
-
- updateFunction updater;
- int width;
-
-
- public:
- Line(const std::string &txt, int width = 0);
- Line(const char *txt, int width = 0);
- Line(const std::string &txt, int width, ANSIColor c);
- Line(const char *txt, int width, ANSIColor c);
- Line(const std::string &txt, int width, renderFunction rf);
- Line(const char *txt, int width, renderFunction rf);
- Line(const Line &rhs);
- Line(Line &&rhs);
-
- bool hasRender(void);
- int length(void);
- void fit(void);
-
- void setPadding(std::string &padstring, ANSIColor padColor);
-
- void setPadding(const char *padstring, ANSIColor padcolor);
- void setText(std::string &txt);
- void setText(const char *txt);
- const char *getText(void) { return text.c_str(); };
- void setColor(ANSIColor c);
- void setRender(renderFunction rf);
- void setUpdater(updateFunction uf);
- bool update(void);
- std::string debug(void);
-
- friend std::ostream &operator<<(std::ostream &os, const Line &l);
- };
- extern renderFunction rBlueYellow;
- enum class BarStyle { SOLID, HALF_STEP, GRADIENT, PERCENTAGE, PERCENT_SPACE };
- struct BarColorRange {
- unsigned long percent;
- ANSIColor c;
- };
- class BarLine : public Line {
- protected:
- BarStyle barstyle;
- unsigned long current_percent;
- void init(void);
- std::string update_bar(void);
- int length;
- vector<BarColorRange> colorRange;
- public:
- BarLine(int width);
- BarLine(int width, BarStyle style);
- BarLine(int width, BarStyle style, ANSIColor c);
-
- void watch(float &percent);
- void watch(int &value, int &max);
-
- void setStyle(BarStyle s);
- void set(int value, int max);
- void set(float percent);
- void set(unsigned long percent);
- void setColorRange(vector<BarColorRange> bcr);
-
- };
- enum class BorderStyle {
-
- NONE,
-
- SINGLE,
-
- DOUBLE,
-
- SINGLE_DOUBLE,
-
- DOUBLE_SINGLE,
-
- BLANK
- };
- class Panel {
- protected:
- int x;
- int y;
- int width;
- BorderStyle border_style;
- ANSIColor border_color;
-
- std::vector<std::unique_ptr<Line>> lines;
- bool hidden;
-
-
- bool shown_once;
- std::unique_ptr<Line> title;
- int offset;
- public:
- Panel(int x, int y, int width);
- Panel(int width);
-
- Panel(Panel &) = delete;
- Panel(Panel &&ref);
- void set(int x, int y);
- void get(int &x, int &y) {
- x = this->x;
- y = this->y;
- };
- void setTitle(std::unique_ptr<Line> T, int off = 1);
- void setStyle(BorderStyle bs);
- void setColor(ANSIColor c);
- int getWidth(void) { return width; };
- int getHeight(void) {
- if (border_style == BorderStyle::NONE)
- return lines.size();
- else
- return lines.size() + 2;
- };
- void hide(void);
- void show(void);
- void addLine(std::unique_ptr<Line> l);
-
-
-
- bool update(Door &d);
- void update(Door &d, int line);
- void update(void);
- door::Goto gotoEnd(void);
- std::unique_ptr<Line> spacer_line(bool single);
- void lineSetBack(ANSIColor back);
- friend std::ostream &operator<<(std::ostream &os, const Panel &p);
- };
- class Menu : public Panel {
- protected:
- unsigned int chosen;
- std::vector<char> options;
- renderFunction selectedRender;
- renderFunction unselectedRender;
-
- public:
- static renderFunction defaultSelectedRender;
- static renderFunction defaultUnselectedRender;
-
- Menu(int x, int y, int width);
- Menu(int width);
-
- Menu(const Menu &) = delete;
- Menu(Menu &&);
- void addSelection(char c, const char *line);
- void addSelection(char c, const char *line, updateFunction update);
- void defaultSelection(int d);
- void setRender(bool selected, renderFunction render);
- int choose(Door &door);
- char which(int d);
- static renderFunction makeRender(ANSIColor c1, ANSIColor c2, ANSIColor c3,
- ANSIColor c4);
- };
- class Screen {
- protected:
-
-
- std::vector<std::unique_ptr<Panel>> panels;
- public:
- Screen(void);
- Screen(Screen &) = default;
- void addPanel(std::unique_ptr<Panel> p);
-
- bool update(Door &d);
- void update(void);
- friend std::ostream &operator<<(std::ostream &os, const Screen &s);
- };
- }
- #endif
|