Skip to content

✨ keypad keybindings #50

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion browsr/browsr.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CurrentFileInfoBar {

/* -- WindowSwitcher -- */

VimScroll {
KeyBindScroll {
overflow: auto scroll;
min-width: 100%;
}
Expand Down
12 changes: 9 additions & 3 deletions browsr/widgets/code_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ def bind_keys(self) -> None:
self.app.bind(
keys="x", action="download_file", description="Download File", show=True
)
self.app.bind(
keys="kp_up", action="cursor_up", description="Cursor Up", show=False
)
self.app.bind(
keys="kp_down", action="cursor_down", description="Cursor Down", show=False
)

def watch_show_tree(self, show_tree: bool) -> None:
"""
Expand Down Expand Up @@ -169,7 +175,7 @@ def handle_table_view_display_toggle(
Handle the table view display toggle.
"""
self.datatable_window.display = self.table_view_status
self.window_switcher.vim_scroll.display = self.static_window_status
self.window_switcher.keybind_scroll.display = self.static_window_status

@on(DirectoryTree.FileSelected)
def handle_file_selected(self, message: DirectoryTree.FileSelected) -> None:
Expand Down Expand Up @@ -247,9 +253,9 @@ def download_file_workflow(self) -> None:
self.confirmation.download_message.update(Markdown(prompt_message))
self.confirmation.refresh()
self.table_view_status = self.datatable_window.display
self.static_window_status = self.window_switcher.vim_scroll.display
self.static_window_status = self.window_switcher.keybind_scroll.display
self.datatable_window.display = False
self.window_switcher.vim_scroll.display = False
self.window_switcher.keybind_scroll.display = False
self.confirmation_window.display = True

@work(thread=True)
Expand Down
65 changes: 65 additions & 0 deletions browsr/widgets/keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import annotations

from typing import ClassVar

from textual.binding import Binding, BindingType
from textual.containers import VerticalScroll
from textual.widgets import DataTable

vim_scroll_bindings = [
Binding(key="k", action="scroll_up", description="Scroll Up", show=False),
Binding(key="j", action="scroll_down", description="Scroll Down", show=False),
Binding(key="h", action="scroll_left", description="Scroll Left", show=False),
Binding(key="l", action="scroll_right", description="Scroll Right", show=False),
]
vim_cursor_bindings = [
Binding(key="k", action="cursor_up", description="Cursor Up", show=False),
Binding(key="j", action="cursor_down", description="Cursor Down", show=False),
Binding(key="h", action="cursor_left", description="Cursor Left", show=False),
Binding(key="l", action="cursor_right", description="Cursor Right", show=False),
]
keypad_scroll_bindings = [
Binding(key="kp_up", action="scroll_up", description="Scroll Up", show=False),
Binding(key="kp_down", action="scroll_down", description="Scroll Down", show=False),
Binding(key="kp_left", action="scroll_left", description="Scroll Left", show=False),
Binding(key="kp_right", action="scroll_right", description="Scroll Right", show=False),
Binding(key="kp_page_up", action="page_up", description="Page Up", show=False),
Binding(key="kp_page_down", action="page_down", description="Page Down", show=False),
Binding(key="kp_home", action="scroll_home", description="Scroll Home", show=False),
Binding(key="kp_end", action="scroll_end", description="Scroll End", show=False),
]
keypad_cursor_bindings = [
Binding(key="kp_up", action="cursor_up", description="Cursor Up", show=False),
Binding(key="kp_down", action="cursor_down", description="Cursor Down", show=False),
Binding(key="kp_left", action="cursor_left", description="Cursor Left", show=False),
Binding(key="kp_right", action="cursor_right", description="Cursor Right", show=False),
Binding(key="kp_page_up", action="page_up", description="Page Up", show=False),
Binding(key="kp_page_down", action="page_down", description="Page Down", show=False),
Binding(key="kp_home", action="scroll_home", description="Scroll Home", show=False),
Binding(key="kp_end", action="scroll_end", description="Scroll End", show=False),
Binding(key="kp_enter", action="select_cursor", description="Select Item", show=False),
]


class KeyBindScroll(VerticalScroll):
"""
A VerticalScroll with Vim and Keypad Keybindings
"""

BINDINGS: ClassVar[list[BindingType]] = [
*VerticalScroll.BINDINGS,
*vim_scroll_bindings,
*keypad_scroll_bindings,
]


class KeyBindDataTable(DataTable[str]):
"""
A DataTable with Vim and Keypad Keybindings
"""

BINDINGS: ClassVar[list[BindingType]] = [
*DataTable.BINDINGS,
*vim_cursor_bindings,
*keypad_cursor_bindings,
]
31 changes: 29 additions & 2 deletions browsr/widgets/universal_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

from typing import ClassVar, Iterable

from textual.binding import BindingType
from textual.binding import Binding, BindingType
from textual.widgets._directory_tree import DirEntry
from textual.widgets._tree import TreeNode
from textual_universal_directorytree import UniversalDirectoryTree, UPath

from browsr.widgets.double_click_directory_tree import DoubleClickDirectoryTree
from browsr.widgets.vim import vim_cursor_bindings
from browsr.widgets.keys import vim_cursor_bindings, keypad_cursor_bindings


class BrowsrDirectoryTree(DoubleClickDirectoryTree, UniversalDirectoryTree):
Expand All @@ -23,6 +23,9 @@ class BrowsrDirectoryTree(DoubleClickDirectoryTree, UniversalDirectoryTree):
BINDINGS: ClassVar[list[BindingType]] = [
*UniversalDirectoryTree.BINDINGS,
*vim_cursor_bindings,
*keypad_cursor_bindings,
Binding(key="h,left,kp_left", action="collapse_parent", description="Collapse (Parent) Directory", show=False),
Binding(key="l,right,kp_right", action="expand_or_select", description="Expand Directory or Select File", show=False),
]

@classmethod
Expand Down Expand Up @@ -64,3 +67,27 @@ def _populate_node(
allow_expand=self._safe_is_dir(path),
)
node.expand()

def action_collapse_parent(self) -> None:
cursor_node = self.cursor_node
cursor_path = cursor_node.data.path
collapse_parent = False
if self._safe_is_dir(cursor_path):
if cursor_node.is_expanded:
cursor_node.collapse()
elif not cursor_node.is_root:
collapse_parent = True
else:
collapse_parent = True
if collapse_parent:
self.action_cursor_parent()
cursor_node.parent.collapse()

def action_expand_or_select(self) -> None:
cursor_node = self.cursor_node
cursor_path = cursor_node.data.path
if self._safe_is_dir(cursor_path):
if not cursor_node.is_expanded:
cursor_node.expand()
else:
self.action_select_cursor()
42 changes: 0 additions & 42 deletions browsr/widgets/vim.py

This file was deleted.

22 changes: 11 additions & 11 deletions browsr/widgets/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
FileInfo,
open_image,
)
from browsr.widgets.vim import VimDataTable, VimScroll
from browsr.widgets.keys import KeyBindDataTable, KeyBindScroll


class BaseCodeWindow(Widget):
Expand Down Expand Up @@ -226,7 +226,7 @@ def next_theme(self) -> str | None:
return next_theme


class DataTableWindow(VimDataTable, BaseCodeWindow):
class DataTableWindow(KeyBindDataTable, BaseCodeWindow):
"""
A DataTable widget for displaying code.
"""
Expand Down Expand Up @@ -311,22 +311,22 @@ def __init__(
zebra_stripes=True, show_header=True, show_cursor=True, id="table-view"
)
self.datatable_window.display = False
self.vim_scroll = VimScroll(self.static_window)
self.keybind_scroll = KeyBindScroll(self.static_window)
self.rendered_file: UPath | None = None

def compose(self) -> ComposeResult:
"""
Compose the widget
"""
yield self.vim_scroll
yield self.keybind_scroll
yield self.datatable_window

def get_active_widget(self) -> Widget:
"""
Get the active widget
"""
if self.vim_scroll.display:
return self.vim_scroll
if self.keybind_scroll.display:
return self.keybind_scroll
elif self.datatable_window.display:
return self.datatable_window

Expand All @@ -335,7 +335,7 @@ def switch_window(self, window: BaseCodeWindow) -> None:
Switch to the window
"""
screens: dict[Widget, Widget] = {
self.static_window: self.vim_scroll,
self.static_window: self.keybind_scroll,
self.datatable_window: self.datatable_window,
}
for window_screen in screens:
Expand Down Expand Up @@ -382,11 +382,11 @@ def render_file(self, file_path: UPath, scroll_home: bool = True) -> None:
self.switch_window(switch_window)
active_widget = self.get_active_widget()
if scroll_home:
if active_widget is self.vim_scroll:
self.vim_scroll.scroll_home(animate=False)
if active_widget is self.keybind_scroll:
self.keybind_scroll.scroll_home(animate=False)
else:
switch_window.scroll_home(animate=False)
if active_widget is self.vim_scroll:
if active_widget is self.keybind_scroll:
self.app.sub_title = str(file_path) + f" [{self.static_window.theme}]"
else:
self.app.sub_title = str(file_path)
Expand All @@ -396,7 +396,7 @@ def next_theme(self) -> str | None:
"""
Switch to the next theme
"""
if self.get_active_widget() is not self.vim_scroll:
if self.get_active_widget() is not self.keybind_scroll:
return None
current_index = favorite_themes.index(self.static_window.theme)
next_theme = favorite_themes[(current_index + 1) % len(favorite_themes)]
Expand Down