pretty.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 int current_sid;
  26. extern char *usrname;
  27. struct theme theme_def = {
  28. .usrname = {
  29. .fg = FG_LIGHTBLUE,
  30. .bg = BG_BLACK,
  31. .mode = MODE_LINE
  32. },
  33. .sysusername = {
  34. .fg = FG_RED,
  35. .bg = BG_BLACK,
  36. .mode = MODE_LINE
  37. },
  38. .msg = {
  39. .fg = FG_YELLOW,
  40. .bg = BG_BLACK,
  41. .mode = MODE_HIGHLIGHT
  42. },
  43. .sysmsg = {
  44. .fg = FG_GREEN,
  45. .bg = BG_BLACK,
  46. .mode = MODE_HIGHLIGHT
  47. },
  48. .sid = {
  49. .fg = FG_RED,
  50. .bg = BG_BLACK ,
  51. .mode = MODE_HIGHLIGHT
  52. },
  53. .lvl = {
  54. .fg = FG_YELLOW,
  55. .bg = BG_BLACK,
  56. .mode = MODE_LINE
  57. },
  58. .time = {
  59. .fg = FG_YELLOW,
  60. .bg = BG_BLACK,
  61. .mode = MODE_HIGHLIGHT
  62. },
  63. .errmsg = {
  64. .fg = FG_RED,
  65. .bg = BG_BLACK,
  66. .mode = MODE_HIGHLIGHT
  67. },
  68. .helpcmd = {
  69. .fg = FG_LIGHTBLUE,
  70. .bg = BG_BLACK,
  71. .mode = MODE_HIGHLIGHT
  72. },
  73. .helpmsg = {
  74. .fg = FG_WHITE,
  75. .bg = BG_BLACK,
  76. .mode = MODE_LINE
  77. },
  78. .cqd = {
  79. .fg = FG_BLACK,
  80. .bg = BG_RED,
  81. .mode = MODE_BLINK
  82. }
  83. };
  84. struct theme *current_theme = &theme_def;
  85. int self_level;
  86. #define curr_usrname (&current_theme->usrname)
  87. #define curr_sysusername (&current_theme->sysusername)
  88. #define curr_msg (&current_theme->msg)
  89. #define curr_sysmsg (&current_theme->sysmsg)
  90. #define curr_sid (&current_theme->sid)
  91. #define curr_lvl (&current_theme->lvl)
  92. #define curr_time (&current_theme->time)
  93. #define curr_errmsg (&current_theme->errmsg)
  94. #define curr_helpcmd (&current_theme->helpcmd)
  95. #define curr_helpmsg (&current_theme->helpmsg)
  96. #define curr_cqd (&current_theme->cqd)
  97. int pretty_new_msg(char *usrname, char *buf, int sid) {
  98. time_t time_now;
  99. struct tm *tm_now;
  100. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  101. setcolor(stdout, curr_time);
  102. time_now = time(NULL);
  103. tm_now = localtime(&time_now);
  104. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  105. setcolor(stdout, curr_sid);
  106. printf("#%d ", sid);
  107. if (unlikely(!strcmp(usrname, "system"))) {
  108. setcolor(stdout, curr_sysusername);
  109. printf("system");
  110. }
  111. else {
  112. setcolor(stdout, curr_usrname);
  113. printf("%s", usrname);
  114. }
  115. ansi_csi_ed(stdout);
  116. printf("@: ");
  117. if (unlikely(!strcmp(usrname, "system"))) {
  118. setcolor(stdout, curr_sysmsg);
  119. printf("%s", buf);
  120. }
  121. else {
  122. setcolor(stdout, curr_msg);
  123. printf("%s", buf);
  124. }
  125. ansi_csi_ed(stdout);
  126. return 0;
  127. }
  128. int pretty_prompt(char *usrname) {
  129. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  130. setcolor(stderr, curr_sid);
  131. fprintf(stderr, "#%03d ", current_sid);
  132. setcolor(stderr, curr_lvl);
  133. fprintf(stderr, "lvl%d", self_level);
  134. setcolor(stderr, curr_usrname);
  135. fprintf(stderr, "%s", usrname);
  136. ansi_csi_ed(stderr);
  137. fprintf(stderr, "$: ");
  138. return 0;
  139. }
  140. int pretty_unknown_cmd(char *s) {
  141. fprintf(stderr, "unknown command: ");
  142. setcolor(stderr, curr_errmsg);
  143. fputs(s, stderr);
  144. ansi_csi_ed(stderr);
  145. return 0;
  146. }
  147. int pretty_cmd_help(char *cmdname, char *descr) {
  148. setcolor(stdout, curr_helpcmd);
  149. printf("%s:", cmdname);
  150. setcolor(stdout, curr_helpmsg);
  151. printf("\t%s\n", descr);
  152. ansi_csi_ed(stdout);
  153. return 0;
  154. }
  155. int pretty_cqd(char *username) {
  156. printf("\n");
  157. setcolor(stdout, curr_cqd);
  158. printf("CQD");
  159. ansi_csi_ed(stdout);
  160. printf(" %s sent CQD. \n", username);
  161. return 0;
  162. }