-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathscreen.py
More file actions
121 lines (95 loc) · 3.28 KB
/
screen.py
File metadata and controls
121 lines (95 loc) · 3.28 KB
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
116
117
118
119
120
121
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Tuple
from .command_mode import run_command
if TYPE_CHECKING:
import curses
class ScreenBase(ABC):
@abstractmethod
def getmaxyx(self) -> Tuple[int, int]:
pass
@abstractmethod
def refresh(self) -> None:
pass
@abstractmethod
def erase(self) -> None:
pass
@abstractmethod
def move(self, y_pos: int, x_pos: int) -> None:
pass
@abstractmethod
def addstr(self, y_pos: int, x_pos: int, string: str, attr: int) -> None:
pass
@abstractmethod
def delch(self, y_pos: int, x_pos: int) -> None:
pass
@abstractmethod
def getch(self) -> int:
pass
@abstractmethod
def getstr(self, y_pos: int, x_pos: int, max_len: int) -> str:
pass
class CursesScreen(ScreenBase):
def __init__(self, screen: "curses._CursesWindow"):
self.screen = screen
def getmaxyx(self) -> Tuple[int, int]:
return self.screen.getmaxyx()
def refresh(self) -> None:
self.screen.refresh()
def erase(self) -> None:
self.screen.erase()
def move(self, y_pos: int, x_pos: int) -> None:
self.screen.move(y_pos, x_pos)
def addstr(self, y_pos: int, x_pos: int, string: str, attr: int) -> None:
self.screen.addstr(y_pos, x_pos, string, attr)
def delch(self, y_pos: int, x_pos: int) -> None:
self.screen.delch(y_pos, x_pos)
def getch(self) -> int:
return self.screen.getch()
def getstr(self, y_pos: int, x_pos: int, max_len: int) -> str:
result = self.screen.getstr(y_pos, x_pos, max_len)
if isinstance(result, str):
return result
if isinstance(result, int):
return str(result)
return result.decode("utf-8")
def clrtoeol(self) -> None:
"""
Clears from cursor to end of line using curses.
"""
if hasattr(self.screen, "clrtoeol"):
self.screen.clrtoeol()
else:
max_y, max_x = self.getmaxyx()
y, x = self.screen.getyx()
for _ in range(x, max_x):
self.delch(y, x)
self.refresh()
def handle_command_mode(self, current_selection: str) -> None:
"""
Trigger command mode for the selected file/directory.
"""
self.addstr(0, 0, "Enter command: ", 0)
self.refresh()
cmd_input = self.getstr(0, 15, 100) # read input from user
# Run the command with token expansion
run_command(cmd_input, current_selection, auto_mode=False)
# Clear the input line after running
self.move(0, 0)
self.clrtoeol()
self.refresh()
def handle_input(self, current_selection: str) -> None:
"""
Main input loop for the screen.
"""
while True:
key = self.getch()
if key in (ord('q'), ord('Q')):
break # quit
elif key == ord(':'):
# Enter command mode
self.handle_command_mode(current_selection)
# Add other key handling here, e.g., navigation