pretty.c 4.0 KB

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