parent
88726de97e
commit
b5fbae5f23
131
bufferlist.py
131
bufferlist.py
|
@ -26,93 +26,60 @@ class Slot(object):
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
class VSlot(Slot):
|
class Box(object):
|
||||||
size = 0
|
boxtype = 'box'
|
||||||
slots = []
|
def _set_hwyx(self, h, w, y, x):
|
||||||
heights = []
|
self.height = h
|
||||||
def __init__(self, height, width, y_offset=0, x_offset=0):
|
self.width = w
|
||||||
self.window = None
|
self.y_offset = y
|
||||||
self.resize(height, width, y_offset, x_offset)
|
self.x_offset = x
|
||||||
def is_empty(self):
|
def __init__(self, h, w, y, x):
|
||||||
for slot in self.slots:
|
self._set_hwyx(h, w, y, x)
|
||||||
if not slot.is_empty():
|
def resize(self, h, w, y, x):
|
||||||
return False
|
self._set_hwyx(h, w, y, x)
|
||||||
return True
|
|
||||||
def get_percs(self):
|
class VBoxes(Box):
|
||||||
return [float(x) / self.height for x in self.heights]
|
boxtype = 'vboxes'
|
||||||
def resize(self, height, width, y_offset=0, x_offset=0):
|
def __init__(self, h, w, y, x):
|
||||||
|
Box.__init__(self, h, w, y, x)
|
||||||
|
self.boxes = [Box(h, w, y, x)]
|
||||||
|
|
||||||
|
def rescale(self):
|
||||||
|
n = len(self.boxes)
|
||||||
|
|
||||||
|
# first let's divide the new space based on old proportions
|
||||||
|
oldtotal = sum([box.height for box in self.boxes])
|
||||||
|
newtotal = self.height - len(self.boxes) + 1
|
||||||
|
heights = [None] * n
|
||||||
|
for i in range(0, n):
|
||||||
|
heights[i] = int(float(newtotal * self.boxes[i]) / oldtotal)
|
||||||
|
|
||||||
|
# fix any rounding errors
|
||||||
|
_sum = sum([h for h in heights])
|
||||||
|
if _sum < total:
|
||||||
|
adder = 1
|
||||||
|
else:
|
||||||
|
adder = -1
|
||||||
|
for i in range(0, abs(total - _sum)):
|
||||||
|
heights[i % n] += adder
|
||||||
|
|
||||||
|
# now do it!
|
||||||
|
offset = self.y_offset
|
||||||
|
for i in range(0, n):
|
||||||
|
self.boxes[i].resize(heights[i], self.width, offset, self.x_offset)
|
||||||
|
offset += heights[i] + 1
|
||||||
|
|
||||||
|
def resize(self, height, width, y_offset, x_offset):
|
||||||
self.height = height
|
self.height = height
|
||||||
self.width = width
|
self.width = width
|
||||||
self.y_offset = y_offset
|
self.y_offset = y_offset
|
||||||
self.x_offset = x_offset
|
self.x_offset = x_offset
|
||||||
percs = self.get_percs()
|
self.rescale()
|
||||||
self.heights = [int(self.height * p) for p in percs]
|
|
||||||
assert sum(self.heights) <= self.height
|
|
||||||
i = 0
|
|
||||||
while sum(self.heights) < self.height:
|
|
||||||
self.heights[i] += 1
|
|
||||||
i = (i + 1) % self.height
|
|
||||||
self.window.set_size(self.width, self.height)
|
|
||||||
total = 0
|
|
||||||
for i in range(0, self.size):
|
|
||||||
if self.slots[i] is not None:
|
|
||||||
(slot, height) = (self.slots[i], self.heights[i])
|
|
||||||
slot.resize(height, width, y_offset + total, x_offset)
|
|
||||||
total += height
|
|
||||||
|
|
||||||
def add_slot(self):
|
def split_box(self, n):
|
||||||
self.size += 1
|
for box in boxes:
|
||||||
self.slots.append(Slot(self.height, self.width, 0))
|
b.height = 0
|
||||||
self.heights.append(0)
|
self.boxes.extend(boxes)
|
||||||
self.fit_slots()
|
|
||||||
return len(self.slots) - 1
|
|
||||||
def empty_slot(self, i):
|
|
||||||
assert i > -1 and i < len(self.slots), "slot %d does not exist" % i
|
|
||||||
return self.slots[i].is_empty()
|
|
||||||
def unset_slot(self, i):
|
|
||||||
assert i > -1 and i < len(self.slots), "slot %d does not exist" % i
|
|
||||||
old_w = self.slots[i].unset()
|
|
||||||
if old_w is not None:
|
|
||||||
old_b = old_w.buffer
|
|
||||||
if not self.is_buffer_visible(old_b):
|
|
||||||
self.hidden_buffers.insert(0, old_b)
|
|
||||||
if len(old_b.windows) > 1:
|
|
||||||
old_b.remove_window(old_w)
|
|
||||||
|
|
||||||
def set_slot(self, i, b):
|
|
||||||
assert i > -1 and i < len(self.slots), "slot %d does not exist" % i
|
|
||||||
assert b in self.buffers, "buffer %s does not exist" % (b.name())
|
|
||||||
slot = self.slots[i]
|
|
||||||
self.unset_slot(i)
|
|
||||||
|
|
||||||
if b in self.hidden_buffers:
|
|
||||||
self.hidden_buffers.remove(b)
|
|
||||||
if self.is_window_visible(b.windows[0]):
|
|
||||||
app = b.windows[0].application
|
|
||||||
w = window.Window(b, app, height=slot.height, width=slot.width)
|
|
||||||
else:
|
|
||||||
w = b.windows[0]
|
|
||||||
slot.set(w)
|
|
||||||
|
|
||||||
def remove_slot(self, i):
|
|
||||||
assert i > -1 and i < len(self.slots), "slot %d does not exist" % i
|
|
||||||
self.unset_slot(i)
|
|
||||||
del self.slots[i]
|
|
||||||
self.fit_slots()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def set(self, i, w):
|
|
||||||
self.window = w
|
|
||||||
self.resize(self.height, self.width, self.y_offset, self.x_offset)
|
|
||||||
w.set_size(self.width, self.height)
|
|
||||||
def unset(self, i):
|
|
||||||
if not self.is_empty():
|
|
||||||
old_w = self.window
|
|
||||||
self.window = None
|
|
||||||
return old_w
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
class BufferList(object):
|
class BufferList(object):
|
||||||
def __init__(self, height, width, buffers=()):
|
def __init__(self, height, width, buffers=()):
|
||||||
|
|
|
@ -11,6 +11,7 @@ WORD_LETTERS = list(string.letters + string.digits)
|
||||||
# error. both buffer and window need to be aware of this possibility for points.
|
# error. both buffer and window need to be aware of this possibility for points.
|
||||||
|
|
||||||
class Window(object):
|
class Window(object):
|
||||||
|
boxtype = 'window'
|
||||||
margins = ((80, 'blue'),)
|
margins = ((80, 'blue'),)
|
||||||
margins_visible = False
|
margins_visible = False
|
||||||
def __init__(self, b, a, height=24, width=80, mode_name=None):
|
def __init__(self, b, a, height=24, width=80, mode_name=None):
|
||||||
|
|
Loading…
Reference in New Issue