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.

ansi_screen.c 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <console.h>
  4. static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
  5. static void ansiscr_movetoyx(struct _curses_screen *scr,
  6. unsigned int y, unsigned int x) __nonnull;
  7. static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
  8. unsigned short _COLS = 80;
  9. unsigned short _LINES = 24;
  10. static void ansiscr_reset ( struct _curses_screen *scr ) {
  11. /* Reset terminal attributes and clear screen */
  12. scr->attrs = 0;
  13. scr->curs_x = 0;
  14. scr->curs_y = 0;
  15. printf ( "\033[0m\033[2J\033[1;1H" );
  16. }
  17. static void ansiscr_movetoyx ( struct _curses_screen *scr,
  18. unsigned int y, unsigned int x ) {
  19. if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
  20. /* ANSI escape sequence to update cursor position */
  21. printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
  22. scr->curs_x = x;
  23. scr->curs_y = y;
  24. }
  25. }
  26. static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
  27. unsigned int character = ( c & A_CHARTEXT );
  28. attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
  29. int bold = ( attrs & A_BOLD );
  30. attr_t cpair = PAIR_NUMBER ( attrs );
  31. short fcol;
  32. short bcol;
  33. /* Update attributes if changed */
  34. if ( attrs != scr->attrs ) {
  35. scr->attrs = attrs;
  36. pair_content ( cpair, &fcol, &bcol );
  37. /* ANSI escape sequence to update character attributes */
  38. printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
  39. }
  40. /* Print the actual character */
  41. putchar ( character );
  42. /* Update expected cursor position */
  43. if ( ++(scr->curs_x) == _COLS ) {
  44. scr->curs_x = 0;
  45. ++scr->curs_y;
  46. }
  47. }
  48. static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
  49. return getchar();
  50. }
  51. static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
  52. return iskey();
  53. }
  54. SCREEN _ansi_screen = {
  55. .init = ansiscr_reset,
  56. .exit = ansiscr_reset,
  57. .movetoyx = ansiscr_movetoyx,
  58. .putc = ansiscr_putc,
  59. .getc = ansiscr_getc,
  60. .peek = ansiscr_peek,
  61. };