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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Default landing page for the pgt TUI."""
from __future__ import annotations
from typing import Literal
from unicurses import ( # type: ignore[import-untyped]
A_BOLD,
A_REVERSE,
KEY_DOWN,
KEY_ENTER,
KEY_RESIZE,
KEY_UP,
)
from pygittools.tui.branches import current_branch_name
from pygittools.tui.changes_list import ChangesList
from pygittools.tui.draw import draw_line
from pygittools.tui.pages.branches import branch_picker_page
from pygittools.tui.pages.help import help_page
from pygittools.tui.pages.projects import projects_page
from pygittools.tui.types import Page, PageAction, PageContext, PageResult
_HEADER_ROWS = 5
_REPO_ROW = 2
_BRANCH_ROW = 3
_HeaderFocus = Literal["changes", "repo", "branch"]
class HomePage:
title = "pgt"
def __init__(self) -> None:
self._ctx: PageContext | None = None
self._changes = ChangesList()
self._header_focus: _HeaderFocus = "changes"
def on_enter(self, ctx: PageContext) -> None:
self._ctx = ctx
self._header_focus = "changes"
self._changes.refresh(ctx.repo)
def status_text(self) -> str:
if self._header_focus == "repo":
return "Enter browse projects — j/k move"
if self._header_focus == "branch":
return "Enter switch branch — j/k move"
return self._changes.status_hint()
def draw(self, stdscr: int, height: int, width: int) -> None:
del stdscr
ctx = self._ctx
if ctx is None:
return
self._changes.refresh(ctx.repo)
worktree = ctx.repo.workdir or str(ctx.repo_path)
branch = current_branch_name(ctx.repo)
header_lines: list[tuple[str, int]] = [
("pgt", A_BOLD),
("", 0),
(f"Repository: {worktree}", 0),
(f"Branch: {branch}", 0),
("", 0),
]
for row, (text, attr) in enumerate(header_lines):
if row >= height:
break
if row == _REPO_ROW and self._header_focus == "repo":
attr = A_REVERSE
if row == _BRANCH_ROW and self._header_focus == "branch":
attr = A_REVERSE
draw_line(row, 0, text, width, attr)
list_height = max(0, height - _HEADER_ROWS)
if list_height > 0 and height > _HEADER_ROWS:
self._changes.draw(_HEADER_ROWS, list_height, width, highlight=self._header_focus == "changes")
def handle_key(self, key: int) -> PageResult:
if key in (ord("q"), ord("Q")):
return PageResult(action=PageAction.QUIT)
if key in (ord("h"), ord("H")):
return PageResult(action=PageAction.PUSH, next_page=help_page())
if key == KEY_RESIZE:
return PageResult()
if self._header_focus == "repo":
if key in (KEY_DOWN, ord("j"), ord("J")):
self._header_focus = "branch"
elif key in (KEY_ENTER, 10, 13):
return PageResult(action=PageAction.PUSH, next_page=projects_page())
return PageResult()
if self._header_focus == "branch":
if key in (KEY_UP, ord("k"), ord("K")):
self._header_focus = "repo"
elif key in (KEY_DOWN, ord("j"), ord("J")):
self._header_focus = "changes"
elif key in (KEY_ENTER, 10, 13):
return PageResult(action=PageAction.PUSH, next_page=branch_picker_page())
return PageResult()
if key in (KEY_UP, ord("k"), ord("K")) and self._changes.is_at_top():
self._header_focus = "branch"
return PageResult()
ctx = self._ctx
if ctx is not None:
self._changes.handle_key(key, ctx.repo)
return PageResult()
def home_page() -> Page:
return HomePage()