pretty.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. extern int current_sid;
  26. struct theme theme_def = {
  27. .usrname = {
  28. .fg = FG_LIGHTBLUE,
  29. .bg = BG_BLACK,
  30. .mode = MODE_LINE
  31. },
  32. .sysusername = {
  33. .fg = FG_RED,
  34. .bg = BG_BLACK,
  35. .mode = MODE_LINE
  36. },
  37. .msg = {
  38. .fg = FG_YELLOW,
  39. .bg = BG_BLACK,
  40. .mode = MODE_HIGHLIGHT
  41. },
  42. .sysmsg = {
  43. .fg = FG_GREEN,
  44. .bg = BG_BLACK,
  45. .mode = MODE_HIGHLIGHT
  46. },
  47. .sid = {
  48. .fg = FG_RED,
  49. .bg = BG_BLACK ,
  50. .mode = MODE_HIGHLIGHT
  51. },
  52. .lvl = {
  53. .fg = FG_YELLOW,
  54. .bg = BG_BLACK,
  55. .mode = MODE_LINE
  56. },
  57. .time = {
  58. .fg = FG_YELLOW,
  59. .bg = BG_BLACK,
  60. .mode = MODE_HIGHLIGHT
  61. },
  62. .errmsg = {
  63. .fg = FG_RED,
  64. .bg = BG_BLACK,
  65. .mode = MODE_HIGHLIGHT
  66. },
  67. .helpcmd = {
  68. .fg = FG_LIGHTBLUE,
  69. .bg = BG_BLACK,
  70. .mode = MODE_HIGHLIGHT
  71. },
  72. .helpmsg = {
  73. .fg = FG_WHITE,
  74. .bg = BG_BLACK,
  75. .mode = MODE_LINE
  76. }
  77. };
  78. struct theme *current_theme = &theme_def;
  79. int self_level;
  80. #define curr_usrname (&current_theme->usrname)
  81. #define curr_sysusername (&current_theme->sysusername)
  82. #define curr_msg (&current_theme->msg)
  83. #define curr_sysmsg (&current_theme->sysmsg)
  84. #define curr_sid (&current_theme->sid)
  85. #define curr_lvl (&current_theme->lvl)
  86. #define curr_time (&current_theme->time)
  87. #define curr_errmsg (&current_theme->errmsg)
  88. #define curr_helpcmd (&current_theme->helpcmd)
  89. #define curr_helpmsg (&current_theme->helpmsg)
  90. int pretty_new_msg(char *usrname, char *buf, int sid) {
  91. time_t time_now;
  92. struct tm *tm_now;
  93. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  94. setcolor(stdout, curr_time);
  95. time_now = time(NULL);
  96. tm_now = localtime(&time_now);
  97. printf("\r[%02d:%02d] ", tm_now->tm_hour, tm_now->tm_min);
  98. setcolor(stdout, curr_sid);
  99. printf("#%d ", sid);
  100. if (unlikely(!strcmp(usrname, "system"))) {
  101. setcolor(stdout, curr_sysusername);
  102. printf("system");
  103. }
  104. else {
  105. setcolor(stdout, curr_usrname);
  106. printf("%s", usrname);
  107. }
  108. ansi_csi_ed(stdout);
  109. printf("@: ");
  110. if (unlikely(!strcmp(usrname, "system"))) {
  111. setcolor(stdout, curr_sysmsg);
  112. printf("%s", buf);
  113. }
  114. else {
  115. setcolor(stdout, curr_msg);
  116. printf("%s", buf);
  117. }
  118. ansi_csi_ed(stdout);
  119. return 0;
  120. }
  121. int pretty_prompt(char *usrname) {
  122. ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
  123. setcolor(stderr, curr_sid);
  124. fprintf(stderr, "#%03d ", current_sid);
  125. setcolor(stderr, curr_lvl);
  126. fprintf(stderr, "lvl%d", self_level);
  127. setcolor(stderr, curr_usrname);
  128. fprintf(stderr, "%s", usrname);
  129. ansi_csi_ed(stderr);
  130. fprintf(stderr, "$: ");
  131. return 0;
  132. }
  133. int pretty_unknown_cmd(char *s) {
  134. fprintf(stderr, "unknown command: ");
  135. setcolor(stderr, curr_errmsg);
  136. fputs(s, stderr);
  137. ansi_csi_ed(stderr);
  138. return 0;
  139. }
  140. int pretty_cmd_help(char *cmdname, char *descr) {
  141. setcolor(stdout, curr_helpcmd);
  142. printf("%s:", cmdname);
  143. setcolor(stdout, curr_helpmsg);
  144. printf("\t%s\n", descr);
  145. ansi_csi_ed(stdout);
  146. return 0;
  147. }