runtime/lib_ansi.c - ktap

Global variables defined

Functions defined

Source code

  1. /*
  2. * lib_ansi.c - ANSI escape sequences library
  3. *
  4. * http://en.wikipedia.org/wiki/ANSI_escape_code
  5. *
  6. * This file is part of ktap by Jovi Zhangwei.
  7. *
  8. * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
  9. *
  10. * ktap is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * ktap is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. */

  23. #include "../include/ktap_types.h"
  24. #include "ktap.h"
  25. #include "kp_vm.h"

  26. /**
  27. * function ansi.clear_screen - Move cursor to top left and clear screen.
  28. *
  29. * Description: Sends ansi code for moving cursor to top left and then the
  30. * ansi code for clearing the screen from the cursor position to the end.
  31. */

  32. static int kplib_ansi_clear_screen(ktap_state_t *ks)
  33. {
  34.     kp_printf(ks, "\033[1;1H\033[J");
  35.     return 0;
  36. }

  37. /**
  38. * function ansi.set_color - Set the ansi Select Graphic Rendition mode.
  39. * @fg: Foreground color to set.
  40. *
  41. * Description: Sends ansi code for Select Graphic Rendition mode for the
  42. * given forground color. Black (30), Blue (34), Green (32), Cyan (36),
  43. * Red (31), Purple (35), Brown (33), Light Gray (37).
  44. */

  45. static int kplib_ansi_set_color(ktap_state_t *ks)
  46. {
  47.     int fg = kp_arg_checknumber(ks, 1);

  48.     kp_printf(ks, "\033[%dm", fg);
  49.     return 0;
  50. }

  51. /**
  52. * function ansi.set_color2 - Set the ansi Select Graphic Rendition mode.
  53. * @fg: Foreground color to set.
  54. * @bg: Background color to set.
  55. *
  56. * Description: Sends ansi code for Select Graphic Rendition mode for the
  57. * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
  58. * Red (31), Purple (35), Brown (33), Light Gray (37) and the given
  59. * background color, Black (40), Red (41), Green (42), Yellow (43),
  60. * Blue (44), Magenta (45), Cyan (46), White (47).
  61. */
  62. static int kplib_ansi_set_color2(ktap_state_t *ks)
  63. {
  64.     int fg = kp_arg_checknumber(ks, 1);
  65.     int bg = kp_arg_checknumber(ks, 2);

  66.     kp_printf(ks, "\033[%d;%dm", fg, bg);
  67.     return 0;
  68. }

  69. /**
  70. * function ansi.set_color3 - Set the ansi Select Graphic Rendition mode.
  71. * @fg: Foreground color to set.
  72. * @bg: Background color to set.
  73. * @attr: Color attribute to set.
  74. *
  75. * Description: Sends ansi code for Select Graphic Rendition mode for the
  76. * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
  77. * Red (31), Purple (35), Brown (33), Light Gray (37), the given
  78. * background color, Black (40), Red (41), Green (42), Yellow (43),
  79. * Blue (44), Magenta (45), Cyan (46), White (47) and the color attribute
  80. * All attributes off (0), Intensity Bold (1), Underline Single (4),
  81. * Blink Slow (5), Blink Rapid (6), Image Negative (7).
  82. */
  83. static int kplib_ansi_set_color3(ktap_state_t *ks)
  84. {
  85.     int fg = kp_arg_checknumber(ks, 1);
  86.     int bg = kp_arg_checknumber(ks, 2);
  87.     int attr = kp_arg_checknumber(ks, 3);

  88.     if (attr)
  89.         kp_printf(ks, "\033[%d;%d;%dm", fg, bg, attr);
  90.     else
  91.         kp_printf(ks, "\033[%d;%dm", fg, bg);

  92.     return 0;
  93. }

  94. /**
  95. * function ansi.reset_color - Resets Select Graphic Rendition mode.
  96. *
  97. * Description: Sends ansi code to reset foreground, background and color
  98. * attribute to default values.
  99. */
  100. static int kplib_ansi_reset_color(ktap_state_t *ks)
  101. {
  102.     kp_printf(ks, "\033[0;0m");
  103.     return 0;
  104. }

  105. /**
  106. * function ansi.new_line - Move cursor to new line.
  107. *
  108. * Description: Sends ansi code new line.
  109. */
  110. static int kplib_ansi_new_line (ktap_state_t *ks)
  111. {
  112.     kp_printf(ks, "\12");
  113.     return 0;
  114. }

  115. static const ktap_libfunc_t ansi_lib_funcs[] = {
  116.     {"clear_screen", kplib_ansi_clear_screen},
  117.     {"set_color", kplib_ansi_set_color},
  118.     {"set_color2", kplib_ansi_set_color2},
  119.     {"set_color3", kplib_ansi_set_color3},
  120.     {"reset_color", kplib_ansi_reset_color},
  121.     {"new_line", kplib_ansi_new_line},
  122.     {NULL}
  123. };

  124. int kp_lib_init_ansi(ktap_state_t *ks)
  125. {
  126.     return kp_vm_register_lib(ks, "ansi", ansi_lib_funcs);
  127. }