scrollpad: add support for regex searching
This commit is contained in:
14
configure.in
14
configure.in
@@ -91,13 +91,23 @@ AC_ARG_VAR([BOOST_LIB_SUFFIX], [Boost library name suffix [default=-mt]])
|
||||
dnl =============================
|
||||
dnl = checking for boost.locale =
|
||||
dnl =============================
|
||||
AC_CHECK_HEADERS([boost/locale/encoding.hpp], ,
|
||||
AC_MSG_ERROR(boost/locale/encoding.hpp is missing)
|
||||
AC_CHECK_HEADERS([boost/locale.hpp], ,
|
||||
AC_MSG_ERROR(boost/locale.hpp is missing)
|
||||
)
|
||||
AC_CHECK_LIB(boost_locale$BOOST_LIB_SUFFIX, main, LDFLAGS="$LDFLAGS -lboost_locale$BOOST_LIB_SUFFIX",
|
||||
AC_MSG_ERROR([no boost.locale library found])
|
||||
)
|
||||
|
||||
dnl ============================
|
||||
dnl = checking for boost.regex =
|
||||
dnl ============================
|
||||
AC_CHECK_HEADERS([boost/regex.hpp], ,
|
||||
AC_MSG_ERROR(boost/regex.hpp is missing)
|
||||
)
|
||||
AC_CHECK_LIB(boost_regex$BOOST_LIB_SUFFIX, main, LDFLAGS="$LDFLAGS -lboost_regex$BOOST_LIB_SUFFIX",
|
||||
AC_MSG_ERROR([no boost.regex library found])
|
||||
)
|
||||
|
||||
dnl ==============================
|
||||
dnl = checking for regex (win32) =
|
||||
dnl ==============================
|
||||
|
||||
@@ -19,9 +19,33 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <cassert>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
#include "scrollpad.h"
|
||||
#include "utility/wide_string.h"
|
||||
|
||||
namespace {//
|
||||
|
||||
template <typename PropT>
|
||||
bool regexSearch(NC::Buffer &buf, PropT begin, const std::string &ws, PropT end, size_t id)
|
||||
{
|
||||
try {
|
||||
boost::regex rx(ws, boost::regex::icase);
|
||||
auto first = boost::sregex_iterator(buf.str().begin(), buf.str().end(), rx);
|
||||
auto last = boost::sregex_iterator();
|
||||
bool success = first != last;
|
||||
for (; first != last; ++first)
|
||||
{
|
||||
buf.setProperty(first->position(), begin, id);
|
||||
buf.setProperty(first->position() + first->length(), end, id);
|
||||
}
|
||||
return success;
|
||||
} catch (boost::bad_expression &e) {
|
||||
std::cerr << "regexSearch: bad_expression: " << e.what() << "\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace NC {//
|
||||
|
||||
@@ -38,6 +62,82 @@ m_real_height(height)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void Scrollpad::refresh()
|
||||
{
|
||||
assert(m_real_height >= m_height);
|
||||
size_t max_beginning = m_real_height - m_height;
|
||||
m_beginning = std::min(m_beginning, max_beginning);
|
||||
prefresh(m_window, m_beginning, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
|
||||
}
|
||||
|
||||
void Scrollpad::resize(size_t new_width, size_t new_height)
|
||||
{
|
||||
adjustDimensions(new_width, new_height);
|
||||
flush();
|
||||
}
|
||||
|
||||
void Scrollpad::scroll(Scroll where)
|
||||
{
|
||||
assert(m_real_height >= m_height);
|
||||
size_t max_beginning = m_real_height - m_height;
|
||||
switch (where)
|
||||
{
|
||||
case Scroll::Up:
|
||||
{
|
||||
if (m_beginning > 0)
|
||||
--m_beginning;
|
||||
break;
|
||||
}
|
||||
case Scroll::Down:
|
||||
{
|
||||
if (m_beginning < max_beginning)
|
||||
++m_beginning;
|
||||
break;
|
||||
}
|
||||
case Scroll::PageUp:
|
||||
{
|
||||
if (m_beginning > m_height)
|
||||
m_beginning -= m_height;
|
||||
else
|
||||
m_beginning = 0;
|
||||
break;
|
||||
}
|
||||
case Scroll::PageDown:
|
||||
{
|
||||
m_beginning = std::min(m_beginning + m_height, max_beginning);
|
||||
break;
|
||||
}
|
||||
case Scroll::Home:
|
||||
{
|
||||
m_beginning = 0;
|
||||
break;
|
||||
}
|
||||
case Scroll::End:
|
||||
{
|
||||
m_beginning = max_beginning;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scrollpad::clear()
|
||||
{
|
||||
m_real_height = m_height;
|
||||
m_buffer.clear();
|
||||
wclear(m_window);
|
||||
delwin(m_window);
|
||||
m_window = newpad(m_height, m_width);
|
||||
setTimeout(m_window_timeout);
|
||||
setColor(m_color, m_bg_color);
|
||||
keypad(m_window, 1);
|
||||
}
|
||||
|
||||
const std::string &Scrollpad::buffer()
|
||||
{
|
||||
return m_buffer.str();
|
||||
}
|
||||
|
||||
void Scrollpad::flush()
|
||||
{
|
||||
auto &w = static_cast<Window &>(*this);
|
||||
@@ -152,79 +252,24 @@ void Scrollpad::flush()
|
||||
write_buffer(false);
|
||||
}
|
||||
|
||||
void Scrollpad::refresh()
|
||||
{
|
||||
assert(m_real_height >= m_height);
|
||||
size_t max_beginning = m_real_height - m_height;
|
||||
m_beginning = std::min(m_beginning, max_beginning);
|
||||
prefresh(m_window, m_beginning, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
|
||||
}
|
||||
|
||||
void Scrollpad::resize(size_t new_width, size_t new_height)
|
||||
{
|
||||
adjustDimensions(new_width, new_height);
|
||||
flush();
|
||||
}
|
||||
|
||||
void Scrollpad::scroll(Scroll where)
|
||||
{
|
||||
assert(m_real_height >= m_height);
|
||||
size_t max_beginning = m_real_height - m_height;
|
||||
switch (where)
|
||||
{
|
||||
case Scroll::Up:
|
||||
{
|
||||
if (m_beginning > 0)
|
||||
--m_beginning;
|
||||
break;
|
||||
}
|
||||
case Scroll::Down:
|
||||
{
|
||||
if (m_beginning < max_beginning)
|
||||
++m_beginning;
|
||||
break;
|
||||
}
|
||||
case Scroll::PageUp:
|
||||
{
|
||||
if (m_beginning > m_height)
|
||||
m_beginning -= m_height;
|
||||
else
|
||||
m_beginning = 0;
|
||||
break;
|
||||
}
|
||||
case Scroll::PageDown:
|
||||
{
|
||||
m_beginning = std::min(m_beginning + m_height, max_beginning);
|
||||
break;
|
||||
}
|
||||
case Scroll::Home:
|
||||
{
|
||||
m_beginning = 0;
|
||||
break;
|
||||
}
|
||||
case Scroll::End:
|
||||
{
|
||||
m_beginning = max_beginning;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scrollpad::clear()
|
||||
{
|
||||
m_real_height = m_height;
|
||||
m_buffer.clear();
|
||||
wclear(m_window);
|
||||
delwin(m_window);
|
||||
m_window = newpad(m_height, m_width);
|
||||
setTimeout(m_window_timeout);
|
||||
setColor(m_color, m_bg_color);
|
||||
keypad(m_window, 1);
|
||||
}
|
||||
|
||||
void Scrollpad::reset()
|
||||
{
|
||||
m_beginning = 0;
|
||||
}
|
||||
|
||||
bool Scrollpad::setProperties(Color begin, const std::string &s, Color end, size_t id)
|
||||
{
|
||||
return regexSearch(m_buffer, begin, s, end, id);
|
||||
}
|
||||
|
||||
bool Scrollpad::setProperties(Format begin, const std::string &s, Format end, size_t id)
|
||||
{
|
||||
return regexSearch(m_buffer, begin, s, end, id);
|
||||
}
|
||||
|
||||
void Scrollpad::removeProperties(size_t id)
|
||||
{
|
||||
m_buffer.removeProperties(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,35 +35,25 @@ struct Scrollpad: public Window
|
||||
Scrollpad(size_t startx, size_t starty, size_t width, size_t height,
|
||||
const std::string &title, Color color, Border border);
|
||||
|
||||
const std::string &buffer() { return m_buffer.str(); }
|
||||
|
||||
void flush();
|
||||
void reset();
|
||||
|
||||
template <typename PropertyT>
|
||||
bool setProperties(PropertyT begin, const std::string &ws, PropertyT end, size_t id = -2)
|
||||
{
|
||||
bool success = false;
|
||||
for (size_t i = 0; (i = m_buffer.str().find(ws, i)) != std::string::npos;)
|
||||
{
|
||||
success = true;
|
||||
m_buffer.setProperty(i, begin, id);
|
||||
i += ws.length();
|
||||
m_buffer.setProperty(i, end, id);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
void removeProperties(size_t id = -2) { m_buffer.removeProperties(id); }
|
||||
|
||||
// override a few Window functions
|
||||
virtual void refresh() OVERRIDE;
|
||||
virtual void scroll(Scroll where) OVERRIDE;
|
||||
virtual void resize(size_t new_width, size_t new_height) OVERRIDE;
|
||||
virtual void clear() OVERRIDE;
|
||||
|
||||
template <typename T> Scrollpad &operator<<(const T &obj)
|
||||
const std::string &buffer();
|
||||
|
||||
void flush();
|
||||
void reset();
|
||||
|
||||
bool setProperties(Color begin, const std::string &s, Color end, size_t id = -2);
|
||||
bool setProperties(Format begin, const std::string &s, Format end, size_t id = -2);
|
||||
void removeProperties(size_t id = -2);
|
||||
|
||||
template <typename ItemT>
|
||||
Scrollpad &operator<<(const ItemT &item)
|
||||
{
|
||||
m_buffer << obj;
|
||||
m_buffer << item;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "status.h"
|
||||
#include "statusbar.h"
|
||||
#include "bindings.h"
|
||||
#include "utility/wide_string.h"
|
||||
|
||||
using Global::wFooter;
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
#include <set>
|
||||
#include "utility/numeric_conversions.h"
|
||||
#include "utility/wide_string.h"
|
||||
#include "window.h"
|
||||
|
||||
namespace NC {//
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "global.h"
|
||||
#include "settings.h"
|
||||
#include "title.h"
|
||||
#include "utility/wide_string.h"
|
||||
|
||||
#ifdef USE_PDCURSES
|
||||
void windowTitle(const std::string &) { }
|
||||
|
||||
Reference in New Issue
Block a user