From 2e15d9391c4a66ce89e131088152a07a5b14c736 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 18 Feb 2025 16:40:50 +0000 Subject: [PATCH 1/2] :sparkles: Suggest known commands if there is no history If the history for the command line is empty pull suggestions from the known commands; this might help make commands more discoverable to the user when they first get started. --- ChangeLog.md | 2 ++ src/hike/widgets/command_line/base_command.py | 31 ++++++++++++++----- src/hike/widgets/command_line/widget.py | 13 ++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 82f76ca..6f7b381 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -22,6 +22,8 @@ ([#24](https://github.com/davep/hike/pull/24)) - Added `help` as a command that the command line understands. ([#24](https://github.com/davep/hike/pull/24)) +- Changed command line completion so that if history is empty, known + commands are suggested. ## v0.2.0 diff --git a/src/hike/widgets/command_line/base_command.py b/src/hike/widgets/command_line/base_command.py index 6cd7a86..4c09518 100644 --- a/src/hike/widgets/command_line/base_command.py +++ b/src/hike/widgets/command_line/base_command.py @@ -54,23 +54,38 @@ def split_command(text: str) -> tuple[str, str]: return command.strip(), tail.strip() @classmethod - def is_command(cls, command: str) -> bool: - """Does the given command appear to be a match? - - Args: - command: The command to test. + def suggestions(cls) -> tuple[str, ...]: + """The suggested command matches for this command. Returns: - `True` if the given command seems to be a match, `False` if not. + A tuple of suggestions for the command. + + Notes: + Any commands that look like value placeholders will be filtered + out. """ # Build up all the possible matches. These are built from the main # command and also the aliases. By convention the code will often # use `code` fences for commands, and the aliases will be a comma # list, so we clean that up as we go... - return command.strip().lower() in ( - candidate.strip().lower().removeprefix("`").removesuffix("`") + return tuple( + candidate.strip().removeprefix("`").removesuffix("`") for candidate in (cls.COMMAND, *cls.ALIASES.split(",")) + # Note that we filter out anything that looks like ``. + if not candidate.startswith("`<") ) + @classmethod + def is_command(cls, command: str) -> bool: + """Does the given command appear to be a match? + + Args: + command: The command to test. + + Returns: + `True` if the given command seems to be a match, `False` if not. + """ + return command.strip().lower() in cls.suggestions() + ### base_command.py ends here diff --git a/src/hike/widgets/command_line/widget.py b/src/hike/widgets/command_line/widget.py index e7de04f..d486827 100644 --- a/src/hike/widgets/command_line/widget.py +++ b/src/hike/widgets/command_line/widget.py @@ -7,6 +7,7 @@ ############################################################################## # Python imports. from dataclasses import dataclass +from itertools import chain from typing import Final ############################################################################## @@ -149,8 +150,16 @@ class CommandLine(Vertical): @property def _history_suggester(self) -> SuggestFromList: - """A suggester for the history of input.""" - return SuggestFromList(reversed(list(self.history))) + """A suggester for the history of input. + + If there us no history yet then a list of commands and aliases will + be used. + """ + return SuggestFromList( + reversed(list(self.history)) + if self.history + else chain(*(command.suggestions() for command in COMMANDS)) + ) def compose(self) -> ComposeResult: """Compose the content of the widget.""" From cc3bd9a2bf0efc04bc971fba1778f4ff00fed2d6 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Tue, 18 Feb 2025 16:43:50 +0000 Subject: [PATCH 2/2] :books: Link the ChangeLog to the PR --- ChangeLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 6f7b381..a4d2261 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -23,7 +23,7 @@ - Added `help` as a command that the command line understands. ([#24](https://github.com/davep/hike/pull/24)) - Changed command line completion so that if history is empty, known - commands are suggested. + commands are suggested. ([#29](https://github.com/davep/hike/pull/29)) ## v0.2.0