Skip to content

Commit

Permalink
Merge pull request #44 from darrenburns/17jul24
Browse files Browse the repository at this point in the history
Various changes/improvements
  • Loading branch information
darrenburns authored Jul 17, 2024
2 parents b7d9b39 + e39fedf commit d09411e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/posting/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,13 @@ def watch_theme(self, theme: str | None) -> None:
def action_toggle_jump_mode(self) -> None:
self._jumping = not self._jumping

async def watch__jumping(self, jumping: bool) -> None:
def watch__jumping(self, jumping: bool) -> None:
focused_before = self.focused
if focused_before is not None:
self.set_focus(None, scroll_visible=False)
# TODO - the call below is working around a Textual bug
# that is fixed in https://github.com/Textualize/textual/pull/4771
self.screen._update_focus_styles(None, blurred=focused_before)

def handle_jump_target(target: str | Widget | None) -> None:
if isinstance(target, str):
Expand All @@ -841,22 +844,23 @@ def handle_jump_target(target: str | Widget | None) -> None:
)
else:
if target_widget.focusable:
target_widget.focus()
self.set_focus(target_widget)
else:
target_widget.post_message(
Click(0, 0, 0, 0, 0, False, False, False)
)

elif isinstance(target, Widget):
target.focus()
self.set_focus(target)
else:
# If there's no target (i.e. the user pressed ESC to dismiss)
# then re-focus the widget that was focused before we opened
# the jumper.
self.set_focus(focused_before, scroll_visible=False)
if focused_before is not None:
self.set_focus(focused_before, scroll_visible=False)

self.clear_notifications()
await self.push_screen(JumpOverlay(self.jumper), callback=handle_jump_target)
self.push_screen(JumpOverlay(self.jumper), callback=handle_jump_target)

async def action_help(self) -> None:
focused = self.focused
Expand Down
9 changes: 6 additions & 3 deletions src/posting/jump_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
from posting.jumper import Jumper


class JumpOverlay(ModalScreen[str | Widget]):
class JumpOverlay(ModalScreen[str | Widget | None]):
"""Overlay showing the jump targets.
Returns the ID of the widget the jump was requested for on closing."""
Dismissed with the ID of the widget the jump was requested for on closing,
or a reference to the widget. Is dismissed with None if the user dismissed
the overlay without making a selection."""

AUTO_FOCUS = None
DEFAULT_CSS = """\
JumpOverlay {
background: black 25%;
Expand Down Expand Up @@ -58,7 +61,7 @@ def on_key(self, key_event: events.Key) -> None:
return

def action_dismiss_overlay(self) -> None:
self.dismiss()
self.dismiss(None)

async def on_resize(self) -> None:
self._sync()
Expand Down
49 changes: 39 additions & 10 deletions src/posting/widgets/text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def action_focus_text_area(self) -> None:

class PostingTextArea(TextArea):
BINDINGS = [
Binding("f3", "open_in_pager", "Pager"),
Binding("f4", "open_in_editor", "Editor"),
Binding("f3,ctrl+P", "open_in_pager", "Pager"),
Binding("f4,ctrl+E", "open_in_editor", "Editor"),
]

def on_mount(self) -> None:
Expand Down Expand Up @@ -235,6 +235,14 @@ def on_change(self, event: TextArea.Changed) -> None:

def action_open_in_editor(self) -> None:
editor_command = SETTINGS.get().editor
if not editor_command:
self.app.notify(
severity="warning",
title="No editor configured",
message="Set the [b]$EDITOR[/b] environment variable.",
)
return

self._open_as_tempfile(editor_command)

def action_open_in_pager(self) -> None:
Expand All @@ -244,15 +252,26 @@ def action_open_in_pager(self) -> None:
# want to use a specific pager for JSON, let's use that.
if self.language == "json" and settings.pager_json:
pager_command = settings.pager_json
if not pager_command:
self.app.notify(
severity="warning",
title="No JSON pager configured",
message="Set the [b]$POSTING_PAGER_JSON[/b] environment variable.",
)
return
else:
pager_command = settings.pager
if not pager_command:
self.app.notify(
severity="warning",
title="No pager configured",
message="Set the [b]$POSTING_PAGER[/b] environment variable.",
)
return

self._open_as_tempfile(pager_command)

def _open_as_tempfile(self, command: str | None) -> None:
if command is None:
return

def _open_as_tempfile(self, command: str) -> None:
editor_args: list[str] = shlex.split(command)

if self.language in {"json", "html", "yaml"}:
Expand All @@ -270,7 +289,14 @@ def _open_as_tempfile(self, command: str | None) -> None:
editor_args.append(temp_file_name)

with self.app.suspend():
subprocess.call(editor_args)
try:
subprocess.call(editor_args)
except OSError:
self.app.notify(
severity="error",
title="Can't run command",
message=f"The command [b]{command}[/b] failed to run.",
)

with open(temp_file_name, "r") as temp_file:
if not self.read_only:
Expand Down Expand Up @@ -324,11 +350,14 @@ class ReadOnlyTextArea(PostingTextArea):
Binding(
"v",
"toggle_visual_mode",
description="Select mode",
key_display="v",
description="Toggle visual mode",
show=False,
),
Binding(
"y,c", "copy_to_clipboard", description="Copy selection", key_display="y"
"y,c",
"copy_to_clipboard",
description="Copy selection",
show=False,
),
Binding("g", "cursor_top", "Go to top", show=False),
Binding("G", "cursor_bottom", "Go to bottom", show=False),
Expand Down

0 comments on commit d09411e

Please sign in to comment.