term.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Handy terminal functions for vt* terms
  20. */
  21. #ifndef _ASTERISK_TERM_H
  22. #define _ASTERISK_TERM_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #define ESC 0x1b
  27. /*! \name Terminal Attributes
  28. */
  29. /*@{ */
  30. #define ATTR_RESET 0
  31. #define ATTR_BRIGHT 1
  32. #define ATTR_DIM 2
  33. #define ATTR_UNDER 4
  34. #define ATTR_BLINK 5
  35. #define ATTR_REVER 7
  36. #define ATTR_HIDDEN 8
  37. /*@} */
  38. /*! \name Terminal Colors
  39. */
  40. /*@{ */
  41. #define COLOR_BLACK 30
  42. #define COLOR_GRAY (30 | 128)
  43. #define COLOR_RED 31
  44. #define COLOR_BRRED (31 | 128)
  45. #define COLOR_GREEN 32
  46. #define COLOR_BRGREEN (32 | 128)
  47. #define COLOR_BROWN 33
  48. #define COLOR_YELLOW (33 | 128)
  49. #define COLOR_BLUE 34
  50. #define COLOR_BRBLUE (34 | 128)
  51. #define COLOR_MAGENTA 35
  52. #define COLOR_BRMAGENTA (35 | 128)
  53. #define COLOR_CYAN 36
  54. #define COLOR_BRCYAN (36 | 128)
  55. #define COLOR_WHITE 37
  56. #define COLOR_BRWHITE (37 | 128)
  57. /*@} */
  58. /*! \brief Shortcut macros for coloring a set of text
  59. */
  60. #define COLORIZE_FMT "%s%s%s"
  61. #define COLORIZE(fg, bg, str) ast_term_color(fg,bg),str,ast_term_reset()
  62. /*! \brief Maximum number of characters needed for a color escape sequence,
  63. * and another one for a trailing reset, plus a null char */
  64. #define AST_TERM_MAX_ESCAPE_CHARS 23
  65. #define AST_TERM_MAX_ROTATING_BUFFERS 15
  66. /*! \brief Colorize a specified string by adding terminal color codes
  67. *
  68. * \param outbuf Result buffer
  69. * \param inbuf Starting string
  70. * \param fgcolor Foreground color, specified as one of the constants in include/asterisk/term.h. Use '0' if the want the normal terminal foreground color.
  71. * \param bgcolor Background color, specified as one of the constants in include/asterisk/term.h. Use '0' if you want the normal terminal background color.
  72. * \param maxout Maximum size of outbuf
  73. *
  74. * \return outbuf
  75. *
  76. * \deprecated Due to the necessity of pre-sizing a result buffer, new code should avoid using this function in preference to ast_term_color_code() or ast_term_color().
  77. */
  78. char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout);
  79. /*!
  80. * \brief Append a color sequence to an ast_str
  81. *
  82. * \param str The string to append to
  83. * \param fgcolor foreground color
  84. * \param bgcolor background color
  85. *
  86. * \retval 0 success
  87. * \retval -1 failure
  88. */
  89. int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor);
  90. /*!
  91. * \brief Return a color sequence string
  92. * \param fgcolor foreground color
  93. * \param bgcolor background color
  94. * \note This function may be called up to 15 times within the arguments to a single function without the danger of overwriting a common buffer.
  95. *
  96. * \return A color sequence string, or the empty string, on error
  97. */
  98. const char *ast_term_color(int fgcolor, int bgcolor);
  99. /*!
  100. * \brief Returns the terminal reset code
  101. * \return String which, when sent to the screen, resets the terminal colors
  102. */
  103. const char *ast_term_reset(void);
  104. /*!
  105. * \brief Write a color sequence to a string
  106. *
  107. * \param outbuf the location to write to
  108. * \param fgcolor foreground color
  109. * \param bgcolor background color
  110. * \param maxout maximum number of characters to write
  111. * \deprecated You should use ast_term_color_code or ast_term_color, instead.
  112. *
  113. * \return outbuf
  114. */
  115. char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout);
  116. /*!
  117. * \brief Remove colorings from a specified string
  118. * \param outbuf the location to write to
  119. * \param inbuf the original string
  120. * \param maxout the available size of outbuf
  121. * \return outbuf
  122. */
  123. char *term_strip(char *outbuf, const char *inbuf, int maxout);
  124. void term_filter_escapes(char *line);
  125. char *term_prompt(char *outbuf, const char *inbuf, int maxout);
  126. const char *term_prep(void);
  127. const char *term_end(void);
  128. const char *term_quit(void);
  129. #if defined(__cplusplus) || defined(c_plusplus)
  130. }
  131. #endif
  132. #endif /* _ASTERISK_TERM_H */