7 def set_window_geometries():
10 win["size"], win["start"] = [0, 0], [0, 0]
11 win["size"][0] = win["config"][0]
12 if (win["config"][0] == 0):
13 win["size"][0] = screen_size[0] - sep_size
14 elif (win["config"][0] < 0):
15 win["size"][0] = screen_size[0] + win["config"][0] - sep_size
16 win["size"][1] = win["config"][1]
17 if (win["config"][1] == 0):
18 win["size"][1] = screen_size[1]
19 elif (win["config"][1] < 0):
20 win["size"][1] = screen_size[1] + win["config"][1]
23 win_i = windows.index(win)
25 # If win is first window, it goes into the top left corner.
26 win["start"][0] = 0 + sep_size
30 # If not, get win's closest predecessor starting a new stack on the
31 # screen top,fit win's top left to that win_top's top right corner.
33 for i in range(win_i - 1, -1, -1):
35 if (win_top["start"][0] == 0 + sep_size):
37 win["start"][1] = win_top["start"][1] + win_top["size"][1] \
40 # If enough space is found below win's predecessor, fit win's top
41 # left corner to that predecessor's bottom left corner.
42 win_prev = windows[win_i - 1]
43 next_free_y = win_prev["start"][0] + win_prev["size"][0] + sep_size
44 if (win["size"][1] <= win_prev["size"][1] and
45 win["size"][0] <= screen_size[0] - next_free_y):
46 win["start"][1] = win_prev["start"][1]
47 win["start"][0] = next_free_y
49 # If that fails, try to fit win's top left corner to the top right
50 # corner of its closest predecessor win_test 1) below win_top (see
51 # above) 2) and with enough space open to its right between its
52 # right edge and the lower edge of a win_high located directly
53 # above win_test to fit win there (without growing further to the
54 # right than win_high does or surpassing the screen's lower edge).
58 while (win_test != win_top):
59 for i in range(win_i - 2, -1, -1):
61 if win_test["start"][0] > win_high["start"][0]:
63 next_free_y = win_high["start"][0] + win_high["size"][0] \
65 first_free_x = win_test["start"][1] + win_test["size"][1] \
67 last_free_x = win_high["start"][1] + win_high["size"][1]
68 if (win["size"][0] <= screen_size[0] - next_free_y and
69 win["size"][1] <= last_free_x - first_free_x):
70 win["start"][1] = first_free_x
71 win["start"][0] = next_free_y
75 global screen_size, stdscr
77 stdscr = curses.initscr()
78 screen_size = stdscr.getmaxyx()
82 cursed_main.redraw = True
87 def draw_window_border_lines():
90 j = win["start"][int(k == 0)] - sep_size
91 if (j >= 0 and j < screen_size[int(k == 0)]):
92 start = win["start"][k]
93 # start = start if start >= 0 else 0
94 end = win["start"][k] + win["size"][k]
95 end = end if end < screen_size[k] else screen_size[k]
97 [stdscr.addch(j, i, '-') for i in range(start, end)]
99 [stdscr.addch(i, j, '|') for i in range(start, end)]
101 def draw_window_border_corners():
103 up = win["start"][0] - sep_size
104 down = win["start"][0] + win["size"][0]
105 left = win["start"][1] - sep_size
106 right = win["start"][1] + win["size"][1]
107 if (up >= 0 and up < screen_size[0]):
108 if (left >= 0 and left < screen_size[1]):
109 stdscr.addch(up, left, '+')
110 if (right >= 0 and right < screen_size[1]):
111 stdscr.addch(up, right, '+')
112 if (down >= 0 and down < screen_size[0]):
113 if (left >= 0 and left < screen_size[1]):
114 stdscr.addch(down, left, '+')
115 if (right >= 0 and right < screen_size[1]):
116 stdscr.addch(down, right, '+')
118 def draw_window_contents():
122 stop[i] = win["size"][i] + offset[i]
123 stop[i] = stop[i] if stop[i] < size[i] else size[i]
124 for y in range(offset[0], stop[0]):
125 for x in range(offset[1], stop[1]):
126 cell = winmap[y * size[1] + x]
127 y_in_screen = win["start"][0] + (y - offset[0])
128 x_in_screen = win["start"][1] + (x - offset[1])
129 if (y_in_screen < screen_size[0]
130 and x_in_screen < screen_size[1]):
131 stdscr.addch(y_in_screen, x_in_screen, cell)
132 def draw_scroll_hints():
133 def draw_scroll_string(n_lines_outside):
134 hint = ' ' + str(n_lines_outside + 1) + ' more ' + unit + ' '
135 if len(hint) <= win["size"][ni]:
136 non_hint_space = win["size"][ni] - len(hint)
137 hint_offset = int(non_hint_space / 2)
138 for j in range(win["size"][ni] - non_hint_space):
139 pos_2 = win["start"][ni] + hint_offset + j
140 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
141 stdscr.addch(y, x, hint[j], curses.A_REVERSE)
142 def draw_scroll_arrows(ar1, ar2):
143 for j in range(win["size"][ni]):
144 pos_2 = win["start"][ni] + j
145 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
146 stdscr.addch(y, x, ar1 if ni else ar2, curses.A_REVERSE)
149 unit = 'rows' if ni else 'columns'
151 pos_1 = win["start"][i]
152 draw_scroll_arrows('^', '<')
153 draw_scroll_string(offset[i])
154 if (size[i] > offset[i] + win["size"][i]):
155 pos_1 = win["start"][i] + win["size"][i] - 1
156 draw_scroll_arrows('v', '>')
157 draw_scroll_string(size[i] - offset[i] - win["size"][i])
159 offset, size, winmap = win["func"]()
164 draw_window_border_lines()
165 draw_window_border_corners()
166 draw_window_contents()
172 def cursed_main(stdscr):
176 if len(new_data_from_server) > 0:
177 ping_test.sent = False
178 elif ping_test.wait_start + half_wait_time < time.time():
179 if not ping_test.sent:
180 io["file_out"].write("PING\n")
181 io["file_out"].flush()
182 ping_test.sent = True
183 ping_test.wait_start = time.time()
185 raise SystemExit("Server not answering anymore.")
186 ping_test.wait_start = 0
188 def read_into_message_queue():
189 if new_data_from_server == "":
192 if new_data_from_server[-1] is not "\n":
194 new_messages = new_data_from_server.splitlines()
195 if message_queue["open_end"]:
196 message_queue["messages"][-1] += new_messages[0]
198 message_queue["messages"] += new_messages
200 message_queue["open_end"] = True
203 curses.curs_set(False)
204 # stdscr.keypad(True)
205 signal.signal(signal.SIGWINCH,
206 lambda ignore_1, ignore_2: set_window_geometries())
207 set_window_geometries()
210 stdscr.timeout(delay)
211 delay = delay * 2 if delay < 1000 else delay
212 if cursed_main.redraw:
215 cursed_main.redraw = False
216 char = stdscr.getch()
217 if char >= 0 and chr(char) in commands:
218 commands[chr(char)]()
219 cursed_main.redraw = True
220 new_data_from_server = io["file_in"].read()
222 read_into_message_queue()
226 winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
229 return offset, size, winmap
233 io["file_out"].write("QUIT\n")
234 io["file_out"].flush()
235 raise SystemExit("Received QUIT command, forwarded to server, leaving.")
239 {"config": [1, 33], "func": foo},
240 {"config": [-7, 33], "func": foo},
241 {"config": [4, 16], "func": foo},
242 {"config": [4, 16], "func": foo},
243 {"config": [0, -34], "func": foo}
246 "path_out": "server/in",
247 "path_in": "server/out"
256 sep_size = 1 # Width of inter-window borders and title bars.
262 if (not os.access(io["path_out"], os.F_OK)):
263 msg = "No server input file found at " + io["path_out"] + "."
264 raise SystemExit(msg)
265 io["file_out"] = open(io["path_out"], "a")
266 io["file_in"] = open(io["path_in"], "r")
267 curses.wrapper(cursed_main)
268 except SystemExit as exit:
269 print("ABORTING: " + exit.args[0])
271 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
275 io["file_out"].close()
277 io["file_in"].close()