-
Notifications
You must be signed in to change notification settings - Fork 80
/
viewer.py
executable file
·175 lines (140 loc) · 5.77 KB
/
viewer.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from argparse import ArgumentParser
from curses import curs_set, newpad, is_term_resized, wrapper, \
KEY_UP, KEY_DOWN, KEY_PPAGE, KEY_NPAGE, KEY_HOME, KEY_END, \
A_REVERSE, A_BOLD, A_ITALIC
from json import load as json_load
from textwrap import wrap
class Window:
def __init__(self):
self.pad = newpad(1, 1)
self.y = 0
self.off_y = 0
self.off_x = 0
def resize(self, height, width):
self.pad.resize(height, width)
self.y = 0
def clear(self):
self.pad.clear()
self.y = 0
def print(self, text="", attr=0):
self.pad.addstr(self.y, 0, text, attr)
self.y += 1
def refresh(self, min_y, min_x, max_y, max_x):
self.pad.refresh(self.off_y, self.off_x, min_y, min_x, max_y, max_x)
class ListWindow(Window):
def __init__(self, items):
super().__init__()
self.items = items
self.n_items = len(self.items)
self.max_item_width = max(len(item) for item in self.items)
self.current_index = 0
def refresh(self, min_y, min_x, max_y, max_x):
self.resize(len(self.items), self.max_item_width + 1)
if self.current_index < self.off_y:
self.off_y = self.current_index
view_height = max_y - min_y + 1
while self.current_index >= self.off_y + view_height:
self.off_y += 1
for y, name in enumerate(self.items):
if y == self.current_index:
self.pad.addstr(y, 0, name.ljust(self.max_item_width), A_REVERSE)
else:
self.pad.addstr(y, 0, name.ljust(self.max_item_width))
super().refresh(min_y, min_x, max_y, max_x)
def move_by(self, delta):
return self.move_to(self.current_index + delta)
def move_to(self, new_index):
old_index = self.current_index
if 0 <= new_index < self.n_items:
self.current_index = new_index
return new_index - old_index
class EndpointWindow(Window):
def __init__(self):
super().__init__()
self.endpoint = {}
def update(self, endpoint):
self.endpoint = endpoint
def refresh(self, min_y, min_x, max_y, max_x):
view_width = max_x - min_x + 1
self.resize(200, view_width)
self.print(f"{self.endpoint['name']} [{self.endpoint['visibility']}] [{self.endpoint['stability']}]", A_BOLD)
since = self.endpoint.get('since', '0.0.0')
if since != '0.0.0':
self.print(f"(since {since})", A_ITALIC)
self.print()
description = wrap(self.endpoint["description"], width=view_width - 1)
for line in description:
self.print(line)
self.print()
for url in self.endpoint["urls"]:
for method in url["methods"]:
self.print(f"{method} {url['path']}")
self.print()
self.print(f"{self.endpoint['docUrl']}")
super().refresh(min_y, min_x, max_y, max_x)
class Viewer:
def __init__(self, filename):
with open(filename) as f:
self.spec = json_load(f)
self.title_window = Window()
self.endpoint_list_window = ListWindow([endpoint["name"] for endpoint in self.spec["endpoints"]])
self.endpoint_window = EndpointWindow()
def _refresh_title(self, min_y, min_x, max_y, max_x):
title = self.spec["_info"]["title"]
self.title_window.resize(2, len(title) + 1)
self.title_window.print(title)
self.title_window.print("=" * len(title))
self.title_window.refresh(min_y, min_x, max_y, max_x)
def _refresh_names(self, min_y, min_x, max_y, max_x):
self.endpoint_list_window.refresh(min_y, min_x, max_y, max_x)
def _refresh_details(self, min_y, min_x, max_y, max_x):
self.endpoint_window.update(self.spec["endpoints"][self.endpoint_list_window.current_index])
self.endpoint_window.refresh(min_y, min_x, max_y, max_x)
def refresh(self, screen):
screen.refresh()
height, width = screen.getmaxyx()
aside_width = int(min(self.endpoint_list_window.max_item_width - 1, width / 4))
self._refresh_title(0, 0, 1, width - 1)
self._refresh_names(3, 0, height - 1, aside_width)
self._refresh_details(3, aside_width + 4, height - 1, width - 1)
return height, width
def view(self, screen):
curs_set(0)
height, width = self.refresh(screen)
while True:
try:
key = screen.getch()
except KeyboardInterrupt:
break
else:
moved = 0
if key == ord("q"):
break
elif key == KEY_UP:
moved = self.endpoint_list_window.move_by(-1)
elif key == KEY_DOWN:
moved = self.endpoint_list_window.move_by(+1)
elif key == KEY_PPAGE:
moved = self.endpoint_list_window.move_by(-(height - 3))
elif key == KEY_NPAGE:
moved = self.endpoint_list_window.move_by(+(height - 3))
elif key == KEY_HOME:
moved = self.endpoint_list_window.move_to(0)
elif key == KEY_END:
moved = self.endpoint_list_window.move_to(self.endpoint_list_window.n_items - 1)
if moved:
self.endpoint_window.clear()
height, width = self.refresh(screen)
elif is_term_resized(height, width):
screen.clear()
height, width = self.refresh(screen)
def main(screen):
parser = ArgumentParser()
parser.add_argument("filename")
parsed = parser.parse_args()
viewer = Viewer(parsed.filename)
viewer.view(screen)
if __name__ == "__main__":
wrapper(main)