1 /** 2 * Copyright (c) 2013, Timothy Stack 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * * Neither the name of Timothy Stack nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * @file ansi_scrubber.hh 30 */ 31 32 #ifndef lnav_ansi_scrubber_hh 33 #define lnav_ansi_scrubber_hh 34 35 #include <map> 36 #include <string> 37 38 #include "attr_line.hh" 39 40 #define ANSI_CSI "\x1b[" 41 #define ANSI_CHAR_ATTR "m" 42 #define ANSI_BOLD_PARAM "1" 43 #define ANSI_BOLD_START ANSI_CSI ANSI_BOLD_PARAM ANSI_CHAR_ATTR 44 #define ANSI_UNDERLINE_START ANSI_CSI "4m" 45 #define ANSI_NORM ANSI_CSI "0m" 46 #define ANSI_STRIKE_PARAM "9" 47 #define ANSI_STRIKE_START ANSI_CSI ANSI_STRIKE_PARAM ANSI_CHAR_ATTR 48 49 #define ANSI_BOLD(msg) ANSI_BOLD_START msg ANSI_NORM 50 #define ANSI_UNDERLINE(msg) ANSI_UNDERLINE_START msg ANSI_NORM 51 52 #define ANSI_ROLE(msg) ANSI_CSI "%dO" msg ANSI_NORM 53 #define XANSI_COLOR(col) "3" #col 54 #define ANSI_COLOR_PARAM(col) XANSI_COLOR(col) 55 #define ANSI_COLOR(col) ANSI_CSI XANSI_COLOR(col) "m" 56 57 /** 58 * Check a string for ANSI escape sequences, process them, remove them, and add 59 * any style attributes to the given attribute container. 60 * 61 * @param str The string to check for ANSI escape sequences. 62 * @param sa The container for any style attributes. 63 */ 64 void scrub_ansi_string(std::string &str, string_attrs_t &sa); 65 66 /** 67 * Populate a variable map with strings that contain escape sequences that 68 * might be useful to script writers. 69 */ 70 void add_ansi_vars(std::map<std::string, std::string> &vars); 71 72 #endif 73