Skip to content

Commit

Permalink
[prompt] some cleanup for field completions
Browse files Browse the repository at this point in the history
  • Loading branch information
tstack committed Feb 18, 2025
1 parent fd40fc9 commit f321d6c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/cmd.parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ parse_for(mode_t mode,
}
case help_parameter_format_t::HPF_STRING:
case help_parameter_format_t::HPF_FILENAME:
case help_parameter_format_t::HPF_DIRECTORY:
case help_parameter_format_t::HPF_LOADED_FILE:
case help_parameter_format_t::HPF_FORMAT_FIELD:
case help_parameter_format_t::HPF_NUMERIC_FIELD:
case help_parameter_format_t::HPF_TIMEZONE: {
if (!param.ht_enum_values.empty()) {
auto enum_iter = std::find(param.ht_enum_values.begin(),
Expand Down
1 change: 1 addition & 0 deletions src/help_text.hh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ enum class help_parameter_format_t {
HPF_FILENAME,
HPF_LOADED_FILE,
HPF_FORMAT_FIELD,
HPF_NUMERIC_FIELD,
HPF_DIRECTORY,
HPF_TIME_FILTER_POINT,
HPF_TIMEZONE,
Expand Down
62 changes: 58 additions & 4 deletions src/lnav.prompt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ prompt::get_cmd_parameter_completion(textview_curses& tc,
}
return retval;
}
case help_parameter_format_t::HPF_FILENAME: {
case help_parameter_format_t::HPF_FILENAME:
case help_parameter_format_t::HPF_DIRECTORY: {
auto str_as_path = std::filesystem::path{str};
auto parent = str_as_path.parent_path();
std::vector<std::string> poss_paths;
Expand All @@ -494,6 +495,10 @@ prompt::get_cmd_parameter_completion(textview_curses& tc,
auto path_str = entry.path().string();
if (entry.is_directory()) {
path_str.push_back('/');
} else if (ht->ht_format
== help_parameter_format_t::HPF_DIRECTORY)
{
continue;
}
poss_paths.emplace_back(path_str);
}
Expand Down Expand Up @@ -535,9 +540,58 @@ prompt::get_cmd_parameter_completion(textview_curses& tc,
case help_parameter_format_t::HPF_FORMAT_FIELD: {
std::unordered_set<std::string> field_names;

for (const auto& format : log_format::get_root_formats()) {
for (const auto& lvm : format->get_value_metadata()) {
field_names.emplace(lvm.lvm_name.to_string());
tc.map_top_row([&field_names](const auto& al) {
auto attr_opt = get_string_attr(al.al_attrs, SA_FORMAT);
if (attr_opt) {
auto format_name = attr_opt->get();
auto format
= log_format::find_root_format(format_name.c_str());
for (const auto& lvm : format->get_value_metadata()) {
field_names.emplace(lvm.lvm_name.to_string());
}
}
return std::nullopt;
});

if (field_names.empty()) {
for (const auto& format : log_format::get_root_formats()) {
for (const auto& lvm : format->get_value_metadata()) {
field_names.emplace(lvm.lvm_name.to_string());
}
}
}

return field_names | lnav::itertools::similar_to(str, 10)
| lnav::itertools::map([](const auto& x) {
return attr_line_t().append(x).with_attr_for_all(
SUBST_TEXT.value(x + " "));
});
}
case help_parameter_format_t::HPF_NUMERIC_FIELD: {
std::unordered_set<std::string> field_names;

tc.map_top_row([&field_names](const auto& al) {
auto attr_opt = get_string_attr(al.al_attrs, SA_FORMAT);
if (attr_opt) {
auto format_name = attr_opt->get();
auto format
= log_format::find_root_format(format_name.c_str());
for (const auto& lvm : format->get_value_metadata()) {
if (lvm.is_numeric()) {
field_names.emplace(lvm.lvm_name.to_string());
}
}
}
return std::nullopt;
});

if (field_names.empty()) {
for (const auto& format : log_format::get_root_formats()) {
for (const auto& lvm : format->get_value_metadata()) {
if (lvm.is_numeric()) {
field_names.emplace(lvm.lvm_name.to_string());
}
}
}
}

Expand Down
29 changes: 16 additions & 13 deletions src/lnav_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3489,9 +3489,7 @@ com_spectrogram(exec_context& ec,
{
std::string retval;

if (args.empty()) {
args.emplace_back("numeric-colname");
} else if (ec.ec_dry_run) {
if (ec.ec_dry_run) {
retval = "";
} else if (args.size() == 2) {
auto colname = remaining_args(cmdline, args);
Expand Down Expand Up @@ -4480,17 +4478,22 @@ readline_context::command_t STD_COMMANDS[] = {
"builtin default",
"/ui/clock-format"})
.with_tags({"configuration"})},
{"spectrogram",
com_spectrogram,
{
"spectrogram",
com_spectrogram,

help_text(":spectrogram")
.with_summary("Visualize the given message field or database column "
"using a spectrogram")
.with_parameter(help_text(
"field-name", "The name of the numeric field to visualize."))
.with_example({"To visualize the sc_bytes field in the "
"access_log format",
"sc_bytes"})},
help_text(":spectrogram")
.with_summary(
"Visualize the given message field or database column "
"using a spectrogram")
.with_parameter(
help_text("field-name",
"The name of the numeric field to visualize.")
.with_format(help_parameter_format_t::HPF_NUMERIC_FIELD))
.with_example({"To visualize the sc_bytes field in the "
"access_log format",
"sc_bytes"}),
},
{"quit",
com_quit,

Expand Down
14 changes: 14 additions & 0 deletions src/log_format.hh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ struct logline_value_meta {
return this->lvm_hidden;
}

bool is_numeric() const
{
if (this->lvm_identifier || this->lvm_foreign_key) {
return false;
}
switch (this->lvm_kind) {
case value_kind_t::VALUE_FLOAT:
case value_kind_t::VALUE_INTEGER:
return true;
default:
return false;
}
}

logline_value_meta& with_struct_name(intern_string_t name)
{
this->lvm_struct_name = name;
Expand Down

0 comments on commit f321d6c

Please sign in to comment.