Files
pip/piconsole.h
2010-12-08 16:56:14 +03:00

199 lines
9.0 KiB
C++

#ifndef PICONSOLE_H
#define PICONSOLE_H
#include "pikbdlistener.h"
#include "pistring.h"
#ifndef WINDOWS
#include <sys/ioctl.h>
#include <fcntl.h>
#endif
typedef void (*KeyFunc)(void * , char);
class PIConsole: public PIThread
{
public:
PIConsole(bool startNow = true, KeyFunc slot = 0);
~PIConsole();
enum Format {Normal = 0x01,
Bold = 0x02,
Faint = 0x04,
Italic = 0x08,
Underline = 0x10,
Blink = 0x20,
Inverse = 0x40,
Black = 0x100,
Red = 0x200,
Green = 0x400,
Yellow = 0x800,
Blue = 0x1000,
Magenta = 0x2000,
Cyan = 0x4000,
White = 0x8000,
Dec = 0x10000,
Hex = 0x20000,
Oct = 0x40000,
Scientific = 0x80000};
void addString(PIString name, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, PIString * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, char * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, bool * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, short * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, int * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, long * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, llong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, uchar * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, ushort * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, uint * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, ulong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, ullong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, float * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addVariable(PIString name, double * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addBitVariable(PIString name, uchar * ptr, int fromBit, int bitCount, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
void addEmptyLine(int column = 1);
int addTab(PIString name, char bind_key = 0);
void removeTab(uint index);
void removeTab(PIString name);
uint tabsCount() const {return tabs.size();}
bool setTab(uint index);
bool setTab(PIString name);
bool setTabBindKey(uint index, char bind_key);
bool setTabBindKey(PIString name, char bind_key);
void addCustomStatus(PIString str) {tabs[cur_tab].status = str;}
void clearCustomStatus() {tabs[cur_tab].status = "";}
void clearVariables(bool clearScreen = true) {if (clearScreen && isRunning()) {toUpperLeft(); clearScreenLower();} vars().clear();}
void clearTabs(bool clearScreen = true) {if (clearScreen && isRunning()) {toUpperLeft(); clearScreenLower();} tabs.clear();}
PIString fstr(Flags<PIConsole::Format> f);
PIString currentTab() const {return tabs[cur_tab].name;}
private:
void begin();
void run();
void fillLabels();
#ifdef WINDOWS
inline void getWinCurCoord() {GetConsoleScreenBufferInfo(hOut, &csbi); ccoord = csbi.dwCursorPosition;}
inline COORD & getWinCoord(int dx = 0, int dy = 0) {getWinCurCoord(); ccoord.X += dx; ccoord.Y += dy; return ccoord;}
inline void toUpperLeft() {SetConsoleCursorPosition(hOut, ulcoord);}
inline void moveRight(int n = 1) {SetConsoleCursorPosition(hOut, getWinCoord(n));}
inline void moveLeft(int n = 1) {SetConsoleCursorPosition(hOut, getWinCoord(-n));}
inline void moveTo(int x = 0, int y = 0) {ccoord.X = x; ccoord.Y = y; SetConsoleCursorPosition(hOut, ccoord);}
inline void clearScreen() {FillConsoleOutputAttribute(hOut, dattr, width * (height + 1), ulcoord, &written);
FillConsoleOutputCharacter(hOut, ' ', width * (height + 1), ulcoord, &written);}
inline void clearScreenLower() {getWinCurCoord(); FillConsoleOutputAttribute(hOut, dattr, width * height - width * ccoord.Y + ccoord.X, ccoord, &written);
FillConsoleOutputCharacter(hOut, ' ', width * height - width * ccoord.Y + ccoord.X, ccoord, &written);}
inline void clearLine() {getWinCurCoord(); FillConsoleOutputAttribute(hOut, dattr, width - ccoord.X, ccoord, &written);
FillConsoleOutputCharacter(hOut, ' ', width - ccoord.X, ccoord, &written);}
inline void newLine() {getWinCurCoord(); ccoord.X = 0; ccoord.Y++; SetConsoleCursorPosition(hOut, ccoord);}
inline void hideCursor() {curinfo.bVisible = false; SetConsoleCursorInfo(hOut, &curinfo);}
inline void showCursor() {curinfo.bVisible = true; SetConsoleCursorInfo(hOut, &curinfo);}
#else
inline void toUpperLeft() {printf("\e[H");}
inline void moveRight(int n = 1) {if (n > 0) printf("\e[%dC", n);}
inline void moveLeft(int n = 1) {if (n > 0) printf("\e[%dD", n);}
inline void moveTo(int x = 0, int y = 0) {printf("\e[%d;%dH", y, x);}
inline void clearScreen() {printf("\e[H\e[J");}
inline void clearScreenLower() {printf("\e[J");}
inline void clearLine() {printf("\e[K");}
inline void newLine() {printf("\eE");}
inline void hideCursor() {printf("\e[?25l");}
inline void showCursor() {printf("\e[?25h");}
#endif
void status();
void checkColumn(uint col) {if (vars().size() < col) {vars().resize(col);}}
int bitsValue(unsigned char * src, int offset, int count);
inline void printLine(const PIString & str, int dx = 0, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const PIString & str, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const char * str, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const bool value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const int value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const long value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const llong value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const float value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const double value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const char value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const short value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const uchar value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const ushort value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const uint value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const ulong value, Flags<PIConsole::Format> format = PIConsole::Normal);
inline int printValue(const ullong value, Flags<PIConsole::Format> format = PIConsole::Normal);
static void key_event(void * t, char key);
struct Variable {
PIString name;
Flags<PIConsole::Format> format;
int type;
int offset;
int bitFrom;
int bitCount;
union {
PIString * s;
bool * b;
short * sh;
int * i;
long * l;
llong * ll;
float * f;
double * d;
char * c;
uchar * uc;
ushort * ush;
uint * ui;
ulong * ul;
ullong * ull;
};
void operator =(const Variable & src) {name = src.name; format = src.format; type = src.type; offset = src.offset;
bitFrom = src.bitFrom; bitCount = src.bitCount; c = src.c;}
};
struct Tab {
vector<vector<Variable> > variables;
PIString name;
PIString status;
char key;
Tab() {;}
Tab(PIString n, char k) {name = n; key = k;}
};
inline vector<vector<Variable> > & vars() {return tabs[cur_tab].variables;}
inline int couts(const string v);
inline int couts(const char * v);
inline int couts(const bool v);
inline int couts(const char v);
inline int couts(const short v);
inline int couts(const int v);
inline int couts(const long v);
inline int couts(const llong v);
inline int couts(const uchar v);
inline int couts(const ushort v);
inline int couts(const uint v);
inline int couts(const ulong v);
inline int couts(const ullong v);
inline int couts(const float v);
inline int couts(const double v);
#ifdef WINDOWS
void * hOut;
CONSOLE_SCREEN_BUFFER_INFO sbi, csbi;
CONSOLE_CURSOR_INFO curinfo;
COORD ccoord, ulcoord;
WORD dattr;
DWORD smode, written;
#else
struct termios sterm, vterm;
#endif
vector<Tab> tabs;
Variable tv;
PIKbdListener * listener;
KeyFunc ret_func;
int width, height, ret, col_wid, num_format;
uint my;
uint cur_tab, col_cnt;
};
#endif // PICONSOLE_H