[text] hide text files

This commit is contained in:
Timothy Stack 2020-10-28 21:23:16 -07:00
parent 7b77a612c2
commit c22c78220c
9 changed files with 309 additions and 145 deletions

View File

@ -305,6 +305,7 @@ add_library(diag STATIC
strnatcmp.c
text_format.cc
textfile_highlighters.cc
textfile_sub_source.cc
textview_curses.cc
time-extension-functions.cc
timer.cc

View File

@ -440,6 +440,7 @@ libdiag_a_SOURCES = \
string-extension-functions.cc \
styling.cc \
text_format.cc \
textfile_sub_source.cc \
timer.cc \
piper_proc.cc \
sql_util.cc \

View File

@ -850,8 +850,34 @@ bool handle_paging_key(int ch)
tc->reload_data();
} else if (tc_tss != nullptr && tc_tss->tss_supports_filtering) {
lnav_data.ld_mode = LNM_FILTER;
lnav_data.ld_mode = lnav_data.ld_last_config_mode;
lnav_data.ld_filter_view.reload_data();
lnav_data.ld_files_view.reload_data();
if (tc->get_inner_height() > 0_vl) {
string_attrs_t::const_iterator line_attr;
std::vector<attr_line_t> rows(1);
tc->get_data_source()->
listview_value_for_rows(*tc, tc->get_top(), rows);
string_attrs_t &sa = rows[0].get_attrs();
line_attr = find_string_attr(sa, &logline::L_FILE);
if (line_attr != sa.end()) {
const auto &fc = lnav_data.ld_active_files;
auto lf = ((logfile *)line_attr->sa_value.sav_ptr)->
shared_from_this();
auto iter = find(fc.fc_files.begin(),
fc.fc_files.end(),
lf);
if (iter != fc.fc_files.end()) {
auto index = distance(fc.fc_files.begin(), iter);
auto index_vl = vis_line_t(index);
log_debug("index %d", index);
lnav_data.ld_files_view.set_top(index_vl);
lnav_data.ld_files_view.set_selection(index_vl);
}
}
}
} else {
alerter::singleton().chime();
}

View File

