-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.py
More file actions
142 lines (126 loc) · 3.68 KB
/
Copy pathbuffer.py
File metadata and controls
142 lines (126 loc) · 3.68 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import curses
import subprocess
from public import *
from entries import *
class Buffer:
window: curses.window;
entries: Entries;
# curent data search string
data: str = "data";
# buffer mode
mode: modes = modes.NAV;
# current app selected of self.entries
pos: int = 0;
cpos: int = 0;
# maximum cursor pos
pos_max: int;
cpos_max: int;
# keybinds are set inside __init__
# first int is the mode for that keybind to trigger
# second int is for key ascii code
keybinds: dict[modes, dict[int, callable]];
# move list offset only
def move_c_down(self) -> None:
self.cpos = clamp(self.cpos + 1, 0, self.cpos_max + 1);
def move_c_up(self) -> None:
self.cpos = clamp(self.cpos - 1, 0, self.cpos_max + 1);
# move cursor pos in the list
def move_down(self) -> None:
'''
if (
self.pos - self.cpos - 1
<
self.cpos_max
):
self.pos = self.cpos
return
'''
self.pos = clamp(self.pos + 1, 0, self.pos_max);
# move cpos if cursor goes offscreen
if (
self.pos - self.cpos - 1
>
self.cpos_max
):
self.move_c_down()
def move_up(self) -> None:
'''
if (
self.pos - self.cpos - 1
>
self.cpos_max
):
self.pos = self.cpos + self.cpos_max
return
'''
self.pos = clamp(self.pos - 1, 0, self.pos_max);
# move cpos if cursor goes offscreen
if (
self.pos
<
self.cpos
):
self.move_c_up()
# NOTE: only works like this because it only has 2 modes
def change_mode(self) -> None:
self.mode = modes(not self.mode.value)
def term(self) -> None:
exit(0)
pass
def select(self) -> None:
exec_cmd: str = self.entries.apps[self.pos].ex_cmd
subprocess.Popen(
exec_cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
exit(0)
# handles input for seach data
def add_chr(self) -> str:
return ""
# handles keypress
def parse_keypress(self) -> None:
# current key pressed
k: int = self.window.getch()
try:
# NOTE: for some reason it wont run the dict functions unless i have Exception in except
self.keybinds[self.mode][k]()
except Exception as e:
pass
# runs after each time gui draws
def loop(self) -> None:
self.parse_keypress()
def __init__(self,
win: curses.window, # window
ent: Entries, # entries pointer
) -> None:
self.window = win
self.entries = ent
e_count: int = len(self.entries.apps)
self.pos_max = e_count
self.cpos_max = e_count - win.getmaxyx()[0] + 1
self.keybinds = {
modes.SEARCH: {
# esc
27: self.term,
# ctrl+space
0: self.change_mode,
# select
10: self.select,
},
modes.NAV: {
# esc
27: self.term,
# ctrl+space
0: self.change_mode,
#ord('e'): self.select
# move select
ord('j'): self.move_down,
ord('k'): self.move_up,
# move list offset
ord('J'): self.move_c_down,
ord('K'): self.move_c_up,
# select
10: self.select,
}
};