pretty.c 3.2 KB

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