From 2fb77b89152f2111fda552eff0502b6b3fc2c22c Mon Sep 17 00:00:00 2001 From: Tim Stack Date: Thu, 30 Jan 2025 15:10:40 -0800 Subject: [PATCH] [session] add (commented-out) hide-fields/show-fields commands to export Related to #1352 --- NEWS.md | 7 ++-- src/session.export.cc | 69 ++++++++++++++++++++++++++++++--------- test/expected/Makefile.am | 4 +-- test/test_sessions.sh | 1 + 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3fdf67f6ac..ef7ec2f41e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -29,9 +29,12 @@ Features: adding a column with the name `__lnav_style__`. * Added "format test" management command to make it easier to test a format against a file. - Can be helpful for determining why a file is not being recognized - by particular format. + This can be helpful for determining why a file is not being + recognized by particular format. * Added a "performance" section to the documentation. +* Session exports now include `:hide-fields` and `:show-fields` + commands from the session. + They are currently commented out by default. Interface changes: * DB query results that start with a number are right justified diff --git a/src/session.export.cc b/src/session.export.cc index 0aae2ef988..af188109fe 100644 --- a/src/session.export.cc +++ b/src/session.export.cc @@ -30,6 +30,7 @@ #include "session.export.hh" #include "base/injector.hh" +#include "base/itertools.hh" #include "bound_tags.hh" #include "lnav.hh" #include "sqlitepp.client.hh" @@ -49,9 +50,9 @@ struct log_message_session_state { template<> struct from_sqlite { - inline log_message_session_state operator()(int argc, - sqlite3_value** argv, - int argi) + log_message_session_state operator()(int argc, + sqlite3_value** argv, + int argi) { return { from_sqlite()(argc, argv, argi + 0), @@ -76,9 +77,9 @@ struct log_filter_session_state { template<> struct from_sqlite { - inline log_filter_session_state operator()(int argc, - sqlite3_value** argv, - int argi) + log_filter_session_state operator()(int argc, + sqlite3_value** argv, + int argi) { return { from_sqlite()(argc, argv, argi + 0), @@ -98,9 +99,7 @@ struct log_file_session_state { template<> struct from_sqlite { - inline log_file_session_state operator()(int argc, - sqlite3_value** argv, - int argi) + log_file_session_state operator()(int argc, sqlite3_value** argv, int argi) { return { from_sqlite()(argc, argv, argi + 0), @@ -199,7 +198,7 @@ SELECT content_id, format, time_offset FROM lnav_file WHERE format IS NOT NULL AND time_offset != 0 )"; - static constexpr const char HEADER[] = R"(#!lnav -Nf + static constexpr char HEADER[] = R"(#!lnav -Nf # This file is an export of an lnav session. You can type # '|/path/to/this/file' in lnav to execute this file and # restore the state of the session. @@ -211,12 +210,12 @@ SELECT content_id, format, time_offset FROM lnav_file )"; - static constexpr const char LOG_DIR_INSERT[] = R"( + static constexpr char LOG_DIR_INSERT[] = R"( # Set this environment variable to override this value or edit this script. ;INSERT OR IGNORE INTO environ (name, value) VALUES ('LOG_DIR_{}', {}) )"; - static constexpr const char MARK_HEADER[] = R"( + static constexpr char MARK_HEADER[] = R"( # The following SQL statements will restore the bookmarks, # comments, and tags that were added in the session. @@ -224,7 +223,7 @@ SELECT content_id, format, time_offset FROM lnav_file ;SELECT total_changes() AS before_mark_changes )"; - static constexpr const char MARK_FOOTER[] = R"( + static constexpr char MARK_FOOTER[] = R"( ;SELECT {} - (total_changes() - $before_mark_changes) AS failed_mark_changes ;SELECT echoln(printf('%sERROR%s: failed to restore %d bookmarks', $ansi_red, $ansi_norm, $failed_mark_changes)) @@ -246,14 +245,20 @@ SELECT content_id, format, time_offset FROM lnav_file ;SELECT total_changes() AS before_file_changes )"; - static constexpr const char FILE_FOOTER[] = R"( + static const char* FIELD_HEADER = R"( +# The following field visibility commands were run by the +# original user during this session. Uncomment them if +# desired. +)"; + + static constexpr char FILE_FOOTER[] = R"( ;SELECT {} - (total_changes() - $before_file_changes) AS failed_file_changes ;SELECT echoln(printf('%sERROR%s: failed to restore the state of %d files', $ansi_red, $ansi_norm, $failed_file_changes)) WHERE $failed_file_changes != 0 )"; - static constexpr const char VIEW_HEADER[] = R"( + static constexpr char VIEW_HEADER[] = R"( # The following commands will restore the state of the {} view. @@ -430,6 +435,40 @@ SELECT content_id, format, time_offset FROM lnav_file FMT_STRING(":switch-to-view {}\n"), lnav_view_strings[view_index]); + if (view_index == LNV_LOG) { + std::vector field_cmds; + for (const auto& format : log_format::get_root_formats()) { + auto field_states = format->get_field_states(); + + for (const auto& fs_pair : field_states) { + if (!fs_pair.second.lvm_user_hidden) { + continue; + } + + if (fs_pair.second.lvm_user_hidden.value()) { + field_cmds.emplace_back( + fmt::format(FMT_STRING("# :hide-fields {}.{}\n"), + + format->get_name(), + fs_pair.first)); + } else if (fs_pair.second.lvm_hidden) { + field_cmds.emplace_back( + fmt::format(FMT_STRING("# :show-fields {}.{}\n"), + format->get_name().to_string(), + fs_pair.first)); + } + } + } + + if (!field_cmds.empty()) { + fmt::print(file, FMT_STRING("{}"), FIELD_HEADER); + field_cmds + | lnav::itertools::for_each([&file](const auto& cmd) { + fmt::print(file, FMT_STRING("{}"), cmd); + }); + } + } + auto* tss = tc.get_sub_source(); auto* lss = dynamic_cast(tss); if (lss != nullptr) { diff --git a/test/expected/Makefile.am b/test/expected/Makefile.am index ef0695e0ae..4c0f6fec0b 100644 --- a/test/expected/Makefile.am +++ b/test/expected/Makefile.am @@ -568,6 +568,8 @@ dist_noinst_DATA = \ test_regex101.sh_fc41b6ee90cbf038620151f16d164b361acf82dd.out \ test_sessions.sh_0300a1391c33b1c45ddfa90198a6bd0a5404a77f.err \ test_sessions.sh_0300a1391c33b1c45ddfa90198a6bd0a5404a77f.out \ + test_sessions.sh_14aae534ade7470dee4e812297dd719f0725fee4.err \ + test_sessions.sh_14aae534ade7470dee4e812297dd719f0725fee4.out \ test_sessions.sh_17b85654b929b2a8fc1705a170ced544783292fa.err \ test_sessions.sh_17b85654b929b2a8fc1705a170ced544783292fa.out \ test_sessions.sh_2ee066bea01006950407a34421e763020d36acd5.err \ @@ -598,8 +600,6 @@ dist_noinst_DATA = \ test_sessions.sh_a2b217a4102aa5cbe4952f935c5d416f02fa4cb7.out \ test_sessions.sh_a92822d121a836140a401fd71535dc4a7a8d5b48.err \ test_sessions.sh_a92822d121a836140a401fd71535dc4a7a8d5b48.out \ - test_sessions.sh_b3d71a87fcb4e3487f71ccad8c6ce681db220572.err \ - test_sessions.sh_b3d71a87fcb4e3487f71ccad8c6ce681db220572.out \ test_sessions.sh_b932b33dd087b94d4306dd179c5d4f9ddd394960.err \ test_sessions.sh_b932b33dd087b94d4306dd179c5d4f9ddd394960.out \ test_sessions.sh_ba1ded92531350668301431db64df2d2f4a2e9ee.err \ diff --git a/test/test_sessions.sh b/test/test_sessions.sh index a1b91db91f..90ec0b401f 100644 --- a/test/test_sessions.sh +++ b/test/test_sessions.sh @@ -41,6 +41,7 @@ run_cap_test ${lnav_test} -nq \ run_cap_test ${lnav_test} -nq \ -c ";update access_log set log_mark = 1 where sc_bytes > 60000" \ + -c ":hide-fields cs_user_agent" \ -c ":set-min-log-level debug" \ -c ":hide-lines-before 2005" \ -c ":hide-lines-after 2030" \