From ea5ac46c0eb9113263d81fd4a2187d7680128994 Mon Sep 17 00:00:00 2001 From: Timothy Stack Date: Thu, 11 Oct 2018 07:09:52 -0700 Subject: [PATCH] [cmds] the output of the :pipe commands should go to the top of the output stack and not to a temp file --- src/command_executor.cc | 56 +++++++++++++++++++++++++++-------------- test/test_cmds.sh | 2 ++ 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/command_executor.cc b/src/command_executor.cc index f93ee6ae..8032bdbe 100644 --- a/src/command_executor.cc +++ b/src/command_executor.cc @@ -731,29 +731,47 @@ int sql_callback(exec_context &ec, sqlite3_stmt *stmt) future pipe_callback(exec_context &ec, const string &cmdline, auto_fd &fd) { - auto pp = make_shared(fd, false); - static int exec_count = 0; - char desc[128]; + if (lnav_data.ld_output_stack.empty()) { + auto pp = make_shared(fd, false); + static int exec_count = 0; + char desc[128]; + + lnav_data.ld_pipers.push_back(pp); + snprintf(desc, + sizeof(desc), "[%d] Output of %s", + exec_count++, + cmdline.c_str()); + lnav_data.ld_file_names[desc] + .with_fd(pp->get_fd()) + .with_detect_format(false); + lnav_data.ld_files_to_front.emplace_back(desc, 0); + if (lnav_data.ld_rl_view != nullptr) { + lnav_data.ld_rl_view->set_alt_value(HELP_MSG_1( + X, "to close the file")); + } - lnav_data.ld_pipers.push_back(pp); - snprintf(desc, - sizeof(desc), "[%d] Output of %s", - exec_count++, - cmdline.c_str()); - lnav_data.ld_file_names[desc] - .with_fd(pp->get_fd()) - .with_detect_format(false); - lnav_data.ld_files_to_front.emplace_back(desc, 0); - if (lnav_data.ld_rl_view != nullptr) { - lnav_data.ld_rl_view->set_alt_value(HELP_MSG_1( - X, "to close the file")); - } + packaged_task task([]() { return ""; }); + + task(); + + return task.get_future(); + } else { + return std::async(std::launch::async, [&]() { + FILE *file = lnav_data.ld_output_stack.top(); + char buffer[1024]; + ssize_t rc; - packaged_task task([]() { return ""; }); + if (file == stdout) { + lnav_data.ld_stdout_used = true; + } - task(); + while ((rc = read(fd, buffer, sizeof(buffer))) > 0) { + fwrite(buffer, rc, 1, file); + } - return task.get_future(); + return string(); + }); + } } void add_global_vars(exec_context &ec) diff --git a/test/test_cmds.sh b/test/test_cmds.sh index 8b1cbe91..99a8219f 100644 --- a/test/test_cmds.sh +++ b/test/test_cmds.sh @@ -562,10 +562,12 @@ check_output "pipe-to is not working" <