pretty.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <limits.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <sys/times.h>
  26. #include <vcc/vcc.h>
  27. #include <vcc/version.h>
  28. #include <vcc/pretty.h>
  29. #include <vcc/plugin.h>
  30. #include <vcc/interfaces.h>
  31. #include <readconf/readconf.h>
  32. struct theme theme_def = {
  33. .usrname = {
  34. .fg = FG_LIGHTBLUE,
  35. .bg = BG_BLACK,
  36. .mode = MODE_LINE
  37. },
  38. .sysusername = {
  39. .fg = FG_RED,
  40. .bg = BG_BLACK,
  41. .mode = MODE_LINE
  42. },
  43. .msg = {
  44. .fg = FG_YELLOW,
  45. .bg = BG_BLACK,
  46. .mode = MODE_HIGHLIGHT
  47. },
  48. .sysmsg = {
  49. .fg = FG_GREEN,
  50. .bg = BG_BLACK,
  51. .mode = MODE_HIGHLIGHT
  52. },
  53. .sid = {
  54. .fg = FG_RED,
  55. .bg = BG_BLACK ,
  56. .mode = MODE_HIGHLIGHT
  57. },
  58. .lvl = {
  59. .fg = FG_YELLOW,
  60. .bg = BG_BLACK,
  61. .mode = MODE_LINE
  62. },
  63. .time = {
  64. .fg = FG_YELLOW,
  65. .bg = BG_BLACK,
  66. .mode = MODE_HIGHLIGHT
  67. },
  68. .relay = {
  69. .fg = FG_BLUE,
  70. .bg = BG_BLACK,
  71. .mode = MODE_HIGHLIGHT
  72. },
  73. .errmsg = {
  74. .fg = FG_RED,
  75. .bg = BG_BLACK,
  76. .mode = MODE_HIGHLIGHT
  77. },
  78. .helpcmd = {
  79. .fg = FG_LIGHTBLUE,
  80. .bg = BG_BLACK,
  81. .mode = MODE_HIGHLIGHT
  82. },
  83. .helpmsg = {
  84. .fg = FG_WHITE,
  85. .bg = BG_BLACK,
  86. .mode = MODE_LINE
  87. },
  88. .cqd = {
  89. .fg = FG_BLACK,
  90. .bg = BG_RED,
  91. .mode = MODE_BLINK
  92. }
  93. };
  94. struct theme *current_theme = &theme_def;
  95. int self_level;
  96. int conf_fd;
  97. #include "gnr_theme_macros.s"
  98. int open_pretty_conf(void) {
  99. char *home;
  100. char path[PATH_MAX];
  101. if (unlikely(!(home = getenv("HOME"))))
  102. goto abandon;
  103. sprintf(path, "%s/.vcc-theme", home);
  104. if ((conf_fd = open(path, O_RDONLY)) < 0)
  105. goto abandon;
  106. printf("reading theme ...");
  107. return 0;
  108. abandon:
  109. fprintf(stderr, "using default theme. \n");
  110. return 1;
  111. }
  112. int nr_ready = 0;
  113. int parse_theme(char *name, struct color *p) {
  114. struct readconf_uintconf *conf;
  115. char key[32];
  116. sprintf(key, "%s_mode", name);
  117. #define tryload \
  118. if (!(conf = readconf_find_uintname(key))) { \
  119. fprintf(stderr, "cannot load theme %s\n", name); \
  120. return 1; \
  121. }
  122. tryload;
  123. p->mode = conf->conf_value;
  124. memset(key, 0, 32);
  125. sprintf(key, "%s_fg", name);
  126. tryload;
  127. p->fg = conf->conf_value;
  128. memset(key, 0, 32);
  129. sprintf(key, "%s_bg", name);
  130. tryload;
  131. p->bg = conf->conf_value;
  132. return 0;
  133. }
  134. int load_themes(void) {
  135. #include "gnr_load_themes.s"
  136. return 0;
  137. }
  138. int pretty_init(void) {
  139. if (open_pretty_conf())
  140. return 0;
  141. readconf_init();
  142. load_themes();
  143. close(conf_fd);
  144. return 0;
  145. }
  146. int pretty_new_msg(char *usrname, char *buf, int sid, int flags) {
  147. time_t time_now;
  148. struct tm *tm_now;
  149. char *sname;
  150. sname = getsname(sid);
  151. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  152. setcolor(stdout, curr_time);
  153. time_now = time(NULL);
  154. tm_now = localtime(&time_now);
  155. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  156. setcolor(stdout, curr_sid);
  157. printf("#%s ", sname);
  158. if (flags & MSG_NEW_RELAY) {
  159. setcolor(stdout, curr_relay);
  160. printf("[relay] ");
  161. if (flags & MSG_NEW_ONLY_VISIBLE)
  162. printf("[to-u-only] ");
  163. else
  164. printf("[broadcast] ");
  165. }
  166. if (flags & MSG_ENCRYPTED) {
  167. setcolor(stdout, curr_time);
  168. printf("[encrypted] ");
  169. }
  170. if (unlikely(!strcmp(usrname, "system"))) {
  171. setcolor(stdout, curr_sysusername);
  172. printf("system");
  173. }
  174. else {
  175. setcolor(stdout, curr_usrname);
  176. printf("%s", usrname);
  177. }
  178. ansi_csi_ed(stdout);
  179. printf("@: ");
  180. if (unlikely(!strcmp(usrname, "system"))) {
  181. setcolor(stdout, curr_sysmsg);
  182. printf("%s\n", buf);
  183. }
  184. else {
  185. setcolor(stdout, curr_msg);
  186. printf("%s\n", buf);
  187. }
  188. ansi_csi_ed(stdout);
  189. return 0;
  190. }
  191. int pretty_prompt(char *usrname) {
  192. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  193. setcolor(stderr, curr_sid);
  194. fprintf(stderr, "#%03d ", current_sid);
  195. setcolor(stderr, curr_lvl);
  196. fprintf(stderr, "lvl%d ", self_level);
  197. setcolor(stderr, curr_usrname);
  198. fprintf(stderr, "%s", usrname);
  199. ansi_csi_ed(stderr);
  200. fprintf(stderr, "$: ");
  201. return 0;
  202. }
  203. int pretty_unknown_cmd(char *s) {
  204. fprintf(stderr, _("unknown command: "));
  205. setcolor(stderr, curr_errmsg);
  206. fputs(s, stderr);
  207. fputc('\n', stderr);
  208. ansi_csi_ed(stderr);
  209. return 0;
  210. }
  211. int pretty_cmd_help(char *cmdname, char *descr) {
  212. setcolor(stdout, curr_helpcmd);
  213. printf("%s:", cmdname);
  214. setcolor(stdout, curr_helpmsg);
  215. printf("\t%s\n", _(descr));
  216. ansi_csi_ed(stdout);
  217. return 0;
  218. }
  219. int pretty_cqd(char *username, char *msg) {
  220. printf("\n");
  221. setcolor(stdout, curr_cqd);
  222. printf("CQD");
  223. ansi_csi_ed(stdout);
  224. printf(_(" %s sent %s"), username, msg);
  225. return 0;
  226. }