pretty.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. .relay = {
  67. .fg = FG_BLUE,
  68. .bg = BG_BLACK,
  69. .mode = MODE_HIGHLIGHT
  70. },
  71. .errmsg = {
  72. .fg = FG_RED,
  73. .bg = BG_BLACK,
  74. .mode = MODE_HIGHLIGHT
  75. },
  76. .helpcmd = {
  77. .fg = FG_LIGHTBLUE,
  78. .bg = BG_BLACK,
  79. .mode = MODE_HIGHLIGHT
  80. },
  81. .helpmsg = {
  82. .fg = FG_WHITE,
  83. .bg = BG_BLACK,
  84. .mode = MODE_LINE
  85. },
  86. .cqd = {
  87. .fg = FG_BLACK,
  88. .bg = BG_RED,
  89. .mode = MODE_BLINK
  90. }
  91. };
  92. struct theme *current_theme = &theme_def;
  93. int self_level;
  94. #define curr_usrname (&current_theme->usrname)
  95. #define curr_sysusername (&current_theme->sysusername)
  96. #define curr_msg (&current_theme->msg)
  97. #define curr_sysmsg (&current_theme->sysmsg)
  98. #define curr_sid (&current_theme->sid)
  99. #define curr_lvl (&current_theme->lvl)
  100. #define curr_time (&current_theme->time)
  101. #define curr_relay (&current_theme->relay)
  102. #define curr_errmsg (&current_theme->errmsg)
  103. #define curr_helpcmd (&current_theme->helpcmd)
  104. #define curr_helpmsg (&current_theme->helpmsg)
  105. #define curr_cqd (&current_theme->cqd)
  106. int pretty_new_msg(char *usrname, char *buf, int sid, int flags) {
  107. time_t time_now;
  108. struct tm *tm_now;
  109. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  110. setcolor(stdout, curr_time);
  111. time_now = time(NULL);
  112. tm_now = localtime(&time_now);
  113. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  114. setcolor(stdout, curr_sid);
  115. printf("#%d ", sid);
  116. if (flags & MSG_NEW_RELAY) {
  117. setcolor(stdout, curr_relay);
  118. printf("[relay] ");
  119. if (flags & MSG_NEW_ONLY_VISIBLE)
  120. printf("[to-u-only] ");
  121. else
  122. printf("[broadcast] ");
  123. }
  124. if (unlikely(!strcmp(usrname, "system"))) {
  125. setcolor(stdout, curr_sysusername);
  126. printf("system");
  127. }
  128. else {
  129. setcolor(stdout, curr_usrname);
  130. printf("%s", usrname);
  131. }
  132. ansi_csi_ed(stdout);
  133. printf("@: ");
  134. if (unlikely(!strcmp(usrname, "system"))) {
  135. setcolor(stdout, curr_sysmsg);
  136. printf("%s\n", buf);
  137. }
  138. else {
  139. setcolor(stdout, curr_msg);
  140. printf("%s\n", buf);
  141. }
  142. ansi_csi_ed(stdout);
  143. return 0;
  144. }
  145. int pretty_prompt(char *usrname) {
  146. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  147. setcolor(stderr, curr_sid);
  148. fprintf(stderr, "#%03d ", current_sid);
  149. setcolor(stderr, curr_lvl);
  150. fprintf(stderr, "lvl%d ", self_level);
  151. setcolor(stderr, curr_usrname);
  152. fprintf(stderr, "%s", usrname);
  153. ansi_csi_ed(stderr);
  154. fprintf(stderr, "$: ");
  155. return 0;
  156. }
  157. int pretty_unknown_cmd(char *s) {
  158. fprintf(stderr, _("unknown command: "));
  159. setcolor(stderr, curr_errmsg);
  160. fputs(s, stderr);
  161. fputc('\n', stderr);
  162. ansi_csi_ed(stderr);
  163. return 0;
  164. }
  165. int pretty_cmd_help(char *cmdname, char *descr) {
  166. setcolor(stdout, curr_helpcmd);
  167. printf("%s:", cmdname);
  168. setcolor(stdout, curr_helpmsg);
  169. printf("\t%s\n", _(descr));
  170. ansi_csi_ed(stdout);
  171. return 0;
  172. }
  173. int pretty_cqd(char *username) {
  174. printf("\n");
  175. setcolor(stdout, curr_cqd);
  176. printf("CQD");
  177. ansi_csi_ed(stdout);
  178. printf(_(" %s sent CQD. \n"), username);
  179. return 0;
  180. }