# HG changeset patch # User David Demelier # Date 1454491859 -3600 # Node ID 964b8b7000876aa53fe9d8a59069d0e61ed95596 # Parent 26ce6a7ba6958493b63c0c50a00f68f129932641 Get rid of format (see https://github.com/ikalnitsky/termcolor instead) diff -r 26ce6a7ba695 -r 964b8b700087 modules/format/format.cpp --- a/modules/format/format.cpp Thu Jan 28 09:50:34 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - * format.cpp -- convenient function for formatting text with escape sequences - * - * Copyright (c) 2015 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#include "format.h" - -namespace fmt { - -namespace { - -const std::array colorsTable{ - 0, // Default - 30, // Black - 31, // Red - 32, // Green - 33, // Yellow - 34, // Blue - 35, // Magenta - 36, // Cyan - 37, // Light gray - 90, // Dark gray - 91, // Light red - 92, // Light green - 93, // Light yellow - 94, // Light blue - 95, // Light cyan - 96, // White - 97 -}; - -const std::unordered_map attributesTable{ - { Bold, 1 }, - { Dim, 2 }, - { Underline, 4 }, - { Blink, 5 }, - { Reverse, 7 }, - { Hidden, 8 } -}; - -} // !namespace - -std::string convert(std::string input, unsigned short flags) -{ - std::ostringstream oss; - -#if !defined(_WIN32) - // Attributes - for (const auto &pair : attributesTable) { - if (flags & pair.first) { - oss << "\033[" << pair.second << "m"; - } - } - - // Background - if (((flags >> 11) & 0x3f) != Default) { - oss << "\033[" << std::to_string(colorsTable[static_cast((flags >> 11) & 0x3f)] + 10) << "m"; - } - - // Foreground - if (((flags >> 6) & 0x3f) != Default) { - oss << "\033[" << std::to_string(colorsTable[static_cast((flags >> 6) & 0x3f)]) << "m"; - } - - oss << std::move(input) << "\033[0m"; -#else - oss << std::move(input); -#endif - - return oss.str(); -} - -} // !fmt diff -r 26ce6a7ba695 -r 964b8b700087 modules/format/format.h --- a/modules/format/format.h Thu Jan 28 09:50:34 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ -/* - * format.h -- convenient function for formatting text with escape sequences - * - * Copyright (c) 2015 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _FORMAT_H_ -#define _FORMAT_H_ - -#include - -/** - * Format namespace. - */ -namespace fmt { - -/** - * Enumeration of supported colors. - */ -enum Color { - Default = 0, - Black, - Red, - Green, - Yellow, - Blue, - Magenta, - Cyan, - LightGray, - DarkGray, - LightRed, - LightGreen, - LightYellow, - LightBlue, - LightMagenta, - LightCyan, - White -}; - -/** - * Enumeration of supported attributes. - */ -enum Attributes { - None = 0, - Bold = (1 << 0), //! [1m - Dim = (1 << 1), //! [2m - Underline = (1 << 2), //! [4m - Blink = (1 << 3), //! [5m - Reverse = (1 << 4), //! [7m - Hidden = (1 << 5) //! [8m -}; - -/** - * Create a mask for the foreground color. - * - * @param color the foreground color - * @return the correct mask - */ -constexpr unsigned short fg(unsigned short color) noexcept -{ - return color << 6; -} - -/** - * Create a mask for the background color. - * - * @param color the background color - * @return the correct mask - */ -constexpr unsigned short bg(unsigned short color) noexcept -{ - return color << 11; -} - -/** - * Return a formatted string with escape sequences. The string is terminated with reset sequence. - * - * Flags is defined as following: - * - * b b b b b f f f | f f a a a a a a - * - * Where: - * - b is the background color - * - g is the foreground color - * - a are attributes - * - * The attributes and colors are OR'ed together and to specify the background or foreground - * you must use the dedicated fg() and bg() functions. - * - * @param input the input string - * @param flags the flags - * @return the formatted string - * @example convert(fg(Red) | bg(White) | Bold | Underline); - */ -std::string convert(std::string input, unsigned short flags); - -} // !fmt - -#endif // !_FORMAT_H_