pretty.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 openconf(void) {
  99. char *home;
  100. char path[PATH_MAX];
  101. if (unlikely(!(home = getenv("HOME"))))
  102. goto abandon;
  103. sprintf(path, "%s/.vcc-config", home);
  104. if ((conf_fd = open(path, O_RDONLY)) < 0)
  105. goto abandon;
  106. return 0;
  107. abandon:
  108. fprintf(stderr, "using default config. \n");
  109. return 1;
  110. }
  111. int nr_ready = 0;
  112. int parse_theme(char *name, struct color *p) {
  113. struct readconf_uintconf *conf;
  114. char key[32];
  115. sprintf(key, "%s_mode", name);
  116. #define tryload \
  117. if (!(conf = readconf_find_uintname(key))) { \
  118. fprintf(stderr, "cannot load theme %s\n", name); \
  119. return 1; \
  120. }
  121. tryload;
  122. p->mode = conf->conf_value;
  123. memset(key, 0, 32);
  124. sprintf(key, "%s_fg", name);
  125. tryload;
  126. p->fg = conf->conf_value;
  127. memset(key, 0, 32);
  128. sprintf(key, "%s_bg", name);
  129. tryload;
  130. p->bg = conf->conf_value;
  131. return 0;
  132. }
  133. int load_themes(void) {
  134. #include "gnr_load_themes.s"
  135. return 0;
  136. }
  137. int pretty_init(void) {
  138. load_themes();
  139. close(conf_fd);
  140. return 0;
  141. }
  142. int pretty_new_msg(char *usrname, char *buf, int sid, int flags) {
  143. time_t time_now;
  144. struct tm *tm_now;
  145. char *sname;
  146. sname = getsname(sid);
  147. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  148. setcolor(stdout, curr_time);
  149. time_now = time(NULL);
  150. tm_now = localtime(&time_now);
  151. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  152. setcolor(stdout, curr_sid);
  153. printf("#%s ", sname);
  154. if (flags & MSG_NEW_RELAY) {
  155. setcolor(stdout, curr_relay);
  156. printf("[relay] ");
  157. if (flags & MSG_NEW_ONLY_VISIBLE)
  158. printf("[to-u-only] ");
  159. else
  160. printf("[broadcast] ");
  161. }
  162. if (flags & MSG_ENCRYPTED) {
  163. setcolor(stdout, curr_time);
  164. printf("[encrypted] ");
  165. }
  166. if (unlikely(!strcmp(usrname, "system"))) {
  167. setcolor(stdout, curr_sysusername);
  168. printf("system");
  169. }
  170. else {
  171. setcolor(stdout, curr_usrname);
  172. printf("%s", usrname);
  173. }
  174. ansi_csi_ed(stdout);
  175. printf("@: ");
  176. if (unlikely(!strcmp(usrname, "system"))) {
  177. setcolor(stdout, curr_sysmsg);
  178. printf("%s\n", buf);
  179. }
  180. else {
  181. setcolor(stdout, curr_msg);
  182. printf("%s\n", buf);
  183. }
  184. ansi_csi_ed(stdout);
  185. return 0;
  186. }
  187. int pretty_prompt(char *usrname) {
  188. char *sname;
  189. sname = getsname(current_sid);
  190. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  191. setcolor(stderr, curr_sid);
  192. fprintf(stderr, "#%s ", sname);
  193. setcolor(stderr, curr_lvl);
  194. fprintf(stderr, "lvl%d ", self_level);
  195. setcolor(stderr, curr_usrname);
  196. fprintf(stderr, "%s", usrname);
  197. ansi_csi_ed(stderr);
  198. fprintf(stderr, "$: ");
  199. return 0;
  200. }
  201. int pretty_unknown_cmd(char *s) {
  202. fprintf(stderr, _("unknown command: "));
  203. setcolor(stderr, curr_errmsg);
  204. fputs(s, stderr);
  205. fputc('\n', stderr);
  206. ansi_csi_ed(stderr);
  207. return 0;
  208. }
  209. int pretty_cmd_help(char *cmdname, char *descr) {
  210. setcolor(stdout, curr_helpcmd);
  211. printf("%s:", cmdname);
  212. setcolor(stdout, curr_helpmsg);
  213. printf("\t%s\n", _(descr));
  214. ansi_csi_ed(stdout);
  215. return 0;
  216. }
  217. int pretty_cqd(char *username, char *msg) {
  218. printf("\n");
  219. setcolor(stdout, curr_cqd);
  220. printf("CQD");
  221. ansi_csi_ed(stdout);
  222. printf(_(" %s sent %s"), username, msg);
  223. return 0;
  224. }