mirror of https://github.com/tstack/lnav
[console] colorize console output
parent
7652c58248
commit
0785a432fa
@ -0,0 +1,265 @@
|
||||
/**
|
||||
* Copyright (c) 2022, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "lnav.console.hh"
|
||||
|
||||
#include "config.h"
|
||||
#include "fmt/color.h"
|
||||
#include "view_curses.hh"
|
||||
|
||||
namespace lnav {
|
||||
namespace console {
|
||||
|
||||
user_message
|
||||
user_message::error(const attr_line_t& al)
|
||||
{
|
||||
user_message retval;
|
||||
|
||||
retval.um_level = level::error;
|
||||
retval.um_message.append(al);
|
||||
return retval;
|
||||
}
|
||||
|
||||
user_message
|
||||
user_message::info(const attr_line_t& al)
|
||||
{
|
||||
user_message retval;
|
||||
|
||||
retval.um_level = level::info;
|
||||
retval.um_message.append(al);
|
||||
return retval;
|
||||
}
|
||||
|
||||
user_message
|
||||
user_message::ok(const attr_line_t& al)
|
||||
{
|
||||
user_message retval;
|
||||
|
||||
retval.um_level = level::ok;
|
||||
retval.um_message.append(al);
|
||||
return retval;
|
||||
}
|
||||
|
||||
user_message
|
||||
user_message::warning(const attr_line_t& al)
|
||||
{
|
||||
user_message retval;
|
||||
|
||||
retval.um_level = level::warning;
|
||||
retval.um_message.append(al);
|
||||
return retval;
|
||||
}
|
||||
|
||||
attr_line_t
|
||||
user_message::to_attr_line(std::set<render_flags> flags) const
|
||||
{
|
||||
attr_line_t retval;
|
||||
|
||||
if (flags.count(render_flags::prefix)) {
|
||||
switch (this->um_level) {
|
||||
case level::ok:
|
||||
retval.append(lnav::roles::ok("\u2714 "));
|
||||
break;
|
||||
case level::info:
|
||||
retval.append(lnav::roles::status("\u24d8 info")).append(": ");
|
||||
break;
|
||||
case level::warning:
|
||||
retval.append(lnav::roles::warning("\u26a0 warning"))
|
||||
.append(": ");
|
||||
break;
|
||||
case level::error:
|
||||
retval.append(lnav::roles::error("\u2718 error")).append(": ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
retval.append(this->um_message).append("\n");
|
||||
if (!this->um_reason.empty()) {
|
||||
bool first_line = true;
|
||||
for (const auto& line : this->um_reason.split_lines()) {
|
||||
attr_line_t prefix;
|
||||
|
||||
if (first_line) {
|
||||
prefix.append(lnav::roles::error(" reason")).append(": ");
|
||||
first_line = false;
|
||||
} else {
|
||||
prefix.append(lnav::roles::error(" | "));
|
||||
}
|
||||
retval.append(prefix).append(line).append("\n");
|
||||
}
|
||||
}
|
||||
if (!this->um_snippets.empty()) {
|
||||
for (const auto& snip : this->um_snippets) {
|
||||
attr_line_t header;
|
||||
|
||||
header.append(lnav::roles::comment(" --> "))
|
||||
.append(lnav::roles::file(snip.s_source));
|
||||
if (snip.s_line > 0) {
|
||||
header.append(":").append(FMT_STRING("{}"), snip.s_line);
|
||||
if (snip.s_column > 0) {
|
||||
header.append(":").append(FMT_STRING("{}"), snip.s_column);
|
||||
}
|
||||
}
|
||||
retval.append(header).append("\n");
|
||||
if (!snip.s_content.blank()) {
|
||||
for (const auto& line : snip.s_content.split_lines()) {
|
||||
retval.append(lnav::roles::comment(" | "))
|
||||
.append(line)
|
||||
.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!this->um_notes.empty()) {
|
||||
bool first_line = true;
|
||||
for (const auto& note : this->um_notes) {
|
||||
for (const auto& line : note.split_lines()) {
|
||||
attr_line_t prefix;
|
||||
|
||||
if (first_line) {
|
||||
prefix.append(lnav::roles::comment(" = note")).append(": ");
|
||||
first_line = false;
|
||||
} else {
|
||||
prefix.append(" ");
|
||||
}
|
||||
|
||||
retval.append(prefix).append(line).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!this->um_help.empty()) {
|
||||
bool first_line = true;
|
||||
for (const auto& line : this->um_help.split_lines()) {
|
||||
attr_line_t prefix;
|
||||
|
||||
if (first_line) {
|
||||
prefix.append(lnav::roles::comment(" = help")).append(": ");
|
||||
first_line = false;
|
||||
} else {
|
||||
prefix.append(" ");
|
||||
}
|
||||
|
||||
retval.append(prefix).append(line).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void
|
||||
println(FILE* file, const attr_line_t& al)
|
||||
{
|
||||
const auto& str = al.get_string();
|
||||
|
||||
if (!isatty(fileno(file))) {
|
||||
fmt::print(file, "{}\n", str);
|
||||
return;
|
||||
}
|
||||
|
||||
string_attrs_t style_attrs;
|
||||
|
||||
for (const auto& sa : al.get_attrs()) {
|
||||
if (sa.sa_type != &VC_ROLE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
style_attrs.emplace_back(sa);
|
||||
}
|
||||
|
||||
std::sort(style_attrs.begin(), style_attrs.end(), [](auto lhs, auto rhs) {
|
||||
return lhs.sa_range < rhs.sa_range;
|
||||
});
|
||||
|
||||
auto start = size_t{0};
|
||||
for (const auto& attr : style_attrs) {
|
||||
fmt::print(
|
||||
file, "{}", str.substr(start, attr.sa_range.lr_start - start));
|
||||
if (attr.sa_type == &VC_ROLE) {
|
||||
auto saw = string_attr_wrapper<role_t>(&attr);
|
||||
auto role = saw.get();
|
||||
auto line_style = fmt::text_style();
|
||||
|
||||
switch (role) {
|
||||
case role_t::VCR_ERROR:
|
||||
line_style = fmt::fg(fmt::terminal_color::red);
|
||||
break;
|
||||
case role_t::VCR_WARNING:
|
||||
line_style = fmt::fg(fmt::terminal_color::yellow);
|
||||
break;
|
||||
case role_t::VCR_COMMENT:
|
||||
line_style = fmt::fg(fmt::terminal_color::cyan);
|
||||
break;
|
||||
case role_t::VCR_OK:
|
||||
line_style = fmt::emphasis::bold
|
||||
| fmt::fg(fmt::terminal_color::red);
|
||||
break;
|
||||
case role_t::VCR_STATUS:
|
||||
line_style = fmt::emphasis::bold
|
||||
| fmt::fg(fmt::terminal_color::magenta);
|
||||
break;
|
||||
case role_t::VCR_VARIABLE:
|
||||
line_style = fmt::emphasis::underline;
|
||||
break;
|
||||
case role_t::VCR_SYMBOL:
|
||||
case role_t::VCR_NUMBER:
|
||||
case role_t::VCR_FILE:
|
||||
line_style = fmt::emphasis::bold;
|
||||
break;
|
||||
case role_t::VCR_H1:
|
||||
case role_t::VCR_H2:
|
||||
case role_t::VCR_H3:
|
||||
case role_t::VCR_H4:
|
||||
case role_t::VCR_H5:
|
||||
case role_t::VCR_H6:
|
||||
line_style = fmt::emphasis::underline;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
fmt::print(
|
||||
file,
|
||||
line_style,
|
||||
"{}",
|
||||
str.substr(attr.sa_range.lr_start, attr.sa_range.length()));
|
||||
}
|
||||
start = attr.sa_range.lr_end;
|
||||
}
|
||||
fmt::print(file, "{}\n", str.substr(start));
|
||||
}
|
||||
|
||||
void
|
||||
print(FILE* file, const user_message& um)
|
||||
{
|
||||
println(file, um.to_attr_line().rtrim());
|
||||
}
|
||||
|
||||
} // namespace console
|
||||
} // namespace lnav
|
@ -0,0 +1,149 @@
|
||||
/**
|
||||
* Copyright (c) 2022, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lnav_console_hh
|
||||
#define lnav_console_hh
|
||||
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "base/attr_line.hh"
|
||||
|
||||
namespace lnav {
|
||||
namespace console {
|
||||
|
||||
void println(FILE* file, const attr_line_t& al);
|
||||
|
||||
struct snippet {
|
||||
static snippet from(std::string src, const attr_line_t& content)
|
||||
{
|
||||
snippet retval;
|
||||
|
||||
retval.s_source = std::move(src);
|
||||
retval.s_content = content;
|
||||
return retval;
|
||||
}
|
||||
|
||||
snippet& with_line(int32_t line)
|
||||
{
|
||||
this->s_line = line;
|
||||
return *this;
|
||||
}
|
||||
|
||||
snippet& with_column(int32_t column)
|
||||
{
|
||||
this->s_column = column;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string s_source;
|
||||
int32_t s_line{0};
|
||||
int32_t s_column{0};
|
||||
attr_line_t s_content;
|
||||
};
|
||||
|
||||
struct user_message {
|
||||
enum class level {
|
||||
ok,
|
||||
info,
|
||||
warning,
|
||||
error,
|
||||
};
|
||||
|
||||
static user_message error(const attr_line_t& al);
|
||||
|
||||
static user_message warning(const attr_line_t& al);
|
||||
|
||||
static user_message info(const attr_line_t& al);
|
||||
|
||||
static user_message ok(const attr_line_t& al);
|
||||
|
||||
user_message& with_reason(const attr_line_t& al)
|
||||
{
|
||||
this->um_reason = al;
|
||||
this->um_reason.rtrim();
|
||||
return *this;
|
||||
}
|
||||
|
||||
user_message& with_errno_reason()
|
||||
{
|
||||
this->um_reason = strerror(errno);
|
||||
return *this;
|
||||
}
|
||||
|
||||
user_message& with_snippet(const snippet& sn)
|
||||
{
|
||||
this->um_snippets.emplace_back(sn);
|
||||
return *this;
|
||||
}
|
||||
|
||||
user_message& with_snippets(std::vector<snippet> snippets)
|
||||
{
|
||||
this->um_snippets.insert(this->um_snippets.end(),
|
||||
std::make_move_iterator(snippets.begin()),
|
||||
std::make_move_iterator(snippets.end()));
|
||||
return *this;
|
||||
}
|
||||
|
||||
user_message& with_note(const attr_line_t& al)
|
||||
{
|
||||
this->um_notes.emplace_back(al);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
user_message& with_help(const attr_line_t& al)
|
||||
{
|
||||
this->um_help = al;
|
||||
this->um_help.rtrim();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
enum class render_flags {
|
||||
prefix,
|
||||
};
|
||||
|
||||
attr_line_t to_attr_line(std::set<render_flags> flags
|
||||
= {render_flags::prefix}) const;
|
||||
|
||||
level um_level{level::ok};
|
||||
attr_line_t um_message;
|
||||
std::vector<snippet> um_snippets;
|
||||
attr_line_t um_reason;
|
||||
std::vector<attr_line_t> um_notes;
|
||||
attr_line_t um_help;
|
||||
};
|
||||
|
||||
void print(FILE* file, const user_message& um);
|
||||
|
||||
} // namespace console
|
||||
} // namespace lnav
|
||||
|
||||
#endif
|
@ -0,0 +1,346 @@
|
||||
/**
|
||||
* Copyright (c) 2020, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lnav_string_attr_type_hh
|
||||
#define lnav_string_attr_type_hh
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "base/intern_string.hh"
|
||||
#include "mapbox/variant.hpp"
|
||||
|
||||
class logfile;
|
||||
struct bookmark_metadata;
|
||||
|
||||
/** Roles that can be mapped to curses attributes using attrs_for_role() */
|
||||
enum class role_t : int32_t {
|
||||
VCR_NONE = -1,
|
||||
|
||||
VCR_TEXT, /*< Raw text. */
|
||||
VCR_IDENTIFIER,
|
||||
VCR_SEARCH, /*< A search hit. */
|
||||
VCR_OK,
|
||||
VCR_ERROR, /*< An error message. */
|
||||
VCR_WARNING, /*< A warning message. */
|
||||
VCR_ALT_ROW, /*< Highlight for alternating rows in a list */
|
||||
VCR_HIDDEN,
|
||||
VCR_ADJUSTED_TIME,
|
||||
VCR_SKEWED_TIME,
|
||||
VCR_OFFSET_TIME,
|
||||
VCR_INVALID_MSG,
|
||||
VCR_STATUS, /*< Normal status line text. */
|
||||
VCR_WARN_STATUS,
|
||||
VCR_ALERT_STATUS, /*< Alert status line text. */
|
||||
VCR_ACTIVE_STATUS, /*< */
|
||||
VCR_ACTIVE_STATUS2, /*< */
|
||||
VCR_STATUS_TITLE,
|
||||
VCR_STATUS_SUBTITLE,
|
||||
VCR_STATUS_STITCH_TITLE_TO_SUB,
|
||||
VCR_STATUS_STITCH_SUB_TO_TITLE,
|
||||
VCR_STATUS_STITCH_SUB_TO_NORMAL,
|
||||
VCR_STATUS_STITCH_NORMAL_TO_SUB,
|
||||
VCR_STATUS_STITCH_TITLE_TO_NORMAL,
|
||||
VCR_STATUS_STITCH_NORMAL_TO_TITLE,
|
||||
VCR_STATUS_TITLE_HOTKEY,
|
||||
VCR_STATUS_DISABLED_TITLE,
|
||||
VCR_STATUS_HOTKEY,
|
||||
VCR_INACTIVE_STATUS,
|
||||
VCR_INACTIVE_ALERT_STATUS,
|
||||
VCR_SCROLLBAR,
|
||||
VCR_SCROLLBAR_ERROR,
|
||||
VCR_SCROLLBAR_WARNING,
|
||||
VCR_FOCUSED,
|
||||
VCR_DISABLED_FOCUSED,
|
||||
VCR_POPUP,
|
||||
VCR_COLOR_HINT,
|
||||
|
||||
VCR_KEYWORD,
|
||||
VCR_STRING,
|
||||
VCR_COMMENT,
|
||||
VCR_DOC_DIRECTIVE,
|
||||
VCR_VARIABLE,
|
||||
VCR_SYMBOL,
|
||||
VCR_NUMBER,
|
||||
VCR_RE_SPECIAL,
|
||||
VCR_RE_REPEAT,
|
||||
VCR_FILE,
|
||||
|
||||
VCR_DIFF_DELETE, /*< Deleted line in a diff. */
|
||||
VCR_DIFF_ADD, /*< Added line in a diff. */
|
||||
VCR_DIFF_SECTION, /*< Section marker in a diff. */
|
||||
|
||||
VCR_LOW_THRESHOLD,
|
||||
VCR_MED_THRESHOLD,
|
||||
VCR_HIGH_THRESHOLD,
|
||||
|
||||
VCR_H1,
|
||||
VCR_H2,
|
||||
VCR_H3,
|
||||
VCR_H4,
|
||||
VCR_H5,
|
||||
VCR_H6,
|
||||
|
||||
VCR__MAX
|
||||
};
|
||||
|
||||
using string_attr_value = mapbox::util::variant<int64_t,
|
||||
role_t,
|
||||
const intern_string_t,
|
||||
std::string,
|
||||
std::shared_ptr<logfile>,
|
||||
bookmark_metadata*>;
|
||||
|
||||
class string_attr_type_base {
|
||||
public:
|
||||
explicit string_attr_type_base(const char* name) noexcept : sat_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
const char* const sat_name;
|
||||
};
|
||||
|
||||
using string_attr_pair
|
||||
= std::pair<const string_attr_type_base*, string_attr_value>;
|
||||
|
||||
template<typename T>
|
||||
class string_attr_type : public string_attr_type_base {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
explicit string_attr_type(const char* name) noexcept
|
||||
: string_attr_type_base(name)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
std::enable_if_t<!std::is_void<U>::value, string_attr_pair> value(
|
||||
const U& val) const
|
||||
{
|
||||
return std::make_pair(this, val);
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
std::enable_if_t<std::is_void<U>::value, string_attr_pair> value() const
|
||||
{
|
||||
return std::make_pair(this, string_attr_value{});
|
||||
}
|
||||
};
|
||||
|
||||
extern string_attr_type<void> SA_ORIGINAL_LINE;
|
||||
extern string_attr_type<void> SA_BODY;
|
||||
extern string_attr_type<void> SA_HIDDEN;
|
||||
extern string_attr_type<const intern_string_t> SA_FORMAT;
|
||||
extern string_attr_type<void> SA_REMOVED;
|
||||
extern string_attr_type<std::string> SA_INVALID;
|
||||
extern string_attr_type<std::string> SA_ERROR;
|
||||
|
||||
extern string_attr_type<role_t> VC_ROLE;
|
||||
extern string_attr_type<role_t> VC_ROLE_FG;
|
||||
extern string_attr_type<int64_t> VC_STYLE;
|
||||
extern string_attr_type<int64_t> VC_GRAPHIC;
|
||||
extern string_attr_type<int64_t> VC_FOREGROUND;
|
||||
extern string_attr_type<int64_t> VC_BACKGROUND;
|
||||
|
||||
namespace lnav {
|
||||
namespace roles {
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
error(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_ERROR));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
warning(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_WARNING));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
status(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_STATUS));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
ok(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_OK));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
file(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_FILE));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
symbol(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_SYMBOL));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
keyword(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_KEYWORD));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
variable(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_VARIABLE));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
number(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_NUMBER));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
comment(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_COMMENT));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h1(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H1));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h2(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H2));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h3(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H3));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h4(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H4));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h5(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H5));
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
inline std::pair<S, string_attr_pair>
|
||||
h6(S str)
|
||||
{
|
||||
return std::make_pair(std::move(str),
|
||||
VC_ROLE.template value(role_t::VCR_H6));
|
||||
}
|
||||
|
||||
namespace literals {
|
||||
|
||||
inline std::pair<std::string, string_attr_pair> operator"" _symbol(
|
||||
const char* str, std::size_t len)
|
||||
{
|
||||
return std::make_pair(std::string(str, len),
|
||||
VC_ROLE.template value(role_t::VCR_SYMBOL));
|
||||
}
|
||||
|
||||
inline std::pair<std::string, string_attr_pair> operator"" _variable(
|
||||
const char* str, std::size_t len)
|
||||
{
|
||||
return std::make_pair(std::string(str, len),
|
||||
VC_ROLE.template value(role_t::VCR_VARIABLE));
|
||||
}
|
||||
|
||||
inline std::pair<std::string, string_attr_pair> operator"" _h1(const char* str,
|
||||
std::size_t len)
|
||||
{
|
||||
return std::make_pair(std::string(str, len),
|
||||
VC_ROLE.template value(role_t::VCR_H1));
|
||||
}
|
||||
|
||||
inline std::pair<std::string, string_attr_pair> operator"" _h2(const char* str,
|
||||
std::size_t len)
|
||||
{
|
||||
return std::make_pair(std::string(str, len),
|
||||
VC_ROLE.template value(role_t::VCR_H2));
|
||||
}
|
||||
|
||||
inline std::pair<std::string, string_attr_pair> operator"" _h3(const char* str,
|
||||
std::size_t len)
|
||||
{
|
||||
return std::make_pair(std::string(str, len),
|
||||
VC_ROLE.template value(role_t::VCR_H3));
|
||||
}
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace roles
|
||||
} // namespace lnav
|
||||
|
||||
#endif
|
@ -1,94 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2020, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lnav_string_attr_type_hh
|
||||
#define lnav_string_attr_type_hh
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "base/intern_string.hh"
|
||||
#include "mapbox/variant.hpp"
|
||||
|
||||
class logfile;
|
||||
struct bookmark_metadata;
|
||||
|
||||
using string_attr_value = mapbox::util::variant<int64_t,
|
||||
const intern_string_t,
|
||||
std::string,
|
||||
std::shared_ptr<logfile>,
|
||||
bookmark_metadata*>;
|
||||
|
||||
class string_attr_type_base {
|
||||
public:
|
||||
explicit string_attr_type_base(const char* name) noexcept : sat_name(name)
|
||||
{
|
||||
}
|
||||
|
||||
const char* const sat_name;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class string_attr_type : public string_attr_type_base {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
explicit string_attr_type(const char* name) noexcept
|
||||
: string_attr_type_base(name)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
std::enable_if_t<!std::is_void<U>::value,
|
||||
std::pair<const string_attr_type_base*, string_attr_value>>
|
||||
value(const U& val) const
|
||||
{
|
||||
return std::make_pair(this, val);
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
std::enable_if_t<std::is_void<U>::value,
|
||||
std::pair<const string_attr_type_base*, string_attr_value>>
|
||||
value() const
|
||||
{
|
||||
return std::make_pair(this, string_attr_value{});
|
||||
}
|
||||
};
|
||||
|
||||
extern string_attr_type<void> SA_ORIGINAL_LINE;
|
||||
extern string_attr_type<void> SA_BODY;
|
||||
extern string_attr_type<void> SA_HIDDEN;
|
||||
extern string_attr_type<const intern_string_t> SA_FORMAT;
|
||||
extern string_attr_type<void> SA_REMOVED;
|
||||
extern string_attr_type<std::string> SA_INVALID;
|
||||
extern string_attr_type<std::string> SA_ERROR;
|
||||
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue