Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow moving the command line to the top of the screen #5

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
([#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.
([#5](https://github.com/davep/hike/pull/5))

## v0.1.0

Expand Down
2 changes: 2 additions & 0 deletions src/hike/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Local imports.
from .main import (
BookmarkLocation,
ChangeCommandLineLocation,
ChangeNavigationSide,
JumpToCommandLine,
Reload,
Expand All @@ -24,6 +25,7 @@
__all__ = [
"Backward",
"BookmarkLocation",
"ChangeCommandLineLocation",
"ChangeNavigationSide",
"Forward",
"JumpToBookmarks",
Expand Down
7 changes: 7 additions & 0 deletions src/hike/commands/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
3 changes: 3 additions & 0 deletions src/hike/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/hike/providers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..commands import (
Backward,
BookmarkLocation,
ChangeCommandLineLocation,
ChangeNavigationSide,
Forward,
JumpToBookmarks,
Expand All @@ -40,6 +41,7 @@ def commands(self) -> CommandHits:
"""
yield Backward()
yield BookmarkLocation()
yield ChangeCommandLineLocation()
yield ChangeNavigationSide()
yield ChangeTheme()
yield Forward()
Expand Down
11 changes: 11 additions & 0 deletions src/hike/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from ..commands import (
Backward,
BookmarkLocation,
ChangeCommandLineLocation,
ChangeNavigationSide,
Forward,
JumpToBookmarks,
Expand Down Expand Up @@ -124,6 +125,7 @@ class Main(EnhancedScreen[None]):
# Everything else.
Backward,
BookmarkLocation,
ChangeCommandLineLocation,
ChangeNavigationSide,
Forward,
JumpToBookmarks,
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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."""
Expand Down
43 changes: 35 additions & 8 deletions src/hike/widgets/command_line/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

##############################################################################
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}
"""

Expand Down Expand Up @@ -123,24 +141,33 @@ 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."""
return SuggestFromList(reversed(list(self.history)))

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."""
Expand Down
Loading