pretty.c 4.3 KB

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