Skip to content

Commit d930e5b

Browse files
committed
[sql] add lnav_focused_msg view
Fixes #57
1 parent 0ba1eb8 commit d930e5b

12 files changed

+106
-14
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Features:
9797
[export of search results from Graylog](https://go2docs.graylog.org/current/interacting_with_your_log_data/export_search_results.html)
9898
can automatically be split into separate streams based on the
9999
`source` property.
100+
* Added an `lnav_focused_msg` SQL VIEW that returns a single row
101+
with the columns from the `all_logs` table for the currently
102+
focused log message. An `UPDATE` of the mutable columns will
103+
update the corresponding row in the `all_logs` table.
100104
101105
Bug Fixes:
102106
* Should start up in tmux and line drawing should show up now as well.

docs/source/sqltab.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ the following tables/views:
1919
* `lnav_view_filters`_
2020
* `lnav_view_filter_stats`_
2121
* `lnav_view_filters_and_stats`_
22+
* `lnav_top_view`_
2223
* `all_logs`_
24+
* `lnav_focused_msg`_
2325
* `http_status_codes`_
2426
* `regexp_capture(<string>, <regex>)`_
2527

@@ -265,6 +267,11 @@ lnav_view_filters_and_stats
265267
The :code:`lnav_view_filters_and_stats` view joins the :code:`lnav_view_filters`
266268
table with the :code:`lnav_view_filter_stats` table into a single view for ease of use.
267269

270+
lnav_top_view
271+
-------------
272+
273+
The :code:`lnav_top_view` view returns the row for the top view on the view stack.
274+
268275
all_logs
269276
--------
270277

@@ -274,6 +281,12 @@ The :code:`all_logs` table lets you query the format derived from the **lnav**
274281
log message parser that is used to automatically extract data, see
275282
:ref:`data-ext` for more details.
276283

284+
lnav_focused_msg
285+
----------------
286+
287+
The :code:`lnav_focused_msg` view returns the row for the focused log
288+
message from the :code:`all_logs` table.
289+
277290
http_status_codes
278291
-----------------
279292

src/base/text_format_enum.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum class text_format_t : uint8_t {
5353
TF_DIFF,
5454
TF_SHELL_SCRIPT,
5555
TF_LNAV_SCRIPT,
56+
TF_RESTRUCTURED_TEXT,
5657
TF_UNKNOWN,
5758
};
5859

src/init.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,43 @@ BEGIN
107107
WHERE name = NEW.name;
108108
END;
109109

110+
CREATE VIEW lnav_focused_msg AS
111+
SELECT *,
112+
log_part,
113+
log_actual_time,
114+
log_idle_msecs,
115+
log_mark,
116+
log_comment,
117+
log_tags,
118+
log_annotations,
119+
log_filters,
120+
log_opid,
121+
log_user_opid,
122+
log_format,
123+
log_format_regex,
124+
log_time_msecs,
125+
log_path,
126+
log_unique_path,
127+
log_text,
128+
log_body,
129+
log_raw_text,
130+
log_line_hash,
131+
log_line_link
132+
FROM all_logs
133+
WHERE log_line = log_msg_line();
134+
135+
CREATE TRIGGER lnav_focused_msg_update
136+
INSTEAD OF UPDATE ON lnav_focused_msg
137+
BEGIN
138+
UPDATE all_logs
139+
SET log_part = NEW.log_part,
140+
log_mark = NEW.log_mark,
141+
log_comment = NEW.log_comment,
142+
log_tags = NEW.log_tags,
143+
log_user_opid = NEW.log_user_opid
144+
WHERE log_line = NEW.log_line;
145+
END;
146+
110147
CREATE VIEW lnav_file_demux_metadata AS
111148
SELECT filepath, jget(content, '/demux_meta') AS metadata
112149
FROM lnav_file_metadata

src/text_format.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ constexpr string_fragment TEXT_FORMAT_STRINGS[text_format_count] = {
6060
"text/x-diff"_frag,
6161
"text/x-shellscript"_frag,
6262
"text/x-lnav-script"_frag,
63+
"text/x-rst"_frag,
6364
"text/plain"_frag,
6465
};
6566

@@ -96,6 +97,7 @@ detect_text_format(string_fragment sf,
9697
static const auto MARKDOWN_EXT = std::filesystem::path(".markdown");
9798
static const auto SH_EXT = std::filesystem::path(".sh");
9899
static const auto LNAV_EXT = std::filesystem::path(".lnav");
100+
static const auto RST_EXT = std::filesystem::path(".rst");
99101

100102
static const auto DIFF_MATCHERS = lnav::pcre2pp::code::from_const(
101103
R"(^--- .*\n\+\+\+ .*\n)", PCRE2_MULTILINE);
@@ -216,13 +218,17 @@ detect_text_format(string_fragment sf,
216218
return text_format_t::TF_MAKEFILE;
217219
}
218220

219-
if (stem == SH_EXT) {
221+
if (ext == SH_EXT) {
220222
return text_format_t::TF_SHELL_SCRIPT;
221223
}
222224

223-
if (stem == LNAV_EXT) {
225+
if (ext == LNAV_EXT) {
224226
return text_format_t::TF_LNAV_SCRIPT;
225227
}
228+
229+
if (ext == RST_EXT) {
230+
return text_format_t::TF_RESTRUCTURED_TEXT;
231+
}
226232
}
227233

228234
{

src/textinput_curses.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,18 @@ textinput_curses::handle_search_key(const ncinput& ch)
522522
}
523523
case 's':
524524
case 'S': {
525-
this->tc_search_start_point = this->tc_cursor;
526-
this->move_cursor_to_next_search_hit();
525+
if (!this->tc_search.empty()) {
526+
this->tc_search_start_point = this->tc_cursor;
527+
this->move_cursor_to_next_search_hit();
528+
}
527529
return true;
528530
}
529531
case 'r':
530532
case 'R': {
531-
this->tc_search_start_point = this->tc_cursor;
532-
this->move_cursor_to_prev_search_hit();
533+
if (!this->tc_search.empty()) {
534+
this->tc_search_start_point = this->tc_cursor;
535+
this->move_cursor_to_prev_search_hit();
536+
}
533537
return true;
534538
}
535539
}
@@ -606,6 +610,10 @@ textinput_curses::handle_search_key(const ncinput& ch)
606610
void
607611
textinput_curses::move_cursor_to_next_search_hit()
608612
{
613+
if (this->tc_search_code == nullptr) {
614+
return;
615+
}
616+
609617
auto x = this->tc_search_start_point.x;
610618
if (this->tc_search_found && !this->tc_search_found.value()) {
611619
this->tc_search_start_point.y = 0;
@@ -796,6 +804,8 @@ textinput_curses::handle_key(const ncinput& ch)
796804
{
797805
static const auto PREFIX_RE = lnav::pcre2pp::code::from_const(
798806
R"(^\s*((?:-|\*|1\.|>)(?:\s+\[( |x|X)\])?\s*))");
807+
static const auto PREFIX_OR_WS_RE = lnav::pcre2pp::code::from_const(
808+
R"(^\s*((?:-|\*|1\.|>)?(?:\s+\[( |x|X)\])?\s+))");
799809
thread_local auto md = lnav::pcre2pp::match_data::unitialized();
800810

801811
if (this->tc_notice) {
@@ -1175,11 +1185,12 @@ textinput_curses::handle_key(const ncinput& ch)
11751185
auto indent = std::string("\n");
11761186
if (!this->tc_selection) {
11771187
log_debug("checking for prefix");
1178-
auto match_opt = PREFIX_RE.capture_from(al_sf)
1188+
auto match_opt = PREFIX_OR_WS_RE.capture_from(al_sf)
11791189
.into(md)
11801190
.matches()
11811191
.ignore_error();
11821192
if (match_opt) {
1193+
log_debug("has prefix");
11831194
this->tc_selection = selected_range::from_key(
11841195
this->tc_cursor.copy_with_x(
11851196
prefix_sf.column_width()),
@@ -1195,13 +1206,18 @@ textinput_curses::handle_key(const ncinput& ch)
11951206
if (!is_comment && !al.empty()
11961207
&& match_opt->f_all.length() == al.length())
11971208
{
1209+
log_debug("clear left");
11981210
this->command_indent(indent_mode_t::clear_left);
1199-
} else {
1211+
} else if (this->is_cursor_at_end_of_line()) {
12001212
indent.append(match_opt->f_all.data(),
12011213
match_opt->f_all.length());
12021214
if (md[2] && md[2]->front() != ' ') {
12031215
indent[1 + md[2]->sf_begin] = ' ';
12041216
}
1217+
} else {
1218+
indent.append(match_opt->f_all.length(), ' ');
1219+
this->tc_selection
1220+
= selected_range::from_point(this->tc_cursor);
12051221
}
12061222
} else {
12071223
this->tc_selection
@@ -1289,7 +1305,8 @@ textinput_curses::handle_key(const ncinput& ch)
12891305
.ignore_error();
12901306

12911307
if (match_opt && !match_opt->f_all.empty()
1292-
&& match_opt->f_all.sf_end == this->tc_cursor.x) {
1308+
&& match_opt->f_all.sf_end == this->tc_cursor.x)
1309+
{
12931310
auto is_comment = al.al_attrs
12941311
| lnav::itertools::find_if([](const string_attr& sa) {
12951312
return (sa.sa_type == &VC_ROLE)
@@ -1311,12 +1328,8 @@ textinput_curses::handle_key(const ncinput& ch)
13111328
this->tc_doc_meta.m_indents.end(),
13121329
this->tc_cursor.x);
13131330
if (indent_iter != this->tc_doc_meta.m_indents.end()) {
1314-
if (indent_iter == this->tc_doc_meta.m_indents.begin())
1331+
if (indent_iter != this->tc_doc_meta.m_indents.begin())
13151332
{
1316-
this->tc_selection = selected_range::from_key(
1317-
this->tc_cursor.copy_with_x(0),
1318-
this->tc_cursor);
1319-
} else {
13201333
auto prev_indent_iter = std::prev(indent_iter);
13211334
this->tc_selection = selected_range::from_key(
13221335
this->tc_cursor.copy_with_x(*prev_indent_iter),

test/expected/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ dist_noinst_DATA = \
708708
test_sql.sh_31df37f254255115611fc321b63374a2fa4a1cd5.out \
709709
test_sql.sh_3445b783808f174b76f55dc6b998f721a1aae271.err \
710710
test_sql.sh_3445b783808f174b76f55dc6b998f721a1aae271.out \
711+
test_sql.sh_36437494301d39b745f637cc66b1dc4b515fc908.err \
712+
test_sql.sh_36437494301d39b745f637cc66b1dc4b515fc908.out \
711713
test_sql.sh_3d77a2092192caf98e141a6039e886ede836f044.err \
712714
test_sql.sh_3d77a2092192caf98e141a6039e886ede836f044.out \
713715
test_sql.sh_4090f96ea11a344c1e2939211da778992dab47d8.err \
@@ -832,6 +834,8 @@ dist_noinst_DATA = \
832834
test_sql.sh_e70dc7d2b686c7f91c2b41b10f3920c50f3ea405.out \
833835
test_sql.sh_ef3cecab4ae0b90760f728add5652378e26b2fe6.err \
834836
test_sql.sh_ef3cecab4ae0b90760f728add5652378e26b2fe6.out \
837+
test_sql.sh_fa7950f195b8cf38e111d0fd8bd04c21baebe7b9.err \
838+
test_sql.sh_fa7950f195b8cf38e111d0fd8bd04c21baebe7b9.out \
835839
test_sql.sh_fea98f976873ee7b55e6f322dda42719a19fb3f0.err \
836840
test_sql.sh_fea98f976873ee7b55e6f322dda42719a19fb3f0.out \
837841
test_sql.sh_ff8a978fc0de0fed675a3cd1454cf435a6856fd5.err \

test/expected/test_sql.sh_36437494301d39b745f637cc66b1dc4b515fc908.err

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
log_line  log_time  log_level log_msg_format log_msg_values log_part log_idle_msecs log_mark log_comment log_tags log_annotations log_filters log_part:1  log_actual_time  log_idle_msecs:1 log_mark:1 log_comment:1 log_tags:1 log_annotations:1 log_filters:1  log_opid  log_user_opid log_format log_format_regex log_time_msecs  log_path   log_unique_path   log_text  log_body  log_raw_text   log_line_hash   log_line_link  
2+
0 2009-07-20 22:59:26.000 info null <NULL>  0 0 <NULL> <NULL> <NULL> <NULL> <NULL> 2009-07-20 22:59:26.000  0  0 <NULL> <NULL> <NULL> <NULL> 192.168.202.254 <NULL> access_log std 1248130766000 {test_dir}/logfile_access_log.0 logfile_access_log.0 192.168.202.254 - - [20/Jul/2009:22:59:26 +0000] "GET /vmw/cgi/tramp HTTP/1.0" 200 134 "-" "gPXE/0.9.7" 192.168.202.254 - - [20/Jul/2009:22:59:26 +0000] "GET /vmw/cgi/tramp HTTP/1.0" 200 134 "-" "gPXE/0.9.7" v1:3f7e0f10f2473f83b2b4eacccfc9b4e2 #msg00046f2b16f0cf80-v1:3f7e0f10f2473f83b2b4eacccfc9b4e2

test/expected/test_sql.sh_fa7950f195b8cf38e111d0fd8bd04c21baebe7b9.err

Whitespace-only changes.

0 commit comments

Comments
 (0)