pretty.c 3.5 KB

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