1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""No-repository fallback page."""
from __future__ import annotations
from pathlib import Path
from pyratatui import Frame, Paragraph, Rect, Style, Text
from pygittools.tui.keys import KeyLike, is_quit, is_slash
from pygittools.tui.pages.help import help_page
from pygittools.tui.types import PageAction, PageContext, PageResult
class NoRepoPage:
title = "pgt"
def __init__(self, start: Path) -> None:
self._start = start
self._command: str | None = None
self._command_error: str | None = None
def on_enter(self, ctx: PageContext) -> None:
del ctx
def status_text(self) -> str:
if self._command_error is not None:
return self._command_error
if self._command is not None:
if not self._command:
return "/ — help quit — Esc cancel"
return f"/{self._command}"
return "no repository — / command — q quit"
def draw(self, frame: Frame, area: Rect) -> None:
lines = "\n".join([
"pgt",
"",
f"No git repository found under {self._start}",
"",
"Run pgt from inside a repository, or pass --repo PATH.",
])
frame.render_widget(
Paragraph(Text.from_string(lines)).style(Style().bold()),
area,
)
def handle_key(self, key: KeyLike) -> PageResult:
if self._command is not None:
return self._handle_command_key(key)
if is_quit(key):
return PageResult(action=PageAction.QUIT)
if is_slash(key):
self._command = ""
self._command_error = None
return PageResult()
return PageResult()
def _handle_command_key(self, key: KeyLike) -> PageResult:
assert self._command is not None
if key.code == "Esc":
self._command = None
self._command_error = None
return PageResult()
if key.code == "Backspace":
self._command_error = None
self._command = self._command[:-1]
return PageResult()
if key.code == "Enter":
name = self._command.strip().casefold().lstrip("/")
self._command = None
if name in {"help", "?"}:
return PageResult(action=PageAction.PUSH, next_page=help_page())
if name in {"quit", "q"}:
return PageResult(action=PageAction.QUIT)
if name:
self._command = ""
self._command_error = f"Unknown command: /{name} — help quit"
return PageResult()
if not key.ctrl and not key.alt and len(key.code) == 1 and key.code.isprintable() and not key.code.isspace():
self._command_error = None
self._command += key.code
return PageResult()