/******************************************************************************* * mm_output.c - Copyright (c) 2014 www.mahonri.info. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. *------------------------------------------------------------------------------ * @file mm_output.c * @author www.mahonri.info * @brief Stream output related functions. */ /******************************************************************************* * Compiler setup. */ #include // errno, ENOMEM #include // printf(), fprintf(), stdin, stderr #include // isatty() #include // uint8_t #include #include /******************************************************************************* * @brief If the stream is mapped to a tty device (console), the I__prompt will be emitted to stdout. * @param I__stream FILE pointer for output stream provided by the caller. * (Caller may specify 'stdout' or any other valid FILE * stream.) * @param I__prompt String to be emitted to the stream.. * If NULL is specified, nothing will be emitted to the stream.. * @return 0 or error code, including: * EINVAL The caller-specified stream is NULL. * ENOMEM Insufficient memory for operation. * @description: This function suppresses its output if the I__stream is mapped to * a file, pipe, etc. It only emits its output to stdout if the * specified I__stream is mapped to a tty device (ie: text console). */ int MM_OutputPrompt( FILE *I__stream, const char *I__prompt ) { int rCode = MM_SUCCESS; if(NULL == I__stream) { rCode=EINVAL; goto CLEANUP; } if(isatty(fileno(I__stream))) printf("%s", I__prompt); CLEANUP: return(rCode); } char *MM_OutputBackgroundColor_ANSI256( char I__buf[9+1], uint8_t I__color ) { snprintf(I__buf, 17+1, "\x1b[48;5;%hhum", I__color); return(I__buf); } char *MM_OutputForegroundColor_ANSI256( char I__buf[9+1], uint8_t I__color ) { snprintf(I__buf, 17+1, "\x1b[38;5;%hhum", I__color); return(I__buf); } char *MM_OUTPUT_Intensity( char I__buf[5+1] MM_OUTPUT_INTENSITY_T I__level, ) { int level[] = {2, 22, 1}; snprintf(I__buf, 5+1, "\x1b[%dm", level[I__level]); return(I__buf); } char *MM_OUTPUT_ForegroundColor( MM_OUTPUT_COLOR_T I__color, char I__buf[5+1] ) { snprintf(I__buf, 5+1, "\x1b[3%dm", I__color); return(I__buf); } char *MM_OUTPUT_BackgroundColor( MM_OUTPUT_COLOR_T I__color, char I__buf[5+1] ) { snprintf(I__buf, 5+1, "\x1b[4%dm", I__color); return(I__buf); }