00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef INCLUDE_TTERMMANIP_H
00013 #define INCLUDE_TTERMMANIP_H
00014
00015 #include <map>
00016 #include <cstring>
00017 #include <stdio.h>
00018
00019 extern "C" typedef int (*PutcFunc_t)(int);
00020
00021
00022 class TTermManip {
00023 public:
00024 TTermManip();
00025 ~TTermManip() { ResetTerm(); }
00026
00027 bool SetColor(unsigned char r, unsigned char g, unsigned char b);
00028 bool SetColor(int idx);
00029
00030 int GetColorIndex(unsigned char r, unsigned char g, unsigned char b);
00031
00032 void
00033 StartUnderline() {
00034 if (!fCurrentlyUnderlined) {
00035 WriteTerm(fStartUnderline);
00036 fCurrentlyUnderlined = true;
00037 }
00038 }
00039
00040
00041 void
00042 StopUnderline() {
00043 if (fCurrentlyUnderlined) {
00044 WriteTerm(fStopUnderline);
00045 fCurrentlyUnderlined = false;
00046 }
00047 }
00048
00049 void StartBold();
00050 void StopBold();
00051
00052
00053 bool ResetTerm();
00054 void SetDefaultColor();
00055
00056 private:
00057 class Color {
00058 public:
00059 Color(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0):
00060 fR((r* 1001) / 256),
00061 fG((g* 1001) / 256),
00062 fB((b* 1001) / 256) {
00063
00064 }
00065
00066
00067 bool
00068 operator <(const Color& c) const {
00069 return fR < c.fR
00070 || (fR == c.fR && (fG < c.fG
00071 || (fG == c.fG && fB < c.fB)));
00072 }
00073
00074
00075 int fR, fG, fB;
00076 };
00077
00078 char* GetTermStr(const char* cap);
00079 int GetTermNum(const char* cap);
00080
00081 bool WriteTerm(char* termstr);
00082
00083 bool WriteTerm(char* termstr, int i);
00084
00085 static int
00086 DefaultPutchar(int c) {
00087
00088 return putchar(c);
00089 }
00090
00091
00092 int fNumColors;
00093 bool fAnsiColors;
00094 char* fSetFg;
00095 char* fSetBold;
00096 char* fSetDefault;
00097 char* fStartUnderline;
00098 char* fStopUnderline;
00099 PutcFunc_t fPutc;
00100 int fCurrentColorIdx;
00101 bool fCurrentlyBold;
00102 bool fCurrentlyUnderlined;
00103 };
00104
00105 #endif // INCLUDE_TTERMMANIP_H