Skip to content

Commit 8a67d6a

Browse files
author
Roberto De Ioris
committed
preliminary support for console history
1 parent b591e05 commit 8a67d6a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Source/PythonConsole/Private/SPythonLog.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ class SPythonConsoleEditableTextBox : public SEditableTextBox
4646
]);
4747
}
4848

49+
void SetPythonBox(SPythonConsoleInputBox *box) {
50+
SPythonConsoleEditableText *PythonEditableText = (SPythonConsoleEditableText *)EditableText.Get();
51+
PythonEditableText->PythonConsoleInputBox = box;
52+
}
53+
4954
private:
5055
class SPythonConsoleEditableText : public SEditableText
5156
{
5257
public:
58+
59+
SPythonConsoleInputBox *PythonConsoleInputBox;
60+
5361
SLATE_BEGIN_ARGS(SPythonConsoleEditableText) {}
5462
/** The text that appears when there is nothing typed into the search box */
5563
SLATE_ATTRIBUTE(FText, HintText)
@@ -81,6 +89,22 @@ class SPythonConsoleEditableTextBox : public SEditableTextBox
8189
{
8290
return FReply::Unhandled();
8391
}
92+
else if (InKeyEvent.GetKey() == EKeys::Up) {
93+
if (PythonConsoleInputBox->HistoryPosition > 0) {
94+
PythonConsoleInputBox->HistoryPosition--;
95+
this->SetText(FText::FromString(PythonConsoleInputBox->History[PythonConsoleInputBox->HistoryPosition]));
96+
}
97+
98+
return FReply::Handled();
99+
}
100+
else if (InKeyEvent.GetKey() == EKeys::Down) {
101+
if (PythonConsoleInputBox->HistoryPosition < PythonConsoleInputBox->History.Num() - 2) {
102+
PythonConsoleInputBox->HistoryPosition++;
103+
this->SetText(FText::FromString(PythonConsoleInputBox->History[PythonConsoleInputBox->HistoryPosition]));
104+
}
105+
106+
return FReply::Handled();
107+
}
84108
else
85109
{
86110
return SEditableText::OnKeyDown(MyGeometry, InKeyEvent);
@@ -128,7 +152,7 @@ SPythonConsoleInputBox::SPythonConsoleInputBox()
128152
: bIgnoreUIUpdate(false)
129153
{
130154
FScopePythonGIL gil;
131-
155+
132156
}
133157

134158
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
@@ -144,6 +168,9 @@ void SPythonConsoleInputBox::Construct(const FArguments& InArgs)
144168
.HintText(NSLOCTEXT("PythonConsole", "TypeInConsoleHint", "Enter python command"))
145169

146170
];
171+
172+
SPythonConsoleEditableTextBox *TextBox = (SPythonConsoleEditableTextBox *)InputText.Get();
173+
TextBox->SetPythonBox(this);
147174
}
148175
void SPythonConsoleInputBox::Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime)
149176
{
@@ -170,6 +197,9 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
170197
// re-entered. We want to make sure the text string is empty on re-entry, so we'll clear it out
171198
const FString ExecString = InText.ToString();
172199

200+
this->History.Add(ExecString);
201+
this->HistoryPosition = this->History.Num();
202+
173203
UE_LOG(LogTemp, Log, TEXT(">>> %s"), *ExecString);
174204

175205
// Clear the console input area
@@ -182,7 +212,7 @@ void SPythonConsoleInputBox::OnTextCommitted(const FText& InText, ETextCommit::T
182212
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
183213
PythonModule.RunString(TCHAR_TO_UTF8(*ExecString));
184214

185-
215+
186216
}
187217

188218
}

Source/PythonConsole/Private/SPythonLog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class SPythonConsoleInputBox
6060
/** SWidget interface */
6161
virtual void Tick( const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime ) override;
6262

63+
64+
TArray<FString> History;
65+
int HistoryPosition;
66+
67+
6368
protected:
6469

6570
virtual bool SupportsKeyboardFocus() const override { return true; }

0 commit comments

Comments
 (0)