You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansicol.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <ipxe/ansiesc.h>
  28. #include <ipxe/ansicol.h>
  29. #include <config/colour.h>
  30. /** @file
  31. *
  32. * ANSI colours
  33. *
  34. */
  35. /** ANSI colour pair definitions */
  36. static struct ansicol_pair ansicol_pairs[] = {
  37. [CPAIR_DEFAULT] = { COLOR_DEFAULT, COLOR_DEFAULT },
  38. [CPAIR_NORMAL] = { COLOR_NORMAL_FG, COLOR_NORMAL_BG },
  39. [CPAIR_SELECT] = { COLOR_SELECT_FG, COLOR_SELECT_BG },
  40. [CPAIR_SEPARATOR] = { COLOR_SEPARATOR_FG, COLOR_SEPARATOR_BG },
  41. [CPAIR_EDIT] = { COLOR_EDIT_FG, COLOR_EDIT_BG },
  42. [CPAIR_ALERT] = { COLOR_ALERT_FG, COLOR_ALERT_BG },
  43. [CPAIR_URL] = { COLOR_URL_FG, COLOR_URL_BG },
  44. [CPAIR_PXE] = { COLOR_PXE_FG, COLOR_PXE_BG },
  45. };
  46. /**
  47. * Set ANSI colour (when no colour definition support is present)
  48. *
  49. * @v colour Colour index
  50. * @v which Foreground/background selector
  51. */
  52. __weak void ansicol_set ( unsigned int colour, unsigned int which ) {
  53. /* Colour indices are hardcoded and should never be out of range */
  54. assert ( colour < 10 );
  55. /* Set basic colour */
  56. printf ( CSI "%c%dm", which, colour );
  57. }
  58. /**
  59. * Set ANSI foreground colour
  60. *
  61. * @v colour Colour index
  62. */
  63. static void ansicol_foreground ( unsigned int colour ) {
  64. ansicol_set ( colour, '3' );
  65. }
  66. /**
  67. * Set ANSI background colour
  68. *
  69. * @v colour Colour index
  70. */
  71. static void ansicol_background ( unsigned int colour ) {
  72. ansicol_set ( colour, '4' );
  73. }
  74. /**
  75. * Set ANSI foreground and background colour
  76. *
  77. * @v cpair Colour pair index
  78. */
  79. void ansicol_set_pair ( unsigned int cpair ) {
  80. struct ansicol_pair *pair;
  81. /* Colour pair indices are hardcoded and should never be out of range */
  82. assert ( cpair < ( sizeof ( ansicol_pairs ) /
  83. sizeof ( ansicol_pairs[0] ) ) );
  84. /* Set both foreground and background colours */
  85. pair = &ansicol_pairs[cpair];
  86. ansicol_foreground ( pair->foreground );
  87. ansicol_background ( pair->background );
  88. }
  89. /**
  90. * Define ANSI colour pair
  91. *
  92. * @v cpair Colour pair index
  93. * @v foreground Foreground colour index
  94. * @v background Background colour index
  95. * @ret rc Return status code
  96. */
  97. int ansicol_define_pair ( unsigned int cpair, unsigned int foreground,
  98. unsigned int background ) {
  99. struct ansicol_pair *pair;
  100. /* Fail if colour index is out of range */
  101. if ( cpair >= ( sizeof ( ansicol_pairs ) / sizeof ( ansicol_pairs[0] )))
  102. return -EINVAL;
  103. /* Update colour pair definition */
  104. pair = &ansicol_pairs[cpair];
  105. pair->foreground = foreground;
  106. pair->background = background;
  107. DBGC ( &ansicol_pairs[0], "ANSICOL redefined colour pair %d as "
  108. "foreground %d background %d\n", cpair, foreground, background );
  109. return 0;
  110. }