Skip to content

Commit

Permalink
[display-] actually resize-height-input and -max; add to menu #1307
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 10, 2023
1 parent ab059d8 commit 3bb81f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
14 changes: 7 additions & 7 deletions visidata/features/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def hide_col(vd, col):
Sheet.addCommand('gv', 'unhide-cols', 'unhide_cols(columns, visibleRows)', 'Show all columns')
Sheet.addCommand('v', 'visibility-sheet', 'for c in visibleCols: c.toggleVisibility()')
Sheet.addCommand('', 'visibility-col', 'cursorCol.toggleVisibility()')
Sheet.addCommand('zv', 'setcol-height-input', 'cursorCol.height=int(input("set height for this column to: ", value=cursorCol.height))', '')
Sheet.addCommand('gzv', 'setcols-height-input', 'Fanout(visibleCols).height=int(input("set height for all columns to: ", value=max(c.height for c in sheet.visibleCols)))', '')
Sheet.addCommand('zv', 'resize-height-input', 'Fanout(visibleCols).height=int(input("set height for all columns to: ", value=max(c.height for c in sheet.visibleCols)))', 'resize row height to N')
Sheet.addCommand('gzv', 'resize-height-max', 'h=calc_height(cursorRow, {}, maxheight=windowHeight-1); vd.status(f"set height for all columns to {h}"); Fanout(visibleCols).height=h', 'resize row height to max height needed to see this row')

vd.addMenuItems('''
Column > Hide > hide-col
Column > Unhide all > unhide-cols
Column > Resize > half width > resize-col-half
Column > Resize > current column width to max > resize-col-max
Column > Resize > current column width to input > resize-col-input
Column > Resize > all columns width to max> resize-cols-max
Column > Resize > all columns width to input > resize-cols-input
Column > Resize > current column height to input > setcol-height-input
Column > Resize > all columns height to input > setcol-height-input
Column > Resize > current column width to N > resize-col-input
Column > Resize > all columns width to max > resize-cols-max
Column > Resize > all columns width to N > resize-cols-input
Row > Resize > height to N > resize-height-input
Row > Resize > height to max > resize-height-max
''')
22 changes: 14 additions & 8 deletions visidata/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@


@drawcache
def _splitcell(sheet, s, width=0):
def _splitcell(sheet, s, width=0, maxheight=1):
if width <= 0 or not sheet.options.textwrap_cells:
return [s]

ret = []
for attr, text in s:
ret.extend([(attr, line)] for line in textwrap.wrap(
for line in textwrap.wrap(
text, width=width, break_long_words=False, replace_whitespace=False
))
):
if len(ret) >= maxheight:
ret[-1][0][1] += ' ' + line
break
else:
ret.append([[attr, line]])
return ret

disp_column_fill = ' ' # pad chars after column value
Expand Down Expand Up @@ -732,7 +737,7 @@ def draw(self, scr):
if vcolidx+1 < self.nVisibleCols:
scr.addstr(headerRow, self.windowWidth-2, self.options.disp_more_right, colors.color_column_sep)

def calc_height(self, row, displines=None, isNull=None):
def calc_height(self, row, displines=None, isNull=None, maxheight=1):
if displines is None:
displines = {} # [vcolidx] -> list of lines in that cell

Expand All @@ -753,13 +758,13 @@ def calc_height(self, row, displines=None, isNull=None):
except (TypeError, ValueError):
pass

if col.voffset or col.height > 1:
lines = _splitcell(self, cellval.display, width=colwidth-2)
if maxheight > 1:
lines = _splitcell(self, cellval.display, width=colwidth-2, maxheight=maxheight)
else:
lines = [cellval.display]
displines[vcolidx] = (col, cellval, lines)

return self.rowHeight
return max(len(lines) for _, _, lines in displines.values())

def drawRow(self, scr, row, rowidx, ybase, rowcattr: ColorAttr, maxheight,
isNull='',
Expand Down Expand Up @@ -789,7 +794,8 @@ def drawRow(self, scr, row, rowidx, ybase, rowcattr: ColorAttr, maxheight,
basecellcattr = rowcattr

displines = {} # [vcolidx] -> list of lines in that cell
height = min(self.calc_height(row, displines), maxheight) or 1 # display even empty rows
self.calc_height(row, displines, maxheight=self.rowHeight)
height = min(self.rowHeight, maxheight) or 1 # display even empty rows
self._rowLayout[rowidx] = (ybase, height)

for vcolidx, (col, cellval, lines) in displines.items():
Expand Down

0 comments on commit 3bb81f6

Please sign in to comment.