Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the rest of the FTCS marks #14341

Merged
19 commits merged into from
Dec 1, 2022
Merged

Conversation

zadjii-msft
Copy link
Member

As noted in #11000.

This adds support for FTCS_COMMAND_START, FTCS_COMMAND_EXECUTED and FTCS_COMMAND_FINISHED, which allow a shell to more clearly markup parts of the buffer.

As a trick, I'm also making the experimental.autoMarkPrompts setting act like a FTCS_COMMAND_EXECUTED if it comes after a FTCS_COMMAND_START. This lets the whole sequence work for cmd.exe (which wouldn't otherwise be possible).

  • My cmd prompt

    PROMPT $e]133;D$e\$e]133;A$e\$e]9;9;$P$e\[$T]$e[97;46m%_seperator%$P$e[36;49m%_seperator%$e[0m$_$e[0m%_GITPROMPT%$e[94m%username%$e[0m@$e[32m%computername%$e[0m$G$e]133;B$e\
  • pwsh profile, heavily cribbed from vscode

      $Global:__LastHistoryId = -1
      
      function Global:__Terminal-Get-LastExitCode {
        if ($? -eq $True) {
          return 0
        }
        # TODO: Should we just return a string instead?
        # return -1
        if ("$LastExitCode" -ne "") { return $LastExitCode }
        return -1
      }
      
      function prompt {
        # $gle = $LastExitCode
      
      
        $gle = $(__Terminal-Get-LastExitCode);
        $LastHistoryEntry = $(Get-History -Count 1)
        # Skip finishing the command if the first command has not yet started
        if ($Global:__LastHistoryId -ne -1) {
          if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
            # Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
            $out += "`e]133;D`a"
          } else {
            # Command finished exit code
            # OSC 633 ; D [; <ExitCode>] ST
            $out += "`e]133;D;$gle`a"
          }
        }
      
      
        $loc = $($executionContext.SessionState.Path.CurrentLocation);
        # IMPORTANT: Make sure there's a printable charater _last_ in the prompt.
        # Otherwise, PSReadline is gonna use the terminating `\` here and colorize
        # that if it detects a syntax error
        $out += "`e]133;A$([char]07)";
        $out += "`e]9;9;`"$loc`"$([char]07)";
        $out += "PWSH $loc$('>' * ($nestedPromptLevel + 1)) ";
        $out += "`e]133;B$([char]07)";
      
        $Global:__LastHistoryId = $LastHistoryEntry.Id
      
        return $out
      }
  • Doesn't close any issues, because this was always just an element in megathread: Scrollbar Marks #11000

  • I work here

  • From FHL code


if (_currentPrompt)
{
OutputStart();
Copy link
Member

@lhecker lhecker Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Both branches end up calling OutputStart at the end.

src/terminal/adapter/adaptDispatch.cpp Outdated Show resolved Hide resolved
src/terminal/adapter/adaptDispatch.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/TerminalApi.cpp Outdated Show resolved Hide resolved
@ghost ghost added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Nov 8, 2022
Copy link
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only got nits. Definitely want resolution on Leonard's comment before approving though (the dangling pointer thing): #14341 (comment)

src/cascadia/TerminalCore/Terminal.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/Terminal.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/TerminalApi.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/TerminalApi.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/TerminalApi.cpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/TerminalApi.cpp Outdated Show resolved Hide resolved
@ghost ghost removed the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Nov 11, 2022
void Terminal::AddMark(const Microsoft::Console::VirtualTerminal::DispatchTypes::ScrollMark& mark,
const til::point& start,
const til::point& end)
const til::point& end,
const bool fromUi)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that using indices would be better than using .back() and adding .insert().
Rust exposes this problem much more obviously, as you can't "borrow" foreign data easily there without introducing lifetime issues. A common solution is to instead save a numeric index (or similar) of where the data is stored at. In this case you could store a size_t _currentPromptMarkIndex which stores the index of the mark inside _scrollMarks, so that you can safely retrieve it from _scrollMarks at a later time.
It's basically exactly like the _currentPrompt pointer since it's still sort of a (relative) memory location, but unlike it, it's much easier to prove that it doesn't reference invalid data (by checking that _currentPromptMarkIndex < .size()). This way you don't need to insert() user marks at the beginning either, allowing you to remove this boolean parameter. Such a numeric index doesn't fix any "stateful" bugs of course, but it does prevent memory lifetime issues, as was the case with the raw pointer.

src/cascadia/TerminalCore/Terminal.hpp Outdated Show resolved Hide resolved
src/cascadia/TerminalCore/Terminal.hpp Outdated Show resolved Hide resolved
@@ -1576,13 +1623,19 @@ void Terminal::ClearMark()
start = til::point{ GetSelectionAnchor() };
end = til::point{ GetSelectionEnd() };
}
auto inSelection = [&start, &end](const DispatchTypes::ScrollMark& m) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I just realized that the two positions were for a selection!)

@zadjii-msft zadjii-msft added Area-VT Virtual Terminal sequence support Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Product-Terminal The new Windows Terminal. labels Nov 16, 2022
@zadjii-msft zadjii-msft added the Needs-Second It's a PR that needs another sign-off label Nov 30, 2022
@ghost ghost requested a review from PankajBhojwani November 30, 2022 19:13
@github-actions

This comment has been minimized.

// We know that "0" will be successfully parsed, and that's close enough.
unsigned int parsedError = 0;
error = Utils::StringToUint(errorString, parsedError) ? parsedError :
static_cast<unsigned int>(-1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am moderately horrified about this actually -- why is this better than UINT_MAX or whatever? Do we really want to use -1 to indicate.. uh, something? But, if we really want -1 shouldn't it be signed?

@zadjii-msft zadjii-msft added the AutoMerge Marked for automatic merge by the bot when requirements are met label Dec 1, 2022
@ghost
Copy link

ghost commented Dec 1, 2022

Hello @zadjii-msft!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 37aa295 into main Dec 1, 2022
@ghost ghost deleted the dev/migrie/f/11000-more-ftcs-marks branch December 1, 2022 22:22
@zadjii-msft zadjii-msft mentioned this pull request Dec 14, 2022
37 tasks
@ghost
Copy link

ghost commented Jan 24, 2023

🎉Windows Terminal Preview v1.17.1023 has been released which incorporates this pull request.:tada:

Handy links:

@JanDeDobbeleer
Copy link

@zadjii-msft how can I play around with this? I'm adding support to oh-my-posh for this, so I would love to validate. I'm already on the correct version (v1.17.1023).

@zadjii-msft
Copy link
Member Author

@JanDeDobbeleer I absolutely need to write a better guide, but in leiu of that,

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Area-VT Virtual Terminal sequence support AutoMerge Marked for automatic merge by the bot when requirements are met Needs-Second It's a PR that needs another sign-off Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants