lnav/src/view_curses.hh

453 lines
12 KiB
C++
Raw Normal View History

2009-09-14 01:07:32 +00:00
/**
2013-05-03 06:02:03 +00:00
* Copyright (c) 2007-2012, Timothy Stack
*
* All rights reserved.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* * 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.
2013-05-28 04:35:00 +00:00
*
2013-05-03 06:02:03 +00:00
* 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.
*
2009-09-14 01:07:32 +00:00
* @file view_curses.hh
*/
#ifndef __view_curses_hh
#define __view_curses_hh
#include "config.h"
2009-09-14 01:07:32 +00:00
#include <assert.h>
#include <stdint.h>
2009-10-14 19:42:58 +00:00
#include <limits.h>
2009-09-14 01:07:32 +00:00
#if defined HAVE_NCURSESW_CURSES_H
# include <ncursesw/curses.h>
#elif defined HAVE_NCURSESW_H
# include <ncursesw.h>
#elif defined HAVE_NCURSES_CURSES_H
# include <ncurses/curses.h>
#elif defined HAVE_NCURSES_H
# include <ncurses.h>
#elif defined HAVE_CURSES_H
# include <curses.h>
#else
# error "SysV or X/Open-compatible Curses header file required"
#endif
2009-09-14 01:07:32 +00:00
#include <map>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
class view_curses;
/**
* An RAII class that initializes and deinitializes curses.
*/
2009-09-14 01:07:32 +00:00
class screen_curses {
public:
screen_curses()
2013-05-28 04:35:00 +00:00
: sc_main_window(initscr()) {};
virtual ~screen_curses()
{
endwin();
2012-04-24 21:31:35 +00:00
};
2009-09-14 01:07:32 +00:00
WINDOW *get_window() { return this->sc_main_window; };
private:
WINDOW *sc_main_window;
};
/**
* Encapsulates a range in a string.
*/
2009-09-14 01:07:32 +00:00
struct line_range {
int lr_start;
int lr_end;
2013-05-28 04:35:00 +00:00
int length() const
{
return this->lr_end == -1 ? INT_MAX : this->lr_end - this->lr_start;
2009-09-14 01:07:32 +00:00
};
2013-05-28 04:35:00 +00:00
bool operator<(const struct line_range &rhs) const
{
if (this->lr_start < rhs.lr_start) { return true; }
else if (this->lr_start > rhs.lr_start) { return false; }
if (this->lr_end == rhs.lr_end) { return false; }
if (this->lr_end < rhs.lr_end) { return true; }
return false;
2009-09-14 01:07:32 +00:00
};
};
/**
* Container for attribute values for a substring.
*/
2009-09-14 01:07:32 +00:00
typedef union {
void *sa_ptr;
2013-05-28 04:35:00 +00:00
int sa_int;
2009-09-14 01:07:32 +00:00
} string_attr_t;
/**
* Construct a string_attr_t the a void pointer value.
*
* @param name The name of the attribute.
* @param val The value to store in the attribute.
*/
2009-09-14 01:07:32 +00:00
inline std::pair<std::string, string_attr_t>
2013-04-17 16:27:12 +00:00
make_string_attr(const std::string &name, void *val)
2009-09-14 01:07:32 +00:00
{
string_attr_t sa;
sa.sa_ptr = val;
return std::make_pair(name, sa);
}
/**
* Construct a string_attr_t the an integer value.
*
* @param name The name of the attribute.
* @param val The value to store in the attribute.
*/
2009-09-14 01:07:32 +00:00
inline std::pair<std::string, string_attr_t>
2013-04-17 16:27:12 +00:00
make_string_attr(const std::string &name, int val)
2009-09-14 01:07:32 +00:00
{
string_attr_t sa;
2013-05-28 04:35:00 +00:00
2009-09-14 01:07:32 +00:00
sa.sa_int = val;
return std::make_pair(name, sa);
}
/** A map of symbolic names to attribute values. */
2009-09-14 01:07:32 +00:00
typedef std::multimap<std::string, string_attr_t> attrs_map_t;
/** A map of line ranges to attributes for that range. */
2013-05-28 04:35:00 +00:00
typedef std::map<struct line_range, attrs_map_t> string_attrs_t;
2009-09-14 01:07:32 +00:00
inline struct line_range
2013-05-28 04:35:00 +00:00
find_string_attr_range(const string_attrs_t &sa, const std::string &name)
{
struct line_range retval = { -1, -1 };
2013-05-28 04:35:00 +00:00
for (string_attrs_t::const_iterator iter = sa.begin();
iter != sa.end();
++iter) {
if (iter->second.find(name) != iter->second.end()) {
2013-05-28 04:35:00 +00:00
retval = iter->first;
break;
}
}
2013-05-28 04:35:00 +00:00
return retval;
}
/**
* A line that has attributes.
*/
2009-09-14 01:07:32 +00:00
class attr_line_t {
public:
attr_line_t() { };
attr_line_t(const std::string &str) : al_string(str) { };
2009-09-14 01:07:32 +00:00
/** @return The string itself. */
2009-09-14 01:07:32 +00:00
std::string &get_string() { return this->al_string; };
2013-05-28 04:35:00 +00:00
/** @return The attributes for the string. */
2009-09-14 01:07:32 +00:00
string_attrs_t &get_attrs() { return this->al_attrs; };
void operator=(const std::string &rhs) { this->al_string = rhs; };
2013-05-28 04:35:00 +00:00
/** Clear the string and the attributes for the string. */
2013-05-28 04:35:00 +00:00
void clear()
{
this->al_string.clear();
this->al_attrs.clear();
2009-09-14 01:07:32 +00:00
};
2013-05-28 04:35:00 +00:00
2009-09-14 01:07:32 +00:00
private:
2013-05-28 04:35:00 +00:00
std::string al_string;
2009-09-14 01:07:32 +00:00
string_attrs_t al_attrs;
};
/**
* Class that encapsulates a method to execute and the object on which to
* execute it.
*
* @param _Sender The type of object that will be triggering an action.
*/
template<class _Sender>
class view_action {
public:
/**
*
* @param _Receiver The type of object that will be triggered by an action.
*/
template<class _Receiver>
class mem_functor_t {
public:
2013-05-28 04:35:00 +00:00
mem_functor_t(_Receiver &receiver,
void(_Receiver::*selector)(_Sender *))
: mf_receiver(receiver),
mf_selector(selector) { };
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
void operator()(_Sender *sender) const
{
(this->mf_receiver.*mf_selector)(sender);
};
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
static void invoke(mem_functor_t *self, _Sender *sender)
{
(*self)(sender);
};
2009-09-14 01:07:32 +00:00
private:
2013-05-28 04:35:00 +00:00
_Receiver & mf_receiver;
void (_Receiver::*mf_selector)(_Sender *);
2009-09-14 01:07:32 +00:00
};
class broadcaster
2013-05-28 04:35:00 +00:00
: public std::vector<view_action> {
2009-09-14 01:07:32 +00:00
public:
2013-05-28 04:35:00 +00:00
broadcaster()
: b_functor(*this, &broadcaster::invoke) { };
virtual ~broadcaster() { };
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
void invoke(_Sender *sender)
{
typename std::vector<view_action>::iterator iter;
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
for (iter = this->begin(); iter != this->end(); ++iter) {
(*iter).invoke(sender);
}
};
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
mem_functor_t<broadcaster> *get_functor()
{
return &this->b_functor;
};
2009-09-14 01:07:32 +00:00
private:
2013-05-28 04:35:00 +00:00
mem_functor_t<broadcaster> b_functor;
2009-09-14 01:07:32 +00:00
};
/**
* @param receiver The object to pass as the first argument to the selector
* function.
* @param selector The function to execute. The function should take two
* parameters, the first being the value of the receiver pointer and the
* second being the sender pointer as passed to invoke().
*/
2013-05-28 04:35:00 +00:00
view_action(void(*invoker)(void *, _Sender *) = NULL)
: va_functor(NULL),
va_invoker(invoker) { };
2009-09-14 01:07:32 +00:00
template<class _Receiver>
2013-05-28 04:35:00 +00:00
view_action(mem_functor_t<_Receiver> *mf)
: va_functor(mf),
va_invoker((void(*) (void *, _Sender *))
mem_functor_t<_Receiver>::invoke) { };
2009-09-14 01:07:32 +00:00
/**
* Performs a shallow copy of another view_action.
*
* @param va The view_action to copy the receiver and selector pointers
* from.
*/
view_action(const view_action &va)
2013-05-28 04:35:00 +00:00
: va_functor(va.va_functor),
va_invoker(va.va_invoker) { };
2009-09-14 01:07:32 +00:00
~view_action() { };
/**
* @param rhs The view_action to shallow copy.
* @return *this
*/
view_action &operator=(const view_action &rhs)
{
2013-05-28 04:35:00 +00:00
this->va_functor = rhs.va_functor;
this->va_invoker = rhs.va_invoker;
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return *this;
2009-09-14 01:07:32 +00:00
};
/**
* Invoke the action by calling the selector function, if one is set.
*
* @param sender Pointer to the object that called this method.
*/
void invoke(_Sender *sender)
{
2013-05-28 04:35:00 +00:00
if (this->va_invoker != NULL) {
this->va_invoker(this->va_functor, sender);
}
2009-09-14 01:07:32 +00:00
};
private:
/** The object to pass as the first argument to the selector function.*/
void *va_functor;
/** The function to call when this action is invoke()'d. */
2013-05-28 04:35:00 +00:00
void (*va_invoker)(void *functor, _Sender *sender);
2009-09-14 01:07:32 +00:00
};
/**
* Singleton used to manage the colorspace.
*/
class view_colors {
public:
/** Roles that can be mapped to curses attributes using attrs_for_role() */
typedef enum {
2013-05-28 04:35:00 +00:00
VCR_NONE = -1,
VCR_TEXT, /*< Raw text. */
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_STATUS, /*< Normal status line text. */
VCR_WARN_STATUS,
VCR_ALERT_STATUS, /*< Alert status line text. */
VCR_ACTIVE_STATUS, /*< */
VCR_ACTIVE_STATUS2, /*< */
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_SHADOW,
VCR__MAX
2009-09-14 01:07:32 +00:00
} role_t;
/** @return A reference to the singleton. */
static view_colors &singleton();
/**
* Performs curses-specific initialization. The other methods can be
* called before this method, but the returned attributes cannot be used
* with curses code until this method is called.
*/
2013-04-17 16:27:12 +00:00
static void init(void);
2009-09-14 01:07:32 +00:00
/**
* @param role The role to retrieve character attributes for.
* @return The attributes to use for the given role.
*/
2013-04-17 16:27:12 +00:00
int attrs_for_role(role_t role) const
2009-09-14 01:07:32 +00:00
{
2013-05-28 04:35:00 +00:00
assert(role >= 0);
assert(role < VCR__MAX + (HL_COLOR_COUNT * 2));
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return this->vc_role_colors[role];
2009-09-14 01:07:32 +00:00
};
2013-04-17 16:27:12 +00:00
int reverse_attrs_for_role(role_t role) const
2009-09-14 01:07:32 +00:00
{
2013-05-28 04:35:00 +00:00
assert(role >= 0);
assert(role < VCR__MAX + (HL_COLOR_COUNT * 2));
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
return this->vc_role_reverse_colors[role];
2009-09-14 01:07:32 +00:00
};
/**
* @return The next set of attributes to use for highlighting text. This
* method will iterate through eight-or-so attributes combinations so there
* is some variety in how text is highlighted.
*/
role_t next_highlight();
role_t next_plain_highlight();
2009-09-14 01:07:32 +00:00
enum {
2013-05-28 04:35:00 +00:00
VC_EMPTY = 0, /* XXX Dead color pair, doesn't work. */
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
VC_BLUE,
VC_CYAN,
VC_GREEN,
VC_MAGENTA,
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
VC_BLUE_ON_WHITE,
VC_CYAN_ON_BLACK,
VC_GREEN_ON_WHITE,
VC_MAGENTA_ON_WHITE,
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
VC_RED,
VC_YELLOW,
VC_WHITE,
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
VC_BLACK_ON_WHITE,
VC_YELLOW_ON_WHITE,
VC_RED_ON_WHITE,
2009-09-14 01:07:32 +00:00
2013-05-28 04:35:00 +00:00
VC_WHITE_ON_GREEN,
2013-05-28 04:35:00 +00:00
VC_GRAY,
2009-09-14 01:07:32 +00:00
};
private:
/** The number of colors used for highlighting. */
static const int HL_COLOR_COUNT = 4;
/** Private constructor that initializes the member fields. */
view_colors();
/** Map of role IDs to attribute values. */
int vc_role_colors[VCR__MAX + (HL_COLOR_COUNT * 2)];
/** Map of role IDs to reverse-video attribute values. */
int vc_role_reverse_colors[VCR__MAX + (HL_COLOR_COUNT * 2)];
/** The index of the next highlight color to use. */
int vc_next_highlight;
int vc_next_plain_highlight;
2009-09-14 01:07:32 +00:00
};
/**
* Interface for "view" classes that will update a curses(3) display.
*/
class view_curses {
public:
virtual ~view_curses() { };
/**
* Update the curses display.
*/
virtual void do_update(void) = 0;
2013-04-17 16:27:12 +00:00
static void mvwattrline(WINDOW *window,
2013-05-28 04:35:00 +00:00
int y,
int x,
attr_line_t &al,
struct line_range &lr,
view_colors::role_t base_role =
view_colors::VCR_TEXT);
2009-09-14 01:07:32 +00:00
};
#endif