@ -212,6 +212,9 @@ public:
vis_line_t height;
this->get_dimensions(height, width);
if (height <= 0) {
return;
}
if (this->lv_selection >= (this->lv_top + height - 1)) {
this->set_top(this->lv_selection - height + 2_vl, true);
} else if (this->lv_selection < this->lv_top) {
@ -359,9 +362,12 @@ public:
/** @return The line number that is displayed at the bottom. */
vis_line_t get_bottom() const
{
vis_line_t retval = this->lv_top;
auto retval = this->lv_top;
auto avail = this->rows_available(retval, RD_DOWN);
retval += vis_line_t(this->rows_available(retval, RD_DOWN) - 1);
if (avail > 0) {
retval += vis_line_t(avail - 1);
}
return retval;
};

View File

@ -1289,6 +1289,7 @@ static bool handle_key(int ch) {
case LNM_FILTER:
if (ch == 'F') {
lnav_data.ld_last_config_mode = LNM_FILES;
lnav_data.ld_mode = LNM_FILES;
lnav_data.ld_files_view.reload_data();
} else if (!lnav_data.ld_filter_view.handle_key(ch)) {
@ -1297,6 +1298,7 @@ static bool handle_key(int ch) {
break;
case LNM_FILES:
if (ch == 'T') {
lnav_data.ld_last_config_mode = LNM_FILTER;
lnav_data.ld_mode = LNM_FILTER;
lnav_data.ld_filter_view.reload_data();
} else if (!lnav_data.ld_files_view.handle_key(ch)) {

View File

@ -265,6 +265,7 @@ struct _lnav_data {
unsigned long ld_flags;
WINDOW * ld_window;
ln_mode_t ld_mode;
ln_mode_t ld_last_config_mode{LNM_FILTER};
statusview_curses ld_status[LNS__MAX];
top_status_source ld_top_source;

239
src/textfile_sub_source.cc Normal file
View File

@ -0,0 +1,239 @@
/**
* 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.
*/
#include "config.h"
#include "textfile_sub_source.hh"
size_t textfile_sub_source::text_line_count()
{
size_t retval = 0;
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lfo->lfo_filter_state.tfs_index.size();
}
return retval;
}
void textfile_sub_source::text_value_for_line(textview_curses &tc, int line,
std::string &value_out,
text_sub_source::line_flags_t flags)
{
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
auto read_result = lf->read_line(lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
if (read_result.isOk()) {
value_out = to_string(read_result.unwrap());
}
}
else {
value_out.clear();
}
}
void textfile_sub_source::text_attrs_for_line(textview_curses &tc, int row,
string_attrs_t &value_out)
{
if (this->current_file() == nullptr) {
return;
}
struct line_range lr;
lr.lr_start = 0;
lr.lr_end = -1;
value_out.push_back(string_attr(lr, &logline::L_FILE, this->current_file().get()));
}
size_t textfile_sub_source::text_size_for_line(textview_curses &tc, int line,
text_sub_source::line_flags_t flags)
{
size_t retval = 0;
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lf->line_length(lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
}
return retval;
}
void textfile_sub_source::to_front(const std::shared_ptr<logfile>& lf)
{
auto iter = std::find(this->tss_files.begin(),
this->tss_files.end(),
lf);
if (iter != this->tss_files.end()) {
this->tss_files.erase(iter);
} else {
iter = std::find(this->tss_hidden_files.begin(),
this->tss_hidden_files.end(),
lf);
if (iter != this->tss_hidden_files.end()) {
this->tss_hidden_files.erase(iter);
lf->show();
}
}
this->tss_files.push_front(lf);
this->tss_view->reload_data();
}
void textfile_sub_source::rotate_left()
{
if (this->tss_files.size() > 1) {
this->tss_files.push_back(this->tss_files.front());
this->tss_files.pop_front();
this->tss_view->reload_data();
this->tss_view->redo_search();
}
}
void textfile_sub_source::rotate_right()
{
if (this->tss_files.size() > 1) {
this->tss_files.push_front(this->tss_files.back());
this->tss_files.pop_back();
this->tss_view->reload_data();
this->tss_view->redo_search();
}
}
void textfile_sub_source::remove(const std::shared_ptr<logfile>& lf)
{
auto iter = std::find(this->tss_files.begin(),
this->tss_files.end(), lf);
if (iter != this->tss_files.end()) {
this->tss_files.erase(iter);
detach_observer(lf);
} else {
iter = std::find(this->tss_hidden_files.begin(),
this->tss_hidden_files.end(),
lf);
if (iter != this->tss_hidden_files.end()) {
this->tss_hidden_files.erase(iter);
detach_observer(lf);
}
}
}
void textfile_sub_source::push_back(const std::shared_ptr<logfile>& lf)
{
auto *lfo = new line_filter_observer(this->get_filters(), lf);
lf->set_logline_observer(lfo);
if (lf->is_visible()) {
this->tss_files.push_back(lf);
} else {
this->tss_hidden_files.push_back(lf);
}
}
void textfile_sub_source::text_filters_changed()
{
for (auto iter = this->tss_files.begin();
iter != this->tss_files.end();) {
if ((*iter)->is_visible()) {
++iter;
} else {
this->tss_hidden_files.push_back(*iter);
iter = this->tss_files.erase(iter);
}
}
for (auto iter = this->tss_hidden_files.begin();
iter != this->tss_hidden_files.end();) {
if (!(*iter)->is_visible()) {
++iter;
} else {
this->tss_files.push_back(*iter);
iter = this->tss_hidden_files.erase(iter);
}
}
std::shared_ptr<logfile> lf = this->current_file();
if (lf == nullptr) {
return;
}
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
uint32_t filter_in_mask, filter_out_mask;
lfo->clear_deleted_filter_state();
lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
lfo->lfo_filter_state.tfs_index.clear();
for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
if (this->tss_apply_filters &&
lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
continue;
}
lfo->lfo_filter_state.tfs_index.push_back(lpc);
}
this->tss_view->redo_search();
}
int textfile_sub_source::get_filtered_count() const
{
std::shared_ptr<logfile> lf = this->current_file();
int retval = 0;
if (lf != nullptr) {
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
}
return retval;
}
int textfile_sub_source::get_filtered_count_for(size_t filter_index) const
{
std::shared_ptr<logfile> lf = this->current_file();
if (lf == nullptr) {
return 0;
}
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
}
text_format_t textfile_sub_source::get_text_format() const
{
if (this->tss_files.empty()) {
return text_format_t::TF_UNKNOWN;
}
return this->tss_files.front()->get_text_format();
}

View File

@ -30,7 +30,7 @@
#ifndef textfile_sub_source_hh
#define textfile_sub_source_hh
#include <list>
#include <deque>
#include "logfile.hh"
#include "textview_curses.hh"
@ -39,7 +39,7 @@
class textfile_sub_source
: public text_sub_source, public vis_location_history {
public:
typedef std::list<std::shared_ptr<logfile>>::iterator file_iterator;
typedef std::deque<std::shared_ptr<logfile>>::iterator file_iterator;
textfile_sub_source() {
this->tss_supports_filtering = true;
@ -53,18 +53,7 @@ public:
return this->tss_files.size();
}
size_t text_line_count()
{
size_t retval = 0;
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lfo->lfo_filter_state.tfs_index.size();
}
return retval;
};
size_t text_line_count();
size_t text_line_width(textview_curses &curses) {
return this->tss_files.empty() ? 0 : this->current_file()->get_longest_line_length();
@ -73,47 +62,13 @@ public:
void text_value_for_line(textview_curses &tc,
int line,
std::string &value_out,
line_flags_t flags)
{
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
auto read_result = lf->read_line(lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
if (read_result.isOk()) {
value_out = to_string(read_result.unwrap());
}
}
else {
value_out.clear();
}
};
line_flags_t flags);
void text_attrs_for_line(textview_curses &tc,
int row,
string_attrs_t &value_out)
{
if (this->current_file() == nullptr) {
return;
}
string_attrs_t &value_out);
struct line_range lr;
lr.lr_start = 0;
lr.lr_end = -1;
value_out.push_back(string_attr(lr, &logline::L_FILE, this->current_file().get()));
};
size_t text_size_for_line(textview_curses &tc, int line, line_flags_t flags) {
size_t retval = 0;
if (!this->tss_files.empty()) {
std::shared_ptr<logfile> lf = this->current_file();
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lf->line_length(lf->begin() + lfo->lfo_filter_state.tfs_index[line]);
}
return retval;
};
size_t text_size_for_line(textview_curses &tc, int line, line_flags_t flags);
std::shared_ptr<logfile> current_file() const
{
@ -132,46 +87,18 @@ public:
return this->tss_files.front()->get_filename();
};
void to_front(std::shared_ptr<logfile> lf) {
this->tss_files.remove(lf);
this->tss_files.push_front(lf);
this->tss_view->reload_data();
};
void to_front(const std::shared_ptr<logfile>& lf);
void rotate_left() {
if (this->tss_files.size() > 1) {
this->tss_files.push_back(this->tss_files.front());
this->tss_files.pop_front();
this->tss_view->reload_data();
this->tss_view->redo_search();
}
};
void rotate_left();
void rotate_right() {
if (this->tss_files.size() > 1) {
this->tss_files.push_front(this->tss_files.back());
this->tss_files.pop_back();
this->tss_view->reload_data();
this->tss_view->redo_search();
}
};
void rotate_right();
void remove(std::shared_ptr<logfile> lf) {
auto iter = std::find(this->tss_files.begin(),
this->tss_files.end(), lf);
if (iter != this->tss_files.end()) {
this->tss_files.erase(iter);
detach_observer(lf);
}
};
void remove(const std::shared_ptr<logfile>& lf);
void push_back(std::shared_ptr<logfile> lf) {
auto *lfo = new line_filter_observer(this->get_filters(), lf);
lf->set_logline_observer(lfo);
this->tss_files.push_back(lf);
};
void push_back(const std::shared_ptr<logfile>& lf);
template<class T> bool rescan_files(T &callback) {
template<class T>
bool rescan_files(T &callback) {
file_iterator iter;
bool retval = false;
@ -213,7 +140,7 @@ public:
uint32_t filter_in_mask, filter_out_mask;
this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
for (uint32_t lpc = old_size; lpc < lf->size(); lpc++) {
if (this->tss_apply_filters &&
lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
@ -240,61 +167,13 @@ public:
return retval;
};
void text_filters_changed() {
std::shared_ptr<logfile> lf = this->current_file();
void text_filters_changed();
if (lf == nullptr) {
return;
}
int get_filtered_count() const;
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
uint32_t filter_in_mask, filter_out_mask;
int get_filtered_count_for(size_t filter_index) const;
lfo->clear_deleted_filter_state();
lf->reobserve_from(lf->begin() + lfo->get_min_count(lf->size()));
this->get_filters().get_enabled_mask(filter_in_mask, filter_out_mask);
lfo->lfo_filter_state.tfs_index.clear();
for (uint32_t lpc = 0; lpc < lf->size(); lpc++) {
if (this->tss_apply_filters &&
lfo->excluded(filter_in_mask, filter_out_mask, lpc)) {
continue;
}
lfo->lfo_filter_state.tfs_index.push_back(lpc);
}
this->tss_view->redo_search();
};
int get_filtered_count() const {
std::shared_ptr<logfile> lf = this->current_file();
int retval = 0;
if (lf != nullptr) {
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
retval = lf->size() - lfo->lfo_filter_state.tfs_index.size();
}
return retval;
}
int get_filtered_count_for(size_t filter_index) const {
std::shared_ptr<logfile> lf = this->current_file();
if (lf == nullptr) {
return 0;
}
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
return lfo->lfo_filter_state.tfs_filter_hits[filter_index];
}
text_format_t get_text_format() const {
if (this->tss_files.empty()) {
return text_format_t::TF_UNKNOWN;
}
return this->tss_files.front()->get_text_format();
}
text_format_t get_text_format() const;
nonstd::optional<location_history *> get_location_history()
{
@ -303,12 +182,13 @@ public:
private:
void detach_observer(std::shared_ptr<logfile> lf) {
line_filter_observer *lfo = (line_filter_observer *) lf->get_logline_observer();
auto *lfo = (line_filter_observer *) lf->get_logline_observer();
lf->set_logline_observer(nullptr);
delete lfo;
};
std::list<std::shared_ptr<logfile>> tss_files;
std::deque<std::shared_ptr<logfile>> tss_files;
std::deque<std::shared_ptr<logfile>> tss_hidden_files;
};
#endif

View File

@ -17,11 +17,19 @@ run_test ${lnav_test} -n -d /tmp/lnav.err \
-c ":hide-file" \
${test_dir}/logfile_access_log.*
check_output "prev-location is not working" <<EOF
check_output "hide-file with log file does not work" <<EOF
10.112.81.15 - - [15/Feb/2013:06:00:31 +0000] "-" 400 0 "-" "-"
EOF
run_test ${lnav_test} -n -d /tmp/lnav.err \
-c ":hide-file" \
${test_dir}/textfile_json_indented.0
check_output "hide-file with text file does not work" <<EOF
EOF
run_test ${lnav_test} -n -d /tmp/lnav.err \
-c ":goto 0" \
-c ":next-mark error" \