您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ansicol.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <ipxe/ansiesc.h>
  24. #include <ipxe/ansicol.h>
  25. #include <config/colour.h>
  26. /** @file
  27. *
  28. * ANSI colours
  29. *
  30. */
  31. /** ANSI colour pair definitions */
  32. static struct ansicol_pair ansicol_pairs[] = {
  33. [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
  34. [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
  35. [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
  36. [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
  37. [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
  38. [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
  39. [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
  40. [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
  41. };
  42. /**
  43. * Set ANSI colour (when no colour definition support is present)
  44. *
  45. * @v colour Colour index
  46. * @v which Foreground/background selector
  47. */
  48. __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
  49. /* Colour indices are hardcoded and should never be out of range */
  50. assert ( colour < 10 );
  51. /* Set basic colour */
  52. printf ( CSI "%c%dm", which, colour );
  53. }
  54. /**
  55. * Set ANSI foreground colour
  56. *
  57. * @v colour Colour index
  58. */
  59. static void ansicol_foreground ( unsigned int colour ) {
  60. ansicol_set ( colour, '3' );
  61. }
  62. /**
  63. * Set ANSI background colour
  64. *
  65. * @v colour Colour index
  66. */
  67. static void ansicol_background ( unsigned int colour ) {
  68. ansicol_set ( colour, '4' );
  69. }
  70. /**
  71. * Set ANSI foreground and background colour
  72. *
  73. * @v cpair Colour pair index
  74. */
  75. void ansicol_set_pair ( unsigned int cpair ) {
  76. struct ansicol_pair *pair;
  77. /* Colour pair indices are hardcoded and should never be out of range */
  78. assert ( cpair < ( sizeof ( ansicol_pairs ) /
  79. sizeof ( ansicol_pairs[0] ) ) );
  80. /* Set both foreground and background colours */
  81. pair = &ansicol_pairs[cpair];
  82. ansicol_foreground ( pair->foreground );
  83. ansicol_background ( pair->background );
  84. }
  85. /**
  86. * Define ANSI colour pair
  87. *
  88. * @v cpair Colour pair index
  89. * @v foreground Foreground colour index
  90. * @v background Background colour index
  91. * @ret rc Return status code
  92. */
  93. int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
  94. unsigned int background ) {
  95. struct ansicol_pair *pair;
  96. /* Fail if colour index is out of range */
  97. if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
  98. return -EINVAL;
  99. /* Update colour pair definition */
  100. pair = &ansicol_pairs[cpair];
  101. pair->foreground = foreground;
  102. pair->background = background;
  103. DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
  104. "foreground %d background %d\n", cpair, foreground, background );
  105. return 0;
  106. }