Skip to content

Commit

Permalink
Implement the FTCS_PROMPT sequence for marking the start of the prompt (
Browse files Browse the repository at this point in the history
#13163)

Implements the **FTCS_PROMPT** sequence, `OSC 133 ; A ST`. In this PR, it's just used to set a simple Prompt mark on the current line, in the same way that the iTerm2 sequence works.

There's rumination in #11000 on how to implement the rest of the FTCS sequences. 

This is broken into its own PR at the moment. [Quoth j4james](#12948 (comment)):

> That should be just as easy, and I've noticed a couple of other terminals that are doing that, so it's not unprecedented. If we don't have any immediate use for the other options, there shouldn't be any harm in ignoring them initially.
> 
> And the benefit of going with the more widely supported sequence is that we're more likely to benefit from any shells that have this functionality built in. Otherwise they're forced to try and detect the terminal, which is practically impossible for Windows Terminal. Even iTerm2 supports the `OSC 133` sequence, so we'd probably be the only odd one out.

This part of the plumbing is super easy, so I thought it would be valuable to add regardless if we get to the whole of FTCS in 1.15.

* [x] I work here
* [x] Tested manually - in my pwsh `$PROFILE`:
  ```pwsh
  function prompt {
    $loc = $($executionContext.SessionState.Path.CurrentLocation);
    $out = "PS $loc$('>' * ($nestedPromptLevel + 1)) ";
    $out += "$([char]27)]9;9;`"$loc`"$([char]07)";
    $out += "$([char]27)]133;A;$([char]7)"; # add the FTCS_PROMPT to the... well, end, but you get the point
    return $out
  }
  ```
* See also #11000
  • Loading branch information
zadjii-msft authored Jun 9, 2022
1 parent 205c09c commit f685720
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ EDDC
Enum'd
Fitt
formattings
FTCS
ftp
fvar
gcc
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/adapter/ITermDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class Microsoft::Console::VirtualTerminal::ITermDispatch

virtual bool DoITerm2Action(const std::wstring_view string) = 0;

virtual bool DoFinalTermAction(const std::wstring_view string) = 0;

virtual StringHandler DownloadDRCS(const VTInt fontNumber,
const VTParameter startChar,
const DispatchTypes::DrcsEraseControl eraseControl,
Expand Down
50 changes: 50 additions & 0 deletions src/terminal/adapter/adaptDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,56 @@ bool AdaptDispatch::DoITerm2Action(const std::wstring_view string)
return false;
}

// Method Description:
// - Performs a FinalTerm action
// - Currently, the actions we support are:
// * `OSC133;A`: mark a line as a prompt line
// - Not actually used in conhost
// - The remainder of the FTCS prompt sequences are tracked in GH#11000
// Arguments:
// - string: contains the parameters that define which action we do
// Return Value:
// - false in conhost, true for the SetMark action, otherwise false.
bool AdaptDispatch::DoFinalTermAction(const std::wstring_view string)
{
// This is not implemented in conhost.
if (_api.IsConsolePty())
{
// Flush the frame manually, to make sure marks end up on the right line, like the alt buffer sequence.
_renderer.TriggerFlush(false);
return false;
}

if constexpr (!Feature_ScrollbarMarks::IsEnabled())
{
return false;
}

const auto parts = Utils::SplitString(string, L';');

if (parts.size() < 1)
{
return false;
}

const auto action = til::at(parts, 0);

if (action == L"A") // FTCS_PROMPT
{
// Simply just mark this line as a prompt line.
DispatchTypes::ScrollMark mark;
mark.category = DispatchTypes::MarkCategory::Prompt;
_api.AddMark(mark);
return true;
}

// When we add the rest of the FTCS sequences (GH#11000), we should add a
// simple state machine here to track the most recently emitted mark from
// this set of sequences, and which sequence was emitted last, so we can
// modify the state of that mark as we go.
return false;
}

// Method Description:
// - DECDLD - Downloads one or more characters of a dynamically redefinable
// character set (DRCS) with a specified pixel pattern. The pixel array is
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/adapter/adaptDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ namespace Microsoft::Console::VirtualTerminal

bool DoITerm2Action(const std::wstring_view string) override;

bool DoFinalTermAction(const std::wstring_view string) override;

StringHandler DownloadDRCS(const VTInt fontNumber,
const VTParameter startChar,
const DispatchTypes::DrcsEraseControl eraseControl,
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/adapter/termDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class Microsoft::Console::VirtualTerminal::TermDispatch : public Microsoft::Cons

bool DoITerm2Action(const std::wstring_view /*string*/) override { return false; }

bool DoFinalTermAction(const std::wstring_view /*string*/) override { return false; }

StringHandler DownloadDRCS(const VTInt /*fontNumber*/,
const VTParameter /*startChar*/,
const DispatchTypes::DrcsEraseControl /*eraseControl*/,
Expand Down
5 changes: 5 additions & 0 deletions src/terminal/parser/OutputStateMachineEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@ bool OutputStateMachineEngine::ActionOscDispatch(const wchar_t /*wch*/,
success = _dispatch->DoITerm2Action(string);
break;
}
case OscActionCodes::FinalTermAction:
{
success = _dispatch->DoFinalTermAction(string);
break;
}
default:
// If no functions to call, overall dispatch was a failure.
success = false;
Expand Down
1 change: 1 addition & 0 deletions src/terminal/parser/OutputStateMachineEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace Microsoft::Console::VirtualTerminal
ResetForegroundColor = 110, // Not implemented
ResetBackgroundColor = 111, // Not implemented
ResetCursorColor = 112,
FinalTermAction = 133,
ITerm2Action = 1337,
};

Expand Down

0 comments on commit f685720

Please sign in to comment.