pretty.c 4.1 KB

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