30.11.2010 - initial commit
This commit is contained in:
197
piconsole.h
Normal file
197
piconsole.h
Normal file
@@ -0,0 +1,197 @@
|
||||
#ifndef PICONSOLE_H
|
||||
#define PICONSOLE_H
|
||||
|
||||
#include "pikbdlistener.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(string name, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, string * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, char * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, bool * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, short * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, int * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, long * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, llong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, uchar * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, ushort * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, uint * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, ulong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, ullong * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, float * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addVariable(string name, double * ptr, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addBitVariable(string name, uchar * ptr, int fromBit, int bitCount, int column = 1, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
void addEmptyLine(int column = 1);
|
||||
int addTab(string name, char bind_key = 0);
|
||||
void removeTab(uint index);
|
||||
void removeTab(string name);
|
||||
uint tabsCount() const {return tabs.size();}
|
||||
bool setTab(uint index);
|
||||
bool setTab(string name);
|
||||
bool setTabBindKey(uint index, char bind_key);
|
||||
bool setTabBindKey(string name, char bind_key);
|
||||
void addCustomStatus(string 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();}
|
||||
string fstr(Flags<PIConsole::Format> f);
|
||||
string 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);
|
||||
void printLine(string str, int dx = 0, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(string str, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(char * str, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(bool value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(int value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(long value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(llong value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(float value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(double value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(char value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(short value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(uchar value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(ushort value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(uint value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(ulong value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
int printValue(ullong value, Flags<PIConsole::Format> format = PIConsole::Normal);
|
||||
static void key_event(void * t, char key);
|
||||
|
||||
struct Variable {
|
||||
string name;
|
||||
Flags<PIConsole::Format> format;
|
||||
int type;
|
||||
int offset;
|
||||
int bitFrom;
|
||||
int bitCount;
|
||||
union {
|
||||
string * 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;
|
||||
string name;
|
||||
string status;
|
||||
char key;
|
||||
Tab() {;}
|
||||
Tab(string n, char k) {name = n; key = k;}
|
||||
};
|
||||
|
||||
inline vector<vector<Variable> > & vars() {return tabs[cur_tab].variables;}
|
||||
inline int couts(string v);
|
||||
inline int couts(char * v);
|
||||
inline int couts(bool v);
|
||||
inline int couts(char v);
|
||||
inline int couts(short v);
|
||||
inline int couts(int v);
|
||||
inline int couts(long v);
|
||||
inline int couts(llong v);
|
||||
inline int couts(uchar v);
|
||||
inline int couts(ushort v);
|
||||
inline int couts(uint v);
|
||||
inline int couts(ulong v);
|
||||
inline int couts(ullong v);
|
||||
inline int couts(float v);
|
||||
inline int couts(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
|
||||
Reference in New Issue
Block a user