Skip to content

Commit

Permalink
Fix console behavior when pasting multiple lines of text
Browse files Browse the repository at this point in the history
Ensure all pasted lines are added to the console history instead of only the last one.

Ensure the prompt `> [command]` is printed for all pasted lines instead of only for the last one.

Fix commands being executed when pasting multiple lines while the console is in searching-mode. Now, newline characters will be replaced with spaces while the input is used for searching.
  • Loading branch information
Robyt3 committed Dec 16, 2024
1 parent 9797da8 commit 5cbb41c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
59 changes: 39 additions & 20 deletions src/game/client/components/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,31 @@ void CGameConsole::CInstance::Reset()

void CGameConsole::CInstance::ExecuteLine(const char *pLine)
{
if(m_Type == CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
{
const char *pPrevEntry = m_History.Last();
if(pPrevEntry == nullptr || str_comp(pPrevEntry, pLine) != 0)
{
const size_t Size = str_length(pLine) + 1;
char *pEntry = m_History.Allocate(Size);
str_copy(pEntry, pLine, Size);
}
// print out the user's commands before they get run
char aBuf[IConsole::CMDLINE_LENGTH + 3];
str_format(aBuf, sizeof(aBuf), "> %s", pLine);
m_pGameConsole->PrintLine(m_Type, aBuf);
}

if(m_Type == CGameConsole::CONSOLETYPE_LOCAL)
{
m_pGameConsole->m_pConsole->ExecuteLine(pLine);
}
else
{
if(m_pGameConsole->Client()->RconAuthed())
{
m_pGameConsole->Client()->Rcon(pLine);
}
else
{
if(!m_UserGot && m_UsernameReq)
Expand Down Expand Up @@ -404,22 +423,9 @@ bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
{
if(!m_Input.IsEmpty() || (m_UsernameReq && !m_pGameConsole->Client()->RconAuthed() && !m_UserGot))
{
if(m_Type == CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
{
const char *pPrevEntry = m_History.Last();
if(pPrevEntry == nullptr || str_comp(pPrevEntry, m_Input.GetString()) != 0)
{
char *pEntry = m_History.Allocate(m_Input.GetLength() + 1);
str_copy(pEntry, m_Input.GetString(), m_Input.GetLength() + 1);
}
// print out the user's commands before they get run
char aBuf[IConsole::CMDLINE_LENGTH + 3];
str_format(aBuf, sizeof(aBuf), "> %s", m_Input.GetString());
m_pGameConsole->PrintLine(m_Type, aBuf);
}
ExecuteLine(m_Input.GetString());
m_Input.Clear();
m_pHistoryEntry = 0x0;
m_pHistoryEntry = nullptr;
}
}
else
Expand Down Expand Up @@ -577,16 +583,12 @@ bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
}
else if(Event.m_Key == KEY_ESCAPE && m_Searching)
{
m_Searching = false;
m_Input.Clear();
SetSearching(false);
Handled = true;
}
else if(Event.m_Key == KEY_F && m_pGameConsole->Input()->ModifierIsPressed())
{
m_Searching = true;
m_Input.Set(m_aCurrentSearchString);
m_Input.SelectAll();
UpdateSearch();
SetSearching(true);
Handled = true;
}
}
Expand Down Expand Up @@ -725,6 +727,23 @@ void CGameConsole::CInstance::UpdateEntryTextAttributes(CBacklogEntry *pEntry) c
pEntry->m_LineCount = Cursor.m_LineCount;
}

void CGameConsole::CInstance::SetSearching(bool Searching)
{
m_Searching = Searching;
if(Searching)
{
m_Input.SetClipboardLineCallback(nullptr); // restore default behavior (replace newlines with spaces)
m_Input.Set(m_aCurrentSearchString);
m_Input.SelectAll();
UpdateSearch();
}
else
{
m_Input.SetClipboardLineCallback([this](const char *pLine) { ExecuteLine(pLine); });
m_Input.Clear();
}
}

void CGameConsole::CInstance::ClearSearch()
{
m_vSearchMatches.clear();
Expand Down
3 changes: 2 additions & 1 deletion src/game/client/components/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class CGameConsole : public CComponent
void PrintLine(const char *pLine, int Len, ColorRGBA PrintColor) REQUIRES(!m_BacklogPendingLock);
int GetLinesToScroll(int Direction, int LinesToScroll);
void ScrollToCenter(int StartLine, int EndLine);
void ClearSearch();
void Dump() REQUIRES(!m_BacklogPendingLock);

const char *GetString() const { return m_Input.GetString(); }
Expand All @@ -135,6 +134,8 @@ class CGameConsole : public CComponent
void UpdateEntryTextAttributes(CBacklogEntry *pEntry) const;

private:
void SetSearching(bool Searching);
void ClearSearch();
void UpdateSearch();

friend class CGameConsole;
Expand Down

0 comments on commit 5cbb41c

Please sign in to comment.