Skip to content

Commit

Permalink
Strip "Operating System Command" - OSC from build process output
Browse files Browse the repository at this point in the history
  • Loading branch information
eranif committed Jan 14, 2025
1 parent 50349b6 commit 52a3a41
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
42 changes: 42 additions & 0 deletions CodeLite/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,48 @@ void StringUtils::StripTerminalColouring(const std::string& buffer, std::string&
modbuffer.shrink_to_fit();
}

namespace
{
wxChar SafeGetChar(const wxString& buf, size_t pos)
{
if (pos >= buf.length()) {
return '\0';
}
return buf[pos];
}
} // namespace

// see : https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
// Operating System Command (OSC)
wxString StringUtils::StripTerminalOSC(const wxString& buffer)
{
wxString output;
output.reserve(buffer.length());

short state = BUFF_STATE_NORMAL;
for (size_t i = 0; i < buffer.length(); ++i) {
wxChar ch = SafeGetChar(buffer, i);
wxChar next_ch = SafeGetChar(buffer, i + 1);
switch (state) {
case BUFF_STATE_NORMAL:
if (ch == 0x1B && next_ch == ']') {
state = BUFF_STATE_IN_OSC;
i++;
} else {
output << ch;
}
break;
case BUFF_STATE_IN_OSC:
if (ch == '\a') {
// BELL, leave the current state
state = BUFF_STATE_NORMAL;
}
break;
}
}
return output;
}

void StringUtils::StripTerminalColouring(const wxString& buffer, wxString& modbuffer)
{
std::string source = ToStdString(buffer);
Expand Down
14 changes: 8 additions & 6 deletions CodeLite/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#ifndef STRINGUTILS_H
#define STRINGUTILS_H
#pragma once

#include "AsyncProcess/asyncprocess.h"
#include "codelite_exports.h"
Expand Down Expand Up @@ -61,6 +60,11 @@ class WXDLLIMPEXP_CL StringUtils
*/
static void StripTerminalColouring(const wxString& buffer, wxString& modbuffer);

/**
* @brief strip ANSI OSC ("Operating System Command sequences")sequence from `buffer`
*/
static wxString StripTerminalOSC(const wxString& buffer);

/**
* @brief add backslash to markdown styling characters
*/
Expand Down Expand Up @@ -124,13 +128,11 @@ class WXDLLIMPEXP_CL StringUtils
wxArrayString arr;
std::unordered_set<wxString> unique_set;
arr.reserve(container.size());
for(const auto& str : container) {
if(unique_set.insert(str).second) {
for (const auto& str : container) {
if (unique_set.insert(str).second) {
arr.Add(str);
}
}
return arr;
}
};

#endif // STRINGUTILS_H
3 changes: 3 additions & 0 deletions LiteEditor/BuildTabView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ wxString BuildTabView::Add(const wxString& output, bool process_last_line)
}
line.Trim();

// Remove unwanted ANSI OSC escape sequences
line = StringUtils::StripTerminalOSC(line);

// easy path: check for common makefile messages
wxString lcLine = line.Lower();
if (lcLine.Contains("entering directory") || lcLine.Contains("leaving directory")) {
Expand Down
4 changes: 3 additions & 1 deletion Plugin/wxTerminalCtrl/wxTerminalOutputCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ wxTerminalOutputCtrl::~wxTerminalOutputCtrl()
void wxTerminalOutputCtrl::AppendText(const wxString& buffer)
{
EditorEnabler d{ m_ctrl };
m_ctrl->AppendText(buffer);

// Remove unwanted ANSI OSC escape sequences
m_ctrl->AppendText(StringUtils::StripTerminalOSC(buffer));
RequestScrollToEnd();
}

Expand Down
3 changes: 2 additions & 1 deletion docs/docs/release_notes/v19_0_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ As always, all downloads are available through our [download page][1]
- Configurable "Find next"/"Find previous" behaviour when the "Find" dialog is not visible (#3570)
- Find dialog: user can now use the arrows (up/down) to toggle through the previous searches / replacements
- [CodeLite doesn't detect existing mingw-w64 installations][3]
- [Some unfiltered escape codes in Build output][4]

[1]: https://downloads.codelite.org
[2]: https://github.com/eranif/codelite/issues/3566
[3]: https://github.com/eranif/codelite/issues/3326
[4]: https://github.com/eranif/codelite/issues/3571

0 comments on commit 52a3a41

Please sign in to comment.