pretty.c 3.0 KB

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