Skip to content

Commit

Permalink
terminal control: when copying text, strip the terminal colour escape…
Browse files Browse the repository at this point in the history
… codes

Signed-off-by: Eran Ifrah <[email protected]>
  • Loading branch information
eranif committed Dec 10, 2024
1 parent c1b8bbf commit f6e9e1e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Plugin/wxTerminalCtrl/wxTerminalCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class WXDLLIMPEXP_SDK wxTerminalCtrl : public wxPanel
const wxString& GetShellCommand() const { return m_shellCommand; }

/**
* @brief execute a command in the temrinal
* @brief execute a command in the terminal
* @param command
*/
void Run(const wxString& command);
Expand Down
82 changes: 79 additions & 3 deletions Plugin/wxTerminalCtrl/wxTerminalOutputCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "wxTerminalInputCtrl.hpp"

#include <wx/menu.h>
#include <wx/msgdlg.h>
#include <wx/sizer.h>
#include <wx/uiaction.h>
#include <wx/wupdlock.h>
Expand All @@ -41,6 +42,24 @@ class MyEventsHandler : public clEditEventsHandler
CHECK_PTR_RET(m_input_ctrl);
m_input_ctrl->Paste();
}

void OnCopy(wxCommandEvent& event) override
{
CHECK_FOCUS_WINDOW();
CHECK_PTR_RET(m_stc);
if (!m_stc->CanCopy()) {
return;
}

wxString text = m_stc->GetSelectedText();
if (text.empty()) {
return;
}
wxString modbuffer;
StringUtils::StripTerminalColouring(text, modbuffer);

::CopyToClipboard(modbuffer);
}
};

struct EditorEnabler {
Expand All @@ -62,8 +81,11 @@ wxTerminalOutputCtrl::wxTerminalOutputCtrl(wxWindow* parent, wxWindowID winid)
m_editEvents = std::make_unique<MyEventsHandler>(nullptr, m_ctrl);
}

wxTerminalOutputCtrl::wxTerminalOutputCtrl(wxTerminalCtrl* parent, wxWindowID winid, const wxFont& font,
const wxColour& bg_colour, const wxColour& text_colour)
wxTerminalOutputCtrl::wxTerminalOutputCtrl(wxTerminalCtrl* parent,
wxWindowID winid,
const wxFont& font,
const wxColour& bg_colour,
const wxColour& text_colour)
: wxWindow(parent, winid)
, m_terminal(parent)
{
Expand All @@ -87,7 +109,8 @@ void wxTerminalOutputCtrl::Initialise(const wxFont& font, const wxColour& bg_col
m_ctrl->SetMarginWidth(i, 0);
}

m_ctrl->UsePopUp(1);
m_ctrl->UsePopUp(0);
m_ctrl->Bind(wxEVT_CONTEXT_MENU, &wxTerminalOutputCtrl::OnMenu, this);
m_ctrl->SetLexer(wxSTC_LEX_CONTAINER);
m_ctrl->SetWrapMode(wxSTC_WRAP_CHAR);
m_ctrl->SetEditable(false);
Expand Down Expand Up @@ -414,3 +437,56 @@ void wxTerminalOutputCtrl::DoPatternClicked(const wxString& pattern)
};
clGetManager()->OpenFileAndAsyncExecute(file, std::move(cb));
}

void wxTerminalOutputCtrl::OnMenu(wxContextMenuEvent& event)
{
wxUnusedVar(event);
wxMenu menu;
menu.Append(wxID_COPY);
menu.Append(XRCID("copy-with-ansi-colors"), _("Copy with Terminal Colours"));
menu.AppendSeparator();
menu.Append(wxID_SELECTALL);
menu.AppendSeparator();
menu.Append(wxID_CLEAR);

menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
wxString text = m_ctrl->GetSelectedText();
if (text.empty()) {
return;
}

wxString modbuffer;
StringUtils::StripTerminalColouring(text, modbuffer);
::CopyToClipboard(modbuffer);
},
wxID_COPY);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
wxString text = m_ctrl->GetSelectedText();
if (text.empty()) {
return;
}
::CopyToClipboard(text);
},
XRCID("copy-with-ansi-colors"));
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
m_ctrl->SelectAll();
},
wxID_SELECTALL);
menu.Bind(
wxEVT_MENU,
[this](wxCommandEvent& event) {
wxUnusedVar(event);
Clear();
},
wxID_CLEAR);
PopupMenu(&menu);
}
10 changes: 7 additions & 3 deletions Plugin/wxTerminalCtrl/wxTerminalOutputCtrl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,21 @@ class WXDLLIMPEXP_SDK wxTerminalOutputCtrl : public wxWindow
void OnLeftUp(wxMouseEvent& event);
void ApplyTheme();
void OnKeyDown(wxKeyEvent& event);
void Initialise(const wxFont& font = wxNullFont, const wxColour& bg_colour = *wxBLACK,
void Initialise(const wxFont& font = wxNullFont,
const wxColour& bg_colour = *wxBLACK,
const wxColour& text_colour = *wxWHITE);
void ClearIndicators();
void ProcessIdle();
void OnEnterWindow(wxMouseEvent& event);
void OnLeaveWindow(wxMouseEvent& event);
void DoPatternClicked(const wxString& pattern);
void OnMenu(wxContextMenuEvent& event);

public:
explicit wxTerminalOutputCtrl(wxTerminalCtrl* parent, wxWindowID winid = wxNOT_FOUND,
const wxFont& font = wxNullFont, const wxColour& bg_colour = *wxBLACK,
explicit wxTerminalOutputCtrl(wxTerminalCtrl* parent,
wxWindowID winid = wxNOT_FOUND,
const wxFont& font = wxNullFont,
const wxColour& bg_colour = *wxBLACK,
const wxColour& text_colour = *wxWHITE);
explicit wxTerminalOutputCtrl(wxWindow* parent, wxWindowID winid = wxNOT_FOUND);
virtual ~wxTerminalOutputCtrl();
Expand Down

0 comments on commit f6e9e1e

Please sign in to comment.