├── LICENSE ├── Makefile ├── README.md ├── examples └── main.c ├── package.json └── sources ├── c_printf.c └── c_printf.h /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Giovanny Andres Gongora Granada 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | main: examples/main.c sources/c_printf.c 2 | $(CC) $^ $(CFLAGS) -o $@ 3 | 4 | clean: 5 | rm main 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # c_printf 2 | `c_printf` wraps `printf(, ...)` to include color specification for each parameter. The main idea of the API is keep the printf structure, but with a small extension. 3 | 4 | ## Usage 5 | Examples about how to use it: 6 | ``` 7 | c_printf("[r]%s", "red text"); 8 | c_printf("[g]%s", "green text"); 9 | c_printf("[r]%s [g]%s", "red", "green"); 10 | c_printf("[y]%d", 52334); 11 | c_printf("[r]%s %s", "red", "normal"); 12 | ``` 13 | 14 | ## Available Colors 15 | Each color is specified with its first character (in lowercase, i.e. `[r]` for red). The following are currently available: 16 | 17 | * RED = `[r]` 18 | * GREEN = `[g]` 19 | * YELLOW = `[y]` 20 | * BLUE = `[b]` 21 | * MAGENTA = `[m]` 22 | * CYAN = `[c]` 23 | * NORMAL = `[n]` 24 | 25 | ## Installation 26 | Just include it on you file as always `#include ` to import necessary symbols. You can install using `clib` package manager: 27 | 28 | ``` 29 | $ clib install gioyik/c_printf 30 | ``` 31 | -------------------------------------------------------------------------------- /examples/main.c: -------------------------------------------------------------------------------- 1 | #include "../sources/c_printf.h" 2 | 3 | int main(int argc, char *argv[]) { 4 | c_printf(" * [r]%s\n", "Red"); 5 | c_printf(" * [g]%s\n", "Green"); 6 | c_printf(" * [y]%s\n", "Yellow"); 7 | c_printf(" * [b]%s\n", "Blue"); 8 | c_printf(" * [m]%s\n", "Magenta"); 9 | c_printf(" * [c]%s\n", "Cyan"); 10 | c_printf(" * [n]%s\n", "Normal"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "c_printf", 3 | "version": "0.0.1", 4 | "repo": "Gioyik/c_printf", 5 | "description": "Color C lib for printf", 6 | "keywords": ["color", "c", "printf", "lib"], 7 | "license": "MIT", 8 | "src": ["sources/c_printf.c", "sources/c_printf.h"] 9 | } 10 | -------------------------------------------------------------------------------- /sources/c_printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "c_printf.h" 6 | 7 | // We define colors in ANSI escape codes. 8 | #define RED "\x1b[31m" 9 | #define GREEN "\x1b[32m" 10 | #define YELLOW "\x1b[33m" 11 | #define BLUE "\x1b[34m" 12 | #define MAGENTA "\x1b[35m" 13 | #define CYAN "\x1b[36m" 14 | // Normal == No color 15 | #define NORMAL "\x1b[0m" 16 | 17 | // Because I want and easy to use library 18 | // to print colors, I am going to define 19 | // key values for every color code. So, will 20 | // be more easier to include in the output. 21 | static const char *c_key(char k) { 22 | switch(k){ 23 | // Maybe I will add the word instead of a letter 24 | // or both. Let's see whats better. 25 | case 'r': return RED; 26 | case 'g': return GREEN; 27 | case 'y': return YELLOW; 28 | case 'b': return BLUE; 29 | case 'm': return MAGENTA; 30 | case 'c': return CYAN; 31 | case 'n': return NORMAL; 32 | default: 33 | // Yes, we use c_printf lib on c_printf source. :) 34 | // Best dogfooding example? 35 | c_printf("[r]%s[-]%c\n", " - ERROR: invalid key for color: ", k); 36 | exit(1); 37 | } 38 | } 39 | 40 | // Counts occurences of character in string. 41 | static int howMany(const char *s, char c) { 42 | int n = 0; 43 | while(*s){ 44 | if(*s == c) 45 | ++n; 46 | ++s; 47 | } 48 | return n; 49 | } 50 | 51 | // This is to append characters to string. 52 | // There's a better way for this? 53 | static void c_append(char *s, char c) { 54 | s += strlen(s); 55 | s[0] = c; 56 | s[1] = 0; 57 | } 58 | 59 | // Reads printf format string, replacing color-indicating 60 | // characters with their respective color codes. 61 | static char *c_f(const char *f) { 62 | char *r = malloc(strlen(f) * 8); 63 | if(!r) 64 | return NULL; 65 | *r = 0; 66 | 67 | strcat(r, NORMAL); 68 | while(*f){ 69 | if(f[0] == '[' && f[2] == ']' && f[3] == '%'){ 70 | strcat(r, c_key(f[1])), f += 3; 71 | while(!isalpha(*f)) 72 | c_append(r, *f), ++f; 73 | c_append(r, *f), ++f; 74 | strcat(r, NORMAL); 75 | } else{ 76 | c_append(r, *f); 77 | ++f; 78 | } 79 | } 80 | strcat(r, NORMAL); 81 | return r; 82 | } 83 | 84 | // c_printf main function. 85 | // What we really want to use 86 | int c_printf(const char *cf, ...) { 87 | va_list args; 88 | va_start(args, cf); 89 | char *f = c_f(cf); 90 | if(f){ 91 | vprintf(f, args); 92 | free(f); 93 | va_end(args); 94 | return 0; 95 | } 96 | return 1; 97 | } 98 | -------------------------------------------------------------------------------- /sources/c_printf.h: -------------------------------------------------------------------------------- 1 | #ifndef C_PRINTF 2 | #define C_PRINTF 3 | #include 4 | 5 | // Simple as we want 6 | int c_printf(const char*, ...); 7 | 8 | #endif 9 | --------------------------------------------------------------------------------