From 18b1a6901dd78533b049224ec15fc108e9c2f7e2 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Fri, 14 Feb 2025 11:57:47 +0000 Subject: [PATCH 1/2] :sparkles: Allow moving the command line to the top of the screen --- ChangeLog.md | 1 + src/hike/commands/__init__.py | 2 ++ src/hike/commands/main.py | 7 ++++ src/hike/data/config.py | 3 ++ src/hike/providers/main.py | 2 ++ src/hike/screens/main.py | 11 +++++++ src/hike/widgets/command_line/widget.py | 43 ++++++++++++++++++++----- 7 files changed, 61 insertions(+), 8 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 6e8af80..b3eb1ef 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -8,6 +8,7 @@ ([#2](https://github.com/davep/hike/pull/2)) - The suggester for the command line now prefers newer history entries over older. ([#3](https://github.com/davep/hike/pull/3)) +- Add the ability to move the command line to the top of the screen. ## v0.1.0 diff --git a/src/hike/commands/__init__.py b/src/hike/commands/__init__.py index 786fa56..c6696ae 100644 --- a/src/hike/commands/__init__.py +++ b/src/hike/commands/__init__.py @@ -4,6 +4,7 @@ # Local imports. from .main import ( BookmarkLocation, + ChangeCommandLineLocation, ChangeNavigationSide, JumpToCommandLine, Reload, @@ -24,6 +25,7 @@ __all__ = [ "Backward", "BookmarkLocation", + "ChangeCommandLineLocation", "ChangeNavigationSide", "Forward", "JumpToBookmarks", diff --git a/src/hike/commands/main.py b/src/hike/commands/main.py index 9bc4016..61202c9 100644 --- a/src/hike/commands/main.py +++ b/src/hike/commands/main.py @@ -28,6 +28,13 @@ class JumpToCommandLine(Command): BINDING_KEY = "/" +############################################################################## +class ChangeCommandLineLocation(Command): + """Swap the position of the command line between top and bottom""" + + BINDING_KEY = "ctrl+up, ctrl+down" + + ############################################################################## class BookmarkLocation(Command): """Bookmark the current location""" diff --git a/src/hike/data/config.py b/src/hike/data/config.py index f662c65..25a1a1d 100644 --- a/src/hike/data/config.py +++ b/src/hike/data/config.py @@ -31,6 +31,9 @@ class Configuration: markdown_extensions: list[str] = field(default_factory=lambda: [".md", ".markdown"]) """The file extensions to consider to be Markdown files.""" + command_line_on_top: bool = False + """Should the command line live at the top of the screen?""" + ############################################################################## def configuration_file() -> Path: diff --git a/src/hike/providers/main.py b/src/hike/providers/main.py index 226434f..c64262e 100644 --- a/src/hike/providers/main.py +++ b/src/hike/providers/main.py @@ -15,6 +15,7 @@ from ..commands import ( Backward, BookmarkLocation, + ChangeCommandLineLocation, ChangeNavigationSide, Forward, JumpToBookmarks, @@ -40,6 +41,7 @@ def commands(self) -> CommandHits: """ yield Backward() yield BookmarkLocation() + yield ChangeCommandLineLocation() yield ChangeNavigationSide() yield ChangeTheme() yield Forward() diff --git a/src/hike/screens/main.py b/src/hike/screens/main.py index 6488138..f4cdc12 100644 --- a/src/hike/screens/main.py +++ b/src/hike/screens/main.py @@ -28,6 +28,7 @@ from ..commands import ( Backward, BookmarkLocation, + ChangeCommandLineLocation, ChangeNavigationSide, Forward, JumpToBookmarks, @@ -124,6 +125,7 @@ class Main(EnhancedScreen[None]): # Everything else. Backward, BookmarkLocation, + ChangeCommandLineLocation, ChangeNavigationSide, Forward, JumpToBookmarks, @@ -171,6 +173,7 @@ def on_mount(self) -> None: BookmarkCommands.bookmarks = bookmarks self.query_one(Viewer).history = load_history() self.query_one(CommandLine).history = load_command_history() + self.query_one(CommandLine).dock_top = config.command_line_on_top if self._arguments.command: self.query_one(CommandLine).handle_input(" ".join(self._arguments.command)) @@ -314,6 +317,14 @@ def action_change_navigation_side_command(self) -> None: with update_configuration() as config: config.navigation_on_right = navigation.dock_right + @on(ChangeCommandLineLocation) + def action_change_command_line_location_command(self) -> None: + """Change the location of the command line.""" + command_line = self.query_one(CommandLine) + command_line.dock_top = not command_line.dock_top + with update_configuration() as config: + config.command_line_on_top = command_line.dock_top + @on(JumpToCommandLine) def action_jump_to_command_line_command(self) -> None: """Jump to the command line.""" diff --git a/src/hike/widgets/command_line/widget.py b/src/hike/widgets/command_line/widget.py index 66b77fd..45361da 100644 --- a/src/hike/widgets/command_line/widget.py +++ b/src/hike/widgets/command_line/widget.py @@ -13,11 +13,11 @@ # Textual imports. from textual import on from textual.app import ComposeResult -from textual.containers import Horizontal +from textual.containers import Horizontal, Vertical from textual.message import Message from textual.reactive import var from textual.suggester import SuggestFromList -from textual.widgets import Input, Label +from textual.widgets import Input, Label, Rule from textual.widgets.input import Selection ############################################################################## @@ -57,15 +57,17 @@ ############################################################################## -class CommandLine(Horizontal): +class CommandLine(Vertical): """A command line for getting input from the user.""" DEFAULT_CSS = """ CommandLine { height: 1; + Label, Input { color: $text-muted; } + &:focus-within { Label, Input { color: $text; @@ -74,12 +76,28 @@ class CommandLine(Horizontal): text-style: bold; } } + + Rule { + height: 1; + margin: 0 !important; + color: $foreground 10%; + display: none; + } + Input, Input:focus { border: none; padding: 0; height: 1fr; background: transparent; } + + &.--top { + dock: top; + height: 2; + Rule { + display: block; + } + } } """ @@ -123,6 +141,9 @@ class CommandLine(Horizontal): history: var[CommandHistory] = var(CommandHistory) """The command line history.""" + dock_top: var[bool] = var(False) + """Should the input dock to the top of the screen?""" + @property def _history_suggester(self) -> SuggestFromList: """A suggester for the history of input.""" @@ -130,17 +151,23 @@ def _history_suggester(self) -> SuggestFromList: def compose(self) -> ComposeResult: """Compose the content of the widget.""" - yield Label("> ") - yield Input( - placeholder="Enter a directory, file, path or command", - suggester=self._history_suggester, - ) + with Horizontal(): + yield Label("> ") + yield Input( + placeholder="Enter a directory, file, path or command", + suggester=self._history_suggester, + ) + yield Rule(line_style="heavy") def _watch_history(self) -> None: """React to history being updated.""" if self.is_mounted: self.query_one(Input).suggester = self._history_suggester + def _watch_dock_top(self) -> None: + """React to being asked to dock input to the top.""" + self.set_class(self.dock_top, "--top") + @dataclass class HistoryUpdated(Message): """Message posted when the command history is updated.""" From 2606ff33b191882b014130e88cefbfbb8c04658d Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Fri, 14 Feb 2025 11:59:01 +0000 Subject: [PATCH 2/2] :books: Link the ChangeLog to the PR --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog.md b/ChangeLog.md index b3eb1ef..f13b425 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -9,6 +9,7 @@ - The suggester for the command line now prefers newer history entries over older. ([#3](https://github.com/davep/hike/pull/3)) - Add the ability to move the command line to the top of the screen. + ([#5](https://github.com/davep/hike/pull/5)) ## v0.1.0