Skip to content

Commit

Permalink
add reporting to OSC 4
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett committed Aug 17, 2024
1 parent 50d508d commit ccb10a3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/terminal/adapter/ITermDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Microsoft::Console::VirtualTerminal::ITermDispatch
virtual bool TabClear(const DispatchTypes::TabClearType clearType) = 0; // TBC
virtual bool TabSet(const VTParameter setType) = 0; // DECST8C
virtual bool SetColorTableEntry(const size_t tableIndex, const DWORD color) = 0; // OSCColorTable
virtual bool RequestColorTableEntry(const size_t tableIndex) = 0;
virtual bool SetXtermColorResource(const DispatchTypes::XtermColorResource resource, const DWORD color) = 0; // OSCDefaultForeground, OSCDefaultBackground, OSC TODO
virtual bool RequestXtermColorResource(const DispatchTypes::XtermColorResource resource) = 0;
virtual bool AssignColor(const DispatchTypes::ColorItem item, const VTInt fgIndex, const VTInt bgIndex) = 0; // DECAC
Expand Down
18 changes: 18 additions & 0 deletions src/terminal/adapter/adaptDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,24 @@ bool AdaptDispatch::SetColorTableEntry(const size_t tableIndex, const DWORD dwCo
return true;
}

bool AdaptDispatch::RequestColorTableEntry(const size_t tableIndex)
{
if (tableIndex >= TextColor::TABLE_SIZE)
{
return false;
}

const auto color = _renderSettings.GetColorTableEntry(tableIndex);
if (color != INVALID_COLOR)
{
const til::color c{ color };
// Scale values up to match xterm's 16-bit color report format.
_api.ReturnResponse(fmt::format(FMT_COMPILE(L"\033]4;{};rgb:{:04x}/{:04x}/{:04x}\033\\"), tableIndex, c.r * 0x0101, c.g * 0x0101, c.b * 0x0101));
}

return true;
}

// Method Description:
// - Sets one Xterm Color Resource such as Default Foreground, Background, Cursor
// Return Value:
Expand Down
1 change: 1 addition & 0 deletions src/terminal/adapter/adaptDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace Microsoft::Console::VirtualTerminal

bool SetColorTableEntry(const size_t tableIndex,
const DWORD color) override; // OSCColorTable
bool RequestColorTableEntry(const size_t tableIndex) override;
bool SetXtermColorResource(const DispatchTypes::XtermColorResource resource, const DWORD color) override; // OSCDefaultForeground, OSCDefaultBackground, OSCTODO

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

OSCTODO is not a recognized word. (unrecognized-spelling)
bool RequestXtermColorResource(const DispatchTypes::XtermColorResource resource) override;
bool AssignColor(const DispatchTypes::ColorItem item, const VTInt fgIndex, const VTInt bgIndex) override; // DECAC
Expand Down
1 change: 1 addition & 0 deletions src/terminal/adapter/termDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Microsoft::Console::VirtualTerminal::TermDispatch : public Microsoft::Cons
bool TabClear(const DispatchTypes::TabClearType /*clearType*/) override { return false; } // TBC
bool TabSet(const VTParameter /*setType*/) override { return false; } // DECST8C
bool SetColorTableEntry(const size_t /*tableIndex*/, const DWORD /*color*/) override { return false; } // OSCColorTable, OSCDefaultForeground, OSCDefaultBackground, OSCSetCursorColor, OSCResetCursorColor
bool RequestColorTableEntry(const size_t /*tableIndex*/) override { return false; }
bool SetXtermColorResource(const DispatchTypes::XtermColorResource /*resource*/, const DWORD /*color*/) override { return false; } // OSCDefaultForeground, OSCDefaultBackground, OSC TODO
bool RequestXtermColorResource(const DispatchTypes::XtermColorResource /*resource*/) override { return false; }
bool AssignColor(const DispatchTypes::ColorItem /*item*/, const VTInt /*fgIndex*/, const VTInt /*bgIndex*/) override { return false; } // DECAC
Expand Down
33 changes: 27 additions & 6 deletions src/terminal/parser/OutputStateMachineEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,14 @@ bool OutputStateMachineEngine::ActionOscDispatch(const size_t parameter, const s
{
const auto tableIndex = til::at(tableIndexes, i);
const auto rgb = til::at(colors, i);
success = success && _dispatch->SetColorTableEntry(tableIndex, rgb);
if (rgb == COLOR_INQUIRY_COLOR)
{
success = success && _dispatch->RequestColorTableEntry(tableIndex);
}
else
{
success = success && _dispatch->SetColorTableEntry(tableIndex, rgb);
}
}
break;
}
Expand Down Expand Up @@ -918,6 +925,8 @@ bool OutputStateMachineEngine::_GetOscSetColorTable(const std::wstring_view stri
std::vector<size_t>& tableIndexes,
std::vector<DWORD>& rgbs) const
{
using namespace std::string_view_literals;

const auto parts = Utils::SplitString(string, L';');
if (parts.size() < 2)
{
Expand All @@ -929,13 +938,25 @@ bool OutputStateMachineEngine::_GetOscSetColorTable(const std::wstring_view stri

for (size_t i = 0, j = 1; j < parts.size(); i += 2, j += 2)
{
auto&& index = til::at(parts, i);
auto&& color = til::at(parts, j);
unsigned int tableIndex = 0;
const auto indexSuccess = Utils::StringToUint(til::at(parts, i), tableIndex);
const auto colorOptional = Utils::ColorFromXTermColor(til::at(parts, j));
if (indexSuccess && colorOptional.has_value())
const auto indexSuccess = Utils::StringToUint(index, tableIndex);

if (indexSuccess)
{
newTableIndexes.push_back(tableIndex);
newRgbs.push_back(colorOptional.value());
if (color == L"?"sv) [[unlikely]]
{
newTableIndexes.push_back(tableIndex);
newRgbs.push_back(COLOR_INQUIRY_COLOR);
continue;
}

if (const auto colorOptional = Utils::ColorFromXTermColor(color))
{
newTableIndexes.push_back(tableIndex);
newRgbs.push_back(colorOptional.value());
}
}
}

Expand Down

0 comments on commit ccb10a3

Please sign in to comment.