Skip to content

Commit

Permalink
Propagate command execution information to the terminal (OSC133 & OSC…
Browse files Browse the repository at this point in the history
…9001)
  • Loading branch information
alabuzhev committed Jul 18, 2024
1 parent 7374303 commit bd66ca9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
10 changes: 10 additions & 0 deletions far/cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ void CommandLine::DisplayObject()

void CommandLine::DrawFakeCommand(string_view const FakeCommand)
{
console.SetCursorPosition({ m_Where.left, m_Where.top });
console.start_prompt();
DrawPrompt();

console.SetCursorPosition({ WhereX(), WhereY() });
console.start_command();
SetColor(COL_COMMANDLINE);
// TODO: wrap & scroll if too long
Text(FakeCommand);
Expand Down Expand Up @@ -1056,6 +1061,8 @@ void CommandLine::ExecString(execute_info& Info)
{
// Just scroll the screen
Activator(false);
console.start_output();
console.command_finished();
return;
}

Expand Down Expand Up @@ -1098,7 +1105,10 @@ void CommandLine::ExecString(execute_info& Info)
return;

if (ProcessOSCommands(Info.Command, Activator))
{
console.command_finished(EXIT_SUCCESS);
return;
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions far/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,37 @@ namespace console_detail

#undef SERVICE_PAGE_NUMBER

void console::start_prompt() const
{
send_vt_command(OSC("133;D"));
send_vt_command(OSC("133;A"));
}

void console::start_command() const
{
send_vt_command(OSC("133;B"));
}

void console::start_output() const
{
send_vt_command(OSC("133;C"));
}

void console::command_finished() const
{
send_vt_command(OSC("133;D"));
}

void console::command_finished(int const ExitCode) const
{
send_vt_command(far::format(OSC("133;D;{}"), ExitCode));
}

void console::command_not_found(string_view const Command) const
{
send_vt_command(far::format(OSC("9001;CmdNotFound;{}"), Command));
}

bool console::GetCursorRealPosition(point& Position) const
{
CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
Expand Down
7 changes: 7 additions & 0 deletions far/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ namespace console_detail
void stash_output() const;
void unstash_output(rectangle Coordinates) const;

void start_prompt() const;
void start_command() const;
void start_output() const;
void command_finished() const;
void command_finished(int ExitCode) const;
void command_not_found(string_view Command) const;

[[nodiscard]]
short GetDelta() const;

Expand Down
2 changes: 2 additions & 0 deletions far/console_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class context final: noncopyable, public i_context

if (SetTextColour)
console.SetTextAttributes(colors::PaletteColorToFarColor(COL_COMMANDLINEUSERSCREEN));

console.start_output();
}

void DoPrologue() override
Expand Down
28 changes: 21 additions & 7 deletions far/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,12 @@ static os::handle wait_for_process_or_detach(os::handle Process, int const Conso
return {};
}

static void log_process_exit_code(os::handle const& Process)
static void log_process_exit_code(execute_info const& Info, os::handle const& Process, bool const UsingComspec)
{
DWORD ExitCode;
if (!GetExitCodeProcess(Process.native_handle(), &ExitCode))
{
LOGWARNING(L"GetExitCodeProcess(): {}"sv, os::last_error());
LOGWARNING(L"GetExitCodeProcess({}): {}"sv, Info.Command, os::last_error());
return;
}

Expand All @@ -525,9 +525,23 @@ static void log_process_exit_code(os::handle const& Process)
LOGWARNING(L"{}"sv, os::format_error(ExitCode));
LOGWARNING(L"{}"sv, os::format_ntstatus(ExitCode));
}

console.command_finished(ExitCode);

if (UsingComspec && ExitCode == EXIT_FAILURE)
console.command_not_found(Info.Command);
}

static void after_process_creation(os::handle Process, execute_info::wait_mode const WaitMode, os::handle Thread, point const& ConsoleSize, rectangle const& ConsoleWindowRect, function_ref<void(bool)> const ConsoleActivator)
static void after_process_creation(
execute_info const& Info,
os::handle Process,
execute_info::wait_mode const WaitMode,
os::handle Thread,
point const& ConsoleSize,
rectangle const& ConsoleWindowRect,
function_ref<void(bool)> const ConsoleActivator,
bool const UsingComspec
)
{
const auto resume_process = [&](bool const Consolise)
{
Expand Down Expand Up @@ -557,14 +571,14 @@ static void after_process_creation(os::handle Process, execute_info::wait_mode c

Process = wait_for_process_or_detach(std::move(Process), KeyNameToKey(Global->Opt->ConsoleDetachKey), ConsoleSize, ConsoleWindowRect);
if (Process)
log_process_exit_code(Process);
log_process_exit_code(Info, Process, UsingComspec);
}
return;

case execute_info::wait_mode::wait_finish:
resume_process(true);
Process.wait();
log_process_exit_code(Process);
log_process_exit_code(Info, Process, UsingComspec);
return;
}
}
Expand Down Expand Up @@ -769,7 +783,7 @@ static bool execute_impl(
if (!execute_createprocess(Command, Parameters, CurrentDirectory, Info.RunAs, Info.WaitMode != execute_info::wait_mode::no_wait, pi))
return false;

after_process_creation(os::handle(pi.hProcess), Info.WaitMode, os::handle(pi.hThread), ConsoleSize, ConsoleWindowRect, ExtendedActivator);
after_process_creation(Info, os::handle(pi.hProcess), Info.WaitMode, os::handle(pi.hThread), ConsoleSize, ConsoleWindowRect, ExtendedActivator, UsingComspec);
return true;

};
Expand All @@ -796,7 +810,7 @@ static bool execute_impl(
return false;

if (Process)
after_process_creation(os::handle(Process), Info.WaitMode, {}, ConsoleSize, ConsoleWindowRect, [](bool){});
after_process_creation(Info, os::handle(Process), Info.WaitMode, {}, ConsoleSize, ConsoleWindowRect, [](bool){}, UsingComspec);
return true;
};

Expand Down
2 changes: 1 addition & 1 deletion far/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ using fuzzy_ic_searcher = fuzzy_searcher<icase_searcher>;

using searchers = std::variant
<
bool, // Just to make it default-constructible
std::monostate, // Just to make it default-constructible
exact_searcher,
icase_searcher,
fuzzy_cs_searcher,
Expand Down

0 comments on commit bd66ca9

Please sign in to comment.