[cmds] add /dev/clipboard support to :redirect-to

This commit is contained in:
Timothy Stack 2021-08-22 13:50:53 -07:00
parent c9d22e7941
commit 97530070c9
6 changed files with 30 additions and 17 deletions

1
NEWS
View File

@ -9,6 +9,7 @@ lnav v0.10.1:
* Key handling in the visual filter editor will no longer swallow
certain key-presses when editing a filter.
* Scrolling performance restored in the SQL view.
* The ':redirect-to' command now works with '/dev/clipboard'
lnav v0.10.0:
Features:

View File

@ -866,23 +866,23 @@ std::string exec_context::get_error_prefix()
return fmt::format("{}:{}: error: ", source.first, source.second);
}
void exec_context::set_output(const string &name, FILE *file)
void exec_context::set_output(const string &name, FILE *file, int (*closer)(FILE *))
{
log_info("redirecting command output to: %s", name.c_str());
this->ec_output_stack.back().second | [](auto file) {
if (file != stdout) {
fclose(file);
this->ec_output_stack.back().second | [](auto out) {
if (out.second != nullptr) {
out.second(out.first);
}
};
this->ec_output_stack.back() = std::make_pair(name, file);
this->ec_output_stack.back() = std::make_pair(name, std::make_pair(file, closer));
}
void exec_context::clear_output()
{
log_info("redirecting command output to screen");
this->ec_output_stack.back().second | [](auto file) {
if (file != stdout) {
fclose(file);
this->ec_output_stack.back().second | [](auto out) {
if (out.second != nullptr) {
out.second(out.first);
}
};
this->ec_output_stack.back() = std::make_pair("default", nonstd::nullopt);
@ -890,7 +890,7 @@ void exec_context::clear_output()
exec_context::output_guard::output_guard(exec_context &context,
std::string name,
const nonstd::optional<FILE *> &file)
const nonstd::optional<output_t> &file)
: sg_context(context) {
if (file) {
log_info("redirecting command output to: %s", name.c_str());

View File

@ -57,6 +57,8 @@ struct exec_context {
READ_ONLY,
};
using output_t = std::pair<FILE *, int(*)(FILE *)>;
exec_context(std::vector<logline_value> *line_values = nullptr,
sql_callback_t sql_callback = ::sql_callback,
pipe_callback_t pipe_callback = nullptr)
@ -95,15 +97,15 @@ struct exec_context {
for (auto iter = this->ec_output_stack.rbegin();
iter != this->ec_output_stack.rend();
++iter) {
if (iter->second && *iter->second) {
return *iter->second;
if (iter->second && (*iter->second).first) {
return (*iter->second).first;
}
}
return nonstd::nullopt;
}
void set_output(const std::string& name, FILE *file);
void set_output(const std::string& name, FILE *file, int (*closer)(FILE *));
void clear_output();
@ -122,7 +124,7 @@ struct exec_context {
struct output_guard {
explicit output_guard(exec_context &context,
std::string name = "default",
const nonstd::optional<FILE *>& file = nonstd::nullopt);
const nonstd::optional<output_t>& file = nonstd::nullopt);
~output_guard();
@ -151,7 +153,8 @@ struct exec_context {
std::map<std::string, std::string> ec_global_vars;
std::vector<ghc::filesystem::path> ec_path_stack;
std::stack<std::pair<std::string, int>> ec_source;
std::vector<std::pair<std::string, nonstd::optional<FILE *>>> ec_output_stack;
std::vector<std::pair<std::string, nonstd::optional<output_t>>> ec_output_stack;
attr_line_t ec_accumulator;

View File

@ -2835,7 +2835,7 @@ SELECT tbl_name FROM sqlite_master WHERE sql LIKE 'CREATE VIRTUAL TABLE%'
return EXIT_FAILURE;
}
init_session();
lnav_data.ld_exec_context.set_output("stdout", stdout);
lnav_data.ld_exec_context.set_output("stdout", stdout, nullptr);
alerter::singleton().enabled(false);
log_tc = &lnav_data.ld_views[LNV_LOG];

View File

@ -1400,13 +1400,22 @@ static Result<string, string> com_redirect_to(exec_context &ec, string cmdline,
if (split_args[0] == "-") {
ec.clear_output();
} else if (split_args[0] == "/dev/clipboard") {
auto out = open_clipboard(CT_GENERAL);
if (!out) {
alerter::singleton().chime();
return ec.make_error("Unable to copy to clipboard. "
"Make sure xclip or pbcopy is installed.");
}
ec.set_output(split_args[0], out, pclose);
} else {
FILE *file = fopen(split_args[0].c_str(), "w");
if (file == nullptr) {
return ec.make_error("unable to open file -- {}", split_args[0]);
}
ec.set_output(split_args[0], file);
ec.set_output(split_args[0], file, fclose);
}
return Ok("info: redirecting output to file -- " + split_args[0]);

View File

@ -677,7 +677,7 @@ static void rl_callback_int(readline_curses *rc, bool is_alt)
string path_and_args = rc->get_value();
{
exec_context::output_guard og(ec, "tmp", tmpout.release());
exec_context::output_guard og(ec, "tmp", std::make_pair(tmpout.release(), fclose));
string result = execute_file(ec, path_and_args)
.map(ok_prefix)