pretty.c 2.8 KB

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