Skip to content

Commit

Permalink
Core: CommonClient: command history and echo (ArchipelagoMW#3236)
Browse files Browse the repository at this point in the history
* client: Added command history access with up/down and command echo in common client

* client: Changed command echo colour to orange

* client: removed star import from typing

* client: updated code style to match style guideline

* client: adjusted ordering of calling parent constructor in command prompt input constructor

* client: Fixed issues identified by beauxq in PR; fixed some typing issues

* client: PR comments; replaced command history list with deque
  • Loading branch information
MatthewMarinets authored Jun 9, 2024
1 parent c478e55 commit 2198a70
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CommonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ def on_user_say(self, text: str) -> typing.Optional[str]:
"""Gets called before sending a Say to the server from the user.
Returned text is sent, or sending is aborted if None is returned."""
return text

def on_ui_command(self, text: str) -> None:
"""Gets called by kivy when the user executes a command starting with `/` or `!`.
The command processor is still called; this is just intended for command echoing."""
self.ui.print_json([{"text": text, "type": "color", "color": "orange"}])

def update_permissions(self, permissions: typing.Dict[str, int]):
for permission_name, permission_flag in permissions.items():
Expand Down
3 changes: 2 additions & 1 deletion NetUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class JSONtoTextParser(metaclass=HandlerMeta):
"slateblue": "6D8BE8",
"plum": "AF99EF",
"salmon": "FA8072",
"white": "FFFFFF"
"white": "FFFFFF",
"orange": "FF7700",
}

def __init__(self, ctx):
Expand Down
1 change: 1 addition & 0 deletions data/client.kv
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
plum: "AF99EF" # typically progression item
salmon: "FA8072" # typically trap item
white: "FFFFFF" # not used, if you want to change the generic text color change color in Label
orange: "FF7700" # Used for command echo
<Label>:
color: "FFFFFF"
<TabbedPanel>:
Expand Down
62 changes: 59 additions & 3 deletions kvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import typing
import re
from collections import deque

if sys.platform == "win32":
import ctypes
Expand Down Expand Up @@ -380,6 +381,57 @@ def insert_text(self, substring, from_undo=False):
return super(ConnectBarTextInput, self).insert_text(s, from_undo=from_undo)


def is_command_input(string: str) -> bool:
return len(string) > 0 and string[0] in "/!"


class CommandPromptTextInput(TextInput):
MAXIMUM_HISTORY_MESSAGES = 50

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._command_history_index = -1
self._command_history: typing.Deque[str] = deque(maxlen=CommandPromptTextInput.MAXIMUM_HISTORY_MESSAGES)

def update_history(self, new_entry: str) -> None:
self._command_history_index = -1
if is_command_input(new_entry):
self._command_history.appendleft(new_entry)

def keyboard_on_key_down(
self,
window,
keycode: typing.Tuple[int, str],
text: typing.Optional[str],
modifiers: typing.List[str]
) -> bool:
"""
:param window: The kivy window object
:param keycode: A tuple of (keycode, keyname). Keynames are always lowercase
:param text: The text printed by this key, not accounting for modifiers, or `None` if no text.
Seems to pretty naively interpret the keycode as unicode, so numlock can return odd characters.
:param modifiers: A list of string modifiers, like `ctrl` or `numlock`
"""
if keycode[1] == 'up':
self._change_to_history_text_if_available(self._command_history_index + 1)
return True
if keycode[1] == 'down':
self._change_to_history_text_if_available(self._command_history_index - 1)
return True
return super().keyboard_on_key_down(window, keycode, text, modifiers)

def _change_to_history_text_if_available(self, new_index: int) -> None:
if new_index < -1:
return
if new_index >= len(self._command_history):
return
self._command_history_index = new_index
if new_index == -1:
self.text = ""
return
self.text = self._command_history[self._command_history_index]


class MessageBox(Popup):
class MessageBoxLabel(Label):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -415,7 +467,7 @@ def __init__(self, ctx: context_type):
self.commandprocessor = ctx.command_processor(ctx)
self.icon = r"data/icon.png"
self.json_to_kivy_parser = KivyJSONtoTextParser(ctx)
self.log_panels = {}
self.log_panels: typing.Dict[str, Widget] = {}

# keep track of last used command to autofill on click
self.last_autofillable_command = "hint"
Expand Down Expand Up @@ -499,7 +551,7 @@ def connect_bar_validate(sender):
info_button = Button(size=(dp(100), dp(30)), text="Command:", size_hint_x=None)
info_button.bind(on_release=self.command_button_action)
bottom_layout.add_widget(info_button)
self.textinput = TextInput(size_hint_y=None, height=dp(30), multiline=False, write_tab=False)
self.textinput = CommandPromptTextInput(size_hint_y=None, height=dp(30), multiline=False, write_tab=False)
self.textinput.bind(on_text_validate=self.on_message)
self.textinput.text_validate_unfocus = False
bottom_layout.add_widget(self.textinput)
Expand Down Expand Up @@ -557,14 +609,18 @@ def on_stop(self):

self.ctx.exit_event.set()

def on_message(self, textinput: TextInput):
def on_message(self, textinput: CommandPromptTextInput):
try:
input_text = textinput.text.strip()
textinput.text = ""
textinput.update_history(input_text)

if self.ctx.input_requests > 0:
self.ctx.input_requests -= 1
self.ctx.input_queue.put_nowait(input_text)
elif is_command_input(input_text):
self.ctx.on_ui_command(input_text)
self.commandprocessor(input_text)
elif input_text:
self.commandprocessor(input_text)

Expand Down
1 change: 0 additions & 1 deletion worlds/sc2/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def send(self, ctx: SC2Context) -> None:

class StarcraftClientProcessor(ClientCommandProcessor):
ctx: SC2Context
echo_commands = True

def formatted_print(self, text: str) -> None:
"""Prints with kivy formatting to the GUI, and also prints to command-line and to all logs"""
Expand Down

0 comments on commit 2198a70

Please sign in to comment.