pretty.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  150. setcolor(stdout, curr_time);
  151. time_now = time(NULL);
  152. tm_now = localtime(&time_now);
  153. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  154. setcolor(stdout, curr_sid);
  155. printf("#%d ", sid);
  156. if (flags & MSG_NEW_RELAY) {
  157. setcolor(stdout, curr_relay);
  158. printf("[relay] ");
  159. if (flags & MSG_NEW_ONLY_VISIBLE)
  160. printf("[to-u-only] ");
  161. else
  162. printf("[broadcast] ");
  163. }
  164. if (flags & MSG_ENCRYPTED) {
  165. setcolor(stdout, curr_time);
  166. printf("[encrypted] ");
  167. }
  168. if (unlikely(!strcmp(usrname, "system"))) {
  169. setcolor(stdout, curr_sysusername);
  170. printf("system");
  171. }
  172. else {
  173. setcolor(stdout, curr_usrname);
  174. printf("%s", usrname);
  175. }
  176. ansi_csi_ed(stdout);
  177. printf("@: ");
  178. if (unlikely(!strcmp(usrname, "system"))) {
  179. setcolor(stdout, curr_sysmsg);
  180. printf("%s\n", buf);
  181. }
  182. else {
  183. setcolor(stdout, curr_msg);
  184. printf("%s\n", buf);
  185. }
  186. ansi_csi_ed(stdout);
  187. return 0;
  188. }
  189. int pretty_prompt(char *usrname) {
  190. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  191. setcolor(stderr, curr_sid);
  192. fprintf(stderr, "#%03d ", current_sid);
  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. }