/* vt100.c - simple subset of vt100 that should work on all terminals */ /* PUBLIC DOMAIN - October 10, 2006 - Jon Mayo */ /* this is actually a subset of ECMA-48/ISO 6429/ANSI X3.64 */ #define VT_COLOR_IGNORE -1 /* ignore this parameter */ #define VT_COLOR_BLACK 0 #define VT_COLOR_RED 1 #define VT_COLOR_GREEN 2 #define VT_COLOR_BROWN 3 #define VT_COLOR_BLUE 4 #define VT_COLOR_MAGENTA 5 #define VT_COLOR_CYAN 6 #define VT_COLOR_WHITE 7 #define VT_COLOR_DEFAULT 9 /* use the terminals default color */ #define VT_ATTR_IGNORE -1 /* ignore this parameter */ #define VT_ATTR_OFF 0 #define VT_ATTR_ON 1 #define VT_ERASE_END 1 /* erase to end of line or end of screen */ #define VT_ERASE_ALL 2 /* erase everything */ #define VT_RESET_TERMINAL "\033c" /*! * paramters: * mode - VT_ERASE_END to erase cursor to end-of-screen, VT_ERASE_ALL to erase screen */ void vt_erase_screen(int mode) { printf("\033[%uJ", mode); fflush(stdout); } /*! * paramters: * mode - VT_ERASE_END to erase cursor to end-of-line, VT_ERASE_ALL to erase entire line */ void vt_erase_line(int mode) { printf("\033[%uK", mode); fflush(stdout); } /*! * description: saves the current cursor position * parameters: * old - non-zero to use old vt100 style code, zero uses ECMA-48 which is fairly portable */ void vt_cursor_save(int old) { if(old) { /* old */ printf("\0337"); } else { /* ECMA-48 */ printf("\033[s"); } fflush(stdout); } /*! * description: restore the last saved cursor position * parameters: * old - non-zero to use old vt100 style code, zero uses ECMA-48 which is fairly portable */ void vt_cursor_restore(int old) { if(old) { /* old */ printf("\0338"); } else { /* ECMA-48 */ printf("\033[u"); } fflush(stdout); } /*! * description: send code to move the cursor position. */ void vt_moveto(unsigned x, unsigned y) { printf("\033[%u;%uH", y, x); fflush(stdout); } /*! * description: sets a scroll region */ void vt_setwindow(unsigned top, unsigned bottom) { printf("\033[%u;%ur", top, bottom); fflush(stdout); } /*! * description: clears the scroll region, making the scrolling full screen again */ void vt_setwindow_clear() { printf("\033[r"); fflush(stdout); } void vt_init() { vt_setwindow_clear(); vt_color(VT_COLOR_DEFAULT, VT_COLOR_DEFAULT); } /*! * description: set colors * parameters: * fg - foreground color (use VT_COLOR_XXX macro), negative means ignore * bg - background color (use VT_COLOR_XXX macro), negative means ignore * */ void vt_color(int fg, int bg) { if(fg<0 && bg<0) { /* -1,-1 we'll use for reset */ printf("\033[0m", bg+40); } else if(fg<0) { printf("\033[%um", bg+40); } else if(bg<0) { printf("\033[%um", fg+30); } else { printf("\033[0;%u;%um", fg+30, bg+40); } fflush(stdout); } /*! * description: set special video attributes * parameters: * reverse - use reverse video (VT_ATTR_IGNORE, VT_ATTR_OFF, VT_ATTR_ON) * bold - use bold video (VT_ATTR_IGNORE, VT_ATTR_OFF, VT_ATTR_ON) */ void vt_attr(int reverse, int bold) { int count; count=(reverse!=VT_ATTR_IGNORE)+(bold!=VT_ATTR_IGNORE); printf("\033["); if(reverse!=VT_ATTR_IGNORE) { printf("%u", reverse==VT_ATTR_OFF?27:7); if(--count) printf(";"); } if(bold!=VT_ATTR_IGNORE) { printf("%u", bold==VT_ATTR_OFF?22:1); if(--count) printf(";"); } printf("m"); fflush(stdout); } /*! * description: turns on xterm style mouse reporting * parameters: * enable - non-zero to enable, zero to disable reporting */ void vt_xterm_mouse(int enable) { printf("\033[?1000%c", enable?'h':'l'); fflush(stdout); } /*! * description: enables/disables full screen reverse video, useful for visual bell effect * parameters: * enable - non-zero to have reverse video, zero to have normal video */ void vt_reverse_video(int enable) { printf("\033[?5%c", enable?'h':'l'); }