diff options
author | 2010-08-16 18:49:13 +0100 | |
---|---|---|
committer | 2010-08-16 21:15:08 +0100 | |
commit | 72b33576aac686ac850ea5b67cc35792d1f611d5 (patch) | |
tree | 9de3dee55dbef39f1f8b7d17a2feb23b18d66dd4 | |
parent | 87ecc0bdd0c41ec9770224b59060987941341570 (diff) | |
download | paludis-72b33576aac686ac850ea5b67cc35792d1f611d5.tar.gz paludis-72b33576aac686ac850ea5b67cc35792d1f611d5.tar.xz |
Make cave colours user specifiable
-rw-r--r-- | src/clients/cave/Makefile.am | 1 | ||||
-rw-r--r-- | src/clients/cave/format_user_config.cc | 103 | ||||
-rw-r--r-- | src/clients/cave/format_user_config.hh | 233 | ||||
-rw-r--r-- | src/clients/cave/formats.cc | 33 |
4 files changed, 354 insertions, 16 deletions
diff --git a/src/clients/cave/Makefile.am b/src/clients/cave/Makefile.am index 244b24045..eb9edf12a 100644 --- a/src/clients/cave/Makefile.am +++ b/src/clients/cave/Makefile.am @@ -157,6 +157,7 @@ libcave_a_SOURCES = \ format_plain_contents_entry.cc format_plain_contents_entry.hh \ format_plain_metadata_key.cc format_plain_metadata_key.hh \ format_string.cc format_string.hh \ + format_user_config.cc format_user_config.hh \ formats.cc formats.hh \ script_command.cc script_command.hh \ search_extras_handle.cc search_extras_handle.hh \ diff --git a/src/clients/cave/format_user_config.cc b/src/clients/cave/format_user_config.cc new file mode 100644 index 000000000..2b1463e06 --- /dev/null +++ b/src/clients/cave/format_user_config.cc @@ -0,0 +1,103 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2010 Ciaran McCreesh + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "format_user_config.hh" +#include <paludis/util/config_file.hh> +#include <paludis/util/system.hh> +#include <paludis/util/fs_entry.hh> +#include <paludis/util/singleton-impl.hh> +#include <paludis/util/pimp-impl.hh> +#include <paludis/util/options.hh> +#include <paludis/util/stringify.hh> + +using namespace paludis; +using namespace cave; + +std::string +user_config_file_presets( + const KeyValueConfigFile &, + const std::string & s) +{ + if (s == "red") + return "\033[0;31m"; + else if (s == "bold_red") + return "\033[1;31m"; + else if (s == "green") + return "\033[0;32m"; + else if (s == "bold_green") + return "\033[1;32m"; + else if (s == "yellow") + return "\033[0;33m"; + else if (s == "bold_yellow") + return "\033[1;33m"; + else if (s == "blue") + return "\033[0;34m"; + else if (s == "bold_blue") + return "\033[1;34m"; + else if (s == "pink") + return "\033[0;35m"; + else if (s == "bold_pink") + return "\033[1;35m"; + else if (s == "normal") + return "\033[0;0m"; + else if (s == "bold_normal") + return "\033[1m"; + else + return ""; +} + +namespace paludis +{ + template <> + struct Imp<FormatUserConfigFile> + { + FSEntry path; + KeyValueConfigFile conf; + + Imp() : + path(getenv_with_default("CAVE_FORMATS_CONF", getenv_with_default("HOME", "/") + "/.cave/formats.conf")), + conf(path.exists() ? ConfigFile::Source(path) : ConfigFile::Source(""), + { kvcfo_allow_sections, kvcfo_preserve_whitespace }, + &user_config_file_presets, + &KeyValueConfigFile::no_transformation) + { + } + }; +} + +FormatUserConfigFile::FormatUserConfigFile() : + Pimp<FormatUserConfigFile>() +{ +} + +FormatUserConfigFile::~FormatUserConfigFile() = default; + +std::string +FormatUserConfigFile::fetch(const std::string & v, int vi, const std::string & d) const +{ + std::string k(0 == vi ? v : v + "." + stringify(vi)); + std::string result(_imp->conf.get(k)); + if (result.empty()) + return d; + else + return result; +} + +template class Singleton<cave::FormatUserConfigFile>; + diff --git a/src/clients/cave/format_user_config.hh b/src/clients/cave/format_user_config.hh new file mode 100644 index 000000000..5835e8c8d --- /dev/null +++ b/src/clients/cave/format_user_config.hh @@ -0,0 +1,233 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2010 Ciaran McCreesh + * + * This file is part of the Paludis package manager. Paludis is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_SRC_CLIENTS_CAVE_FORMAT_USER_CONFIG_HH +#define PALUDIS_GUARD_SRC_CLIENTS_CAVE_FORMAT_USER_CONFIG_HH 1 + +#include <paludis/util/map.hh> +#include <paludis/util/pimp.hh> +#include <paludis/util/singleton.hh> +#include <string> +#include <memory> +#include <utility> +#include "format_string.hh" + +namespace paludis +{ + namespace cave + { + class FormatUserConfigFile : + private Pimp<FormatUserConfigFile>, + public Singleton<FormatUserConfigFile> + { + friend class Singleton<FormatUserConfigFile>; + + private: + FormatUserConfigFile(); + ~FormatUserConfigFile(); + + public: + std::string fetch(const std::string & v, int vi, const std::string & d) const; + }; + + template <char c_> + struct FormatValue + { + std::string value; + }; + + template <char c_> + FormatValue<c_> fv(const std::string & s) + { + return FormatValue<c_>{s}; + } + + template <char... cs_> + struct FormatString + { + std::string format_string; + }; + + template <char c_> + struct FormatValuesBase + { + std::string value; + + FormatValuesBase(const std::string & v) : + value(v) + { + } + }; + + template <char... cs_> + struct FormatValues : + FormatValuesBase<cs_>... + { + template <char... c_> + FormatValues(const FormatValue<c_> & ... c) : + FormatValuesBase<c_>(c.value)... + { + } + }; + + struct OrderedFormatValuesBaseMap + { + std::shared_ptr<Map<char, std::string> > map; + + OrderedFormatValuesBaseMap() : + map(std::make_shared<Map<char, std::string> >()) + { + } + }; + + template <char c_> + struct OrderedFormatValuesBase + { + OrderedFormatValuesBase( + const std::shared_ptr<Map<char, std::string> > & m, + const FormatValuesBase<c_> & v) + { + m->insert(c_, v.value); + } + }; + + template <char... cs_> + struct OrderedFormatValues : + OrderedFormatValuesBaseMap, + OrderedFormatValuesBase<cs_>... + { + template <char... c_> + OrderedFormatValues(const FormatValues<c_...> & c) : + OrderedFormatValuesBase<cs_>(this->map, c)... + { + } + }; + + template <char... cs_> + struct MakeOrderedFormatValues; + + template <char a_> + struct MakeOrderedFormatValues<a_> + { + typedef OrderedFormatValues<a_> Type; + }; + + template <char a_, char b_> + struct MakeOrderedFormatValues<a_, b_> + { + typedef OrderedFormatValues<(a_ < b_ ? a_ : b_), (a_ < b_ ? b_ : a_)> Type; + }; + + template <char... cs_> + typename MakeOrderedFormatValues<cs_...>::Type + order(const FormatValues<cs_...> & f) + { + return typename MakeOrderedFormatValues<cs_...>::Type(f); + } + + template <typename T_> + struct ExtractFormatValue; + + template <char c_> + struct ExtractFormatValue<FormatValue<c_> > + { + static const char value = c_; + }; + + template <char... cs_> + std::string fuc_parameters(const FormatString<cs_...> & s, OrderedFormatValues<cs_...> && p) + { + return format_string(s.format_string, p.map); + } + + template <char... cs_, typename... T_> + std::string fuc(const FormatString<cs_...> & f, const T_ & ... t) + { + return fuc_parameters(f, order(FormatValues<ExtractFormatValue<T_>::value...>{t...})); + } + + template <char... cs_> + struct MakeFormatStringFetcher + { + std::string user_key; + int user_key_version; + + std::string text; + + FormatString<cs_...> operator() () const + { + return FormatString<cs_...>{FormatUserConfigFile::get_instance()->fetch(user_key, user_key_version, text)}; + } + }; + + inline MakeFormatStringFetcher<> + make_format_string_fetcher(const std::string & u, const int v) + { + return MakeFormatStringFetcher<>{u, v, ""}; + } + + template <char... cs_> + struct MakeMakeFormatStringFetcher; + + template <char a_> + struct MakeMakeFormatStringFetcher<a_> + { + typedef MakeFormatStringFetcher<a_> Type; + }; + + template <char a_, char b_> + struct MakeMakeFormatStringFetcher<a_, b_> + { + typedef MakeFormatStringFetcher<(a_ < b_ ? a_ : b_), (a_ < b_ ? b_ : a_)> Type; + }; + + template <char c_> + struct FormatParam + { + }; + + template <char c_> + FormatParam<c_> param() + { + return FormatParam<c_>(); + } + + template <char... cs_> + MakeFormatStringFetcher<cs_...> operator<< (MakeFormatStringFetcher<cs_...> && f, const std::string & s) + { + MakeFormatStringFetcher<cs_...> result{std::move(f.user_key), f.user_key_version, std::move(f.text)}; + result.text.append(s); + return result; + } + + template <char c_, char... cs_> + typename MakeMakeFormatStringFetcher<c_, cs_...>::Type + operator<< (MakeFormatStringFetcher<cs_...> && f, const FormatParam<c_> &) + { + typename MakeMakeFormatStringFetcher<c_, cs_...>::Type result{std::move(f.user_key), f.user_key_version, std::move(f.text)}; + result.text.append("%"); + result.text.append(1, c_); + return result; + } + } + + extern template class Singleton<cave::FormatUserConfigFile>; +} + +#endif diff --git a/src/clients/cave/formats.cc b/src/clients/cave/formats.cc index 0b0968282..b337ca996 100644 --- a/src/clients/cave/formats.cc +++ b/src/clients/cave/formats.cc @@ -18,6 +18,7 @@ */ #include "formats.hh" +#include "format_user_config.hh" #include "config.h" using namespace paludis; @@ -26,61 +27,61 @@ using namespace cave; const std::string paludis::cave::c::bold_blue() { - return "\033[1;34m"; + return FormatUserConfigFile::get_instance()->fetch("bold_blue", 0, ""); } const std::string paludis::cave::c::blue() { - return "\033[0;34m"; + return FormatUserConfigFile::get_instance()->fetch("blue", 0, ""); } const std::string paludis::cave::c::bold_green() { - return "\033[1;32m"; + return FormatUserConfigFile::get_instance()->fetch("bold_green", 0, ""); } const std::string paludis::cave::c::green() { - return "\033[0;32m"; + return FormatUserConfigFile::get_instance()->fetch("green", 0, ""); } const std::string paludis::cave::c::red() { - return "\033[0;31m"; + return FormatUserConfigFile::get_instance()->fetch("red", 0, ""); } const std::string paludis::cave::c::bold_red() { - return "\033[1;31m"; + return FormatUserConfigFile::get_instance()->fetch("bold_red", 0, ""); } const std::string paludis::cave::c::yellow() { - return "\033[0;33m"; + return FormatUserConfigFile::get_instance()->fetch("yellow", 0, ""); } const std::string paludis::cave::c::bold_yellow() { - return "\033[1;33m"; + return FormatUserConfigFile::get_instance()->fetch("bold_yellow", 0, ""); } const std::string paludis::cave::c::pink() { - return "\033[0;35m"; + return FormatUserConfigFile::get_instance()->fetch("pink", 0, ""); } const std::string paludis::cave::c::bold_pink() { - return "\033[1;35m"; + return FormatUserConfigFile::get_instance()->fetch("bold_pink", 0, ""); } const std::string @@ -89,7 +90,7 @@ paludis::cave::c::bold_blue_or_pink() #if PALUDIS_COLOUR_PINK return c::bold_pink(); #else - return "\033[1;34m"; + return c::bold_blue(); #endif } @@ -99,7 +100,7 @@ paludis::cave::c::blue_or_pink() #if PALUDIS_COLOUR_PINK return c::pink(); #else - return "\033[0;34m"; + return c::blue(); #endif } @@ -109,7 +110,7 @@ paludis::cave::c::bold_green_or_pink() #if PALUDIS_COLOUR_PINK return c::bold_pink(); #else - return "\033[1;32m"; + return c::bold_green(); #endif } @@ -119,20 +120,20 @@ paludis::cave::c::green_or_pink() #if PALUDIS_COLOUR_PINK return c::pink(); #else - return "\033[0;32m"; + return c::green(); #endif } const std::string paludis::cave::c::normal() { - return "\033[0;0m"; + return FormatUserConfigFile::get_instance()->fetch("normal", 0, ""); } const std::string paludis::cave::c::bold_normal() { - return "\033[1m"; + return FormatUserConfigFile::get_instance()->fetch("bold_normal", 0, ""); } const std::string |