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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-09-14 01:07:32 +00:00
|
|
|
* @file view_curses.hh
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __view_curses_hh
|
|
|
|
#define __view_curses_hh
|
|
|
|
|
2012-06-05 20:18:59 +00:00
|
|
|
#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
|
|
|
|
2012-04-17 15:16:41 +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;
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* An RAII class that initializes and deinitializes curses.
|
|
|
|
*/
|
2009-09-14 01:07:32 +00:00
|
|
|
class screen_curses {
|
|
|
|
public:
|
|
|
|
screen_curses()
|
2012-04-24 21:31:35 +00:00
|
|
|
: sc_main_window(initscr()) {
|
|
|
|
};
|
|
|
|
virtual ~screen_curses() {
|
|
|
|
endwin();
|
|
|
|
};
|
2009-09-14 01:07:32 +00:00
|
|
|
|
|
|
|
WINDOW *get_window() { return this->sc_main_window; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
WINDOW *sc_main_window;
|
|
|
|
};
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* Encapsulates a range in a string.
|
|
|
|
*/
|
2009-09-14 01:07:32 +00:00
|
|
|
struct line_range {
|
|
|
|
int lr_start;
|
|
|
|
int lr_end;
|
|
|
|
|
|
|
|
int length() const {
|
|
|
|
return this->lr_end == -1 ? INT_MAX : this->lr_end - this->lr_start;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator<(const struct line_range &rhs) const {
|
2009-10-06 21:14:49 +00:00
|
|
|
if (this->lr_start < rhs.lr_start) return true;
|
|
|
|
else if (this->lr_start > rhs.lr_start) return false;
|
2013-05-24 14:55:56 +00:00
|
|
|
|
|
|
|
if (this->lr_end == rhs.lr_end) return false;
|
2009-10-06 21:14:49 +00:00
|
|
|
|
|
|
|
if (this->lr_end < rhs.lr_end) return true;
|
|
|
|
return false;
|
2009-09-14 01:07:32 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* Container for attribute values for a substring.
|
|
|
|
*/
|
2009-09-14 01:07:32 +00:00
|
|
|
typedef union {
|
|
|
|
void *sa_ptr;
|
|
|
|
int sa_int;
|
|
|
|
} string_attr_t;
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
sa.sa_int = val;
|
|
|
|
|
|
|
|
return std::make_pair(name, sa);
|
|
|
|
}
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/** 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;
|
2013-04-20 20:21:10 +00:00
|
|
|
/** A map of line ranges to attributes for that range. */
|
2009-09-14 01:07:32 +00:00
|
|
|
typedef std::map<struct line_range, attrs_map_t> string_attrs_t;
|
|
|
|
|
2013-05-24 14:55:56 +00:00
|
|
|
inline struct line_range
|
|
|
|
find_string_attr_range(const string_attrs_t &sa, const std::string &name) {
|
|
|
|
struct line_range retval = { -1, -1 };
|
|
|
|
|
|
|
|
for (string_attrs_t::const_iterator iter = sa.begin();
|
|
|
|
iter != sa.end();
|
|
|
|
++iter) {
|
|
|
|
attrs_map_t::const_iterator prefix_iter;
|
|
|
|
|
|
|
|
if ((prefix_iter = iter->second.find(name)) != iter->second.end()) {
|
|
|
|
retval = iter->first;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/**
|
|
|
|
* A line that has attributes.
|
|
|
|
*/
|
2009-09-14 01:07:32 +00:00
|
|
|
class attr_line_t {
|
|
|
|
public:
|
|
|
|
attr_line_t() { };
|
2013-05-24 14:55:56 +00:00
|
|
|
attr_line_t(const std::string &str) : al_string(str) { };
|
2009-09-14 01:07:32 +00:00
|
|
|
|
2013-04-20 20:21:10 +00:00
|
|
|
/** @return The string itself. */
|
2009-09-14 01:07:32 +00:00
|
|
|
std::string &get_string() { return this->al_string; };
|
|
|
|
|
2013-04-20 20:21:10 +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-04-20 20:21:10 +00:00
|
|
|
/** Clear the string and the attributes for the string. */
|
2009-09-14 01:07:32 +00:00
|
|
|
void clear() {
|
|
|
|
this->al_string.clear();
|
|
|
|
this->al_attrs.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string al_string;
|
|
|
|
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:
|
|
|
|
mem_functor_t(_Receiver &receiver,
|
|
|
|
void (_Receiver::*selector)(_Sender *))
|
|
|
|
: mf_receiver(receiver),
|
|
|
|
mf_selector(selector) { };
|
|
|
|
|
2013-04-17 16:27:12 +00:00
|
|
|
void operator()(_Sender *sender) const
|
2009-09-14 01:07:32 +00:00
|
|
|
{
|
|
|
|
(this->mf_receiver.*mf_selector)(sender);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void invoke(mem_functor_t *self, _Sender *sender)
|
|
|
|
{
|
|
|
|
(*self)(sender);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
_Receiver & mf_receiver;
|
|
|
|
void (_Receiver::*mf_selector)(_Sender *);
|
|
|
|
};
|
|
|
|
|
|
|
|
class broadcaster
|
|
|
|
: public std::vector<view_action> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
broadcaster()
|
|
|
|
: b_functor(*this, &broadcaster::invoke) { };
|
|
|
|
virtual ~broadcaster() { };
|
|
|
|
|
|
|
|
void invoke(_Sender *sender)
|
|
|
|
{
|
|
|
|
typename std::vector<view_action>::iterator iter;
|
|
|
|
|
2013-04-17 16:27:12 +00:00
|
|
|
for (iter = this->begin(); iter != this->end(); ++iter) {
|
2009-09-14 01:07:32 +00:00
|
|
|
(*iter).invoke(sender);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mem_functor_t<broadcaster> *get_functor()
|
|
|
|
{
|
|
|
|
return &this->b_functor;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
mem_functor_t<broadcaster> b_functor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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().
|
|
|
|
*/
|
|
|
|
view_action(void (*invoker)(void *, _Sender *) = NULL)
|
|
|
|
: va_functor(NULL),
|
|
|
|
va_invoker(invoker) { };
|
|
|
|
|
|
|
|
template<class _Receiver>
|
|
|
|
view_action(mem_functor_t < _Receiver > *mf)
|
|
|
|
: va_functor(mf),
|
|
|
|
va_invoker((void (*)(void *, _Sender *))
|
|
|
|
mem_functor_t<_Receiver>::invoke) { };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
: va_functor(va.va_functor),
|
|
|
|
va_invoker(va.va_invoker) { };
|
|
|
|
|
|
|
|
~view_action() { };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param rhs The view_action to shallow copy.
|
|
|
|
* @return *this
|
|
|
|
*/
|
|
|
|
view_action &operator=(const view_action &rhs)
|
|
|
|
{
|
|
|
|
this->va_functor = rhs.va_functor;
|
|
|
|
this->va_invoker = rhs.va_invoker;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
if (this->va_invoker != NULL) {
|
|
|
|
this->va_invoker(this->va_functor, sender);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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. */
|
|
|
|
void (*va_invoker)(void *functor, _Sender * sender);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton used to manage the colorspace.
|
|
|
|
*/
|
|
|
|
class view_colors {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** Roles that can be mapped to curses attributes using attrs_for_role() */
|
|
|
|
typedef enum {
|
|
|
|
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, /*< */
|
|
|
|
|
2013-04-23 16:25:18 +00:00
|
|
|
VCR_DIFF_DELETE, /*< Deleted line in a diff. */
|
|
|
|
VCR_DIFF_ADD, /*< Added line in a diff. */
|
|
|
|
VCR_DIFF_SECTION, /*< Section marker in a diff. */
|
|
|
|
|
2013-05-24 14:55:56 +00:00
|
|
|
VCR_SHADOW,
|
|
|
|
|
2009-09-14 01:07:32 +00:00
|
|
|
VCR__MAX
|
|
|
|
} 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
|
|
|
{
|
|
|
|
assert(role >= 0);
|
|
|
|
assert(role < VCR__MAX + (HL_COLOR_COUNT * 2));
|
|
|
|
|
|
|
|
return this->vc_role_colors[role];
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
assert(role >= 0);
|
|
|
|
assert(role < VCR__MAX + (HL_COLOR_COUNT * 2));
|
|
|
|
|
|
|
|
return this->vc_role_reverse_colors[role];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2013-05-24 14:55:56 +00:00
|
|
|
role_t next_highlight();
|
|
|
|
|
|
|
|
role_t next_plain_highlight();
|
2009-09-14 01:07:32 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
VC_EMPTY = 0, /* XXX Dead color pair, doesn't work. */
|
|
|
|
|
|
|
|
VC_BLUE,
|
|
|
|
VC_CYAN,
|
|
|
|
VC_GREEN,
|
|
|
|
VC_MAGENTA,
|
|
|
|
|
|
|
|
VC_BLUE_ON_WHITE,
|
|
|
|
VC_CYAN_ON_BLACK,
|
|
|
|
VC_GREEN_ON_WHITE,
|
|
|
|
VC_MAGENTA_ON_WHITE,
|
|
|
|
|
|
|
|
VC_RED,
|
|
|
|
VC_YELLOW,
|
|
|
|
VC_WHITE,
|
|
|
|
|
|
|
|
VC_BLACK_ON_WHITE,
|
|
|
|
VC_YELLOW_ON_WHITE,
|
|
|
|
VC_RED_ON_WHITE,
|
|
|
|
|
|
|
|
VC_WHITE_ON_GREEN,
|
2013-05-24 14:55:56 +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;
|
2013-05-24 14:55:56 +00:00
|
|
|
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,
|
|
|
|
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
|