pretty.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* vcc/pretty.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <sys/times.h>
  22. #include <vcc/vcc.h>
  23. #include <vcc/version.h>
  24. #include <vcc/pretty.h>
  25. extern str_t banned_people[BAN_SIZE];
  26. extern int current_sid;
  27. extern char *usrname;
  28. struct theme theme_def = {
  29. .usrname = {
  30. .fg = FG_LIGHTBLUE,
  31. .bg = BG_BLACK,
  32. .mode = MODE_LINE
  33. },
  34. .sysusername = {
  35. .fg = FG_RED,
  36. .bg = BG_BLACK,
  37. .mode = MODE_LINE
  38. },
  39. .msg = {
  40. .fg = FG_YELLOW,
  41. .bg = BG_BLACK,
  42. .mode = MODE_HIGHLIGHT
  43. },
  44. .sysmsg = {
  45. .fg = FG_GREEN,
  46. .bg = BG_BLACK,
  47. .mode = MODE_HIGHLIGHT
  48. },
  49. .sid = {
  50. .fg = FG_RED,
  51. .bg = BG_BLACK ,
  52. .mode = MODE_HIGHLIGHT
  53. },
  54. .lvl = {
  55. .fg = FG_YELLOW,
  56. .bg = BG_BLACK,
  57. .mode = MODE_LINE
  58. },
  59. .time = {
  60. .fg = FG_YELLOW,
  61. .bg = BG_BLACK,
  62. .mode = MODE_HIGHLIGHT
  63. },
  64. .errmsg = {
  65. .fg = FG_RED,
  66. .bg = BG_BLACK,
  67. .mode = MODE_HIGHLIGHT
  68. },
  69. .helpcmd = {
  70. .fg = FG_LIGHTBLUE,
  71. .bg = BG_BLACK,
  72. .mode = MODE_HIGHLIGHT
  73. },
  74. .helpmsg = {
  75. .fg = FG_WHITE,
  76. .bg = BG_BLACK,
  77. .mode = MODE_LINE
  78. },
  79. .cqd = {
  80. .fg = FG_BLACK,
  81. .bg = BG_RED,
  82. .mode = MODE_BLINK
  83. }
  84. };
  85. struct theme *current_theme = &theme_def;
  86. int self_level;
  87. #define curr_usrname (&current_theme->usrname)
  88. #define curr_sysusername (&current_theme->sysusername)
  89. #define curr_msg (&current_theme->msg)
  90. #define curr_sysmsg (&current_theme->sysmsg)
  91. #define curr_sid (&current_theme->sid)
  92. #define curr_lvl (&current_theme->lvl)
  93. #define curr_time (&current_theme->time)
  94. #define curr_errmsg (&current_theme->errmsg)
  95. #define curr_helpcmd (&current_theme->helpcmd)
  96. #define curr_helpmsg (&current_theme->helpmsg)
  97. #define curr_cqd (&current_theme->cqd)
  98. int pretty_new_msg(char *usrname, char *buf, int sid) {
  99. time_t time_now;
  100. struct tm *tm_now;
  101. int i;
  102. for (i = 0;i < BAN_SIZE;++i) {
  103. if (banned_people[i] && !strcmp(banned_people[i], usrname)) {
  104. return 0;
  105. }
  106. }
  107. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  108. setcolor(stdout, curr_time);
  109. time_now = time(NULL);
  110. tm_now = localtime(&time_now);
  111. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  112. setcolor(stdout, curr_sid);
  113. printf("#%d ", sid);
  114. if (unlikely(!strcmp(usrname, "system"))) {
  115. setcolor(stdout, curr_sysusername);
  116. printf("system");
  117. }
  118. else {
  119. setcolor(stdout, curr_usrname);
  120. printf("%s", usrname);
  121. }
  122. ansi_csi_ed(stdout);
  123. printf("@: ");
  124. if (unlikely(!strcmp(usrname, "system"))) {
  125. setcolor(stdout, curr_sysmsg);
  126. printf("%s", buf);
  127. }
  128. else {
  129. setcolor(stdout, curr_msg);
  130. printf("%s", buf);
  131. }
  132. ansi_csi_ed(stdout);
  133. return 0;
  134. }
  135. int pretty_prompt(char *usrname) {
  136. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  137. setcolor(stderr, curr_sid);
  138. fprintf(stderr, "#%03d ", current_sid);
  139. setcolor(stderr, curr_lvl);
  140. fprintf(stderr, "lvl%d ", self_level);
  141. setcolor(stderr, curr_usrname);
  142. fprintf(stderr, "%s", usrname);
  143. ansi_csi_ed(stderr);
  144. fprintf(stderr, "$: ");
  145. return 0;
  146. }
  147. int pretty_unknown_cmd(char *s) {
  148. fprintf(stderr, "unknown command: ");
  149. setcolor(stderr, curr_errmsg);
  150. fputs(s, stderr);
  151. ansi_csi_ed(stderr);
  152. return 0;
  153. }
  154. int pretty_cmd_help(char *cmdname, char *descr) {
  155. setcolor(stdout, curr_helpcmd);
  156. printf("%s:", cmdname);
  157. setcolor(stdout, curr_helpmsg);
  158. printf("\t%s\n", descr);
  159. ansi_csi_ed(stdout);
  160. return 0;
  161. }
  162. int pretty_cqd(char *username) {
  163. printf("\n");
  164. setcolor(stdout, curr_cqd);
  165. printf("CQD");
  166. ansi_csi_ed(stdout);
  167. printf(" %s sent CQD. \n", username);
  168. return 0;
  169. }