Advertisement
Guest User

ANSI header

a guest
Jun 29th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | Source Code | 0 0
  1. #ifndef STDARG_H
  2. #define STDARG_H
  3. #include <stdarg.h>
  4. #endif
  5.  
  6. #ifndef STDIO_H
  7. #define STDIO_H
  8. #include <stdio.h>
  9. #endif
  10.  
  11. #ifndef CONCOL_H_
  12. #define CONCOL_H_
  13.  
  14. enum CONST {
  15.     NORMAL, BOLD, LIGHT, ITALIC, UNDERSTROKE, INVERT = 7, UNVISIBLE,
  16.     BLACK = 30, RED, GREEN, BROWN, BLUE, PURPLE, CYAN, LIGHT_GRAY,
  17.     BG_BLACK = 40, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE, BG_PURPLE, BG_CYAN, BG_GRAY,
  18.     RIGHT = 50, LEFT, UP, DOWN
  19. };
  20.  
  21. void setColor(int count, ...);          //  - Set color style
  22. void setCursorPosition(int L, int C);   //  - Position the Cursor
  23. void clear(void);                       //  - Clear the screen, move to (0,0)
  24. void out(const char * sym);             //  - Print wrapper
  25. void moveUpN(int N);                    //  - Move the cursor up N lines
  26. void moveDownN(int N);                  //  - Move the cursor down N lines
  27. void moveRightN(int N);                 //  - Move the cursor forward N columns
  28. void moveLeftN(int N);                  //  - Move the cursor backward N columns
  29. void moveCursor(int DIRECTION, int N);  //  - Move the cursor
  30. void eraseToENDL(void);                 //  - Erase to end of line
  31. void saveCurPos(void);                  //  - Save cursor position
  32. void restoreCurPos(void);               //  - Restore cursor position
  33.  
  34. struct ANSI{
  35.     void (*clear)(void);
  36.     void (*pos)(int, int);
  37.     void (*col)(int, ...);
  38.     void (*out)(const char *);
  39.     void (*up)(int);
  40.     void (*down)(int);
  41.     void (*right)(int);
  42.     void (*left)(int);
  43.     void (*move)(int, int);
  44.     void (*erase)(void);
  45.     void (*save)(void);
  46.     void (*res)(void);
  47. };
  48.  
  49. struct ANSI console = {
  50.         clear,
  51.         setCursorPosition,
  52.         setColor,
  53.         out,
  54.         moveUpN,
  55.         moveDownN,
  56.         moveRightN,
  57.         moveLeftN,
  58.         moveCursor,
  59.         eraseToENDL,
  60.         saveCurPos,
  61.         restoreCurPos
  62. };
  63.  
  64.  
  65. void setColor(int count, ...){
  66.     printf("\033[0m");
  67.     if(!count) return;
  68.  
  69.     va_list ap;
  70.     va_start(ap, count);
  71.     printf("\033[");
  72.     for(int i=0; i<count; i++){
  73.         if(i+1 == count) printf("%dm",va_arg(ap,int));
  74.         else             printf("%d;",va_arg(ap,int));
  75.     }
  76.     va_end(ap);
  77. }
  78.  
  79. void setCursorPosition(int L, int C){
  80.     printf("\033[%d;%dH",C,L);
  81. }
  82.  
  83. void clear(void){
  84.     printf("\033[2J");
  85. }
  86.  
  87. void out(const char * sym){
  88.     printf(sym);
  89. }
  90.  
  91. void moveUpN(int N){
  92.     printf("\033[%dA",N);
  93. }
  94.  
  95. void moveDownN(int N){
  96.     printf("\033[%dB",N);
  97. }
  98.  
  99. void moveRightN(int N){
  100.     printf("\033[%dC",N);
  101. }
  102.  
  103. void moveLeftN(int N){
  104.     printf("\033[%dD",N);
  105. }
  106.  
  107. void moveCursor(int DIRECTION, int N){
  108.     switch(DIRECTION){
  109.     case UP: moveUpN(N); break;
  110.     case DOWN: moveDownN(N); break;
  111.     case LEFT: moveLeftN(N); break;
  112.     case RIGHT: moveRightN(N); break;
  113.     default: clear(); break;
  114.     }
  115. }
  116.  
  117. void eraseToENDL(void){
  118.     printf("\033[K");
  119. }
  120.  
  121. void saveCurPos(void){
  122.     printf("\033[s");
  123. }
  124.  
  125. void restoreCurPos(void){
  126.     printf("\033[u");
  127. }
  128.  
  129. #endif /* CONCOL_H_ */
  130.  
Tags: ansi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement