5 def set_window_geometries():
8 win["size"], win["start"] = [0, 0], [0, 0]
9 win["size"][0] = win["config"][0]
10 if (win["config"][0] == 0):
11 win["size"][0] = screen_size[0] - sep_size
12 elif (win["config"][0] < 0):
13 win["size"][0] = screen_size[0] + win["config"][0] - sep_size
14 win["size"][1] = win["config"][1]
15 if (win["config"][1] == 0):
16 win["size"][1] = screen_size[1]
17 elif (win["config"][1] < 0):
18 win["size"][1] = screen_size[1] + win["config"][1]
21 win_i = windows.index(win)
23 # If win is first window, it goes into the top left corner.
24 win["start"][0] = 0 + sep_size
28 # If not, get win's closest predecessor starting a new stack on the
29 # screen top,fit win's top left to that win_top's top right corner.
31 for i in range(win_i - 1, -1, -1):
33 if (win_top["start"][0] == 0 + sep_size):
35 win["start"][1] = win_top["start"][1] + win_top["size"][1] \
38 # If enough space is found below win's predecessor, fit win's top
39 # left corner to that predecessor's bottom left corner.
40 win_prev = windows[win_i - 1]
41 next_free_y = win_prev["start"][0] + win_prev["size"][0] + sep_size
42 if (win["size"][1] <= win_prev["size"][1] and
43 win["size"][0] <= screen_size[0] - next_free_y):
44 win["start"][1] = win_prev["start"][1]
45 win["start"][0] = next_free_y
47 # If that fails, try to fit win's top left corner to the top right
48 # corner of its closest predecessor win_test 1) below win_top (see
49 # above) 2) and with enough space open to its right between its
50 # right edge and the lower edge of a win_high located directly
51 # above win_test to fit win there (without growing further to the
52 # right than win_high does or surpassing the screen's lower edge).
56 while (win_test != win_top):
57 for i in range(win_i - 2, -1, -1):
59 if win_test["start"][0] > win_high["start"][0]:
61 next_free_y = win_high["start"][0] + win_high["size"][0] \
63 first_free_x = win_test["start"][1] + win_test["size"][1] \
65 last_free_x = win_high["start"][1] + win_high["size"][1]
66 if (win["size"][0] <= screen_size[0] - next_free_y and
67 win["size"][1] <= last_free_x - first_free_x):
68 win["start"][1] = first_free_x
69 win["start"][0] = next_free_y
73 global screen_size, stdscr
75 stdscr = curses.initscr()
76 screen_size = stdscr.getmaxyx()
84 def draw_window_border_lines():
87 j = win["start"][int(k == 0)] - sep_size
88 if (j >= 0 and j < screen_size[int(k == 0)]):
89 start = win["start"][k]
90 # start = start if start >= 0 else 0
91 end = win["start"][k] + win["size"][k]
92 end = end if end < screen_size[k] else screen_size[k]
94 [stdscr.addch(j, i, '-') for i in range(start, end)]
96 [stdscr.addch(i, j, '|') for i in range(start, end)]
98 def draw_window_border_corners():
100 up = win["start"][0] - sep_size
101 down = win["start"][0] + win["size"][0]
102 left = win["start"][1] - sep_size
103 right = win["start"][1] + win["size"][1]
104 if (up >= 0 and up < screen_size[0]):
105 if (left >= 0 and left < screen_size[1]):
106 stdscr.addch(up, left, '+')
107 if (right >= 0 and right < screen_size[1]):
108 stdscr.addch(up, right, '+')
109 if (down >= 0 and down < screen_size[0]):
110 if (left >= 0 and left < screen_size[1]):
111 stdscr.addch(down, left, '+')
112 if (right >= 0 and right < screen_size[1]):
113 stdscr.addch(down, right, '+')
115 def draw_window_contents():
119 stop[i] = win["size"][i] + offset[i]
120 stop[i] = stop[i] if stop[i] < size[i] else size[i]
121 for y in range(offset[0], stop[0]):
122 for x in range(offset[1], stop[1]):
123 cell = winmap[y * size[1] + x]
124 y_in_screen = win["start"][0] + (y - offset[0])
125 x_in_screen = win["start"][1] + (x - offset[1])
126 if (y_in_screen < screen_size[0]
127 and x_in_screen < screen_size[1]):
128 stdscr.addch(y_in_screen, x_in_screen, cell)
129 def draw_scroll_hints():
130 def draw_scroll_string(hint_number):
131 hint = ' ' + str(hint_number + 1) + ' more ' + unit + ' '
132 if len(hint) <= win["size"][ni]:
133 non_hint_space = win["size"][ni] - len(hint)
134 hint_offset = int(non_hint_space / 2)
135 for j in range(win["size"][ni] - non_hint_space):
136 pos_2 = win["start"][ni] + hint_offset + j
137 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
138 stdscr.addch(y, x, hint[j], curses.A_REVERSE)
139 def draw_scroll_arrows(ar1, ar2):
140 for j in range(win["size"][ni]):
141 pos_2 = win["start"][ni] + j
142 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
143 stdscr.addch(y, x, ar1 if ni else ar2, curses.A_REVERSE)
146 unit = 'rows' if ni else 'columns'
148 pos_1 = win["start"][i]
149 draw_scroll_arrows('^', '<')
150 draw_scroll_string(offset[i])
151 if (size[i] > offset[i] + win["size"][i]):
152 pos_1 = win["start"][i] + win["size"][i] - 1
153 draw_scroll_arrows('v', '>')
154 draw_scroll_string(size[i] - offset[i] - win["size"][i])
156 offset, size, winmap = win["func"]()
161 draw_window_border_lines()
162 draw_window_border_corners()
163 draw_window_contents()
169 curses.curs_set(False)
170 # stdscr.keypad(True)
171 signal.signal(signal.SIGWINCH,
172 lambda ignore_1, ignore_2: set_window_geometries())
173 set_window_geometries()
176 char = stdscr.getch()
177 if (char >= 0 and chr(char) == 'Q'):
181 winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
184 return offset, size, winmap
188 {"config": [1, 33], "func": foo},
189 {"config": [-7, 33], "func": foo},
190 {"config": [4, 16], "func": foo},
191 {"config": [4, 16], "func": foo},
192 {"config": [0, -34], "func": foo}
195 sep_size = 1 # Width of inter-window borders and title bars.