From 53090c0dd3f2ccb38474b9a1a17eb21a3d90e09b Mon Sep 17 00:00:00 2001 From: Delapouite Date: Sat, 9 Sep 2017 15:55:35 +0200 Subject: [PATCH] Add debug faces --- README.asciidoc | 4 ++-- doc/manpages/commands.asciidoc | 2 +- src/commands.cc | 11 +++++++++-- src/face_registry.cc | 31 +++++++++++++++++++++++++++++++ src/face_registry.hh | 8 +++++++- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 2c96216a..a49d5d84 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -1573,8 +1573,8 @@ Some helper commands can be used to define composite commands: * `reg `: set register to * `select .,.:...`: replace the current selections with the one described in the argument - * `debug {info,buffers,options,memory,shared-strings}`: print some debug - information in the `*debug*` buffer + * `debug {info,buffers,options,memory,shared-strings,profile-hash-maps,faces}`: + print some debug information in the `*debug*` buffer Note that these commands are available in interactive command mode, but are not that useful in this context. diff --git a/doc/manpages/commands.asciidoc b/doc/manpages/commands.asciidoc index 90bfd5fc..97d6af6d 100644 --- a/doc/manpages/commands.asciidoc +++ b/doc/manpages/commands.asciidoc @@ -200,7 +200,7 @@ commands: *select* .,.:...:: replace the current selections with the one described in the argument -*debug* {info,buffers,options,memory,shared-strings}:: +*debug* {info,buffers,options,memory,shared-strings,profile-hash-maps,faces}:: print some debug information in the *\*debug** buffer Note that those commands are also available in the interactive mode, but diff --git a/src/commands.cc b/src/commands.cc index a52c34e3..016323c1 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1116,14 +1116,15 @@ const CommandDesc debug_cmd = { "debug", nullptr, "debug : write some debug informations in the debug buffer\n" - "existing commands: info, buffers, options, memory, shared-strings, profile-hash-maps", + "existing commands: info, buffers, options, memory, shared-strings, profile-hash-maps, faces", ParameterDesc{{}, ParameterDesc::Flags::SwitchesOnlyAtStart, 1}, CommandFlags::None, CommandHelper{}, make_completer( [](const Context& context, CompletionFlags flags, const String& prefix, ByteCount cursor_pos) -> Completions { - auto c = {"info", "buffers", "options", "memory", "shared-strings", "profile-hash-maps"}; + auto c = {"info", "buffers", "options", "memory", "shared-strings", + "profile-hash-maps", "faces"}; return { 0_byte, cursor_pos, complete(prefix, cursor_pos, c) }; }), [](const ParametersParser& parser, Context& context, const ShellContext&) @@ -1173,6 +1174,12 @@ const CommandDesc debug_cmd = { { profile_hash_maps(); } + else if (parser[0] == "faces") + { + write_to_debug_buffer("Faces:"); + for (auto& face : FaceRegistry::instance().aliases()) + write_to_debug_buffer(format(" * {}: {}", face.key, face.value.face)); + } else throw runtime_error(format("unknown debug command '{}'", parser[0])); } diff --git a/src/face_registry.cc b/src/face_registry.cc index 44de9953..0ae1ae46 100644 --- a/src/face_registry.cc +++ b/src/face_registry.cc @@ -42,6 +42,37 @@ static Face parse_face(StringView facedesc) return res; } +String attributes_to_str(Attribute attributes) +{ + if (attributes == Attribute::Normal) + return ""; + + struct Attr { Attribute attr; StringView name; } + attrs[] { + { Attribute::Exclusive, "e" }, + { Attribute::Underline, "u" }, + { Attribute::Reverse, "r" }, + { Attribute::Blink, "B" }, + { Attribute::Bold, "b" }, + { Attribute::Dim, "d" }, + { Attribute::Italic, "i" }, + }; + + auto filteredAttrs = attrs | + filter([=](const Attr& a) { return attributes & a.attr; }) | + transform([](const Attr& a) { return a.name; }); + + return accumulate(filteredAttrs, String{"+"}, std::plus<>{}); +} + +String to_string(Face face) +{ + return format("{},{}{}", + color_to_str(face.fg), + color_to_str(face.bg), + attributes_to_str(face.attributes)); +} + Face FaceRegistry::operator[](const String& facedesc) { auto it = m_aliases.find(facedesc); diff --git a/src/face_registry.hh b/src/face_registry.hh index bbb3ea0b..e6e276aa 100644 --- a/src/face_registry.hh +++ b/src/face_registry.hh @@ -20,13 +20,17 @@ public: CandidateList complete_alias_name(StringView prefix, ByteCount cursor_pos) const; -private: + struct FaceOrAlias { Face face = {}; String alias = {}; }; + using AliasMap = HashMap; + const AliasMap &aliases() const { return m_aliases; } + +private: AliasMap m_aliases; }; @@ -37,6 +41,8 @@ inline Face get_face(const String& facedesc) return Face{}; } +String to_string(Face face); + } #endif // face_registry_hh_INCLUDED